SonataAdminExtension.php 8.2 KB

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