/**
 * Article Block, show the list of articles in the system
 *
 * @param mixed[] $parameters
 *		'category' => list of categories to choose article from
 *		'limit' => number of articles to show
 *		'type' => 0 latest 1 random
 *		'length' => length for the body text preview
 *		'avatar' => whether to show the author avatar or not
 *
 * @param int $id - not used in this block
 * @param boolean $return_parameters if true returns the configuration options for the block
 */
function sp_articles($parameters, $id, $return_parameters = false)
{
    global $scripturl, $txt, $color_profile;
    $block_parameters = array('category' => array(0 => $txt['sp_all']), 'limit' => 'int', 'type' => 'select', 'length' => 'int', 'avatar' => 'check');
    if ($return_parameters) {
        require_once SUBSDIR . '/PortalAdmin.subs.php';
        $categories = sp_load_categories();
        foreach ($categories as $category) {
            $block_parameters['category'][$category['id']] = $category['name'];
        }
        return $block_parameters;
    }
    // Needed utilities
    require_once SUBSDIR . '/Post.subs.php';
    require_once SUBSDIR . '/PortalArticle.subs.php';
    // Set up for the query
    $category = empty($parameters['category']) ? 0 : (int) $parameters['category'];
    $limit = empty($parameters['limit']) ? 5 : (int) $parameters['limit'];
    $type = empty($parameters['type']) ? 0 : 1;
    $length = isset($parameters['length']) ? (int) $parameters['length'] : 250;
    $avatar = empty($parameters['avatar']) ? 0 : (int) $parameters['avatar'];
    $articles = sportal_get_articles(null, true, true, $type ? 'RAND()' : 'spa.date DESC', $category, $limit);
    $colorids = array();
    foreach ($articles as $article) {
        if (!empty($article['author']['id'])) {
            $colorids[$article['author']['id']] = $article['author']['id'];
        }
    }
    // No articles in the system or none they can see
    if (empty($articles)) {
        echo '
								', $txt['error_sp_no_articles_found'];
        return;
    }
    // Doing the color thing
    if (!empty($colorids) && sp_loadColors($colorids) !== false) {
        foreach ($articles as $k => $p) {
            if (!empty($color_profile[$p['author']['id']]['link'])) {
                $articles[$k]['author']['link'] = $color_profile[$p['author']['id']]['link'];
            }
        }
    }
    // Not showing avatars, just use a compact link view
    if (empty($avatar)) {
        echo '
			<ul class="sp_list">';
        foreach ($articles as $article) {
            echo '
				<li>', sp_embed_image('topic'), ' ', $article['link'], '</li>';
        }
        echo '
			</ul>';
    } else {
        echo '
							<table class="sp_fullwidth">';
        foreach ($articles as $article) {
            // Using the cutoff tag?
            $limited = false;
            if (($cutoff = Util::strpos($article['body'], '[cutoff]')) !== false) {
                $article['body'] = Util::substr($article['body'], 0, $cutoff);
                preparsecode($article['body']);
                $limited = true;
            }
            // Good time to do this is ... now
            censorText($article['subject']);
            censorText($article['body']);
            $article['body'] = sportal_parse_content($article['body'], $article['type'], 'return');
            // Shorten the text, link the ellipsis, etc as needed
            if ($limited || !empty($length)) {
                $ellip = '<a href="' . $scripturl . '?article=' . $article['article_id'] . '">&hellip;</a>';
                $article['body'] = $limited ? $article['body'] . $ellip : Util::shorten_html($article['body'], $length, $ellip, false);
            }
            echo '
								<tr class="sp_articles_row">
									<td class="sp_articles centertext">';
            // If we have an avatar to show, show it
            if ($avatar && !empty($article['author']['avatar']['href'])) {
                echo '
										<a href="', $scripturl, '?action=profile;u=', $article['author']['id'], '">
											<img src="', $article['author']['avatar']['href'], '" alt="', $article['author']['name'], '" style="max-width:40px" />
										</a>';
            }
            echo '
									</td>
									<td>
										<span class="sp_articles_title">', $article['author']['link'], '</span><br />
										', $article['link'], '
									</td>
									<td>', $article['body'], '</td>
								</tr>
								<tr>
									<td colspan="3" class="sp_articles_row"></td>
								</tr>';
        }
        echo '
							</table>';
    }
}
 /**
  * Callback for createList()
  * Returns an array of categories
  *
  * @param int $start
  * @param int $items_per_page
  * @param string $sort
  */
 public function list_spLoadCategories($start, $items_per_page, $sort)
 {
     return sp_load_categories($start, $items_per_page, $sort);
 }