FieldDescription.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 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. * - form: used by the form
  16. * - list: used by the list
  17. * - filter: used by the list filter
  18. *
  19. * Some options are global across 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 string|integer the type
  52. */
  53. protected $type;
  54. /**
  55. * @var string|integer the original mapping type
  56. */
  57. protected $mappingType;
  58. /**
  59. * @var string the field name (of the form)
  60. */
  61. protected $fieldName;
  62. /**
  63. * @var array the Doctrine association mapping
  64. */
  65. protected $associationMapping;
  66. /**
  67. * @var array the Doctrine field information
  68. */
  69. protected $fieldMapping;
  70. /**
  71. * @var string the template name
  72. */
  73. protected $template;
  74. /**
  75. * @var array the option collection
  76. */
  77. protected $options = array();
  78. /**
  79. * @var admin|null the parent Admin instance
  80. */
  81. protected $parent = null;
  82. /**
  83. * @var Admin the related admin instance
  84. */
  85. protected $admin;
  86. /**
  87. * @var Admin the associated admin class if the object is associated to another entity
  88. */
  89. protected $associationAdmin;
  90. public function setFieldName($fieldName)
  91. {
  92. $this->fieldName = $fieldName;
  93. }
  94. public function getFieldName()
  95. {
  96. return $this->fieldName;
  97. }
  98. public function setName($name)
  99. {
  100. $this->name = $name;
  101. if (!$this->getFieldName()) {
  102. $this->setFieldName($name);
  103. }
  104. }
  105. public function getName()
  106. {
  107. return $this->name;
  108. }
  109. public function getOption($name, $default = null)
  110. {
  111. return isset($this->options[$name]) ? $this->options[$name] : $default;
  112. }
  113. public function setOption($name, $value)
  114. {
  115. $this->options[$name] = $value;
  116. }
  117. public function setOptions($options)
  118. {
  119. // set the type if provided
  120. if (isset($options['type'])) {
  121. $this->setType($options['type']);
  122. unset($options['type']);
  123. }
  124. // remove property value
  125. if (isset($options['template'])) {
  126. $this->setTemplate($options['template']);
  127. unset($options['template']);
  128. }
  129. $this->options = $options;
  130. }
  131. public function getOptions()
  132. {
  133. return $this->options;
  134. }
  135. public function setTemplate($template)
  136. {
  137. $this->template = $template;
  138. }
  139. public function getTemplate()
  140. {
  141. return $this->template;
  142. }
  143. public function setType($type)
  144. {
  145. $this->type = $type;
  146. }
  147. public function getType()
  148. {
  149. return $this->type;
  150. }
  151. public function setParent($parent)
  152. {
  153. $this->parent = $parent;
  154. }
  155. public function getParent()
  156. {
  157. return $this->parent;
  158. }
  159. public function setAssociationMapping(array $associationMapping)
  160. {
  161. $this->associationMapping = $associationMapping;
  162. $this->type = $this->type ?: $associationMapping['type'];
  163. $this->mappingType = $this->mappingType ?: $associationMapping['type'];
  164. $this->fieldName = $associationMapping['fieldName'];
  165. }
  166. public function getAssociationMapping()
  167. {
  168. return $this->associationMapping;
  169. }
  170. public function getTargetEntity()
  171. {
  172. if ($this->associationMapping) {
  173. return $this->associationMapping['targetEntity'];
  174. }
  175. return null;
  176. }
  177. public function setFieldMapping(array $fieldMapping)
  178. {
  179. $this->fieldMapping = $fieldMapping;
  180. $this->type = $this->type ?: $fieldMapping['type'];
  181. $this->mappingType = $this->mappingType ?: $fieldMapping['type'];
  182. $this->fieldName = $this->fieldName ?: $fieldMapping['fieldName'];
  183. }
  184. public function getFieldMapping()
  185. {
  186. return $this->fieldMapping;
  187. }
  188. /**
  189. * set the association admin instance
  190. *
  191. */
  192. public function setAssociationAdmin(Admin $associationAdmin)
  193. {
  194. $this->associationAdmin = $associationAdmin;
  195. $this->associationAdmin->setParentFieldDescription($this);
  196. }
  197. /**
  198. * return the associated Admin instance, only available when the property
  199. * is linked to an entity
  200. *
  201. * @return Admin
  202. */
  203. public function getAssociationAdmin()
  204. {
  205. return $this->associationAdmin;
  206. }
  207. /**
  208. *
  209. * return true if the FieldDescription is linked to an identifier field
  210. *
  211. * @return bool
  212. */
  213. public function isIdentifier()
  214. {
  215. return isset($this->fieldMapping['id']) ? $this->fieldMapping['id'] : false;
  216. }
  217. /**
  218. * return the value linked to the description
  219. *
  220. * @param $object
  221. * @return bool|mixed
  222. */
  223. public function getValue($object)
  224. {
  225. $value = false;
  226. $fieldName = $this->getFieldName();
  227. $getter = 'get'.self::camelize($fieldName);
  228. if (method_exists($object, $getter)) {
  229. $value = call_user_func(array($object, $getter));
  230. } else if ($this->getOption('code') && method_exists($object, $this->getOption('code'))) {
  231. $value = call_user_func(array($object, $this->getOption('code')));
  232. }
  233. return $value;
  234. }
  235. public function setAdmin($admin)
  236. {
  237. $this->admin = $admin;
  238. }
  239. public function getAdmin()
  240. {
  241. return $this->admin;
  242. }
  243. public static function camelize($property)
  244. {
  245. return preg_replace(array('/(^|_)+(.)/e', '/\.(.)/e'), array("strtoupper('\\2')", "'_'.strtoupper('\\1')"), $property);
  246. }
  247. public function mergeOption($name, array $options = array())
  248. {
  249. if(!isset($this->options[$name])) {
  250. $this->options[$name] = array();
  251. }
  252. $this->options[$name] = array_merge($this->options[$name], $options);
  253. }
  254. public function mergeOptions(array $options = array())
  255. {
  256. $this->setOptions(array_merge($this->options, $options));
  257. }
  258. public function setMappingType(string $mappingType)
  259. {
  260. $this->mappingType = $mappingType;
  261. }
  262. public function getMappingType()
  263. {
  264. return $this->mappingType;
  265. }
  266. }