BaseFieldDescription.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 ORM association mapping
  73. */
  74. protected $associationMapping;
  75. /**
  76. * @var array the ORM field information
  77. */
  78. protected $fieldMapping;
  79. /*
  80. * var array the ORM parent mapping association
  81. */
  82. protected $parentAssociationMappings;
  83. /**
  84. * @var string the template name
  85. */
  86. protected $template;
  87. /**
  88. * @var array the option collection
  89. */
  90. protected $options = array();
  91. /**
  92. * @var Admin|null the parent Admin instance
  93. */
  94. protected $parent = null;
  95. /**
  96. * @var Admin the related admin instance
  97. */
  98. protected $admin;
  99. /**
  100. * @var Admin the associated admin class if the object is associated to another entity
  101. */
  102. protected $associationAdmin;
  103. /**
  104. * @var string the help message to display
  105. */
  106. protected $help;
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function setFieldName($fieldName)
  111. {
  112. $this->fieldName = $fieldName;
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function getFieldName()
  118. {
  119. return $this->fieldName;
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function setName($name)
  125. {
  126. $this->name = $name;
  127. if (!$this->getFieldName()) {
  128. $this->setFieldName(substr(strrchr('.' . $name, '.'), 1));
  129. }
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function getName()
  135. {
  136. return $this->name;
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function getOption($name, $default = null)
  142. {
  143. return isset($this->options[$name]) ? $this->options[$name] : $default;
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function setOption($name, $value)
  149. {
  150. $this->options[$name] = $value;
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function setOptions(array $options)
  156. {
  157. // set the type if provided
  158. if (isset($options['type'])) {
  159. $this->setType($options['type']);
  160. unset($options['type']);
  161. }
  162. // remove property value
  163. if (isset($options['template'])) {
  164. $this->setTemplate($options['template']);
  165. unset($options['template']);
  166. }
  167. // set help if provided
  168. if (isset($options['help'])) {
  169. $this->setHelp($options['help']);
  170. unset($options['help']);
  171. }
  172. $this->options = $options;
  173. }
  174. /**
  175. * {@inheritdoc}
  176. */
  177. public function getOptions()
  178. {
  179. return $this->options;
  180. }
  181. /**
  182. * {@inheritdoc}
  183. */
  184. public function setTemplate($template)
  185. {
  186. $this->template = $template;
  187. }
  188. /**
  189. * {@inheritdoc}
  190. */
  191. public function getTemplate()
  192. {
  193. return $this->template;
  194. }
  195. /**
  196. * {@inheritdoc}
  197. */
  198. public function setType($type)
  199. {
  200. $this->type = $type;
  201. }
  202. /**
  203. * {@inheritdoc}
  204. */
  205. public function getType()
  206. {
  207. return $this->type;
  208. }
  209. /**
  210. * {@inheritdoc}
  211. */
  212. public function setParent(AdminInterface $parent)
  213. {
  214. $this->parent = $parent;
  215. }
  216. /**
  217. * {@inheritdoc}
  218. */
  219. public function getParent()
  220. {
  221. return $this->parent;
  222. }
  223. /**
  224. * {@inheritdoc}
  225. */
  226. public function getAssociationMapping()
  227. {
  228. return $this->associationMapping;
  229. }
  230. /**
  231. * {@inheritdoc}
  232. */
  233. public function getFieldMapping()
  234. {
  235. return $this->fieldMapping;
  236. }
  237. /**
  238. * {@inheritdoc}
  239. */
  240. public function getParentAssociationMappings()
  241. {
  242. return $this->parentAssociationMappings;
  243. }
  244. /**
  245. * set the association admin instance (only used if the field is linked to an Admin)
  246. *
  247. * @param \Sonata\AdminBundle\Admin\AdminInterface $associationAdmin the associated admin
  248. * {@inheritdoc}
  249. */
  250. public function setAssociationAdmin(AdminInterface $associationAdmin)
  251. {
  252. $this->associationAdmin = $associationAdmin;
  253. $this->associationAdmin->setParentFieldDescription($this);
  254. }
  255. /**
  256. * {@inheritdoc}
  257. */
  258. public function getAssociationAdmin()
  259. {
  260. return $this->associationAdmin;
  261. }
  262. /**
  263. * {@inheritdoc}
  264. */
  265. public function hasAssociationAdmin()
  266. {
  267. return $this->associationAdmin !== null;
  268. }
  269. /**
  270. * {@inheritdoc}
  271. */
  272. public function getFieldValue($object, $fieldName)
  273. {
  274. $camelizedFieldName = self::camelize($fieldName);
  275. $getters = array();
  276. // prefer method name given in the code option
  277. if ($this->getOption('code')) {
  278. $getters[] = $this->getOption('code');
  279. }
  280. $getters[] = 'get' . $camelizedFieldName;
  281. $getters[] = 'is' . $camelizedFieldName;
  282. foreach ($getters as $getter) {
  283. if (method_exists($object, $getter)) {
  284. return call_user_func(array($object, $getter));
  285. }
  286. }
  287. if (isset($object->{$fieldName})) {
  288. return $object->{$fieldName};
  289. }
  290. throw new NoValueException(sprintf('Unable to retrieve the value of `%s`', $this->getName()));
  291. }
  292. /**
  293. * {@inheritdoc}
  294. */
  295. public function setAdmin(AdminInterface $admin)
  296. {
  297. $this->admin = $admin;
  298. }
  299. /**
  300. * {@inheritdoc}
  301. */
  302. public function getAdmin()
  303. {
  304. return $this->admin;
  305. }
  306. /**
  307. * {@inheritdoc}
  308. */
  309. public function mergeOption($name, array $options = array())
  310. {
  311. if (!isset($this->options[$name])) {
  312. $this->options[$name] = array();
  313. }
  314. if (!is_array($this->options[$name])) {
  315. throw new \RuntimeException(sprintf('The key `%s` does not point to an array value', $name));
  316. }
  317. $this->options[$name] = array_merge($this->options[$name], $options);
  318. }
  319. /**
  320. * {@inheritdoc}
  321. */
  322. public function mergeOptions(array $options = array())
  323. {
  324. $this->setOptions(array_merge_recursive($this->options, $options));
  325. }
  326. /**
  327. * {@inheritdoc}
  328. */
  329. public function setMappingType($mappingType)
  330. {
  331. $this->mappingType = $mappingType;
  332. }
  333. /**
  334. * {@inheritdoc}
  335. */
  336. public function getMappingType()
  337. {
  338. return $this->mappingType;
  339. }
  340. /**
  341. * Camelize a string
  342. *
  343. * @static
  344. *
  345. * @param string $property
  346. *
  347. * @return string
  348. */
  349. public static function camelize($property)
  350. {
  351. return preg_replace(array('/(^|_| )+(.)/e', '/\.(.)/e'), array("strtoupper('\\2')", "'_'.strtoupper('\\1')"), $property);
  352. }
  353. /**
  354. * Defines the help message
  355. *
  356. * @param string $help
  357. */
  358. public function setHelp($help)
  359. {
  360. $this->help = $help;
  361. }
  362. /**
  363. * {@inheritdoc}
  364. */
  365. public function getHelp()
  366. {
  367. return $this->help;
  368. }
  369. /**
  370. * {@inheritdoc}
  371. */
  372. public function getLabel()
  373. {
  374. return $this->getOption('label');
  375. }
  376. /**
  377. * {@inheritdoc}
  378. */
  379. public function isSortable()
  380. {
  381. return $this->getOption('sortable', false);
  382. }
  383. /**
  384. * {@inheritdoc}
  385. */
  386. public function getSortFieldMapping()
  387. {
  388. return $this->getOption('sort_field_mapping');
  389. }
  390. /**
  391. * {@inheritdoc}
  392. */
  393. public function getSortParentAssociationMapping()
  394. {
  395. return $this->getOption('sort_parent_association_mappings');
  396. }
  397. /**
  398. * {@inheritDoc}
  399. */
  400. public function getTranslationDomain()
  401. {
  402. return $this->getOption('translation_domain') ? : $this->getAdmin()->getTranslationDomain();
  403. }
  404. }