SonataAdminExtension.php 8.3 KB

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