Ejemplo n.º 1
0
 public function generate($slug, $method = 'all', $limit = 20, $page = 1, $sort = [], $fields = [])
 {
     $value = null;
     if ($method == 'byId' || $method == 'bySlug') {
         $value = $limit;
     }
     if (substr_count($method, ':') > 0) {
         $tmp = explode(':', $method);
         $value = array_pop($tmp);
         $method = implode(':', $tmp);
     }
     if ($page < 1) {
         $page = 1;
     }
     if (empty($sort)) {
         $sort = [];
     }
     if (empty($fields)) {
         $fields = [];
     }
     $metadata = $this->model->collection($slug);
     if ($metadata === false) {
         throw new Exception('Can not load collection metadata for: ' . $slug);
     }
     $collection = new Collection($metadata, $this->root, $this->route, $this->db, $this->language, $this->person, $this->search);
     $collection->queryOptionsSet($limit, $page, $sort, $method, $value);
     return $collection;
 }
Ejemplo n.º 2
0
 public function json(\Opine\Collection\Collection $collection)
 {
     $method = $collection->methodGet();
     $value = $collection->valueGet();
     $head = '';
     $tail = '';
     if (isset($_GET['callback'])) {
         if ($_GET['callback'] == '?') {
             $_GET['callback'] = 'callback';
         }
         $head = $_GET['callback'] . '(';
         $tail = ');';
     }
     $options = null;
     $name = $collection->collection();
     if ($method == 'byEmbeddedField') {
         $name = $collection->name;
     }
     if (isset($_GET) && isset($_GET['pretty'])) {
         $options = JSON_PRETTY_PRINT;
         $head = '<html><head></head><body style="margin:0; border:0; padding: 0"><textarea wrap="off" style="overflow: auto; margin:0; border:0; padding: 0; width:100%; height: 100%">';
         $tail = '</textarea></body></html>';
     }
     if (in_array($method, ['byId', 'bySlug'])) {
         $name = $collection->singularGet();
         echo $head . json_encode([$name => $collection->{$method}($value)], $options) . $tail;
     } else {
         echo $head . json_encode([$name => $collection->{$method}($value), 'pagination' => ['limit' => $collection->limitGet(), 'total' => $collection->totalGet(), 'page' => $collection->pageGet(), 'pageCount' => ceil($collection->totalGet() / $collection->limitGet())], 'metadata' => array_merge(['display' => ['collection' => ucwords(str_replace('_', ' ', $collection->collection())), 'document' => ucwords(str_replace('_', ' ', $collection->singularGet()))], 'method' => $method], get_object_vars($collection))], $options) . $tail;
     }
 }