lundi 7 mars 2016

Laravel and yajrabox Datatables server side proccessing

I'm working on a project which uses Laravel 5.1 as the server side framework, I built a tables to show leads using jQuery Datatables plugin and yajrabox Datatables add-on for Laravel.

I am implementing a server side processing but I need to manipulate the data before send it to the client, for do this I need to first pull all the data i'd like to show (to manipulate it) which make the process really long. this is the code i'm using:

public function index()
{ 
   return view('leads.test')->with($data);
}

returns the view, and:

 public function getLeads()
{
    $end = new \MongoDate(Carbon::now()->endOfDay()->setTimezone(Client::getTimezone(5))->timestamp);
    $start = new \MongoDate(Carbon::now()->subWeek()->startOfDay()->setTimezone(Client::getTimezone(5))->timestamp);
    return \Datatables::of(collect($this->setLeadData(Lead::where('deleted',0)->where('client_id','5')->whereBetween('created_at',[$start,$end])->orderBy('created_at','desc')->get())))->make(true);
}

return the leads, and last one, the manipulation process:

private function setLeadData($leads)
{
    $rtn = [];
    $row = [];
    foreach ($leads as $lead) {
        $row['DT_RowId'] = $lead->_id;
        if(\Auth::user()->canDeleteLeads()){
            $row['checkbox'] = "<input type='checkbox' class='bulk-delete-leads-checkbox' name='bulk_delete_leads[]' id='".$lead->_id."' /><label for='".$lead->_id."'></label>";
        }
        if(Carbon::createFromFormat("Y-m-d H:i:s", $lead->created_at)->isSameDay(Carbon::now()->startOfDay())){
            $row['date'] = "<i class='fa fa-clock-o'></i> at ".Carbon::createFromFormat("Y-m-d H:i:s", $lead->created_at)->timezone(Client::getTimezone())->format("H:i");
        }else{
            $row['date'] = "<i class='fa fa-clock-o'></i> ".Carbon::createFromFormat("Y-m-d H:i:s", $lead->created_at)->timezone(Client::getTimezone())->toDateTimeString();
        }
        if(is_array($lead->ga_details) && count($lead->ga_details) > 1){
            $row['user_type'] = $lead->ga_details['user_type'];
            $row['device_category'] = $lead->ga_details['device_category'];
            $row['source'] = $lead->ga_details['source'];
            $row['campaign'] = $lead->ga_details['campaign'];
            $row['ad_group'] = $lead->ga_details['ad_group'];
            $row['path'] = $lead->ga_details['path'];
        }
        $row['last_feed'] = null;
        if ($lead->feeds && count($lead->feeds)) {
            $row['last_feed'] = array_reverse($lead->feeds)[0]['message'];
        }
        $row['status'] = $this->setLeadStatusElement($lead->status,$lead->_id);
        $row['owner'] = $this->setLeadOwnerElement($lead->owner,$lead->_id);
        $data['has_reminder'] = false;
        $icon = '';
        $reminder = Notification::where('lead_id', $lead->_id)
            ->where('type', 'lead_reminder')
            ->where('due_time','<=',Carbon::now()->toDateTimeString())
            ->where('active',1)
            ->where('deleted',0)
            ->first();
        if($reminder){
            $data['has_reminder'] = true;
            $icon = " <i class='fa fa-bell'></i> ";
        }
        $row['full_name'] = '<button type="button" class="btn btn-w-m btn-white single-lead-trigger" style="width:100%!important" data-toggle="modal" data-lead-id="' .$lead->_id. '" data-target="#single_lead">No Name'.$icon.'</button>';
        if(isset($lead->the_lead) && count($lead->the_lead)){
            foreach($lead->the_lead as $k => $lead_detail){
                if($k == "full_name"){
                    $row['full_name'] = '<button type="button" class="btn btn-w-m btn-white single-lead-trigger" style="width:100%!important" data-toggle="modal" data-lead-id="' .$lead->_id. '" data-target="#single_lead">' .$lead_detail.$icon.'</button>';
                }else{
                    $row[$k] = $lead_detail;
                }
            }
            if(isset($lead->the_lead['full_name']) && !empty($lead->the_lead['full_name'])){
            }
        }

        $rtn[] = $row;
    }
    return $rtn;
}

Does any one has any suggestion about how to do it right and proper? thank you very much for any answer! Appreciate it!!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire