BaseFieldDescription.php 11 KB

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