예제 #1
0
 /**
  * Get a list of resource types
  *   Accepts either a numeric array index or a string [id, name]
  *   If index, it'll return the entry matching that index in the list
  *   If string, it'll return either a list of IDs or names
  *
  * @param      mixed $idx Index value
  * @return     array
  */
 public function types($idx = null)
 {
     if (!$this->exists()) {
         return array();
     }
     if (!isset($this->types)) {
         $this->types = array();
         $rt = new Tables\Type($this->_db);
         if ($types = $rt->getMajorTypes()) {
             foreach ($types as $key => $type) {
                 if (!$type->alias) {
                     $types[$key]->alias = $rt->normalize($type->type);
                 }
             }
             $this->types = $types;
         }
     }
     if ($idx !== null) {
         if (is_numeric($idx)) {
             foreach ($this->types as $type) {
                 if ($type->id == $idx) {
                     return $type;
                 }
             }
         } else {
             if (is_string($idx)) {
                 $idx = trim($idx);
                 foreach ($this->types as $type) {
                     if ($type->alias == $idx) {
                         return $type;
                     }
                 }
             }
         }
         $this->setError(Lang::txt('Index not found: ') . __CLASS__ . '::' . __METHOD__ . '[' . $idx . ']');
         return false;
     }
     return $this->types;
 }
예제 #2
0
 /**
  * Browse entries
  *
  * @return     void
  */
 public function browseTask()
 {
     // Set the default sort
     $default_sort = 'date';
     if ($this->config->get('show_ranking')) {
         $default_sort = 'ranking';
     }
     // Incoming
     $this->view->filters = array('type' => Request::getVar('type', ''), 'sortby' => Request::getCmd('sortby', $default_sort), 'limit' => Request::getInt('limit', Config::get('list_limit')), 'start' => Request::getInt('limitstart', 0), 'search' => Request::getVar('search', ''), 'tag' => trim(Request::getVar('tag', '', 'request', 'none', 2)), 'tag_ignored' => array());
     if (!in_array($this->view->filters['sortby'], array('date', 'date_published', 'date_created', 'date_modified', 'title', 'rating', 'ranking', 'random'))) {
         App::abort(404, Lang::txt('Invalid sort value of "%s" used.', $this->view->filters['sortby']));
     }
     if (isset($this->view->filters['tag']) && $this->view->filters['tag'] != '') {
         include_once dirname(dirname(__DIR__)) . DS . 'helpers' . DS . 'tags.php';
         $tagging = new Tags(0);
         $tags = $tagging->parseTags($this->view->filters['tag']);
         if (count($tags) > 5) {
             $keep = array();
             foreach ($tags as $i => $tag) {
                 if ($i < 5) {
                     $keep[] = $tag;
                 } else {
                     $this->view->filters['tag_ignored'][] = $tag;
                 }
             }
             $this->view->filters['tag'] = implode(',', $keep);
         }
     }
     // Determine if user can edit
     $this->view->authorized = $this->_authorize();
     // Get major types
     $t = new Type($this->database);
     $this->view->types = $t->getMajorTypes();
     if (!is_numeric($this->view->filters['type'])) {
         // Normalize the title
         // This is so we can determine the type of resource to display from the URL
         // For example, /resources/learningmodules => Learning Modules
         for ($i = 0; $i < count($this->view->types); $i++) {
             $normalized = $this->view->types[$i]->alias ? $this->view->types[$i]->alias : $t->normalize($this->view->types[$i]->type);
             if (trim($this->view->filters['type']) == $normalized) {
                 $this->view->filters['type'] = $this->view->types[$i]->id;
                 break;
             }
         }
     }
     // Instantiate a resource object
     $rr = new Resource($this->database);
     // Execute count query
     $results = $rr->getCount($this->view->filters);
     $this->view->total = $results && is_array($results) ? count($results) : 0;
     // Run query with limit
     $this->view->results = $rr->getRecords($this->view->filters);
     // Get type if not given
     $this->_title = Lang::txt(strtoupper($this->_option)) . ': ';
     if ($this->view->filters['type'] != '') {
         $t->load($t->normalize($this->view->filters['type']));
         $this->_title .= $t->type;
         $this->_task_title = $t->type;
     } else {
         $this->_title .= Lang::txt('COM_RESOURCES_ALL');
         $this->_task_title = Lang::txt('COM_RESOURCES_ALL');
     }
     // Set page title
     $this->_buildTitle();
     // Set the pathway
     $this->_buildPathway();
     // Output HTML
     $this->view->title = $this->_title;
     $this->view->config = $this->config;
     foreach ($this->getErrors() as $error) {
         $this->view->setError($error);
     }
     $this->view->setName('browse')->setLayout('default')->display();
 }