SonataAdminExtension.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  12. use Sonata\AdminBundle\Filter\FilterInterface;
  13. use Sonata\AdminBundle\Admin\NoValueException;
  14. use Symfony\Component\Form\FormView;
  15. class SonataAdminExtension extends \Twig_Extension
  16. {
  17. /**
  18. * @var \Twig_Environment
  19. */
  20. protected $environment;
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function initRuntime(\Twig_Environment $environment)
  25. {
  26. $this->environment = $environment;
  27. }
  28. /**
  29. * Returns a list of filters to add to the existing list.
  30. *
  31. * @return array An array of filters
  32. */
  33. public function getFilters()
  34. {
  35. return array(
  36. 'render_list_element' => new \Twig_Filter_Method($this, 'renderListElement', array('is_safe' => array('html'))),
  37. 'render_form_element' => new \Twig_Filter_Method($this, 'renderFormElement', array('is_safe' => array('html'))),
  38. 'render_filter_element' => new \Twig_Filter_Method($this, 'renderFilterElement', array('is_safe' => array('html'))),
  39. 'render_view_element' => new \Twig_Filter_Method($this, 'renderViewElement', array('is_safe' => array('html'))),
  40. );
  41. }
  42. public function getTokenParsers()
  43. {
  44. return array();
  45. }
  46. /**
  47. * Returns the name of the extension.
  48. *
  49. * @return string The extension name
  50. */
  51. public function getName()
  52. {
  53. return 'sonata_admin';
  54. }
  55. /**
  56. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  57. * @param string $default
  58. * @return \Twig_TemplateInterface
  59. */
  60. protected function getTemplate(FieldDescriptionInterface $fieldDescription, $default)
  61. {
  62. // todo: find a better solution
  63. try {
  64. $template = $this->environment->loadTemplate($fieldDescription->getTemplate());
  65. } catch(\Twig_Error_Loader $e) {
  66. $template = $this->environment->loadTemplate($default);
  67. }
  68. return $template;
  69. }
  70. /**
  71. * render a list element from the FieldDescription
  72. *
  73. * @param mixed $object
  74. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  75. * @param array $params
  76. * @return
  77. */
  78. public function renderListElement($object, FieldDescriptionInterface $fieldDescription, $params = array())
  79. {
  80. $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_list.html.twig');
  81. return $this->output($fieldDescription, $template, array_merge($params, array(
  82. 'admin' => $fieldDescription->getAdmin(),
  83. 'object' => $object,
  84. 'value' => $this->getValueFromFieldDescription($object, $fieldDescription),
  85. 'field_description' => $fieldDescription
  86. )));
  87. }
  88. /**
  89. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  90. * @param string $content
  91. * @return string
  92. */
  93. public function output(FieldDescriptionInterface $fieldDescription, \Twig_TemplateInterface $template, array $parameters = array())
  94. {
  95. $content = $template->render($parameters);
  96. if ($this->environment->isDebug()) {
  97. return sprintf("\n<!-- START \n fieldName: %s\n template: %s\n compiled template: %s\n -->\n%s\n<!-- END - fieldName: %s -->",
  98. $fieldDescription->getFieldName(),
  99. $fieldDescription->getTemplate(),
  100. $template->getTemplateName(),
  101. $content,
  102. $fieldDescription->getFieldName()
  103. );
  104. }
  105. return $content;
  106. }
  107. /**
  108. * return the value related to FieldDescription, if the associated object does no
  109. * exists => a temporary one is created
  110. *
  111. * @param object $object
  112. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  113. * @param array $params
  114. * @return mixed
  115. */
  116. public function getValueFromFieldDescription($object, FieldDescriptionInterface $fieldDescription, array $params = array())
  117. {
  118. if (isset($params['loop']) && $object instanceof \ArrayAccess) {
  119. throw new \RuntimeException('remove the loop requirement');
  120. }
  121. $value = null;
  122. try {
  123. $value = $fieldDescription->getValue($object);
  124. } catch (NoValueException $e) {
  125. if ($fieldDescription->getAssociationAdmin()) {
  126. $value = $fieldDescription->getAssociationAdmin()->getNewInstance();
  127. }
  128. }
  129. return $value;
  130. }
  131. /**
  132. * render a filter element
  133. *
  134. * @param \Sonata\AdminBundle\Filter\FilterInterface $filter
  135. * @param array $params
  136. * @return string
  137. */
  138. public function renderFilterElement(FilterInterface $filter, array $params = array())
  139. {
  140. $fieldDescription = $filter->getFieldDescription();
  141. $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_filter_field.html.twig');
  142. return $this->output($fieldDescription, $template, array_merge($params, array(
  143. 'filter' => $filter,
  144. 'filter_form' => $filter->getField()->createView()
  145. )));
  146. }
  147. /**
  148. * render a view element
  149. *
  150. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  151. * @param mixed $object
  152. * @return string
  153. */
  154. public function renderViewElement(FieldDescriptionInterface $fieldDescription, $object)
  155. {
  156. $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_view_field.html.twig');
  157. try {
  158. $value = $fieldDescription->getValue($object);
  159. } catch (NoValueException $e) {
  160. $value = null;
  161. }
  162. return $this->output($fieldDescription, $template, array(
  163. 'field_description' => $fieldDescription,
  164. 'object' => $object,
  165. 'value' => $value
  166. ));
  167. }
  168. /**
  169. * render a field element from the FieldDescription
  170. *
  171. * @throws InvalidArgumentException
  172. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  173. * @param \Sumfony\Component\Form\FormView $formView
  174. * @param mixed $object
  175. * @param array $params
  176. * @return string
  177. */
  178. public function renderFormElement(FieldDescriptionInterface $fieldDescription, FormView $formView, $object, $params = array())
  179. {
  180. if (!$fieldDescription->getFieldName()) {
  181. return '';
  182. }
  183. if (!$formView->offsetExists($fieldDescription->getFieldName())) {
  184. return;
  185. }
  186. $children = $formView->offsetGet($fieldDescription->getFieldName());
  187. if (in_array('hidden', $children->get('types'))) {
  188. return '';
  189. }
  190. // find the correct edit parameter
  191. // edit : standard | inline
  192. // inline : natural | table
  193. $parentFieldDescription = $fieldDescription->getAdmin()->getParentFieldDescription();
  194. if (!$parentFieldDescription) {
  195. $params['edit'] = $fieldDescription->getOption('edit', 'standard');
  196. $params['inline'] = $fieldDescription->getOption('inline', 'natural');
  197. $base_template = sprintf('SonataAdminBundle:CRUD:base_%s_edit_field.html.twig', 'standard');
  198. } else {
  199. $params['edit'] = $parentFieldDescription->getOption('edit', 'standard');
  200. $params['inline'] = $parentFieldDescription->getOption('inline', 'natural');
  201. $base_template = sprintf('SonataAdminBundle:CRUD:base_%s_edit_field.html.twig', $params['edit']);
  202. }
  203. $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_standard_edit_field.html.twig');
  204. return $this->output($fieldDescription, $template, array_merge($params, array(
  205. 'admin' => $fieldDescription->getAdmin(),
  206. 'object' => $object,
  207. 'field_description' => $fieldDescription,
  208. 'value' => $this->getValueFromFieldDescription($object, $fieldDescription, $params),
  209. 'field_element' => $children,
  210. 'base_template' => $fieldDescription->getOption('base_template', $base_template)
  211. )));
  212. }
  213. }