Example #1
0
 public function listing()
 {
     $sort = null;
     if ($this->isAdmin == true) {
         $filter['product_id'] = $this->req['product_id'];
         $filter['product_name'] = $this->req['product_name'];
         $filter['product_category'] = $this->req['product_category'];
         $filter['product_price_from'] = $this->req['product_price_from'];
         $filter['product_price_to'] = $this->req['product_price_to'];
         $filter['product_quantity_from'] = $this->req['product_quantity_from'];
         $filter['product_quantity_to'] = $this->req['product_quantity_to'];
         $filter['product_created_from'] = $this->req['product_created_from'];
         $filter['product_created_to'] = $this->req['product_created_to'];
         $filter['product_status'] = $this->req['product_status'];
         $filter['product_category'] = $this->req['product_category'];
         if (isset($this->req['columns'][$this->req['order'][0]['column']]['data'])) {
             $sort[$this->req['columns'][$this->req['order'][0]['column']]['data']] = $this->req['order'][0]['dir'];
         }
     } else {
         $filter['product_status'] = 1;
         //$filter['product_instock']      = 1;
         $filter['product_quantity_from'] = 1;
         if (isset($this->req['ctg'])) {
             $filter['product_category'] = $this->req['ctg'];
         }
         if (is_array($this->req['filter']['catgs'])) {
             foreach ($this->req['filter']['catgs'] as $k => $v) {
                 $filter['product_category'] .= $k . ",";
             }
             $filter['product_category'] = rtrim($filter['product_category'], ",");
         }
         if (isset($this->req['filter']['amtr']) && $this->req['filter']['amtr'] != "") {
             $temp = array();
             $temp = explode(" - ", $this->req['filter']['amtr']);
             if (is_numeric($temp[0])) {
                 $filter['product_price_from'] = $temp[0];
             }
             if (is_numeric($temp[1])) {
                 $filter['product_price_to'] = $temp[1];
             }
         }
         if (isset($this->req['srch']) && $this->req['srch'] != "") {
             $filter['product_srch'] = $this->req['srch'];
         }
         if ($this->req['filter']['instock'] == 1) {
             $filter['product_instock'] = 1;
         }
         if (isset($this->req['srt']) && $this->req['srt'] != "") {
             $temp = explode("_", $this->req['srt']);
             if ($temp[0] == "name") {
                 echo $sort["title"] = $temp[1] == "d" ? "desc" : "asc";
             } elseif ($temp[0] == "price") {
                 $sort["price"] = $temp[1] == "d" ? "desc" : "asc";
             } elseif ($temp[0] == "time") {
                 $sort["time"] = $temp[1] == "d" ? "desc" : "asc";
             } elseif ($temp[0] == "sqty") {
                 $sort["sqty"] = $temp[1] == "d" ? "desc" : "asc";
             }
         }
     }
     if (isset($this->req['filter']['attrib'])) {
         $aids = "";
         $avalues = "";
         foreach ($this->req['filter']['attrib'] as $aid => $vals) {
             $aids .= "," . $aid;
             if (is_array($vals)) {
                 foreach ($vals as $atrvalue) {
                     $avalues .= "," . $atrvalue;
                 }
             }
         }
         $ahc = new AttributeHelper();
         $productIds = $ahc->getProductidsForAttributes(trim($aids, ","), trim($avalues, ","));
         if ($productIds != "") {
             $filter['product_ids'] = $productIds;
         }
     }
     $data = $this->productModel->fetchProductsBySearch($sort, $filter);
     if ($this->isAdmin == true) {
         $iTotalRecords = count($data);
         $iDisplayLength = intval($this->req['length']);
         $iDisplayLength = $iDisplayLength < 0 ? $iTotalRecords : $iDisplayLength;
         $iDisplayStart = intval($this->req['start']);
         $sEcho = intval($this->req['draw']);
         $records = array();
         $records["data"] = array();
         $end = $iDisplayStart + $iDisplayLength;
         $end = $end > $iTotalRecords ? $iTotalRecords : $end;
         for ($i = $iDisplayStart; $i < $end; $i++) {
             if ($data[$i]->getIsActive() == true) {
                 $status = array("success" => "Active");
             } else {
                 $status = array("danger" => "In-Active");
             }
             $tempCatgs = trim($data[$i]->getCategories(), ",");
             $tempCatgArr = null;
             if ($tempCatgs != "") {
                 $tempCatgArr = explode(",", $tempCatgs);
                 foreach ($tempCatgArr as $cat) {
                     $tempCatgs = str_replace($cat, $this->catgListName[$cat], $tempCatgs);
                 }
             }
             $records["data"][] = array('cbox' => '<input type="checkbox" name="id[]" value="' . $data[$i]->getProductId() . '">', 'id' => $data[$i]->getProductId(), 'title' => $data[$i]->getTitle(), 'category' => $tempCatgs, 'price' => $data[$i]->getPrice(), 'qty' => $data[$i]->getQty(), 'time' => $data[$i]->getInsertTime(), 'status' => '<span class="label label-sm label-' . key($status) . '">' . current($status) . '</span>', 'options' => '<a href="/adm/Product/edit/' . $data[$i]->getProductId() . '" class="btn btn-xs default btn-editable"><i class="fa fa-pencil"></i> Edit</a>');
         }
         if (isset($this->req["customActionType"]) && $this->req["customActionType"] == "group_action") {
             $records["customActionStatus"] = "OK";
             // pass custom message(useful for getting status of group actions)
             $records["customActionMessage"] = "Group action successfully has been completed. Well done!";
             // pass custom message(useful for getting status of group actions)
         }
         $records["draw"] = $sEcho;
         $records["recordsTotal"] = $iTotalRecords;
         $records["recordsFiltered"] = $iTotalRecords;
         echo json_encode($records);
     } else {
         return $data;
     }
 }