AdminHelper.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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\Admin;
  11. use Doctrine\Common\Inflector\Inflector;
  12. use Doctrine\Common\Util\ClassUtils;
  13. use Sonata\AdminBundle\Exception\NoValueException;
  14. use Sonata\AdminBundle\Util\FormBuilderIterator;
  15. use Sonata\AdminBundle\Util\FormViewIterator;
  16. use Symfony\Component\Form\FormBuilderInterface;
  17. use Symfony\Component\Form\FormView;
  18. use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
  19. use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
  20. use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
  21. /**
  22. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  23. */
  24. class AdminHelper
  25. {
  26. /**
  27. * @var Pool
  28. */
  29. protected $pool;
  30. /**
  31. * @param Pool $pool
  32. */
  33. public function __construct(Pool $pool)
  34. {
  35. $this->pool = $pool;
  36. }
  37. /**
  38. * @throws \RuntimeException
  39. *
  40. * @param FormBuilderInterface $formBuilder
  41. * @param string $elementId
  42. *
  43. * @return FormBuilderInterface|null
  44. */
  45. public function getChildFormBuilder(FormBuilderInterface $formBuilder, $elementId)
  46. {
  47. foreach (new FormBuilderIterator($formBuilder) as $name => $formBuilder) {
  48. if ($name == $elementId) {
  49. return $formBuilder;
  50. }
  51. }
  52. return;
  53. }
  54. /**
  55. * @param FormView $formView
  56. * @param string $elementId
  57. *
  58. * @return null|FormView
  59. */
  60. public function getChildFormView(FormView $formView, $elementId)
  61. {
  62. foreach (new \RecursiveIteratorIterator(new FormViewIterator($formView), \RecursiveIteratorIterator::SELF_FIRST) as $name => $formView) {
  63. if ($name === $elementId) {
  64. return $formView;
  65. }
  66. }
  67. return;
  68. }
  69. /**
  70. * NEXT_MAJOR: remove this method.
  71. *
  72. * @deprecated
  73. *
  74. * @param string $code
  75. *
  76. * @return AdminInterface
  77. */
  78. public function getAdmin($code)
  79. {
  80. return $this->pool->getInstance($code);
  81. }
  82. /**
  83. * Note:
  84. * This code is ugly, but there is no better way of doing it.
  85. * For now the append form element action used to add a new row works
  86. * only for direct FieldDescription (not nested one).
  87. *
  88. * @throws \RuntimeException
  89. *
  90. * @param AdminInterface $admin
  91. * @param object $subject
  92. * @param string $elementId
  93. *
  94. * @return array
  95. */
  96. public function appendFormFieldElement(AdminInterface $admin, $subject, $elementId)
  97. {
  98. // retrieve the subject
  99. $formBuilder = $admin->getFormBuilder();
  100. $form = $formBuilder->getForm();
  101. $form->setData($subject);
  102. $form->handleRequest($admin->getRequest());
  103. // get the field element
  104. $childFormBuilder = $this->getChildFormBuilder($formBuilder, $elementId);
  105. //Child form not found (probably nested one)
  106. //if childFormBuilder was not found resulted in fatal error getName() method call on non object
  107. if (!$childFormBuilder) {
  108. $propertyAccessor = $this->pool->getPropertyAccessor();
  109. $entity = $admin->getSubject();
  110. $path = $this->getElementAccessPath($elementId, $entity);
  111. $collection = $propertyAccessor->getValue($entity, $path);
  112. if ($collection instanceof \Doctrine\ORM\PersistentCollection || $collection instanceof \Doctrine\ODM\MongoDB\PersistentCollection) {
  113. //since doctrine 2.4
  114. $entityClassName = $collection->getTypeClass()->getName();
  115. } elseif ($collection instanceof \Doctrine\Common\Collections\Collection) {
  116. $entityClassName = $this->getEntityClassName($admin, explode('.', preg_replace('#\[\d*?\]#', '', $path)));
  117. } else {
  118. throw new \Exception('unknown collection class');
  119. }
  120. $collection->add(new $entityClassName());
  121. $propertyAccessor->setValue($entity, $path, $collection);
  122. $fieldDescription = null;
  123. } else {
  124. // retrieve the FieldDescription
  125. $fieldDescription = $admin->getFormFieldDescription($childFormBuilder->getName());
  126. try {
  127. $value = $fieldDescription->getValue($form->getData());
  128. } catch (NoValueException $e) {
  129. $value = null;
  130. }
  131. // retrieve the posted data
  132. $data = $admin->getRequest()->get($formBuilder->getName());
  133. if (!isset($data[$childFormBuilder->getName()])) {
  134. $data[$childFormBuilder->getName()] = array();
  135. }
  136. $objectCount = count($value);
  137. $postCount = count($data[$childFormBuilder->getName()]);
  138. $fields = array_keys($fieldDescription->getAssociationAdmin()->getFormFieldDescriptions());
  139. // for now, not sure how to do that
  140. $value = array();
  141. foreach ($fields as $name) {
  142. $value[$name] = '';
  143. }
  144. // add new elements to the subject
  145. while ($objectCount < $postCount) {
  146. // append a new instance into the object
  147. $this->addNewInstance($form->getData(), $fieldDescription);
  148. ++$objectCount;
  149. }
  150. $this->addNewInstance($form->getData(), $fieldDescription);
  151. }
  152. $finalForm = $admin->getFormBuilder()->getForm();
  153. $finalForm->setData($subject);
  154. // bind the data
  155. $finalForm->setData($form->getData());
  156. return array($fieldDescription, $finalForm);
  157. }
  158. /**
  159. * Add a new instance to the related FieldDescriptionInterface value.
  160. *
  161. * @param object $object
  162. * @param FieldDescriptionInterface $fieldDescription
  163. *
  164. * @throws \RuntimeException
  165. */
  166. public function addNewInstance($object, FieldDescriptionInterface $fieldDescription)
  167. {
  168. $instance = $fieldDescription->getAssociationAdmin()->getNewInstance();
  169. $mapping = $fieldDescription->getAssociationMapping();
  170. $method = sprintf('add%s', Inflector::classify($mapping['fieldName']));
  171. if (!method_exists($object, $method)) {
  172. $method = rtrim($method, 's');
  173. if (!method_exists($object, $method)) {
  174. $method = sprintf('add%s', Inflector::classify(Inflector::singularize($mapping['fieldName'])));
  175. if (!method_exists($object, $method)) {
  176. throw new \RuntimeException(sprintf('Please add a method %s in the %s class!', $method, ClassUtils::getClass($object)));
  177. }
  178. }
  179. }
  180. $object->$method($instance);
  181. }
  182. /**
  183. * Camelize a string.
  184. *
  185. * NEXT_MAJOR: remove this method.
  186. *
  187. * @static
  188. *
  189. * @param string $property
  190. *
  191. * @return string
  192. *
  193. * @deprecated Deprecated since version 3.1. Use \Doctrine\Common\Inflector\Inflector::classify() instead
  194. */
  195. public function camelize($property)
  196. {
  197. @trigger_error(
  198. sprintf(
  199. 'The %s method is deprecated since 3.1 and will be removed in 4.0. '.
  200. 'Use \Doctrine\Common\Inflector\Inflector::classify() instead.',
  201. __METHOD__
  202. ),
  203. E_USER_DEPRECATED
  204. );
  205. return Inflector::classify($property);
  206. }
  207. /**
  208. * Get access path to element which works with PropertyAccessor.
  209. *
  210. * @param string $elementId expects string in format used in form id field. (uniqueIdentifier_model_sub_model or uniqueIdentifier_model_1_sub_model etc.)
  211. * @param mixed $entity
  212. *
  213. * @return string
  214. *
  215. * @throws \Exception
  216. */
  217. public function getElementAccessPath($elementId, $entity)
  218. {
  219. $propertyAccessor = $this->pool->getPropertyAccessor();
  220. $idWithoutIdentifier = preg_replace('/^[^_]*_/', '', $elementId);
  221. $initialPath = preg_replace('#(_(\d+)_)#', '[$2]_', $idWithoutIdentifier);
  222. $parts = explode('_', $initialPath);
  223. $totalPath = '';
  224. $currentPath = '';
  225. foreach ($parts as $part) {
  226. $currentPath .= empty($currentPath) ? $part : '_'.$part;
  227. $separator = empty($totalPath) ? '' : '.';
  228. if ($this->pathExists($propertyAccessor, $entity, $totalPath.$separator.$currentPath)) {
  229. $totalPath .= $separator.$currentPath;
  230. $currentPath = '';
  231. }
  232. }
  233. if (!empty($currentPath)) {
  234. throw new \Exception(sprintf('Could not get element id from %s Failing part: %s', $elementId, $currentPath));
  235. }
  236. return $totalPath;
  237. }
  238. /**
  239. * Recursively find the class name of the admin responsible for the element at the end of an association chain.
  240. *
  241. * @param AdminInterface $admin
  242. * @param array $elements
  243. *
  244. * @return string
  245. */
  246. protected function getEntityClassName(AdminInterface $admin, $elements)
  247. {
  248. $element = array_shift($elements);
  249. $associationAdmin = $admin->getFormFieldDescription($element)->getAssociationAdmin();
  250. if (count($elements) == 0) {
  251. return $associationAdmin->getClass();
  252. }
  253. return $this->getEntityClassName($associationAdmin, $elements);
  254. }
  255. /**
  256. * Check if given path exists in $entity.
  257. *
  258. * @param PropertyAccessorInterface $propertyAccessor
  259. * @param mixed $entity
  260. * @param string $path
  261. *
  262. * @return bool
  263. *
  264. * @throws \RuntimeException
  265. */
  266. private function pathExists(PropertyAccessorInterface $propertyAccessor, $entity, $path)
  267. {
  268. // Symfony <= 2.3 did not have isReadable method for PropertyAccessor
  269. if (method_exists($propertyAccessor, 'isReadable')) {
  270. return $propertyAccessor->isReadable($entity, $path);
  271. }
  272. try {
  273. $propertyAccessor->getValue($entity, $path);
  274. return true;
  275. } catch (NoSuchPropertyException $e) {
  276. return false;
  277. } catch (UnexpectedTypeException $e) {
  278. return false;
  279. }
  280. }
  281. }