SonataAdminExtension.php 12 KB

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