/**
  * Applies a constraint to the query
  *
  * @param Sonata\DoctrinePHPCRAdminBundle\Datagrid\ProxyQuery $queryBuilder
  * @param string $alias has no effect
  * @param string $field field uhere to apply the constraint
  * @param array $data determines the constraint
  * @return
  */
 public function filter($queryBuilder, $alias = null, $field, $data)
 {
     if (!$data || !is_array($data) || !array_key_exists('value', $data)) {
         return;
     }
     $data['value'] = trim($data['value']);
     $data['type'] = !isset($data['type']) ? ChoiceType::TYPE_CONTAINS : $data['type'];
     if (strlen($data['value']) == 0) {
         return;
     }
     $qf = $queryBuilder->getQueryObjectModelFactory();
     switch ($data['type']) {
         case ChoiceType::TYPE_EQUAL:
             $constraint = $qf->comparison($qf->propertyValue($field), Constants::JCR_OPERATOR_EQUAL_TO, $qf->literal($data['value']));
             break;
         case ChoiceType::TYPE_NOT_CONTAINS:
             $constraint = $qf->fulltextSearch($field, "* -" . $data['value'], '[' . $queryBuilder->getNodeType() . ']');
             break;
         case ChoiceType::TYPE_CONTAINS:
             $constraint = $qf->comparison($qf->propertyValue($field), Constants::JCR_OPERATOR_LIKE, $qf->literal('%' . $data['value'] . '%'));
             break;
         case ChoiceType::TYPE_CONTAINS_WORDS:
         default:
             $constraint = $qf->fulltextSearch($field, $data['value'], '[' . $queryBuilder->getNodeType() . ']');
     }
     $queryBuilder->andWhere($constraint);
 }
 /**
  * Add the where statement for this filter to the query.
  *
  * @param ProxyQuery $proxy
  */
 protected function getWhere(ProxyQuery $proxy)
 {
     $queryBuilder = $proxy->getQueryBuilder();
     if ($this->getCondition() == self::CONDITION_OR) {
         return $queryBuilder->orWhere();
     }
     return $queryBuilder->andWhere();
 }
 /**
  * Applies a constraint to the query
  *
  * @param Sonata\DoctrinePHPCRAdminBundle\Datagrid\ProxyQuery $queryBuilder
  * @param string $alias has no effect
  * @param string $field field where to apply the constraint
  * @param array $data determines the date constraint [value => [year => Y, month => m, day => d], type => DateType::TYPE_GREATER_EQUAL|DateType::TYPE_GREATER_THAN|DateType::TYPE_LESS_EQUAL|DateType::TYPE_LESS_THAN|DateType::TYPE_NULL|DateType::TYPE_NOT_NULL|DateType::TYPE_EQUAL]
  * @return
  */
 public function filter($queryBuilder, $alias = null, $field, $data)
 {
     if (!$data || !is_array($data) || !isset($data['value'])) {
         return;
     }
     if (isset($data['value']['year']) && $data['value']['year'] && isset($data['value']['month']) && $data['value']['month'] && isset($data['value']['day']) && $data['value']['day']) {
         $data['type'] = isset($data['type']) ? $data['type'] : DateType::TYPE_EQUAL;
         $qf = $queryBuilder->getQueryObjectModelFactory();
         $date = '' . $data['value']['year'] . '-' . $data['value']['month'] . '-' . $data['value']['day'];
         $from = new \DateTime($date);
         $to = new \DateTime($date . ' +86399 seconds');
         // 23 hours 59 minutes 59 seconds
         switch ($data['type']) {
             case DateType::TYPE_GREATER_EQUAL:
                 $constraint = $qf->comparison($qf->propertyValue($field), Constants::JCR_OPERATOR_GREATER_THAN_OR_EQUAL_TO, $qf->literal($from));
                 break;
             case DateType::TYPE_GREATER_THAN:
                 $constraint = $qf->comparison($qf->propertyValue($field), Constants::JCR_OPERATOR_GREATER_THAN, $qf->literal($to));
                 break;
             case DateType::TYPE_LESS_EQUAL:
                 $constraint = $qf->comparison($qf->propertyValue($field), Constants::JCR_OPERATOR_LESS_THAN_OR_EQUAL_TO, $qf->literal($to));
                 break;
             case DateType::TYPE_LESS_THAN:
                 $constraint = $qf->comparison($qf->propertyValue($field), Constants::JCR_OPERATOR_LESS_THAN, $qf->literal($from));
                 break;
             case DateType::TYPE_NULL:
                 $constraint = $qf->comparison($qf->propertyValue($field), Constants::JCR_OPERATOR_EQUAL_TO, $qf->literal(null));
                 break;
             case DateType::TYPE_NOT_NULL:
                 $constraint = $qf->comparison($qf->propertyValue($field), Constants::JCR_OPERATOR_NOT_EQUAL_TO, $qf->literal(null));
                 break;
             case DateType::TYPE_EQUAL:
             default:
                 $constraint = $qf->comparison($qf->propertyValue($field), Constants::JCR_OPERATOR_LESS_THAN_OR_EQUAL_TO, $qf->literal($to));
                 $queryBuilder->andWhere($constraint);
                 $constraint = $qf->comparison($qf->propertyValue($field), Constants::JCR_OPERATOR_GREATER_THAN_OR_EQUAL_TO, $qf->literal($from));
         }
         $queryBuilder->andWhere($constraint);
     }
 }
 /**
  * @param ProxyQuery $query
  *
  * @return mixed
  */
 public function executeQuery($query)
 {
     return $query->execute();
 }
 public function testGetAndSetDocumentManager()
 {
     $dm = $this->getMockBuilder('Doctrine\\ODM\\PHPCR\\DocumentManager')->disableOriginalConstructor()->getMock();
     $this->pq->setDocumentManager($dm);
     $this->assertEquals($dm, $this->pq->getDocumentManager());
 }
 /**
  * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  * @param array $values
  * @return \Sonata\AdminBundle\Datagrid\DatagridInterface
  */
 public function getBaseDatagrid(AdminInterface $admin, array $values = array())
 {
     $queryBuilder = $admin->getModelManager()->getDocumentManager()->createQueryBuilder();
     $qomFactory = $queryBuilder->getQOMFactory();
     $query = new ProxyQuery($qomFactory, $queryBuilder);
     $query->setDocumentName($admin->getClass());
     $query->setDocumentManager($admin->getModelManager()->getDocumentManager());
     $pager = new Pager();
     $formBuilder = $this->formFactory->createNamedBuilder('form', 'filter', array(), array('csrf_protection' => false));
     return new Datagrid($query, $admin->getList(), $pager, $formBuilder, $values);
 }
 public function testAndWhere()
 {
     $constraint = $this->getMock('PHPCR\\Query\\QOM\\ConstraintInterface');
     $this->qb->expects($this->once())->method('andWhere')->with($constraint);
     $pq = new ProxyQuery($this->qf, $this->qb);
     $pq->andWhere($constraint);
 }