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