public function build()
{
if (is_string($this->source) && strpos(" ", $this->source) === false) {
//tablename
$this->type = "query";
$this->query = $this->table($this->source);
$this->total_rows = $this->query->count();
} elseif (is_a($this->source, "\\Illuminate\\Database\\Eloquent\\Model") || is_a($this->source, "\\Illuminate\\Database\\Eloquent\\Builder")) {
$this->type = "model";
$this->query = $this->source;
$this->total_rows = $this->query->count();
} elseif (is_array($this->source)) {
$this->type = "array";
$this->total_rows = count($this->source);
}
//exception
//offset and pagination setup/detect
$config = array('cid' => $this->cid, 'total_items' => $this->total_rows, 'items_per_page' => $this->per_page, 'num_links' => round($this->num_links / 2), 'hash' => $this->hash, 'url' => $this->url, 'current_page' => $this->current_page);
$this->pagination = new \Rapyd\Helpers\Pagination($config);
$offset = $this->pagination->offset();
$this->limit($this->per_page, $offset);
//build orderby urls
$this->orderby_uri_asc = $this->app->url->remove('pag' . $this->cid)->remove('reset' . $this->cid)->append('orderby' . $this->cid, array("-field-", "asc"))->get() . $this->hash;
$this->orderby_uri_desc = $this->app->url->remove('pag' . $this->cid)->remove('reset' . $this->cid)->append('orderby' . $this->cid, array("-field-", "desc"))->get() . $this->hash;
//detect orderby
$orderby = $this->app->url->value("orderby" . $this->cid);
if ($orderby) {
$this->orderby_field = $orderby[0];
$this->orderby_direction = $orderby[1];
$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] = $row[$field];
}
if ($direction == "asc") {
array_multisort($column, SORT_ASC, $this->source);
} else {
array_multisort($column, SORT_DESC, $this->source);
}
}
//limit-offset
if (isset($this->limit)) {
$this->source = array_slice($this->source, $this->limit[1], $this->limit[0]);
}
$this->data = $this->source;
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->query = $this->query->skip($this->pagination->offset())->take($this->per_page);
}
$this->data = $this->query->get();
break;
}
return $this;
}