/**
  * The manage method serves as both an index listing of all galleries as well
  * as a management tool for items within the gallery.
  * 
  * If no $id is passed, then an indexed listing of galleries (with links) will appear.
  * Clicking on one of these listed galleries will then return to this method with
  * and $id value present.
  * 
  * When an $id is present, the user will be able to add existing gallery items to 
  * the gallery as well as upload new gallery items to be associated with the gallery.
  * 
  * @param string $id The Page id for the gallery
  * @return
  */
 public function manage($id = null)
 {
     // TODO: Use Minerva's access system (li3_access)
     // Bigger todo: update li3_acess (Nate's changes) and redo Minerva's access system completely.
     $user = Auth::check('minerva_user');
     if ($user['role'] != 'administrator' && $user['role'] != 'content_editor') {
         $this->redirect('/');
         return;
     }
     if (empty($id)) {
         // Default options for pagination, merge with URL parameters
         $defaults = array('page' => 1, 'limit' => 10, 'order' => 'created.desc');
         $params = Set::merge($defaults, $this->request->params);
         if (isset($params['page']) && $params['page'] == 0) {
             $params['page'] = 1;
         }
         list($limit, $page, $order) = array($params['limit'], $params['page'], $params['order']);
         // Never allow a limit of 0
         $limit = $limit < 0 ? 1 : $limit;
         // Only pull back minerva_gallery pages
         $conditions = array('document_type' => 'minerva_gallery');
         // For search queries
         if (isset($this->request->query['q']) && !empty($this->request->query['q'])) {
             $search_schema = Page::searchSchema();
             $search_conditions = array();
             // For each searchable field, adjust the conditions to include a regex
             foreach ($search_schema as $k => $v) {
                 $search_regex = new \MongoRegex('/' . $this->request->query['q'] . '/i');
                 $conditions['$or'][] = array($k => $search_regex);
             }
         }
         // Get the documents and the total
         $documents = Page::find('all', array('conditions' => $conditions, 'limit' => (int) $limit, 'offset' => ((int) $page - 1) * (int) $limit, 'order' => $params['order']));
         $total = Page::find('count', array('conditions' => $conditions));
         $page_number = (int) $page;
         $total_pages = (int) $limit > 0 ? ceil($total / $limit) : 0;
         // Use the manage_index template in this case
         $this->_render['template'] = 'manage_index';
         // Set data for the view template
         $this->set(compact('documents', 'limit', 'page_number', 'total_pages', 'total'));
     } else {
         // Only pull the latest 30 gallery items from the entire system...
         // Because it's reasonable. There could be thousands of items and paging
         // through is an option, but not practical for the design and user experience.
         // 30 of the latest is enough and the user can make a search to find what
         // they are after. The point of this listing of items is to allow the user to
         // associate an existing item in the system with the current gallery.
         // It's not going to be as common as adding brand new items instead.
         // Well, unless the user really goes back to share items across multiple
         // galleries on a regular basis...I don't think it's common, but possible.
         // So showing 30 plus a search is plenty.
         $conditions = array('published' => true, '_galleries' => array('$nin' => array($id)));
         // For search queries for items
         if (isset($this->request->query['q']) && !empty($this->request->query['q'])) {
             $search_schema = Item::searchSchema();
             $search_conditions = array();
             // For each searchable field, adjust the conditions to include a regex
             foreach ($search_schema as $k => $v) {
                 $search_regex = new \MongoRegex('/' . $this->request->query['q'] . '/i');
                 $conditions['$or'][] = array($k => $search_regex);
             }
         }
         // Find the unassociated gallery items
         $items = Item::find('all', array('conditions' => $conditions, 'limit' => 30, 'order' => array('created' => 'desc')));
         // Find all items for the current gallery
         $gallery_items = Item::find('all', array('conditions' => array('_galleries' => $id)));
         // Find the gallery document itself
         $document = Page::find('first', array('conditions' => array('_id' => $id)));
         // Order those gallery items based on the gallery document's gallery_item_order field (if set)
         if (isset($document->gallery_item_order) && !empty($document->gallery_item_order)) {
             // This sort() method is the awesome.
             $ordering = $document->gallery_item_order->data();
             // data() must be called so that the iterator loads up all the documents...
             // Something that has to be fixed I guess. Then data() doesn't need to be called.
             $gallery_items->data();
             $gallery_items->sort(function ($a, $b) use($ordering) {
                 if ($a['_id'] == $b['_id']) {
                     return strcmp($a['_id'], $b['_id']);
                 }
                 $cmpa = array_search($a['_id'], $ordering);
                 $cmpb = array_search($b['_id'], $ordering);
                 return $cmpa > $cmpb ? 1 : -1;
             });
         }
         $this->set(compact('document', 'items', 'gallery_items'));
     }
 }