BaseFieldDescription.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. *
  171. * Then the value are copied across to the related property value
  172. *
  173. * @param array $options
  174. * @return void
  175. */
  176. public function setOptions(array $options)
  177. {
  178. // set the type if provided
  179. if (isset($options['type'])) {
  180. $this->setType($options['type']);
  181. unset($options['type']);
  182. }
  183. // remove property value
  184. if (isset($options['template'])) {
  185. $this->setTemplate($options['template']);
  186. unset($options['template']);
  187. }
  188. $this->options = $options;
  189. }
  190. /**
  191. * return options
  192. *
  193. * @return array options
  194. */
  195. public function getOptions()
  196. {
  197. return $this->options;
  198. }
  199. /**
  200. * return the template used to render the field
  201. *
  202. * @param string $template
  203. * @return void
  204. */
  205. public function setTemplate($template)
  206. {
  207. $this->template = $template;
  208. }
  209. /**
  210. * return the template name
  211. *
  212. * @return string the template name
  213. */
  214. public function getTemplate()
  215. {
  216. return $this->template;
  217. }
  218. /**
  219. * return the field type, the type is a mandatory field as it used to select the correct template
  220. * or the logic associated to the current FieldDescription object
  221. *
  222. * @param string $type
  223. * @return void the field type
  224. */
  225. public function setType($type)
  226. {
  227. $this->type = $type;
  228. }
  229. /**
  230. * return the type
  231. *
  232. * @return int|string
  233. */
  234. public function getType()
  235. {
  236. return $this->type;
  237. }
  238. /**
  239. * set the parent Admin (only used in nested admin)
  240. *
  241. * @param \Sonata\AdminBundle\Admin\AdminInterface $parent
  242. * @return void
  243. */
  244. public function setParent(AdminInterface $parent)
  245. {
  246. $this->parent = $parent;
  247. }
  248. /**
  249. * return the parent Admin (only used in nested admin)
  250. *
  251. * @return \Sonata\AdminBundle\Admin\AdminInterface
  252. */
  253. public function getParent()
  254. {
  255. return $this->parent;
  256. }
  257. /**
  258. * return the association mapping definition
  259. *
  260. * @return array
  261. */
  262. public function getAssociationMapping()
  263. {
  264. return $this->associationMapping;
  265. }
  266. /**
  267. * return the field mapping definition
  268. *
  269. * @return array the field mapping definition
  270. */
  271. public function getFieldMapping()
  272. {
  273. return $this->fieldMapping;
  274. }
  275. /**
  276. * set the association admin instance (only used if the field is linked to an Admin)
  277. *
  278. * @param \Sonata\AdminBundle\Admin\AdminInterface $associationAdmin the associated admin
  279. */
  280. public function setAssociationAdmin(AdminInterface $associationAdmin)
  281. {
  282. $this->associationAdmin = $associationAdmin;
  283. $this->associationAdmin->setParentFieldDescription($this);
  284. }
  285. /**
  286. * return the associated Admin instance (only used if the field is linked to an Admin)
  287. * @return \Sonata\AdminBundle\Admin\AdminInterface
  288. */
  289. public function getAssociationAdmin()
  290. {
  291. return $this->associationAdmin;
  292. }
  293. /**
  294. *
  295. * @return boolean
  296. */
  297. public function hasAssociationAdmin()
  298. {
  299. return $this->associationAdmin !== null;
  300. }
  301. /**
  302. * return the value linked to the description
  303. *
  304. * @param $object
  305. * @return bool|mixed
  306. */
  307. public function getValue($object)
  308. {
  309. $camelizedFieldName = self::camelize($this->getFieldName());
  310. $getters = array();
  311. // prefer method name given in the code option
  312. if ($this->getOption('code')) {
  313. $getters[] = $this->getOption('code');
  314. }
  315. $getters[] = 'get'.$camelizedFieldName;
  316. $getters[] = 'is'.$camelizedFieldName;
  317. foreach ($getters as $getter) {
  318. if (method_exists($object, $getter)) {
  319. return call_user_func(array($object, $getter));
  320. }
  321. }
  322. throw new NoValueException(sprintf('Unable to retrieve the value of `%s`', $this->getName()));
  323. }
  324. /**
  325. * set the admin class linked to this FieldDescription
  326. *
  327. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  328. * @return void
  329. */
  330. public function setAdmin(AdminInterface $admin)
  331. {
  332. $this->admin = $admin;
  333. }
  334. /**
  335. * @return \Sonata\AdminBundle\Admin\AdminInterface the admin class linked to this FieldDescription
  336. */
  337. public function getAdmin()
  338. {
  339. return $this->admin;
  340. }
  341. /**
  342. * merge option values related to the provided option name
  343. *
  344. * @throws \RuntimeException
  345. * @param string $name
  346. * @param array $options
  347. * @return void
  348. */
  349. public function mergeOption($name, array $options = array())
  350. {
  351. if (!isset($this->options[$name])) {
  352. $this->options[$name] = array();
  353. }
  354. if (!is_array($this->options[$name])) {
  355. throw new \RuntimeException(sprintf('The key `%s` does not point to an array value', $name));
  356. }
  357. $this->options[$name] = array_merge($this->options[$name], $options);
  358. }
  359. /**
  360. * merge options values
  361. *
  362. * @param array $options
  363. * @return void
  364. */
  365. public function mergeOptions(array $options = array())
  366. {
  367. $this->setOptions(array_merge_recursive($this->options, $options));
  368. }
  369. /**
  370. * set the original mapping type (only used if the field is linked to an entity)
  371. *
  372. * @param string|int $mappingType
  373. * @return void
  374. */
  375. public function setMappingType($mappingType)
  376. {
  377. $this->mappingType = $mappingType;
  378. }
  379. /**
  380. * return the mapping type
  381. *
  382. * @return int|string
  383. */
  384. public function getMappingType()
  385. {
  386. return $this->mappingType;
  387. }
  388. /**
  389. * Camelize a string
  390. *
  391. * @static
  392. * @param string $property
  393. * @return string
  394. */
  395. public static function camelize($property)
  396. {
  397. return preg_replace(array('/(^|_| )+(.)/e', '/\.(.)/e'), array("strtoupper('\\2')", "'_'.strtoupper('\\1')"), $property);
  398. }
  399. /**
  400. * Defines the help message
  401. *
  402. * @param $string help
  403. */
  404. public function setHelp($help)
  405. {
  406. $this->help = $help;
  407. }
  408. /**
  409. * @return string
  410. */
  411. public function getHelp()
  412. {
  413. return $this->help;
  414. }
  415. /**
  416. * return the label to use for the current field
  417. *
  418. * @return string
  419. */
  420. public function getLabel()
  421. {
  422. return $this->getOption('label');
  423. }
  424. }