/**
  * @param string|PropertyPathInterface $propertyPath  The property path to modify
  *
  * @throws NoSuchIndexException
  */
 public function removeParameter($propertyPath)
 {
     $propertyPathAccessor = PropertyAccess::createPropertyAccessor();
     if (!$propertyPath instanceof PropertyPathInterface) {
         $propertyPath = new PropertyPath($propertyPath);
     }
     if (1 === $propertyPath->getLength()) {
         $buffer =& $this->parameters;
         unset($buffer[$propertyPath->getElement(0)]);
     } else {
         $parentPropertyPath = $propertyPath->getParent();
         $buffer = $propertyPathAccessor->getValue($this->parameters, $parentPropertyPath);
         unset($buffer[$propertyPath->getElement($propertyPath->getLength() - 1)]);
         $propertyPathAccessor->setValue($this->parameters, $parentPropertyPath, $buffer);
     }
 }
 /**
  * @param string $propertyPath
  * @return null|\Symfony\Component\PropertyAccess\PropertyPath
  */
 private function validatePropertyPath($propertyPath)
 {
     $propertyPath = new PropertyPath($propertyPath);
     if ($propertyPath->isProperty($propertyPath->getLength() - 1)) {
         return $propertyPath;
     } else {
         return null;
     }
 }
