Esempio n. 1
0
 public function getDocs($state = null)
 {
     // Handle order by.
     $order_field = Input::get('order', 'updated_at');
     $order_dir = Input::get('order_dir', 'DESC');
     $discussion_state = Input::get('discussion_state', null);
     // 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('is_template', '!=', '1');
         if ($discussion_state) {
             $doc->where('discussion_state', '=', $discussion_state);
         }
         if (isset($state) && $state == 'all') {
             if (!Auth::check() || !Auth::user()->hasRole('Admin')) {
                 return response('Unauthorized.', 403);
             }
         } else {
             $doc->where('publish_state', '=', Doc::PUBLISH_STATE_PUBLISHED);
         }
         if (Input::has('category')) {
             $doc->whereHas('categories', function ($q) {
                 $category = Input::get('category');
                 $q->where('categories.name', 'LIKE', "%{$category}%");
             });
         }
         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 Response::json(Doc::prepareCountsAndDates($docs));
 }
 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('publish_state', '=', Doc::PUBLISH_STATE_PUBLISHED)->where('is_template', '!=', '1');
         if (Input::has('category')) {
             $doc->whereHas('categories', function ($q) {
                 $category = Input::get('category');
                 $q->where('categories.name', 'LIKE', "%{$category}%");
             });
         }
         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);
 }