Example #1
0
 /**
  * Paginates the Elasticsearch results.
  *
  * @param int $perPage
  * @return mixed
  */
 public function paginate($perPage = 15)
 {
     $paginator = Paginator::make($this->items, count($this->items), $perPage);
     $start = ($paginator->getFactory()->getCurrentPage() - 1) * $perPage;
     $sliced = array_slice($this->items, $start, $perPage);
     return Paginator::make($sliced, count($this->items), $perPage);
 }
Example #2
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";
         $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);
                 }
             }
             // @TODO: must be refactored
             $this->paginator = Paginator::make($this->source, count($this->source), $this->limit ? $this->limit : 1000000);
             //find better way
             $this->data = array_slice($this->source, $this->paginator->getFrom() - 1, $this->limit);
             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);
                 $this->data = $this->paginator;
             } else {
                 $this->data = $this->query->get();
             }
             break;
     }
     return $this;
 }
Example #3
0
 /**
  * Returns if table have at least 1 row
  * 
  */
 public function hasRows()
 {
     if ($this->_paginator->getTotalItemCount() > 0) {
         return true;
     }
     return false;
 }