Example #1
0
 /**
  * Apply criteria in query repository
  *
  * @param $model
  * @param RepositoryInterface $repository
  * @return mixed
  */
 public function apply($model, RepositoryInterface $repository)
 {
     $fieldsSearchable = $repository->getFieldsSearchable();
     $search = $this->request->get(config('repository.criteria.params.search', 'search'), null);
     $searchFields = $this->request->get(config('repository.criteria.params.searchFields', 'searchFields'), null);
     $filter = $this->request->get(config('repository.criteria.params.filter', 'filter'), null);
     $orderBy = $this->request->get(config('repository.criteria.params.orderBy', 'orderBy'), null);
     $sortedBy = $this->request->get(config('repository.criteria.params.sortedBy', 'sortedBy'), 'asc');
     $with = $this->request->get(config('repository.criteria.params.with', 'with'), null);
     $sortedBy = !empty($sortedBy) ? $sortedBy : 'asc';
     if ($search && is_array($fieldsSearchable) && count($fieldsSearchable)) {
         $searchFields = is_array($searchFields) || is_null($searchFields) ? $searchFields : explode(';', $searchFields);
         $fields = $this->parserFieldsSearch($fieldsSearchable, $searchFields);
         $isFirstField = true;
         $searchData = $this->parserSearchData($search);
         $search = $this->parserSearchValue($search);
         $modelForceAndWhere = false;
         $model = $model->where(function ($query) use($fields, $search, $searchData, $isFirstField, $modelForceAndWhere) {
             foreach ($fields as $field => $condition) {
                 if (is_numeric($field)) {
                     $field = $condition;
                     $condition = "=";
                 }
                 $value = null;
                 $condition = trim(strtolower($condition));
                 if (isset($searchData[$field])) {
                     $value = $condition == "like" ? "%{$searchData[$field]}%" : $searchData[$field];
                 } else {
                     if (!is_null($search)) {
                         $value = $condition == "like" ? "%{$search}%" : $search;
                     }
                 }
                 if ($isFirstField || $modelForceAndWhere) {
                     if (!is_null($value)) {
                         $query->where($field, $condition, $value);
                         $isFirstField = false;
                     }
                 } else {
                     if (!is_null($value)) {
                         $query->orWhere($field, $condition, $value);
                     }
                 }
             }
         });
     }
     if (isset($orderBy) && !empty($orderBy)) {
         $model = $model->orderBy($orderBy, $sortedBy);
     }
     if (isset($filter) && !empty($filter)) {
         if (is_string($filter)) {
             $filter = explode(';', $filter);
         }
         $model = $model->select($filter);
     }
     if ($with) {
         $with = explode(';', $with);
         $model = $model->with($with);
     }
     return $model;
 }
Example #2
0
 public function apply($builder, RepositoryInterface $repository)
 {
     $fieldsSearchable = $repository->getFieldsSearchable();
     $search = $this->request->get(config('repository.criteria.params.search', 'search'), null);
     $searchFields = $this->request->get(config('repository.criteria.params.searchFields', 'searchFields'), null);
     $filter = $this->request->get(config('repository.criteria.params.filter', 'filter'), null);
     $orderBy = $this->request->get(config('repository.criteria.params.orderBy', 'orderBy'), null);
     $sortedBy = $this->request->get(config('repository.criteria.params.sortedBy', 'sortedBy'), 'asc');
     $sortedBy = !empty($sortedBy) ? $sortedBy : 'asc';
     if ($search && is_array($fieldsSearchable) && count($fieldsSearchable)) {
         $searchFields = is_array($searchFields) || is_null($searchFields) ? $searchFields : explode(';', $searchFields);
         $fields = $this->parserFieldsSearch($fieldsSearchable, $searchFields);
         $isFirstField = true;
         $searchData = $this->parserSearchData($search);
         $search = $this->parserSearchValue($search);
         $modelForceAndWhere = false;
         $builder = $builder->where(function ($query) use($fields, $search, $searchData, $isFirstField, $modelForceAndWhere) {
             foreach ($fields as $field => $condition) {
                 if (is_numeric($field)) {
                     $field = $condition;
                     $condition = '=';
                 }
                 $value = null;
                 $condition = trim(strtolower($condition));
                 if (isset($searchData[$field])) {
                     $value = $condition == 'like' ? "%{$searchData[$field]}%" : $searchData[$field];
                 } else {
                     if (!is_null($search)) {
                         $value = $condition == 'like' ? "%{$search}%" : $search;
                     }
                 }
                 if ($isFirstField || $modelForceAndWhere) {
                     if (!is_null($value)) {
                         $query->where($field, $condition, $value);
                         $isFirstField = false;
                     }
                 } else {
                     if (!is_null($value)) {
                         $query->orWhere($field, $condition, $value);
                     }
                 }
             }
         });
     }
     if (isset($orderBy) && in_array(strtolower($sortedBy), ['asc', 'desc'])) {
         $builder = $builder->orderBy($orderBy, $sortedBy);
     }
     if (isset($filter) && !empty($filter)) {
         if (is_string($filter)) {
             foreach (array_unique(explode(',', $filter)) as $filter) {
                 // eg. filter 'hot' 会调用方法 'filterHot'
                 $method_name = camel_case('filter_' . $filter);
                 if (method_exists($this, $method_name)) {
                     $builder = call_user_func([$this, $method_name], $builder);
                 }
             }
         }
     }
     return $builder;
 }
 /**
  * @param                     $model
  * @param RepositoryInterface $repository
  * @return mixed
  */
 public function apply($model, RepositoryInterface $repository)
 {
     $repository->skipCriteria(true);
     $uidList = $repository->getUidList()->filter(function ($item) {
         return $this->user->can(["documents:unmanaged::pool-{$item}-read", "documents:unmanaged::pool-{$item}-write"]);
     });
     $repository->skipCriteria(false);
     $model = $model->whereIn('uid', $uidList);
     return $model;
 }
 /**
  * Apply criteria in query repository
  *
  * @param         Builder|Model $model
  * @param RepositoryInterface $repository
  *
  * @return mixed
  * @throws \Exception
  */
 public function apply($model, RepositoryInterface $repository)
 {
     $fieldsSearchable = $repository->getFieldsSearchable();
     $search = $this->request->get(config('repository.criteria.params.search', 'search'), null);
     $searchFields = $this->request->get(config('repository.criteria.params.searchFields', 'searchFields'), null);
     $filter = $this->request->get(config('repository.criteria.params.filter', 'filter'), null);
     $orderBy = $this->request->get(config('repository.criteria.params.orderBy', 'orderBy'), null);
     $sortedBy = $this->request->get(config('repository.criteria.params.sortedBy', 'sortedBy'), 'asc');
     $with = $this->request->get(config('repository.criteria.params.with', 'with'), null);
     $sortedBy = !empty($sortedBy) ? $sortedBy : 'asc';
     if ($search && is_array($fieldsSearchable) && count($fieldsSearchable)) {
         $searchFields = is_array($searchFields) || is_null($searchFields) ? $searchFields : explode(';', $searchFields);
         $fields = $this->parserFieldsSearch($fieldsSearchable, $searchFields);
         $isFirstField = true;
         $searchData = $this->parserSearchData($search);
         $search = $this->parserSearchValue($search);
         $modelForceAndWhere = method_exists($repository, 'getIsSearchableForceAndWhere') ? $repository->getIsSearchableForceAndWhere() : false;
         $model = $model->where(function ($query) use($fields, $search, $searchData, $isFirstField, $modelForceAndWhere) {
             /** @var Builder $query */
             foreach ($fields as $field => $condition) {
                 if (is_numeric($field)) {
                     $field = $condition;
                     $condition = "=";
                 }
                 $value = null;
                 $condition = trim(strtolower($condition));
                 if (isset($searchData[$field])) {
                     if ($condition == "like_raw") {
                         $condition = "like";
                         $value = $searchData[$field];
                     } else {
                         $value = $condition == "like" || $condition == "ilike" ? "%{$searchData[$field]}%" : $searchData[$field];
                     }
                 } else {
                     if (!is_null($search)) {
                         if ($condition == "like_raw") {
                             $condition = "like";
                             $value = $searchData[$field];
                         } else {
                             $value = $condition == "like" || $condition == "ilike" ? "%{$search}%" : $search;
                         }
                     }
                 }
                 $relation = null;
                 if (stripos($field, '.')) {
                     $explode = explode('.', $field);
                     $field = array_pop($explode);
                     $relation = implode('.', $explode);
                 }
                 $modelTableName = $query->getModel()->getTable();
                 if ($isFirstField || $modelForceAndWhere) {
                     if (!is_null($value)) {
                         if (!is_null($relation)) {
                             $query->whereHas($relation, function ($query) use($field, $condition, $value) {
                                 $query->where($field, $condition, $value);
                             });
                         } else {
                             $query->where($modelTableName . '.' . $field, $condition, $value);
                         }
                         $isFirstField = false;
                     }
                 } else {
                     if (!is_null($value)) {
                         if (!is_null($relation)) {
                             $query->orWhereHas($relation, function ($query) use($field, $condition, $value) {
                                 $query->where($field, $condition, $value);
                             });
                         } else {
                             $query->orWhere($modelTableName . '.' . $field, $condition, $value);
                         }
                     }
                 }
             }
         });
     }
     if (isset($orderBy) && !empty($orderBy)) {
         $split = explode('|', $orderBy);
         if (count($split) > 1) {
             /*
              * ex.
              * products|description -> join products on current_table.product_id = products.id order by description
              *
              * products:custom_id|products.description -> join products on current_table.custom_id = products.id order
              * by products.description (in case both tables have same column name)
              */
             $table = $model->getModel()->getTable();
             $sortTable = $split[0];
             $sortColumn = $split[1];
             $split = explode(':', $sortTable);
             if (count($split) > 1) {
                 $sortTable = $split[0];
                 $keyName = $table . '.' . $split[1];
             } else {
                 /*
                  * If you do not define which column to use as a joining column on current table, it will
                  * use a singular of a join table appended with _id
                  *
                  * ex.
                  * products -> product_id
                  */
                 $prefix = rtrim($sortTable, 's');
                 $keyName = $table . '.' . $prefix . '_id';
             }
             $model = $model->leftJoin($sortTable, $keyName, '=', $sortTable . '.id')->orderBy($sortColumn, $sortedBy)->addSelect($table . '.*');
         } else {
             $model = $model->orderBy($orderBy, $sortedBy);
         }
     }
     if (isset($filter) && !empty($filter)) {
         if (is_string($filter)) {
             $filter = explode(';', $filter);
         }
         $model = $model->select($filter);
     }
     if ($with) {
         $with = explode(';', $with);
         $model = $model->with($with);
     }
     return $model;
 }
 /**
  * Método para deletar um registro
  *
  * @param $id
  * @return int
  *
  */
 public function delete($id)
 {
     return $this->repository->delete($id);
 }