Beispiel #3
0
 /**
  * Returns html and attributes value of rdfa property.
  *
  * @param string $property Could be a property sequence like (block[1].title[0])
  *
  * @return bool
  */
 public function getPropertyValue($property)
 {
     $nodes = $this->crawler;
     $before = '';
     $path = new PropertyPath($property);
     if (1 < $path->getLength()) {
         foreach ($path as $item) {
             // is not integer
             if (!ctype_digit(strval($item))) {
                 $before = $item;
                 $nodes = $nodes->filter('*[property="' . $item . '"]');
             } else {
                 $nodes = $nodes->filter('*[rel="' . $before . '"]')->eq($item);
             }
         }
     } else {
         // FIXME it is a bit complex but there is no :not operator in crawler
         // should be *[property="block"]:not(*[property] *)
         $nodes = $nodes->filter('*[property="' . $property . '"]')->reduce(function (Crawler $node) {
             // get parents
             $parents = $node->parents();
             $count = 0;
             // check if one parent is property exclude it
             $parents->each(function ($node) use(&$count) {
                 if (null !== $node->attr('property') && $node->attr('typeof') === 'collection') {
                     ++$count;
                 }
             });
             return $count === 0;
         });
     }
     // if rdfa property not found return false
     if ($nodes->count() > 0) {
         // create an array of changes
         return $nodes->each(function (Crawler $crawlerNode) {
             $node = $crawlerNode->getNode(0);
             $attributes = [];
             foreach ($node->attributes as $name => $value) {
                 $attributes[$name] = $value->nodeValue;
             }
             $attributes['html'] = $crawlerNode->html();
             return $attributes;
         });
     }
     return false;
 }
 /**
  * @param Request $request
  *
  * @return Response
  */
 public function setObjectFieldValueAction(Request $request)
 {
     $field = $request->get('field');
     $code = $request->get('code');
     $objectId = $request->get('objectId');
     $value = $request->get('value');
     $context = $request->get('context');
     $admin = $this->pool->getInstance($code);
     $admin->setRequest($request);
     // alter should be done by using a post method
     if (!$request->isXmlHttpRequest()) {
         return new JsonResponse(array('status' => 'KO', 'message' => 'Expected a XmlHttpRequest request header'));
     }
     if ($request->getMethod() != 'POST') {
         return new JsonResponse(array('status' => 'KO', 'message' => 'Expected a POST Request'));
     }
     $rootObject = $object = $admin->getObject($objectId);
     if (!$object) {
         return new JsonResponse(array('status' => 'KO', 'message' => 'Object does not exist'));
     }
     // check user permission
     if (false === $admin->isGranted('EDIT', $object)) {
         return new JsonResponse(array('status' => 'KO', 'message' => 'Invalid permissions'));
     }
     if ($context == 'list') {
         $fieldDescription = $admin->getListFieldDescription($field);
     } else {
         return new JsonResponse(array('status' => 'KO', 'message' => 'Invalid context'));
     }
     if (!$fieldDescription) {
         return new JsonResponse(array('status' => 'KO', 'message' => 'The field does not exist'));
     }
     if (!$fieldDescription->getOption('editable')) {
         return new JsonResponse(array('status' => 'KO', 'message' => 'The field cannot be edit, editable option must be set to true'));
     }
     $propertyPath = new PropertyPath($field);
     // If property path has more than 1 element, take the last object in order to validate it
     if ($propertyPath->getLength() > 1) {
         $object = $this->pool->getPropertyAccessor()->getValue($object, $propertyPath->getParent());
         $elements = $propertyPath->getElements();
         $field = end($elements);
         $propertyPath = new PropertyPath($field);
     }
     // Handle date type has setter expect a DateTime object
     if ('' !== $value && $fieldDescription->getType() == 'date') {
         $value = new \DateTime($value);
     }
     $this->pool->getPropertyAccessor()->setValue($object, $propertyPath, '' !== $value ? $value : null);
     $violations = $this->validator->validate($object);
     if (count($violations)) {
         $messages = array();
         foreach ($violations as $violation) {
             $messages[] = $violation->getMessage();
         }
         return new JsonResponse(array('status' => 'KO', 'message' => implode("\n", $messages)));
     }
     $admin->update($object);
     // render the widget
     // todo : fix this, the twig environment variable is not set inside the extension ...
     $extension = $this->twig->getExtension('sonata_admin');
     $extension->initRuntime($this->twig);
     $content = $extension->renderListElement($rootObject, $fieldDescription);
     return new JsonResponse(array('status' => 'OK', 'content' => $content));
 }
 /**
  * Replaces a sub-path by a different (sub-) path.
  *
  * @param integer                      $offset     The offset at which to replace.
  * @param integer                      $length     The length of the piece to replace.
  * @param PropertyPathInterface|string $path       The path to insert.
  * @param integer                      $pathOffset The offset where the inserted piece
  *                                                 starts in $path.
  * @param integer                      $pathLength The length of the inserted piece.
  *                                                 If 0, the full path is inserted.
  *
  * @throws OutOfBoundsException If the offset is invalid
  */
 public function replace($offset, $length, $path, $pathOffset = 0, $pathLength = 0)
 {
     if (is_string($path)) {
         $path = new PropertyPath($path);
     }
     if ($offset < 0 && abs($offset) <= $this->getLength()) {
         $offset = $this->getLength() + $offset;
     } elseif (!isset($this->elements[$offset])) {
         throw new OutOfBoundsException('The offset ' . $offset . ' is not within the property path');
     }
     if (0 === $pathLength) {
         $pathLength = $path->getLength() - $pathOffset;
     }
     $this->resize($offset, $length, $pathLength);
     for ($i = 0; $i < $pathLength; ++$i) {
         $this->elements[$offset + $i] = $path->getElement($pathOffset + $i);
         $this->isIndex[$offset + $i] = $path->isIndex($pathOffset + $i);
     }
 }
Beispiel #6
0
 /**
  * @param $path
  * @param $haystack
  * @return bool
  */
 private function valueExist($path, array $haystack)
 {
     $propertyPath = new PropertyPath($path);
     $length = $propertyPath->getLength();
     $valueExist = true;
     for ($i = 0; $i < $length; ++$i) {
         $property = $propertyPath->getElement($i);
         $isIndex = $propertyPath->isIndex($i);
         $propertyExist = $this->arrayPropertyExists($property, $haystack);
         if ($isIndex && !$propertyExist) {
             $valueExist = false;
             break;
         }
     }
     unset($propertyPath);
     return $valueExist;
 }