<?php
/******************************************************************************/
//                                                                            //
//                             InstantCMS v1.10                                //
//                        http://www.instantcms.ru/                           //
//                                                                            //
//                                                                           //
/******************************************************************************/

function mod_latest_blog($module_id){	
     $inCore = cmsCore::getInstance();
	$inDB   = cmsDatabase::getInstance();

	// конфигурация модуля
	$cfg = $inCore->loadModuleConfig($module_id);
	$default_cfg = array (
				  'sort' => 'pubdate',
				  'owner' => 'user',
				  'shownum' => 5,
				  'minrate' => 0,
                  'blog_id' => 0,
				  'showrss' => 1
				);
	$cfg = array_merge($default_cfg, $cfg);

	cmsCore::loadClass('blog');
	$inBlog = cmsBlogs::getInstance();
	$inBlog->owner = $cfg['owner'];

	if($cfg['owner'] == 'club'){
		cmsCore::loadModel('clubs');
		$model = new cms_model_clubs();
		$inDB->addSelect('b.user_id as bloglink');
	} else {
		cmsCore::loadModel('blogs');
		$model = new cms_model_blogs();
	}

	// получаем аватары владельцев
	$inDB->addSelect('up.imageurl');
	$inDB->addJoin('LEFT JOIN cms_user_profiles up ON up.user_id = u.id');

	$inBlog->whereOnlyPublic();

	if($cfg['minrate']){
		$inBlog->ratingGreaterThan($cfg['minrate']);
	}

	if($cfg['blog_id']){
		$inBlog->whereBlogIs($cfg['blog_id']);
	}

    $inDB->orderBy('p.'.$cfg['sort'], 'DESC');

    $inDB->limit($cfg['shownum']);

	$cons = $inBlog->getPosts(false, $model);
	if(!$cons){ return false; }
$posts = array();
            foreach($cons as $con){
 $con['text'] = strip_tags($con['content']);
                    
                    $con['text'] =  preg_replace('/\[IMG\](.*?)\[\/IMG\]/i', '', $con['text']);
                    $con['text'] =  preg_replace('/\[\/b\]/i', '', $con['text']);
                    $con['text'] =  preg_replace('/\[b\]/i', ' ', $con['text']);
                    $con['text'] =  preg_replace('/\[\/i\]/i', '', $con['text']);
                    $con['text'] =  preg_replace('/\[i\]/i', ' ', $con['text']);
                    $con['text'] =  preg_replace('/\[\/u\]/i', '', $con['text']);
                    $con['text'] =  preg_replace('/\[u\]/i', ' ', $con['text']);
                    $con['text'] =  preg_replace('/\[\/s\]/i', '', $con['text']);
                    $con['text'] =  preg_replace('/\[s\]/i', ' ', $con['text']);

                    $con['text'] =  preg_replace('/\[\/align\]/i', ' ', $con['text']);
                    $con['text'] =  preg_replace('/\[align=left\]/i', '', $con['text']);
                    $con['text'] =  preg_replace('/\[align=center\]/i', '', $con['text']);
                    $con['text'] =  preg_replace('/\[align=right\]/i', '', $con['text']);

                    $con['text'] =  preg_replace('/\[\/quote\]/i', ' ', $con['text']);
                    $con['text'] =  preg_replace('/\[quote\]/i', '', $con['text']);

                    $con['text'] =  preg_replace('/\[url=(.*?)\](.*?)\[\/url\]/i', '', $con['text']);

                    $con['text'] =  preg_replace('/\[\/h1\]/i', '', $con['text']);
                    $con['text'] =  preg_replace('/\[h1\]/i', ' ', $con['text']);
                    $con['text'] =  preg_replace('/\[email\](.*?)\[\/email\]/i', '', $con['text']);
                    $con['text'] =  preg_replace('/\[code=php\](.*?)\[\/code\]/i', '', $con['text']);



                    $con['text'] =  preg_replace('/\[\/h2\]/i', '', $con['text']);
                    $con['text'] =  preg_replace('/\[h2\]/i', ' ', $con['text']);
                    $con['text'] =  preg_replace('/\[\/h3\]/i', '', $con['text']);
                    $con['text'] =  preg_replace('/\[h3\]/i', ' ', $con['text']);
                    $con['text'] =  preg_replace('/\[\/hide\]/i', '', $con['text']);
                    $con['text'] =  preg_replace('/\[hide\]/i', ' ', $con['text']);
                    $con['text'] =  preg_replace('/\[\/spoiler\]/i', '', $con['text']);
                    $con['text'] =  preg_replace('/\[spoiler=Спойлер\]/i', ' ', $con['text']);
                    $con['text'] =  preg_replace('/\[video\](.*?)\[\/video\]/i', '', $con['text']);
                    $con['text'] =  preg_replace('/\[audio\](.*?)\[\/audio\]/i', '', $con['text']);
                    $con['text'] =  preg_replace('/\[img\](.*?)\[\/img\]/i', '', $con['text']);


$posts[]=$con;

}
		$smarty = $inCore->initSmarty('modules', 'mod_latest_blog.tpl');			
		$smarty->assign('posts', $posts);
		$smarty->assign('cfg', $cfg);
		$smarty->display('mod_latest_blog.tpl');	
		
		return true;
}
?>