BaseFieldDescription.php 9.6 KB

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