Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
10 kB
4
Indexable
Never
public function  request_filter_text ($request,$query,$filter){
        //for text area
        foreach ($filter['values'] as $value){
            if(!empty($request[$value])){
                $all_array=array_filter(preg_split("/\r\n/",$request[$value]));
                $field=$filter['table_main'].'.'.$value;
                if(!empty($all_array)){
                    $query=$query ->Where(function ($q) use ($field, $all_array) {
                        $this->active_filter=1;
                        foreach ($all_array as $k=>$v ){
                            if(in_array($v,['null','NULL'])){
                                if($k== 0)  $q->whereNull($field)->orwhere($field,'=','');
                                else $q->orWhereNull($field)->orwhere($field,'=','');
                            }
                            else{
                                $v =str_replace("*","%",$v);
                                if($k== 0) $q->where($field, 'like', $v);
                                else $q->orwhere($field, 'like', $v);
                            }
                        }
                    });
                }
            }
        }

        return $query;
    }

    public function  request_filter_relation_text ($request,$query,$filter){
        $run_history= !empty($filter['history']) && $filter['history']?true:false;
        $field_history=$filter['table_relation'].'.history';
        foreach ($filter['values'] as $key=>$value){
            if(!empty($request[$value])){
                if($run_history){
                    $run_history=false;
                    $this->active_filter=1;
                    $query=$query->WhereHas($filter['relation'],function ($q) use ($filter, $field_history) {
                        $q->orderBy($filter['table_relation'].'.id', 'desc')->where($field_history,1);
                    });
                }
                $all_array=array_filter(preg_split("/\r\n/",$request[$value]));
                $field=$filter['table_relation'].'.'.$value;
                if(!empty($all_array)){
                    $query=$query ->WhereHas($filter['relation'],function ($q) use ($field, $all_array) {
                        $this->active_filter=1;
                        foreach ($all_array as $k=>$v ){
                            if(in_array($v,['null','NULL'])){
                                if($k== 0)  $q->whereNull($field)->orwhere($field,'=','');
                                else $q->orWhereNull($field)->orwhere($field,'=','');
                            }
                            else{
                                $v =str_replace("*","%",$v);
                                if($k== 0) $q->where($field, 'like', $v);
                                else $q->orwhere($field, 'like', $v);
                            }
                        }
                    });
                }
            }
        }
        return $query;
    }

    public function  request_filter_json_text ($request,$query,$filter){
        foreach ($filter['values'] as $value){
            if(!empty($request[$value])){
                $all_array=array_filter(preg_split("/\r\n/",$request[$value]));
                $field=$filter['table_main'].'.'.$value;
                if(!empty($all_array)){
                    $query=$query ->Where(function ($q) use ($field, $all_array) {
                        $this->active_filter=1;
                        foreach ($all_array as $k=>$v ){
                            if(in_array($v,['null','NULL'])){
                                if($k== 0)  $q->whereNull($field)->orwhere($field,'=','');
                                else $q->orWhereNull($field)->orwhere($field,'=','');
                            }
                            else{
                                $v =str_replace("*","%",$v);
                                if($k== 0) $q->whereJsonContains($field, $v);
                                else $q->orwhereJsonContains($field ,$v);
                            }
                        }
                    });
                }
            }
        }
        return $query;
    }

    public function  request_filter_options ($request,$query,$filter){
        //for option
        foreach ($filter['values'] as $value){
            if(!empty($request[$value])){
                $array=$request[$value];
                $field=$filter['table_main'].'.'.$value;
                $query=$query ->Where(function ($q) use ($field, $array) {
                    $this->active_filter=1;
                    if(in_array('NULL',$array)){
                        $q->WhereNull($field)->orwhere($field,'=','')->orwhereIn($field, $array);
                    }
                    else  $q->whereIn($field, $array);
                });
            }
        }
        return $query;
    }
    public function  request_filter_relation_options ($request,$query,$filter){
        $run_history=!empty($filter['history']) && $filter['history']?true:false;
        $field_history=$filter['table_relation'].'.history';
        foreach ($filter['values'] as $key=>$value){
            if(!empty($request[$value])){
                $field=$filter['table_relation'].'.'.$value;
                $array=$request[$value];
                if($run_history){
                    $run_history=false;
                    $this->active_filter=1;
                    $query=$query->WhereHas($filter['relation'],function ($q) use ($filter, $field_history) {
                        $q->orderBy($filter['table_relation'].'.id', 'desc')->where($field_history,1);
                    });
                }
                $query=$query ->WhereHas($filter['relation'],function ($q) use ($key, $run_history, $field_history, $field, $array, $filter) {
                    $this->active_filter=1;
                    if($run_history && $key==0){
                        $q->orderBy($filter['table_relation'].'.id', 'desc')->where($field_history,1);
                    }
                    if(in_array('NULL',$array)){
                        $q->WhereNull($field)->orwhere($field,'=','')->orwhereIn($field, $array);
                    }
                    else $q->whereIn($field, $array);
                });
            }
        }
        return $query;
    }

    public function  request_filter_date ($request,$query,$filter){
        //for date
        foreach ($filter['values'] as $value){
            $from=!empty($request[$value.'_from'])?$request[$value.'_from']:null;
            $to=!empty($request[$value.'_to'])?$request[$value.'_to']:null;
            $field=$filter['table_main'].'.'.$value;
            if (!empty($from) && !empty($to) ) {
                $query=$query ->Where(function ($q) use ($field, $to, $from) {
                    $this->active_filter=1;
                    $startDate = Carbon::createFromFormat('Y-m-d', $from)->startOfDay();
                    $endDate = Carbon::createFromFormat('Y-m-d', $to)->endOfDay();
                    $q->whereBetween($field, [$startDate,$endDate]);
                });
            }
            else if (!empty($from) && empty($to) ) {
                $query=$query ->Where(function ($q) use ($field, $from) {
                    $this->active_filter=1;
                    $q->where($field,'>=',$from);
                });
            }
            else if (empty($from) && !empty($to) ) {
                $query=$query->Where(function ($q) use ($field, $to) {
                    $this->active_filter=1;
                    $q->where($field,'<=',$to);
                });
            }
        }
        return $query;
    }

    public function  request_filter_relation_date ($request,$query,$filter){
        //for relation date
        $run_history=!empty($filter['history']) && $filter['history']?true:false;
        foreach ($filter['values'] as $value){
            $from=!empty($request[$value.'_from'])?$request[$value.'_from']:null;
            $to=!empty($request[$value.'_to'])?$request[$value.'_to']:null;
            $field=$filter['table_relation'].'.'.$value;
            $field_history=$filter['table_relation'].'.history';
            if($run_history && (!empty($from)||!empty($to))){
                $run_history=false;
                $this->active_filter=1;
                $query=$query->WhereHas($filter['relation'],function ($q) use ($filter, $field_history) {
                    $q->orderBy($filter['table_relation'].'.id', 'desc')->where($field_history,1);
                });
            }
            if (!empty($from) && !empty($to) ) {
                $query=$query->WhereHas($filter['relation'],function ($q) use ($field, $to, $from) {
                    $this->active_filter=1;
                    if(is_numeric($from) && is_numeric($to)){
                        $from=(int)$from;
                        $to=(int)$to;
                        $q->whereBetween($field, [$from, $to]);
                    }
                    else{
                        $start = Carbon::createFromFormat('Y-m-d',$from)->startOfDay();
                        $end = Carbon::createFromFormat('Y-m-d', $to)->endOfDay();
                        $q->whereBetween($field, [$start, $end]);
                    }
                });
            }
            else if (!empty($from) && empty($to) ) {
                $query=$query->WhereHas($filter['relation'],function ($q) use ($field, $from) {
                    $this->active_filter=1;
                    if(is_numeric($from)){
                        $from=(int)$from;
                        $q->where($field,'>=',$from);
                    }
                    else{
                        $q->where($field,'>=',$from);
                    }
                });
            }
            else if (empty($from) && !empty($to) ) {
                $query=$query->WhereHas($filter['relation'],function ($q) use ($field, $to) {
                    $this->active_filter=1;
                    if(is_numeric($to)){
                        $to=(int)$to;
                        $q->where($field,'<=',$to);
                    }
                    else{
                        $q->where($field,'<=',$to);
                    }
                });
            }
        }
        return $query;
    }