Пример #1
0
 /**
  * @author Franlin Rivero Grcia <*****@*****.**>
  * @param string $key
  * @param type $fields
  * @param \Doctrine\ORM\Mapping\ClassMetadata $meta
  * @param QueryBuilder $qb
  * @param type $asoc
  */
 public static function fieldFilter($class, &$filters, &$fields, &$asoc, &$meta, &$qb)
 {
     if (array_key_exists('select', $filters)) {
         $qb->select($filters['select']);
         unset($filters['select']);
     }
     $fTemp = json_decode(UtilRepository2::getContainer()->get('request')->get('filters'));
     if (is_array($fTemp)) {
         if (!(is_array($filters) && count($filters) > 0 || !is_array($filters) && $filters != null)) {
             $filters = $fTemp;
         }
     }
     if ($filters == null) {
         $filters = array();
     }
     if (!is_array($filters)) {
         $filters = array($filters);
     }
     foreach ($filters as $key => $value) {
         if ($value !== null && $value !== '') {
             if (in_array($key, $fields)) {
                 $map = $meta->getFieldMapping($key);
                 if ($map['type'] == 'string' || $map['type'] == 'text' || $map['type'] == 'varchar') {
                     if (UtilRepository2Config::$defaultStringComparer == 'like') {
                         $qb->andWhere("lower({$class}.{$key}) like :{$key}");
                         if (strpos($value, '%') === false) {
                             $qb->setParameter($key, "%" . strtolower($value) . "%");
                         } else {
                             $qb->setParameter($key, strtolower($value));
                         }
                     } else {
                         if (is_array($value)) {
                             $op = $value[0];
                             $qb->andWhere("lower({$class}.{$key}) {$op} :{$key}");
                             $qb->setParameter($key, strtolower($value[1]));
                         } else {
                             $qb->andWhere("lower({$class}.{$key}) = :{$key}");
                             $qb->setParameter($key, strtolower($value));
                         }
                     }
                 } else {
                     if (is_array($value)) {
                         $op = $value[0];
                         if ($op == 'in') {
                             $qb->andWhere("{$class}.{$key} {$op} (:{$key})");
                             $qb->setParameter($key, $value[1]);
                         } elseif ($op == 'is') {
                             $qb->andWhere("{$class}.{$key} is null");
                         } else {
                             $qb->andWhere("{$class}.{$key} {$op} :{$key}");
                             $qb->setParameter($key, strtolower($value[1]));
                         }
                     } else {
                         $qb->andWhere("{$class}.{$key} = :{$key}");
                         $qb->setParameter($key, $value);
                     }
                 }
             } elseif (in_array($key, $asoc)) {
                 if (is_array($value)) {
                     $op = $value[0];
                     if ($op == 'is') {
                         $qb->andWhere("{$class}.{$key} is null");
                     }
                 } else {
                     $qb->andWhere("{$class}.{$key} = :{$key}");
                     $qb->setParameter($key, $value);
                 }
             } else {
                 $op = UtilRepository2Config::$defaultForeignCompareOperator;
                 $param = str_replace(".", '', $key);
                 if (!is_array($value)) {
                     if ($op == 'like') {
                         $qb->andWhere("lower({$key}) {$op} :{$param}");
                     } else {
                         $qb->andWhere("{$key} {$op} :{$param}");
                     }
                     if ($op == 'like') {
                         if (strpos($value, '%') === false) {
                             $qb->setParameter($param, "%" . strtolower($value) . "%");
                         } else {
                             $qb->setParameter($param, strtolower($value));
                         }
                     } else {
                         $qb->setParameter($param, $value);
                     }
                 } else {
                     if ($value[0] == 'like') {
                         $qb->andWhere("lower({$key}) {$value['0']} :{$param}");
                     } elseif ($value[0] == 'in') {
                         $qb->andWhere("{$key} {$value['0']} (:{$param})");
                     } else {
                         $qb->andWhere("{$key} {$value['0']} :{$param}");
                     }
                     if ($value[0] == 'like') {
                         if (strpos($value[1], '%') === false) {
                             $qb->setParameter($param, "%" . strtolower($value[1]) . "%");
                         } else {
                             $qb->setParameter($param, strtolower($value[1]));
                         }
                     } else {
                         $qb->setParameter($param, $value[1]);
                     }
                 }
             }
         }
     }
     if (array_key_exists('start', $filters) && array_key_exists('limit', $filters) && $filters['start'] != null && $filters['limit'] != null) {
         $total = UtilRepository2::getTotalResult($qb, $class);
         $start = $filters['start'];
         $limit = $filters['limit'];
         $limit = $limit == -1 ? $total : $limit;
         unset($filters['start']);
         unset($filters['limit']);
         UtilRepository2::getSession()->set("total", $total);
         $qb->setFirstResult($start);
         $qb->setMaxResults($limit);
     } else {
         $start = UtilRepository2::getContainer()->get('request')->get('iDisplayStart');
         $size = UtilRepository2::getContainer()->get('request')->get('iDisplayLength');
         if ($start !== null && $size !== null) {
             $total = UtilRepository2::getTotalResult($qb, $class);
             $size = $size == -1 ? $total : $size;
             if ($start != null && $size != null) {
                 UtilRepository2::getSession()->set("total", $total);
                 $qb->setFirstResult($start);
                 $qb->setMaxResults($size);
             }
         }
     }
 }