SonataAdminExtension.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) 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\Twig\Extension;
  11. use Doctrine\Common\Util\ClassUtils;
  12. use Psr\Log\LoggerInterface;
  13. use Sonata\AdminBundle\Admin\AdminInterface;
  14. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  15. use Sonata\AdminBundle\Admin\Pool;
  16. use Sonata\AdminBundle\Exception\NoValueException;
  17. /**
  18. * Class SonataAdminExtension.
  19. *
  20. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  21. */
  22. class SonataAdminExtension extends \Twig_Extension implements \Twig_Extension_InitRuntimeInterface
  23. {
  24. /**
  25. * @var Pool
  26. */
  27. protected $pool;
  28. /**
  29. * @var LoggerInterface
  30. */
  31. protected $logger;
  32. /**
  33. * @param Pool $pool
  34. * @param LoggerInterface $logger
  35. */
  36. public function __construct(Pool $pool, LoggerInterface $logger = null)
  37. {
  38. $this->pool = $pool;
  39. $this->logger = $logger;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function getFilters()
  45. {
  46. return array(
  47. new \Twig_SimpleFilter(
  48. 'render_list_element',
  49. array($this, 'renderListElement'),
  50. array('is_safe' => array('html'),
  51. 'needs_environment' => true, )
  52. ),
  53. new \Twig_SimpleFilter(
  54. 'render_view_element',
  55. array($this, 'renderViewElement'),
  56. array('is_safe' => array('html'),
  57. 'needs_environment' => true, )
  58. ),
  59. new \Twig_SimpleFilter(
  60. 'render_view_element_compare',
  61. array($this, 'renderViewElementCompare'),
  62. array('is_safe' => array('html'))
  63. ),
  64. new \Twig_SimpleFilter(
  65. 'render_relation_element',
  66. array($this, 'renderRelationElement')
  67. ),
  68. new \Twig_SimpleFilter(
  69. 'sonata_urlsafeid',
  70. array($this, 'getUrlsafeIdentifier')
  71. ),
  72. new \Twig_SimpleFilter(
  73. 'sonata_xeditable_type',
  74. array($this, 'getXEditableType')
  75. ),
  76. );
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function getName()
  82. {
  83. return 'sonata_admin';
  84. }
  85. /**
  86. * Get template.
  87. *
  88. * @param FieldDescriptionInterface $fieldDescription
  89. * @param string $defaultTemplate
  90. *
  91. * @return \Twig_Template
  92. */
  93. protected function getTemplate(
  94. FieldDescriptionInterface $fieldDescription,
  95. $defaultTemplate,
  96. \Twig_Environment $environment
  97. ) {
  98. $templateName = $fieldDescription->getTemplate() ?: $defaultTemplate;
  99. try {
  100. $template = $environment->loadTemplate($templateName);
  101. } catch (\Twig_Error_Loader $e) {
  102. $template = $environment->loadTemplate($defaultTemplate);
  103. if (null !== $this->logger) {
  104. $this->logger->warning(sprintf(
  105. 'An error occured trying to load the template "%s" for the field "%s", '.
  106. 'the default template "%s" was used instead.',
  107. $templateName,
  108. $fieldDescription->getFieldName(),
  109. $defaultTemplate
  110. ), array('exception' => $e));
  111. }
  112. }
  113. return $template;
  114. }
  115. /**
  116. * render a list element from the FieldDescription.
  117. *
  118. * @param mixed $object
  119. * @param FieldDescriptionInterface $fieldDescription
  120. * @param array $params
  121. *
  122. * @return string
  123. */
  124. public function renderListElement(
  125. \Twig_Environment $environment,
  126. $object,
  127. FieldDescriptionInterface $fieldDescription,
  128. $params = array()
  129. ) {
  130. $template = $this->getTemplate(
  131. $fieldDescription,
  132. $fieldDescription->getAdmin()->getTemplate('base_list_field'),
  133. $environment
  134. );
  135. return $this->output($fieldDescription, $template, array_merge($params, array(
  136. 'admin' => $fieldDescription->getAdmin(),
  137. 'object' => $object,
  138. 'value' => $this->getValueFromFieldDescription($object, $fieldDescription),
  139. 'field_description' => $fieldDescription,
  140. )), $environment);
  141. }
  142. /**
  143. * @param FieldDescriptionInterface $fieldDescription
  144. * @param \Twig_Template $template
  145. * @param array $parameters
  146. *
  147. * @return string
  148. */
  149. public function output(
  150. FieldDescriptionInterface $fieldDescription,
  151. \Twig_Template $template,
  152. array $parameters,
  153. \Twig_Environment $environment
  154. ) {
  155. $content = $template->render($parameters);
  156. if ($environment->isDebug()) {
  157. $commentTemplate = <<<EOT
  158. <!-- START
  159. fieldName: %s
  160. template: %s
  161. compiled template: %s
  162. -->
  163. %s
  164. <!-- END - fieldName: %s -->
  165. EOT;
  166. return sprintf(
  167. $commentTemplate,
  168. $fieldDescription->getFieldName(),
  169. $fieldDescription->getTemplate(),
  170. $template->getTemplateName(),
  171. $content,
  172. $fieldDescription->getFieldName()
  173. );
  174. }
  175. return $content;
  176. }
  177. /**
  178. * return the value related to FieldDescription, if the associated object does no
  179. * exists => a temporary one is created.
  180. *
  181. * @param object $object
  182. * @param FieldDescriptionInterface $fieldDescription
  183. * @param array $params
  184. *
  185. * @throws \RuntimeException
  186. *
  187. * @return mixed
  188. */
  189. public function getValueFromFieldDescription(
  190. $object,
  191. FieldDescriptionInterface $fieldDescription,
  192. array $params = array()
  193. ) {
  194. if (isset($params['loop']) && $object instanceof \ArrayAccess) {
  195. throw new \RuntimeException('remove the loop requirement');
  196. }
  197. $value = null;
  198. try {
  199. $value = $fieldDescription->getValue($object);
  200. } catch (NoValueException $e) {
  201. if ($fieldDescription->getAssociationAdmin()) {
  202. $value = $fieldDescription->getAssociationAdmin()->getNewInstance();
  203. }
  204. }
  205. return $value;
  206. }
  207. /**
  208. * render a view element.
  209. *
  210. * @param FieldDescriptionInterface $fieldDescription
  211. * @param mixed $object
  212. *
  213. * @return string
  214. */
  215. public function renderViewElement(
  216. \Twig_Environment $environment,
  217. FieldDescriptionInterface $fieldDescription,
  218. $object
  219. ) {
  220. $template = $this->getTemplate(
  221. $fieldDescription,
  222. 'SonataAdminBundle:CRUD:base_show_field.html.twig',
  223. $environment
  224. );
  225. try {
  226. $value = $fieldDescription->getValue($object);
  227. } catch (NoValueException $e) {
  228. $value = null;
  229. }
  230. return $this->output($fieldDescription, $template, array(
  231. 'field_description' => $fieldDescription,
  232. 'object' => $object,
  233. 'value' => $value,
  234. 'admin' => $fieldDescription->getAdmin(),
  235. ), $environment);
  236. }
  237. /**
  238. * render a compared view element.
  239. *
  240. * @param FieldDescriptionInterface $fieldDescription
  241. * @param mixed $baseObject
  242. * @param mixed $compareObject
  243. *
  244. * @return string
  245. */
  246. public function renderViewElementCompare(
  247. \Twig_Environment $environment,
  248. FieldDescriptionInterface $fieldDescription,
  249. $baseObject,
  250. $compareObject
  251. ) {
  252. $template = $this->getTemplate(
  253. $fieldDescription,
  254. 'SonataAdminBundle:CRUD:base_show_field.html.twig',
  255. $environment
  256. );
  257. try {
  258. $baseValue = $fieldDescription->getValue($baseObject);
  259. } catch (NoValueException $e) {
  260. $baseValue = null;
  261. }
  262. try {
  263. $compareValue = $fieldDescription->getValue($compareObject);
  264. } catch (NoValueException $e) {
  265. $compareValue = null;
  266. }
  267. $baseValueOutput = $template->render(array(
  268. 'admin' => $fieldDescription->getAdmin(),
  269. 'field_description' => $fieldDescription,
  270. 'value' => $baseValue,
  271. ));
  272. $compareValueOutput = $template->render(array(
  273. 'field_description' => $fieldDescription,
  274. 'admin' => $fieldDescription->getAdmin(),
  275. 'value' => $compareValue,
  276. ));
  277. // Compare the rendered output of both objects by using the (possibly) overridden field block
  278. $isDiff = $baseValueOutput !== $compareValueOutput;
  279. return $this->output($fieldDescription, $template, array(
  280. 'field_description' => $fieldDescription,
  281. 'value' => $baseValue,
  282. 'value_compare' => $compareValue,
  283. 'is_diff' => $isDiff,
  284. 'admin' => $fieldDescription->getAdmin(),
  285. ), $environment);
  286. }
  287. /**
  288. * @throws \RunTimeException
  289. *
  290. * @param mixed $element
  291. * @param FieldDescriptionInterface $fieldDescription
  292. *
  293. * @return mixed
  294. */
  295. public function renderRelationElement($element, FieldDescriptionInterface $fieldDescription)
  296. {
  297. if (!is_object($element)) {
  298. return $element;
  299. }
  300. $propertyPath = $fieldDescription->getOption('associated_property');
  301. if (null === $propertyPath) {
  302. // For BC kept associated_tostring option behavior
  303. $method = $fieldDescription->getOption('associated_tostring');
  304. if ($method) {
  305. @trigger_error(
  306. 'Option "associated_tostring" is deprecated since version 2.3. Use "associated_property" instead.',
  307. E_USER_DEPRECATED
  308. );
  309. } else {
  310. $method = '__toString';
  311. }
  312. if (!method_exists($element, $method)) {
  313. throw new \RuntimeException(sprintf(
  314. 'You must define an `associated_property` option or '.
  315. 'create a `%s::__toString` method to the field option %s from service %s is ',
  316. get_class($element),
  317. $fieldDescription->getName(),
  318. $fieldDescription->getAdmin()->getCode()
  319. ));
  320. }
  321. return call_user_func(array($element, $method));
  322. }
  323. if (is_callable($propertyPath)) {
  324. return $propertyPath($element);
  325. }
  326. return $this->pool->getPropertyAccessor()->getValue($element, $propertyPath);
  327. }
  328. /**
  329. * Get the identifiers as a string that is save to use in an url.
  330. *
  331. * @param object $model
  332. * @param AdminInterface $admin
  333. *
  334. * @return string string representation of the id that is save to use in an url
  335. */
  336. public function getUrlsafeIdentifier($model, AdminInterface $admin = null)
  337. {
  338. if (is_null($admin)) {
  339. $admin = $this->pool->getAdminByClass(ClassUtils::getClass($model));
  340. }
  341. return $admin->getUrlsafeIdentifier($model);
  342. }
  343. /**
  344. * @param $type
  345. *
  346. * @return string|bool
  347. */
  348. public function getXEditableType($type)
  349. {
  350. $mapping = array(
  351. 'boolean' => 'select',
  352. 'text' => 'text',
  353. 'textarea' => 'textarea',
  354. 'html' => 'textarea',
  355. 'email' => 'email',
  356. 'string' => 'text',
  357. 'smallint' => 'text',
  358. 'bigint' => 'text',
  359. 'integer' => 'number',
  360. 'decimal' => 'number',
  361. 'currency' => 'number',
  362. 'percent' => 'number',
  363. 'url' => 'url',
  364. 'date' => 'date',
  365. );
  366. return isset($mapping[$type]) ? $mapping[$type] : false;
  367. }
  368. }