BaseFieldDescription.php 11 KB

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