BaseFieldDescription.php 10 KB

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