コード例 #1
0
 public function getDocs()
 {
     // Handle order by.
     $order_field = Input::get('order', 'updated_at');
     $order_dir = Input::get('order_dir', 'DESC');
     // Handle pagination.
     $limit = null;
     $offset = null;
     $title = null;
     if (Input::has('limit')) {
         $limit = Input::get('limit');
         if (Input::has('page')) {
             $offset = (Input::get('page') - 1) * Input::get('limit');
         }
     }
     // Activity is a wholly different beast right now, requiring complicated
     // queries.
     if ($order_field === 'activity') {
         // TODO: Make this handle DESC order, maybe?
         $docs = Doc::getActive($limit, $offset);
     } else {
         $doc = Doc::getEager()->orderBy($order_field, $order_dir)->where('private', '!=', '1')->where('is_template', '!=', '1');
         if (Input::has('category')) {
             $doc = Doc::getEager()->whereHas('categories', function ($q) {
                 $category = Input::get('category');
                 $q->where('categories.name', 'LIKE', "%{$category}%");
             })->where('private', '!=', '1')->where('is_template', '!=', '1');
         }
         if (isset($limit)) {
             $doc->take($limit);
             if (isset($offset)) {
                 $doc->skip($offset);
             }
         }
         if (Input::has('title')) {
             $title = Input::get('title');
             $doc->where('title', 'LIKE', "%{$title}%");
         }
         $docs = $doc->get();
     }
     $return_docs = array();
     if ($docs) {
         foreach ($docs as $doc) {
             $doc->enableCounts();
             $return_doc = $doc->toArray();
             $return_doc['updated_at'] = date('c', strtotime($return_doc['updated_at']));
             $return_doc['created_at'] = date('c', strtotime($return_doc['created_at']));
             $return_docs[] = $return_doc;
         }
     }
     return Response::json($return_docs);
 }