BaseFieldDescription.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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_options (o): options given to the filter field object
  53. * - field_type (o): options given to the filter field object
  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. $code = $this->getOption('code');
  286. if (is_callable($code)) {
  287. return call_user_func($code, $object);
  288. }
  289. $camelizedFieldName = self::camelize($fieldName);
  290. $getters = array();
  291. $parameters = array();
  292. // prefer method name given in the code option
  293. if ($code) {
  294. $getters[] = $code;
  295. }
  296. // parameters for the method given in the code option
  297. if($this->getOption('parameters')){
  298. $parameters = $this->getOption('parameters');
  299. }
  300. $getters[] = 'get' . $camelizedFieldName;
  301. $getters[] = 'is' . $camelizedFieldName;
  302. foreach ($getters as $getter) {
  303. if (method_exists($object, $getter)) {
  304. return call_user_func_array(array($object, $getter),$parameters);
  305. }
  306. }
  307. if (isset($object->{$fieldName})) {
  308. return $object->{$fieldName};
  309. }
  310. throw new NoValueException(sprintf('Unable to retrieve the value of `%s`', $this->getName()));
  311. }
  312. /**
  313. * {@inheritdoc}
  314. */
  315. public function setAdmin(AdminInterface $admin)
  316. {
  317. $this->admin = $admin;
  318. }
  319. /**
  320. * {@inheritdoc}
  321. */
  322. public function getAdmin()
  323. {
  324. return $this->admin;
  325. }
  326. /**
  327. * {@inheritdoc}
  328. */
  329. public function mergeOption($name, array $options = array())
  330. {
  331. if (!isset($this->options[$name])) {
  332. $this->options[$name] = array();
  333. }
  334. if (!is_array($this->options[$name])) {
  335. throw new \RuntimeException(sprintf('The key `%s` does not point to an array value', $name));
  336. }
  337. $this->options[$name] = array_merge($this->options[$name], $options);
  338. }
  339. /**
  340. * {@inheritdoc}
  341. */
  342. public function mergeOptions(array $options = array())
  343. {
  344. $this->setOptions(array_merge_recursive($this->options, $options));
  345. }
  346. /**
  347. * {@inheritdoc}
  348. */
  349. public function setMappingType($mappingType)
  350. {
  351. $this->mappingType = $mappingType;
  352. }
  353. /**
  354. * {@inheritdoc}
  355. */
  356. public function getMappingType()
  357. {
  358. return $this->mappingType;
  359. }
  360. /**
  361. * Camelize a string
  362. *
  363. * @static
  364. *
  365. * @param string $property
  366. *
  367. * @return string
  368. */
  369. public static function camelize($property)
  370. {
  371. return preg_replace_callback('/(^|[_. ])+(.)/', function ($match) {
  372. return ('.' === $match[1] ? '_' : '') . strtoupper($match[2]);
  373. }, $property);
  374. }
  375. /**
  376. * Defines the help message
  377. *
  378. * @param string $help
  379. */
  380. public function setHelp($help)
  381. {
  382. $this->help = $help;
  383. }
  384. /**
  385. * {@inheritdoc}
  386. */
  387. public function getHelp()
  388. {
  389. return $this->help;
  390. }
  391. /**
  392. * {@inheritdoc}
  393. */
  394. public function getLabel()
  395. {
  396. return $this->getOption('label');
  397. }
  398. /**
  399. * {@inheritdoc}
  400. */
  401. public function isSortable()
  402. {
  403. return false !== $this->getOption('sortable', false);
  404. }
  405. /**
  406. * {@inheritdoc}
  407. */
  408. public function getSortFieldMapping()
  409. {
  410. return $this->getOption('sort_field_mapping');
  411. }
  412. /**
  413. * {@inheritdoc}
  414. */
  415. public function getSortParentAssociationMapping()
  416. {
  417. return $this->getOption('sort_parent_association_mappings');
  418. }
  419. /**
  420. * {@inheritDoc}
  421. */
  422. public function getTranslationDomain()
  423. {
  424. return $this->getOption('translation_domain') ? : $this->getAdmin()->getTranslationDomain();
  425. }
  426. }