Example #6
0
 /**
  * Apply criteria in query
  *
  * @param Builder $query
  *
  * @return mixed
  */
 public function apply($query, RepositoryInterface $repository = null)
 {
     if ($repository) {
         $this->searchables = array_merge($this->searchables, $repository->getFieldsSearchable());
     }
     if ($nameSearchable = config('repository.criteria.params.search')) {
         $this->setNameSearchable($nameSearchable);
     }
     if ($orderBy = config('repository.criteria.params.orderBy')) {
         $this->setFieldOrderBy($orderBy);
     }
     if ($sortedBy = config('repository.criteria.params.sortedBy')) {
         $this->setFieldSortedBy($sortedBy);
     }
     $model = $query;
     if ($query instanceof EloquentBuilder) {
         $model = $query->getModel();
     }
     if ($model instanceof Model) {
         $this->dates = $model->getDates();
         $this->table = $model->getTable() . '.';
     }
     if ($model instanceof AbstractEntity) {
         $this->addColumns($model->columns());
     }
     foreach ($this->input as $key => $value) {
         // Parameter Grouping
         if ($value instanceof \Closure) {
             $query = $query->where($value);
             continue;
         }
         // Scope
         $methodScope = 'scope' . studly_case($key);
         if (is_object($model) && method_exists($model, $methodScope)) {
             $methodName = camel_case($key);
             $query = $query->{$methodName}($value);
             continue;
         }
         // Where Search
         if ($key === $this->nameSearchable) {
             $query = $this->whereSearch($query, $value);
             continue;
         }
         if (is_int($key)) {
             // Using A Raw Expression
             if ($value instanceof QueryExpression) {
                 $query = $query->whereRaw($value);
             }
             /**
              * Using String Format
              * eg: {field},{operator},{value}
              */
             if (is_string($value) && preg_match('/^([a-zA-Z0-9_]+),(.+),(.+)$/', $value, $matches)) {
                 if (count($matches) == 4) {
                     $value = array_splice($matches, 1, 3);
                 }
             }
             /**
              * Using Array com Operator
              * eg: ex: ('field', '=', 'value') or ('field', 'value')
              */
             if (is_array($value) && count($value)) {
                 $value = array_pad($value, 3, null);
                 list($field, $operator, $valor) = array_splice($value, 0, 3);
                 $query = $this->whereCriteria($query, $field, $operator, $valor);
             }
             continue;
         }
         $query = $this->whereCriteria($query, $key, '=', $value);
     }
     // Order By
     if ($this->orderBy && in_array($this->orderBy, $this->columns)) {
         $query = $query->orderBy($this->orderBy, $this->sortedBy);
     }
     return $query;
 }
 /**
  * @param $id
  * @return array
  */
 public function delete($id)
 {
     $this->rep->delete($id);
 }