Пример #1
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();
 }
Пример #2
0
 /**
  * Get a list of resources
  *
  * @apiMethod GET
  * @apiUri    /resources/list
  * @apiParameter {
  * 		"name":          "limit",
  * 		"description":   "Number of result to return.",
  * 		"type":          "integer",
  * 		"required":      false,
  * 		"default":       25
  * }
  * @apiParameter {
  * 		"name":          "start",
  * 		"description":   "Number of where to start returning results.",
  * 		"type":          "integer",
  * 		"required":      false,
  * 		"default":       0
  * }
  * @apiParameter {
  * 		"name":          "type",
  * 		"description":   "Type of resource to filter results.",
  * 		"type":          "string",
  * 		"required":      false,
  * 		"default":       ""
  * }
  * @apiParameter {
  * 		"name":          "sortby",
  * 		"description":   "Value to sort results by.",
  * 		"type":          "string",
  * 		"required":      false,
  * 		"default":       "date",
  * 		"allowedValues": "date, title, random"
  * }
  * @apiParameter {
  * 		"name":          "search",
  * 		"description":   "A word or phrase to search for.",
  * 		"type":          "string",
  * 		"required":      false,
  * 		"default":       ""
  * }
  * @return    void
  */
 public function listTask()
 {
     // Incoming
     $filters = array('type' => Request::getVar('type', ''), 'sortby' => Request::getCmd('sortby', 'date'), 'limit' => Request::getInt('limit', Config::get('list_limit')), 'start' => Request::getInt('limitstart', 0), 'search' => Request::getVar('search', ''));
     if (!in_array($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.', $filters['sortby']));
     }
     require_once Component::path('com_resources') . DS . 'tables' . DS . 'resource.php';
     require_once Component::path('com_resources') . DS . 'tables' . DS . 'type.php';
     $database = App::get('db');
     // Instantiate a resource object
     $rr = new Resource($database);
     // encode results and return response
     $response = new stdClass();
     $response->records = array();
     $response->total = $rr->getCount($filters);
     if ($response->total) {
         // Get major types
         $t = new Type($database);
         $types = array();
         foreach ($t->getMajorTypes() as $type) {
             unset($type->params);
             unset($type->customFields);
             $types[$type->id] = $type;
         }
         $response->records = $rr->getRecords($filters);
         $base = rtrim(Request::base(), '/');
         foreach ($response->records as $i => $entry) {
             $entry->url = str_replace('/api', '', $base . '/' . ltrim(Route::url('index.php?option=com_resources&' . ($entry->alias ? 'alias=' . $entry->alias : 'id=' . $entry->id)), '/'));
             if (isset($types[$entry->type])) {
                 $entry->type = $types[$entry->type];
             }
             $response->records[$i] = $entry;
         }
     }
     $response->success = true;
     $this->send($response);
 }