FieldDescription.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) 2010-2011 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\AdminBundle\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. * - link_parameters (o) : add link parameter to the related Admin class when
  27. * the Admin.generateUrl is called
  28. *
  29. * Form Field options :
  30. * - form_field_widget (o): the widget class to use to render the field
  31. * - form_field_options (o): the options to give to the widget
  32. * - edit (o) : list|inline|standard (only used for associated admin)
  33. * - list : open a popup where the user can search, filter and click on one field
  34. * to select one item
  35. * - inline : the associated form admin is embedded into the current form
  36. * - standard : the associated admin is created through a popup
  37. *
  38. * List Field options :
  39. * - identifier (o): if set to true a link appear on to edit the element
  40. *
  41. * Filter Field options :
  42. * - filter_options (o): options given to the Filter object
  43. * - filter_field_options (o): options given to the filter field object
  44. *
  45. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  46. */
  47. class FieldDescription
  48. {
  49. /**
  50. * @var string the field name
  51. */
  52. protected $name;
  53. /**
  54. * @var string|integer the type
  55. */
  56. protected $type;
  57. /**
  58. * @var string|integer the original mapping type
  59. */
  60. protected $mappingType;
  61. /**
  62. * @var string the field name (of the form)
  63. */
  64. protected $fieldName;
  65. /**
  66. * @var array the Doctrine association mapping
  67. */
  68. protected $associationMapping;
  69. /**
  70. * @var array the Doctrine field information
  71. */
  72. protected $fieldMapping;
  73. /**
  74. * @var string the template name
  75. */
  76. protected $template;
  77. /**
  78. * @var array the option collection
  79. */
  80. protected $options = array();
  81. /**
  82. * @var Admin|null the parent Admin instance
  83. */
  84. protected $parent = null;
  85. /**
  86. * @var Admin the related admin instance
  87. */
  88. protected $admin;
  89. /**
  90. * @var Admin the associated admin class if the object is associated to another entity
  91. */
  92. protected $associationAdmin;
  93. /**
  94. * set the field name
  95. *
  96. * @param string $fieldName
  97. * @return void
  98. */
  99. public function setFieldName($fieldName)
  100. {
  101. $this->fieldName = $fieldName;
  102. }
  103. /**
  104. * return the field name
  105. *
  106. * @return string the field name
  107. */
  108. public function getFieldName()
  109. {
  110. return $this->fieldName;
  111. }
  112. /**
  113. * Set the name
  114. *
  115. * @param string $name
  116. * @return void
  117. */
  118. public function setName($name)
  119. {
  120. $this->name = $name;
  121. if (!$this->getFieldName()) {
  122. $this->setFieldName($name);
  123. }
  124. }
  125. /**
  126. * Return the name, the name can be used as a form label or table header
  127. *
  128. * @return string the name
  129. */
  130. public function getName()
  131. {
  132. return $this->name;
  133. }
  134. /**
  135. * Return the value represented by the provided name
  136. *
  137. * @param string $name
  138. * @param null $default
  139. * @return array|null the value represented by the provided name
  140. */
  141. public function getOption($name, $default = null)
  142. {
  143. return isset($this->options[$name]) ? $this->options[$name] : $default;
  144. }
  145. /**
  146. * Define an option, an option is has a name and a value
  147. *
  148. * @param string $name
  149. * @param mixed $value
  150. * @return void set the option value
  151. */
  152. public function setOption($name, $value)
  153. {
  154. $this->options[$name] = $value;
  155. }
  156. /**
  157. * Define the options value, if the options array contains the reserved keywords
  158. * - type
  159. * - template
  160. *
  161. * Then the value are copied across to the related property value
  162. *
  163. * @param array $options
  164. * @return void
  165. */
  166. public function setOptions(array $options)
  167. {
  168. // set the type if provided
  169. if (isset($options['type'])) {
  170. $this->setType($options['type']);
  171. unset($options['type']);
  172. }
  173. // remove property value
  174. if (isset($options['template'])) {
  175. $this->setTemplate($options['template']);
  176. unset($options['template']);
  177. }
  178. $this->options = $options;
  179. }
  180. /**
  181. * return options
  182. *
  183. * @return array options
  184. */
  185. public function getOptions()
  186. {
  187. return $this->options;
  188. }
  189. /**
  190. * return the template used to render the field
  191. *
  192. * @param string $template
  193. * @return void
  194. */
  195. public function setTemplate($template)
  196. {
  197. $this->template = $template;
  198. }
  199. /**
  200. * return the template name
  201. *
  202. * @return string the template name
  203. */
  204. public function getTemplate()
  205. {
  206. return $this->template;
  207. }
  208. /**
  209. * return the field type, the type is a mandatory field as it used to select the correct template
  210. * or the logic associated to the current FieldDescription object
  211. *
  212. * @param string $type
  213. * @return void the field type
  214. */
  215. public function setType($type)
  216. {
  217. $this->type = $type;
  218. }
  219. /**
  220. * return the type
  221. *
  222. * @return int|string
  223. */
  224. public function getType()
  225. {
  226. return $this->type;
  227. }
  228. /**
  229. * set the parent Admin (only used in nested admin)
  230. *
  231. * @param Admin $parent
  232. * @return void
  233. */
  234. public function setParent(Admin $parent)
  235. {
  236. $this->parent = $parent;
  237. }
  238. /**
  239. * return the parent Admin (only used in nested admin)
  240. *
  241. * @return Admin
  242. */
  243. public function getParent()
  244. {
  245. return $this->parent;
  246. }
  247. /**
  248. * Define the association mapping definition
  249. *
  250. * @param array $associationMapping
  251. * @return void
  252. */
  253. public function setAssociationMapping(array $associationMapping)
  254. {
  255. $this->associationMapping = $associationMapping;
  256. $this->type = $this->type ?: $associationMapping['type'];
  257. $this->mappingType = $this->mappingType ?: $associationMapping['type'];
  258. $this->fieldName = $associationMapping['fieldName'];
  259. }
  260. /**
  261. * return the association mapping definition
  262. *
  263. * @return array
  264. */
  265. public function getAssociationMapping()
  266. {
  267. return $this->associationMapping;
  268. }
  269. /**
  270. * return the related Target Entity
  271. *
  272. * @return string|null
  273. */
  274. public function getTargetEntity()
  275. {
  276. if ($this->associationMapping) {
  277. return $this->associationMapping['targetEntity'];
  278. }
  279. return null;
  280. }
  281. /**
  282. * set the field mapping information
  283. *
  284. * @param array $fieldMapping
  285. * @return void
  286. */
  287. public function setFieldMapping(array $fieldMapping)
  288. {
  289. $this->fieldMapping = $fieldMapping;
  290. $this->type = $this->type ?: $fieldMapping['type'];
  291. $this->mappingType = $this->mappingType ?: $fieldMapping['type'];
  292. $this->fieldName = $this->fieldName ?: $fieldMapping['fieldName'];
  293. }
  294. /**
  295. * return the field mapping definition
  296. *
  297. * @return array the field mapping definition
  298. */
  299. public function getFieldMapping()
  300. {
  301. return $this->fieldMapping;
  302. }
  303. /**
  304. * set the association admin instance (only used if the field is linked to an Admin)
  305. *
  306. * @param Admin $associationAdmin the associated admin
  307. */
  308. public function setAssociationAdmin(Admin $associationAdmin)
  309. {
  310. $this->associationAdmin = $associationAdmin;
  311. $this->associationAdmin->setParentFieldDescription($this);
  312. }
  313. /**
  314. * return the associated Admin instance (only used if the field is linked to an Admin)
  315. * @return Admin
  316. */
  317. public function getAssociationAdmin()
  318. {
  319. return $this->associationAdmin;
  320. }
  321. /**
  322. * return true if the FieldDescription is linked to an identifier field
  323. *
  324. * @return bool
  325. */
  326. public function isIdentifier()
  327. {
  328. return isset($this->fieldMapping['id']) ? $this->fieldMapping['id'] : false;
  329. }
  330. /**
  331. * return the value linked to the description
  332. *
  333. * @param $object
  334. * @return bool|mixed
  335. */
  336. public function getValue($object)
  337. {
  338. $value = false;
  339. $fieldName = $this->getFieldName();
  340. $getter = 'get'.self::camelize($fieldName);
  341. if (method_exists($object, $getter)) {
  342. $value = call_user_func(array($object, $getter));
  343. } else if ($this->getOption('code') && method_exists($object, $this->getOption('code'))) {
  344. $value = call_user_func(array($object, $this->getOption('code')));
  345. }
  346. return $value;
  347. }
  348. /**
  349. * set the admin class linked to this FieldDescription
  350. *
  351. * @param Admin $admin
  352. * @return void
  353. */
  354. public function setAdmin(Admin $admin)
  355. {
  356. $this->admin = $admin;
  357. }
  358. /**
  359. * @return Admin the admin class linked to this FieldDescription
  360. */
  361. public function getAdmin()
  362. {
  363. return $this->admin;
  364. }
  365. /**
  366. * Camelize a string
  367. *
  368. * @static
  369. * @param string $property
  370. * @return string
  371. */
  372. public static function camelize($property)
  373. {
  374. return preg_replace(array('/(^|_| )+(.)/e', '/\.(.)/e'), array("strtoupper('\\2')", "'_'.strtoupper('\\1')"), $property);
  375. }
  376. /**
  377. * merge option values related to the provided option name
  378. *
  379. * @throws \RuntimeException
  380. * @param $name
  381. * @param array $options
  382. * @return void
  383. */
  384. public function mergeOption($name, array $options = array())
  385. {
  386. if (!isset($this->options[$name])) {
  387. $this->options[$name] = array();
  388. }
  389. if (!is_array($this->options[$name]))
  390. {
  391. throw new \RuntimeException(sprintf('The key `%s` does not point to an array value', $name));
  392. }
  393. $this->options[$name] = array_merge($this->options[$name], $options);
  394. }
  395. /**
  396. * merge options values
  397. *
  398. * @param array $options
  399. * @return void
  400. */
  401. public function mergeOptions(array $options = array())
  402. {
  403. $this->setOptions(array_merge($this->options, $options));
  404. }
  405. /**
  406. * set the original mapping type (only used if the field is linked to an entity)
  407. *
  408. * @param string|int $mappingType
  409. * @return void
  410. */
  411. public function setMappingType($mappingType)
  412. {
  413. $this->mappingType = $mappingType;
  414. }
  415. /**
  416. * return the mapping type
  417. *
  418. * @return int|string
  419. */
  420. public function getMappingType()
  421. {
  422. return $this->mappingType;
  423. }
  424. }