FieldDescription.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Bundle\Sonata\BaseApplicationBundle\Admin;
  11. /**
  12. * A FieldDescription hold the information about a field. A typical
  13. * admin instance contains different collections of fields
  14. *
  15. * - formFields: used by the form
  16. * - listFields: used by the list
  17. * - filderFields: used by the list filter
  18. *
  19. * Some options are global accross the different contexts, other are
  20. * context specifics.
  21. *
  22. * Global options :
  23. * - type (m): define the field type (use to tweak the form or the list)
  24. * - template (o) : the template used to render the field
  25. * - name (o) : the name used (label in the form, title in the list)
  26. *
  27. * Form Field options :
  28. * - form_field_widget (o): the widget class to use to render the field
  29. * - form_field_options (o): the options to give to the widget
  30. * - edit (o) : list|inline|standard (only used for associated admin)
  31. * - list : open a popup where the user can search, filter and click on one field
  32. * to select one item
  33. * - inline : the associated form admin is embedded into the current form
  34. * - standard : the associated admin is created through a popup
  35. *
  36. * List Field options :
  37. * - identifier (o): if set to true a link appear on to edit the element
  38. *
  39. * Filter Field options :
  40. * - filter_options (o): options given to the Filter object
  41. * - filter_field_options (o): options given to the filter field object
  42. *
  43. */
  44. class FieldDescription
  45. {
  46. /**
  47. * @var string the field name
  48. */
  49. protected $name;
  50. /**
  51. * @var Admin the related admin instance
  52. */
  53. protected $admin;
  54. /**
  55. * @var string|integer the type
  56. */
  57. protected $type;
  58. /**
  59. * @var string the field name (of the form)
  60. */
  61. protected $fieldName;
  62. /**
  63. * @var Admin the associated admin class if the object is associated to another entity
  64. */
  65. protected $associationAdmin;
  66. /**
  67. * @var array the Doctrine association mapping
  68. */
  69. protected $associationMapping;
  70. /**
  71. * @var array the Doctrine field information
  72. */
  73. protected $fieldMapping;
  74. /**
  75. * @var string the template name
  76. */
  77. protected $template;
  78. /**
  79. * @var array the option collection
  80. */
  81. protected $options = array();
  82. /**
  83. * @var admin|null the parent Admin instance
  84. */
  85. protected $parent = null;
  86. public function setFieldName($fieldName)
  87. {
  88. $this->fieldName = $fieldName;
  89. }
  90. public function getFieldName()
  91. {
  92. return $this->fieldName;
  93. }
  94. public function setName($name)
  95. {
  96. $this->name = $name;
  97. if (!$this->getFieldName()) {
  98. $this->setFieldName($name);
  99. }
  100. }
  101. public function getName()
  102. {
  103. return $this->name;
  104. }
  105. public function getOption($name, $default = null)
  106. {
  107. return isset($this->options[$name]) ? $this->options[$name] : $default;
  108. }
  109. public function setOption($name, $value)
  110. {
  111. $this->options[$name] = $value;
  112. }
  113. public function setOptions($options)
  114. {
  115. $this->options = $options;
  116. }
  117. public function getOptions()
  118. {
  119. return $this->options;
  120. }
  121. public function setTemplate($template)
  122. {
  123. $this->template = $template;
  124. }
  125. public function getTemplate()
  126. {
  127. return $this->template;
  128. }
  129. public function setType($type)
  130. {
  131. $this->type = $type;
  132. }
  133. public function getType()
  134. {
  135. return $this->type;
  136. }
  137. public function setParent($parent)
  138. {
  139. $this->parent = $parent;
  140. }
  141. public function getParent()
  142. {
  143. return $this->parent;
  144. }
  145. public function setAssociationMapping(array $associationMapping)
  146. {
  147. $this->associationMapping = $associationMapping;
  148. $this->type = $associationMapping['type'];
  149. $this->fieldName = $associationMapping['fieldName'];
  150. }
  151. public function getAssociationMapping()
  152. {
  153. return $this->associationMapping;
  154. }
  155. public function getTargetEntity()
  156. {
  157. if ($this->associationMapping) {
  158. return $this->associationMapping['targetEntity'];
  159. }
  160. return null;
  161. }
  162. public function setFieldMapping(array $fieldMapping)
  163. {
  164. $this->fieldMapping = $fieldMapping;
  165. $this->type = $this->type ?: $fieldMapping['type'];
  166. $this->fieldName = $this->fieldName ?: $fieldMapping['fieldName'];
  167. }
  168. public function getFieldMapping()
  169. {
  170. return $this->fieldMapping;
  171. }
  172. /**
  173. * set the association admin instance
  174. *
  175. */
  176. public function setAssociationAdmin(Admin $associationAdmin)
  177. {
  178. $this->associationAdmin = $associationAdmin;
  179. $this->associationAdmin->setParentFieldDescription($this);
  180. }
  181. /**
  182. * return the associated Admin instance, only available when the property
  183. * is linked to an entity
  184. *
  185. * @return Admin
  186. */
  187. public function getAssociationAdmin()
  188. {
  189. return $this->associationAdmin;
  190. }
  191. /**
  192. *
  193. * return true if the FieldDescription is linked to an identifier field
  194. *
  195. * @return bool
  196. */
  197. public function isIdentifier()
  198. {
  199. return isset($this->fieldMapping['id']) ? $this->fieldMapping['id'] : false;
  200. }
  201. /**
  202. * return the value linked to the description
  203. *
  204. * @param $object
  205. * @return bool|mixed
  206. */
  207. public function getValue($object)
  208. {
  209. $value = false;
  210. $fieldName = $this->getFieldName();
  211. $getter = 'get'.self::camelize($fieldName);
  212. if (method_exists($object, $getter)) {
  213. $value = call_user_func(array($object, $getter));
  214. } else if ($this->getOption('code') && method_exists($object, $this->getOption('code'))) {
  215. $value = call_user_func(array($object, $this->getOption('code')));
  216. }
  217. return $value;
  218. }
  219. public function setAdmin($admin)
  220. {
  221. $this->admin = $admin;
  222. }
  223. public function getAdmin()
  224. {
  225. return $this->admin;
  226. }
  227. public static function camelize($property)
  228. {
  229. return preg_replace(array('/(^|_)+(.)/e', '/\.(.)/e'), array("strtoupper('\\2')", "'_'.strtoupper('\\1')"), $property);
  230. }
  231. }