/**
  * View a specific category, showing all articles it contains
  */
 public function action_sportal_category()
 {
     global $context, $scripturl, $modSettings;
     // Basic article support
     require_once SUBSDIR . '/PortalArticle.subs.php';
     $category_id = !empty($_REQUEST['category']) ? $_REQUEST['category'] : 0;
     if (is_int($category_id)) {
         $category_id = (int) $category_id;
     } else {
         $category_id = Util::htmlspecialchars($category_id, ENT_QUOTES);
     }
     $context['category'] = sportal_get_categories($category_id, true, true);
     if (empty($context['category']['id'])) {
         fatal_lang_error('error_sp_category_not_found', false);
     }
     // Set up the pages
     $total_articles = sportal_get_articles_in_cat_count($context['category']['id']);
     $per_page = min($total_articles, !empty($modSettings['sp_articles_per_page']) ? $modSettings['sp_articles_per_page'] : 10);
     $start = !empty($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
     if ($total_articles > $per_page) {
         $context['page_index'] = constructPageIndex($context['category']['href'] . ';start=%1$d', $start, $total_articles, $per_page, true);
     }
     // Load the articles in this category
     $context['articles'] = sportal_get_articles(0, true, true, 'spa.id_article DESC', $context['category']['id'], $per_page, $start);
     foreach ($context['articles'] as $article) {
         // Cut me mick
         if (($cutoff = Util::strpos($article['body'], '[cutoff]')) !== false) {
             $article['body'] = Util::substr($article['body'], 0, $cutoff);
             if ($article['type'] === 'bbc') {
                 require_once SUBSDIR . '/Post.subs.php';
                 preparsecode($article['body']);
             }
         }
         $context['articles'][$article['id']]['preview'] = sportal_parse_content($article['body'], $article['type'], 'return');
         $context['articles'][$article['id']]['date'] = htmlTime($article['date']);
     }
     $context['linktree'][] = array('url' => $scripturl . '?category=' . $context['category']['category_id'], 'name' => $context['category']['name']);
     $context['page_title'] = $context['category']['name'];
     $context['sub_template'] = 'view_category';
 }
 /**
  * Sets up for an article preview
  */
 private function _sportal_admin_article_preview()
 {
     global $scripturl, $user_info;
     // Existing article will have some data
     if (!$this->_is_new) {
         $_REQUEST['article_id'] = (int) $_REQUEST['article_id'];
         $current = sportal_get_articles($_REQUEST['article_id']);
         $author = $current['author'];
         $date = standardTime($current['date']);
         list($views, $comments) = sportal_get_article_views_comments($_REQUEST['article_id']);
     } else {
         $author = array('link' => '<a href="' . $scripturl . '?action=profile;u=' . $user_info['id'] . '">' . $user_info['name'] . '</a>');
         $date = standardTime(time());
         $views = 0;
         $comments = 0;
     }
     $article = array('id' => $_POST['article_id'], 'article_id' => $_POST['namespace'], 'category' => sportal_get_categories((int) $_POST['category_id']), 'author' => $author, 'title' => Util::htmlspecialchars($_POST['title'], ENT_QUOTES), 'body' => Util::htmlspecialchars($_POST['content'], ENT_QUOTES), 'type' => $_POST['type'], 'permissions' => $_POST['permissions'], 'date' => $date, 'status' => !empty($_POST['status']), 'view_count' => $views, 'comment_count' => $comments);
     if ($article['type'] === 'bbc') {
         preparsecode($article['body']);
     }
     return $article;
 }
 /**
  * Edit or add a category
  */
 public function action_sportal_admin_category_edit()
 {
     global $context, $txt;
     loadTemplate('PortalAdminCategories');
     $this->_is_new = empty($_REQUEST['category_id']);
     // Saving the category form
     if (!empty($_POST['submit'])) {
         checkSession();
         // Clean what was sent
         // @todo move all this to validator?
         $name = isset($_POST['name']) ? Util::htmltrim(Util::htmlspecialchars($_POST['name'], ENT_QUOTES)) : '';
         $namespace = isset($_POST['namespace']) ? Util::htmltrim(Util::htmlspecialchars($_POST['namespace'], ENT_QUOTES)) : '';
         $current = isset($_POST['category_id']) ? (int) $_POST['category_id'] : 0;
         $description = isset($_POST['description']) ? Util::htmlspecialchars($_POST['description'], ENT_QUOTES) : '';
         if (empty($name)) {
             fatal_lang_error('sp_error_category_name_empty', false);
         }
         if (empty($namespace)) {
             fatal_lang_error('sp_error_category_namespace_empty', false);
         }
         if (sp_check_duplicate_category($current, $namespace)) {
             fatal_lang_error('sp_error_category_namespace_duplicate', false);
         }
         if (preg_match('~[^A-Za-z0-9_]+~', $namespace) != 0) {
             fatal_lang_error('sp_error_category_namespace_invalid_chars', false);
         }
         if (preg_replace('~[0-9]+~', '', $namespace) === '') {
             fatal_lang_error('sp_error_category_namespace_numeric', false);
         }
         $category_info = array('id' => (int) $_POST['category_id'], 'namespace' => $namespace, 'name' => $name, 'description' => $description, 'permissions' => (int) $_POST['permissions'], 'status' => !empty($_POST['status']) ? 1 : 0);
         $category_info['id'] = sp_update_category($category_info, $this->_is_new);
         redirectexit('action=admin;area=portalcategories');
     }
     // Creating a new category, lets set up some defaults for the form
     if ($this->_is_new) {
         $context['category'] = array('id' => 0, 'category_id' => 'category' . mt_rand(1, 5000), 'name' => $txt['sp_categories_default_name'], 'description' => '', 'permissions' => 3, 'groups_allowed' => array(), 'groups_denied' => array(), 'status' => 1);
     } else {
         $_REQUEST['category_id'] = (int) $_REQUEST['category_id'];
         $context['category'] = sportal_get_categories($_REQUEST['category_id']);
     }
     $context['is_new'] = $this->_is_new;
     $context['category']['permission_profiles'] = sportal_get_profiles(null, 1, 'name');
     $context['category']['groups'] = sp_load_membergroups();
     $context['page_title'] = $this->_is_new ? $txt['sp_admin_categories_add'] : $txt['sp_admin_categories_edit'];
     $context['sub_template'] = 'categories_edit';
 }