BaseFieldDescription.php 11 KB

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