/**
  * Get the total.
  *
  * @return int
  */
 public function getTotal()
 {
     if ($this->paginator instanceof LengthAwarePaginator) {
         return $this->paginator->total();
     }
     return $this->paginator->count();
 }
示例#2
0
 /**
  * Grab the actual instances.
  *
  * @return mixed
  */
 public function getInstances()
 {
     // First create the params object with stuff like search, sorting
     $this->createParams();
     // And finally grab the entities
     if ($this->entity->list_template->paginate) {
         // Pagination config is set: grab the current page
         $this->pagination = $this->repo->paginate($this->entity->list_template->paginate, $this->params);
         $this->count = $this->pagination->total();
         return $this->pagination->items();
     } else {
         // Grab all entities of this kind from DB
         $instances = $this->repo->listAll($this->params);
         $this->count = sizeof($instances);
         return $instances;
     }
 }
示例#3
0
 public function build()
 {
     if (is_string($this->source) && strpos(" ", $this->source) === false) {
         //tablename
         $this->type = "query";
         $this->query = $this->table($this->source);
     } elseif (is_a($this->source, "\\Illuminate\\Database\\Eloquent\\Model")) {
         $this->type = "model";
         $this->query = $this->source;
         $this->key = $this->source->getKeyName();
     } elseif (is_a($this->source, "\\Illuminate\\Database\\Eloquent\\Builder")) {
         $this->type = "model";
         $this->query = $this->source;
         $this->key = $this->source->getModel()->getKeyName();
     } elseif (is_a($this->source, "\\Illuminate\\Database\\Query\\Builder")) {
         $this->type = "model";
         $this->query = $this->source;
     } elseif (is_a($this->source, "\\Zofe\\Rapyd\\DataFilter\\DataFilter")) {
         $this->type = "model";
         $this->query = $this->source->query;
         if (is_a($this->query, "\\Illuminate\\Database\\Eloquent\\Model")) {
             $this->key = $this->query->getKeyName();
         } elseif (is_a($this->query, "\\Illuminate\\Database\\Eloquent\\Builder")) {
             $this->key = $this->query->getModel()->getKeyName();
         }
     } elseif (is_array($this->source)) {
         $this->type = "array";
     } else {
         throw new DataSetException(' "source" must be a table name, an eloquent model or an eloquent builder. you passed: ' . get_class($this->source));
     }
     //build orderby urls
     $this->orderby_uri_asc = $this->url->remove('page' . $this->cid)->remove('reset' . $this->cid)->append('ord' . $this->cid, "-field-")->get() . $this->hash;
     $this->orderby_uri_desc = $this->url->remove('page' . $this->cid)->remove('reset' . $this->cid)->append('ord' . $this->cid, "--field-")->get() . $this->hash;
     //detect orderby
     $orderby = $this->url->value("ord" . $this->cid);
     if ($orderby) {
         $this->orderby_field = ltrim($orderby, "-");
         $this->orderby_direction = $orderby[0] === "-" ? "desc" : "asc";
         if ($this->canOrderby($this->orderby_field)) {
             $this->orderBy($this->orderby_field, $this->orderby_direction);
         }
     }
     //build subset of data
     switch ($this->type) {
         case "array":
             //orderby
             if (isset($this->orderby)) {
                 list($field, $direction) = $this->orderby;
                 $column = array();
                 foreach ($this->source as $key => $row) {
                     $column[$key] = is_object($row) ? $row->{$field} : $row[$field];
                 }
                 if ($direction == "asc") {
                     array_multisort($column, SORT_ASC, $this->source);
                 } else {
                     array_multisort($column, SORT_DESC, $this->source);
                 }
             }
             $limit = $this->limit ? $this->limit : 100000;
             $current_page = $this->url->value('page' . $this->cid, 0);
             $offset = max($current_page - 1, 0) * $limit;
             $this->data = array_slice($this->source, $offset, $limit);
             $this->total_rows = count($this->source);
             $this->paginator = new LengthAwarePaginator($this->data, $this->total_rows, $limit, $current_page, ['path' => Paginator::resolveCurrentPath(), 'pageName' => "page" . $this->cid]);
             break;
         case "query":
         case "model":
             //orderby
             if (isset($this->orderby)) {
                 $this->query = $this->query->orderBy($this->orderby[0], $this->orderby[1]);
             }
             //limit-offset
             if (isset($this->limit)) {
                 $this->paginator = $this->query->paginate($this->limit, ['*'], 'page' . $this->cid);
                 $this->data = $this->paginator;
                 $this->total_rows = $this->paginator->total();
             } else {
                 $this->data = $this->query->get();
                 $this->total_rows = count($this->data);
             }
             break;
     }
     return $this;
 }