BaseApplicationExtension.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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\BaseApplicationBundle\Twig\Extension;
  11. use Sonata\BaseApplicationBundle\Admin\FieldDescription;
  12. use Sonata\BaseApplicationBundle\Filter\Filter;
  13. class BaseApplicationExtension extends \Twig_Extension
  14. {
  15. protected $templating;
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function initRuntime(\Twig_Environment $environment)
  20. {
  21. $this->environment = $environment;
  22. }
  23. /**
  24. * Returns a list of filters to add to the existing list.
  25. *
  26. * @return array An array of filters
  27. */
  28. public function getFilters()
  29. {
  30. return array(
  31. 'render_list_element' => new \Twig_Filter_Method($this, 'renderListElement', array('is_safe' => array('html'))),
  32. 'render_form_element' => new \Twig_Filter_Method($this, 'renderFormElement', array('is_safe' => array('html'))),
  33. 'render_filter_element' => new \Twig_Filter_Method($this, 'renderFilterElement', array('is_safe' => array('html'))),
  34. );
  35. }
  36. public function getTokenParsers()
  37. {
  38. return array(
  39. );
  40. }
  41. /**
  42. * Returns the name of the extension.
  43. *
  44. * @return string The extension name
  45. */
  46. public function getName()
  47. {
  48. return 'base_application';
  49. }
  50. /**
  51. * render a list element from the FieldDescription
  52. *
  53. * @param $object
  54. * @param FieldDescription $fieldDescription
  55. * @param array $params
  56. * @return
  57. */
  58. public function renderListElement($object, FieldDescription $fieldDescription, $params = array())
  59. {
  60. $template = $this->environment->loadTemplate($fieldDescription->getTemplate());
  61. return $this->output($fieldDescription, $template->render(array_merge($params, array(
  62. 'admin' => $fieldDescription->getAdmin(),
  63. 'object' => $object,
  64. 'value' => $this->getValueFromFieldDescription($object, $fieldDescription),
  65. 'field_description' => $fieldDescription
  66. ))));
  67. }
  68. public function output(FieldDescription $fieldDescription, $content)
  69. {
  70. return sprintf("\n<!-- fieldName: %s, template: %s -->\n%s\n", $fieldDescription->getFieldName(), $fieldDescription->getTemplate(), $content);
  71. }
  72. /**
  73. * return the value related to FieldDescription, if the associated object does no
  74. * exists => a temporary one is created
  75. *
  76. * @param $object
  77. * @param FieldDescription $fieldDescription
  78. * @return
  79. */
  80. public function getValueFromFieldDescription($object, FieldDescription $fieldDescription)
  81. {
  82. $value = $fieldDescription->getValue($object);
  83. // no value defined, check if the fieldDescription point to an association
  84. // if so, create an empty object instance
  85. // fixme: not sure this is the best place to do that
  86. if (!$value && $fieldDescription->getAssociationAdmin()) {
  87. $value = $fieldDescription->getAssociationAdmin()->getNewInstance();
  88. }
  89. return $value;
  90. }
  91. /**
  92. * render a filter element
  93. *
  94. * @param Filter $filter
  95. * @param array $params
  96. * @return
  97. */
  98. public function renderFilterElement(Filter $filter, array $params = array())
  99. {
  100. $description = $filter->getDescription();
  101. $template = $this->environment->loadTemplate($description->getTemplate());
  102. return $template->render(array_merge($params, array(
  103. 'filter' => $filter
  104. )));
  105. }
  106. /**
  107. * render a field element from the FieldDescription
  108. *
  109. *
  110. * @throws InvalidArgumentException
  111. * @param FieldDescription $fieldDescription
  112. * @param $form
  113. * @param $object
  114. * @param array $params
  115. * @return string
  116. */
  117. public function renderFormElement(FieldDescription $fieldDescription, $form, $object, $params = array())
  118. {
  119. if (!$fieldDescription->getFieldName()) {
  120. return '';
  121. }
  122. try {
  123. $field = $form->get($fieldDescription->getFieldName());
  124. } catch (\InvalidArgumentException $e) {
  125. throw $e;
  126. }
  127. if ($field->isHidden()) {
  128. return '';
  129. }
  130. // find the correct edit parameter
  131. // edit : standard | inline
  132. // inline : natural | table
  133. $parentFieldDescription = $fieldDescription->getAdmin()->getParentFieldDescription();
  134. if (!$parentFieldDescription) {
  135. $params['edit'] = $fieldDescription->getOption('edit', 'standard');
  136. $params['inline'] = $fieldDescription->getOption('inline', 'natural');
  137. $base_template = sprintf('SonataBaseApplicationBundle:CRUD:base_%s_edit_field.html.twig', 'standard');
  138. } else {
  139. $params['edit'] = $parentFieldDescription->getOption('edit', 'standard');
  140. $params['inline'] = $parentFieldDescription->getOption('inline', 'natural');
  141. $base_template = sprintf('SonataBaseApplicationBundle:CRUD:base_%s_edit_field.html.twig', $params['edit']);
  142. }
  143. $template = $this->environment->loadTemplate($fieldDescription->getTemplate());
  144. return $this->output($fieldDescription, $template->render(array_merge($params, array(
  145. 'admin' => $fieldDescription->getAdmin(),
  146. 'object' => $object,
  147. 'field_description' => $fieldDescription,
  148. 'value' => $this->getValueFromFieldDescription($object, $fieldDescription),
  149. 'field_element' => $field,
  150. 'base_template' => $fieldDescription->getOption('base_template', $base_template)
  151. ))));
  152. }
  153. /**
  154. * set the templating engine
  155. *
  156. * @param $templating
  157. * @return void
  158. */
  159. public function setTemplating($templating)
  160. {
  161. $this->templating = $templating;
  162. }
  163. /**
  164. * return the templating engine
  165. *
  166. * @return Engine
  167. */
  168. public function getTemplating()
  169. {
  170. return $this->templating;
  171. }
  172. }