<?php 

class PopularArticles extends Widget {

	var $name;
	var $type;
	var $tpl;

	function PopularArticles($name='', $type='', $tpl='') {
		$this->name = $name;
		$this->type = $type;
		$this->tpl = get_widget_tpl($name);
	}

	function getContent() {
		global $db;
		global $tables;


$set = $this->getFrontSettings();
		if (intval($set['NUM_OF_ARTICLES']) > 0) {
			$limit = intval($set['NUM_OF_ARTICLES']);
		} else {
			$limit = 5;
		}

		
		$article_items = $db->GetAll("SELECT SUM( `{$tables['article']['name']}`.HITS ) AS ARTICLE_HITS,
						     `{$tables['article']['name']}`.ID AS ARTICLE_ID,
						     `{$tables['article']['name']}`.ARTICLE AS ARTICLE,
						     `{$tables['article']['name']}`.TITLE AS ARTICLE_TITLE
						     FROM `{$tables['article']['name']}`
						     WHERE `{$tables['article']['name']}`.STATUS=2
						     GROUP BY  ARTICLE_ID
						     ORDER BY ARTICLE_HITS DESC
						LIMIT 0, ". $limit);

		foreach($article_items as $k => $v) {
			$article_items[$k]['SEO_URL'] = seo_url($v['ARTICLE_ID'], $v['ARTICLE_TITLE'], REWRITE_TYPE, REWRITE_EXT, 1);
			$re = '/src=([\'"])?(.*?)\\1/';
			preg_match($re, $article_items[$k]['ARTICLE'], $art_img);
			$article_items[$k]['ARTICLE_IMAGE'] = $art_img[2];
		}

		$this->tpl->assign('popularArticles', $article_items);

	return $this->tpl->fetch('content.tpl');
	}
}


?>