BaseApplicationExtension.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 $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. /**
  69. * return the value related to FieldDescription, if the associated object does no
  70. * exists => a temporary one is created
  71. *
  72. * @param $object
  73. * @param FieldDescription $fieldDescription
  74. * @return
  75. */
  76. public function getValueFromFieldDescription($object, FieldDescription $fieldDescription)
  77. {
  78. $value = $fieldDescription->getValue($object);
  79. // no value defined, chek if the field_description point to an association
  80. // if so, create an empty object instance
  81. // fixme: not sure this is the best place to do that
  82. if (!$value && $fieldDescription->getAssociationAdmin()) {
  83. $value = $fieldDescription->getAssociationAdmin()->getNewInstance();
  84. }
  85. return $value;
  86. }
  87. /**
  88. * render a filter element
  89. *
  90. * @param Filter $filter
  91. * @param array $params
  92. * @return
  93. */
  94. public function renderFilterElement(Filter $filter, array $params = array())
  95. {
  96. $description = $filter->getDescription();
  97. $template = $this->environment->loadTemplate($description->getTemplate());
  98. return $template->render(array_merge($params, array(
  99. 'filter' => $filter
  100. )));
  101. }
  102. /**
  103. * render a field element from the FieldDescription
  104. *
  105. *
  106. * @throws InvalidArgumentException
  107. * @param FieldDescription $fieldDescription
  108. * @param $form
  109. * @param $object
  110. * @param array $params
  111. * @return string
  112. */
  113. public function renderFormElement(FieldDescription $fieldDescription, $form, $object, $params = array())
  114. {
  115. if (!$fieldDescription->getFieldName()) {
  116. return '';
  117. }
  118. try {
  119. $field = $form->get($fieldDescription->getFieldName());
  120. } catch (\InvalidArgumentException $e) {
  121. throw $e;
  122. }
  123. if ($field->isHidden()) {
  124. return '';
  125. }
  126. // find the correct edit parameter
  127. // edit : standard | inline
  128. // inline : natural | table
  129. $parentFieldDescription = $fieldDescription->getAdmin()->getParentFieldDescription();
  130. if (!$parentFieldDescription) {
  131. $params['edit'] = $fieldDescription->getOption('edit', 'standard');
  132. $params['inline'] = $fieldDescription->getOption('inline', 'natural');
  133. $base_template = sprintf('SonataBaseApplicationBundle:CRUD:base_%s_edit_field.twig.html', 'standard');
  134. } else {
  135. $params['edit'] = $parentFieldDescription->getOption('edit', 'standard');
  136. $params['inline'] = $parentFieldDescription->getOption('inline', 'natural');
  137. $base_template = sprintf('SonataBaseApplicationBundle:CRUD:base_%s_edit_field.twig.html', $params['edit']);
  138. }
  139. $template = $this->environment->loadTemplate($fieldDescription->getTemplate());
  140. return $template->render(array_merge($params, array(
  141. 'admin' => $fieldDescription->getAdmin(),
  142. 'object' => $object,
  143. 'field_description' => $fieldDescription,
  144. 'value' => $this->getValueFromFieldDescription($object, $fieldDescription),
  145. 'field_element' => $field,
  146. 'base_template' => $fieldDescription->getOption('base_template', $base_template)
  147. )));
  148. }
  149. /**
  150. * set the templating engine
  151. *
  152. * @param $templating
  153. * @return void
  154. */
  155. public function setTemplating($templating)
  156. {
  157. $this->templating = $templating;
  158. }
  159. /**
  160. * return the templating engine
  161. *
  162. * @return Engine
  163. */
  164. public function getTemplating()
  165. {
  166. return $this->templating;
  167. }
  168. }