SonataAdminExtension.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /*
  3. * This file is part of sonata-project.
  4. *
  5. * (c) 2010 Thomas Rabaix
  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 Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  13. use Sonata\AdminBundle\Exception\NoValueException;
  14. use Sonata\AdminBundle\Admin\Pool;
  15. class SonataAdminExtension extends \Twig_Extension
  16. {
  17. /**
  18. * @var \Twig_Environment
  19. */
  20. protected $environment;
  21. /**
  22. * @var Pool
  23. */
  24. protected $pool;
  25. /**
  26. * @param Pool $pool
  27. */
  28. public function __construct(Pool $pool)
  29. {
  30. $this->pool = $pool;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function initRuntime(\Twig_Environment $environment)
  36. {
  37. $this->environment = $environment;
  38. }
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function getFilters()
  43. {
  44. return array(
  45. 'render_list_element' => new \Twig_Filter_Method($this, 'renderListElement', array('is_safe' => array('html'))),
  46. 'render_view_element' => new \Twig_Filter_Method($this, 'renderViewElement', array('is_safe' => array('html'))),
  47. 'render_relation_element' => new \Twig_Filter_Method($this, 'renderRelationElement'),
  48. 'sonata_urlsafeid' => new \Twig_Filter_Method($this, 'getUrlsafeIdentifier'),
  49. 'sonata_slugify' => new \Twig_Filter_Method($this, 'slugify'),
  50. );
  51. }
  52. /**
  53. * {@inheritDoc}
  54. */
  55. public function getTokenParsers()
  56. {
  57. return array();
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. public function getName()
  63. {
  64. return 'sonata_admin';
  65. }
  66. /**
  67. * @param FieldDescriptionInterface $fieldDescription
  68. * @param string $defaultTemplate
  69. *
  70. * @return \Twig_TemplateInterface
  71. */
  72. protected function getTemplate(FieldDescriptionInterface $fieldDescription, $defaultTemplate)
  73. {
  74. $templateName = $fieldDescription->getTemplate() ? : $defaultTemplate;
  75. try {
  76. $template = $this->environment->loadTemplate($templateName);
  77. } catch (\Twig_Error_Loader $e) {
  78. $template = $this->environment->loadTemplate($defaultTemplate);
  79. }
  80. return $template;
  81. }
  82. /**
  83. * render a list element from the FieldDescription
  84. *
  85. * @param mixed $object
  86. * @param FieldDescriptionInterface $fieldDescription
  87. * @param array $params
  88. *
  89. * @return string
  90. */
  91. public function renderListElement($object, FieldDescriptionInterface $fieldDescription, $params = array())
  92. {
  93. $template = $this->getTemplate($fieldDescription, $fieldDescription->getAdmin()->getTemplate('base_list_field'));
  94. return $this->output($fieldDescription, $template, array_merge($params, array(
  95. 'admin' => $fieldDescription->getAdmin(),
  96. 'object' => $object,
  97. 'value' => $this->getValueFromFieldDescription($object, $fieldDescription),
  98. 'field_description' => $fieldDescription
  99. )));
  100. }
  101. /**
  102. * @param FieldDescriptionInterface $fieldDescription
  103. * @param \Twig_TemplateInterface $template
  104. * @param array $parameters
  105. *
  106. * @return string
  107. */
  108. public function output(FieldDescriptionInterface $fieldDescription, \Twig_TemplateInterface $template, array $parameters = array())
  109. {
  110. $content = $template->render($parameters);
  111. if ($this->environment->isDebug()) {
  112. return sprintf("\n<!-- START \n fieldName: %s\n template: %s\n compiled template: %s\n -->\n%s\n<!-- END - fieldName: %s -->",
  113. $fieldDescription->getFieldName(),
  114. $fieldDescription->getTemplate(),
  115. $template->getTemplateName(),
  116. $content,
  117. $fieldDescription->getFieldName()
  118. );
  119. }
  120. return $content;
  121. }
  122. /**
  123. * return the value related to FieldDescription, if the associated object does no
  124. * exists => a temporary one is created
  125. *
  126. * @param object $object
  127. * @param FieldDescriptionInterface $fieldDescription
  128. * @param array $params
  129. *
  130. * @throws \RuntimeException
  131. *
  132. * @return mixed
  133. */
  134. public function getValueFromFieldDescription($object, FieldDescriptionInterface $fieldDescription, array $params = array())
  135. {
  136. if (isset($params['loop']) && $object instanceof \ArrayAccess) {
  137. throw new \RuntimeException('remove the loop requirement');
  138. }
  139. $value = null;
  140. try {
  141. $value = $fieldDescription->getValue($object);
  142. } catch (NoValueException $e) {
  143. if ($fieldDescription->getAssociationAdmin()) {
  144. $value = $fieldDescription->getAssociationAdmin()->getNewInstance();
  145. }
  146. }
  147. return $value;
  148. }
  149. /**
  150. * render a view element
  151. *
  152. * @param FieldDescriptionInterface $fieldDescription
  153. * @param mixed $object
  154. *
  155. * @return string
  156. */
  157. public function renderViewElement(FieldDescriptionInterface $fieldDescription, $object)
  158. {
  159. $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_show_field.html.twig');
  160. try {
  161. $value = $fieldDescription->getValue($object);
  162. } catch (NoValueException $e) {
  163. $value = null;
  164. }
  165. return $this->output($fieldDescription, $template, array(
  166. 'field_description' => $fieldDescription,
  167. 'object' => $object,
  168. 'value' => $value,
  169. 'admin' => $fieldDescription->getAdmin()
  170. ));
  171. }
  172. /**
  173. * @throws \RunTimeException
  174. *
  175. * @param mixed $element
  176. * @param FieldDescriptionInterface $fieldDescription
  177. *
  178. * @return mixed
  179. */
  180. public function renderRelationElement($element, FieldDescriptionInterface $fieldDescription)
  181. {
  182. if (!is_object($element)) {
  183. return $element;
  184. }
  185. $method = $fieldDescription->getOption('associated_tostring', '__toString');
  186. if (!method_exists($element, $method)) {
  187. throw new \RunTimeException(sprintf('You must define an `associated_tostring` option or create a `%s::__toString` method to the field option "%s" from service "%s".', get_class($element), $fieldDescription->getName(), $fieldDescription->getAdmin()->getCode()));
  188. }
  189. return call_user_func(array($element, $method));
  190. }
  191. /**
  192. * Get the identifiers as a string that is save to use in an url.
  193. *
  194. * @param object $model
  195. *
  196. * @return string string representation of the id that is save to use in an url
  197. */
  198. public function getUrlsafeIdentifier($model)
  199. {
  200. $admin = $this->pool->getAdminByClass(
  201. ClassUtils::getClass($model)
  202. );
  203. return $admin->getUrlsafeIdentifier($model);
  204. }
  205. /**
  206. * Slugify a text
  207. *
  208. * @param $text
  209. *
  210. * @return string
  211. */
  212. public function slugify($text)
  213. {
  214. // replace non letter or digits by -
  215. $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
  216. // trim
  217. $text = trim($text, '-');
  218. // transliterate
  219. if (function_exists('iconv')) {
  220. $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  221. }
  222. // lowercase
  223. $text = strtolower($text);
  224. // remove unwanted characters
  225. $text = preg_replace('~[^-\w]+~', '', $text);
  226. return $text;
  227. }
  228. }