<?php
/*
	DevSaver Web Framework
	Copyright (c) 2013-2016 DevSaver. 
	All rights reserved.
		web:  www.devsaver.com
		mail: support@devsaver.com				
*/


class CTestimonialsWidgetTestimonialsWCarousel extends CWidget {

	/**
	* description
	*
	* @var type
	*
	* @access type
	*/
	var $meta = array(
		"type"	=> "2",
		"name"	=> "Testimonials w/ Carousel",
	);

	
	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function Render($widget) {

		$cnt = max(1, $widget["settings"]["set_items"]);

		switch ($widget["settings"]["set_sort"]) {
			default:
				$sort = " rand() ";
			break;

			case "2":
				$sort = " t.item_id DESC ";
			break;
		}

		if ($widget["settings"]["set_cat"]) {
			$tmp = explode("," , $widget["settings"]["set_cat"]);
			foreach ($tmp as $k => $v) {
				if ($v) {
					$_cond[] = "find_in_set($v , item_cat) ";
				}				
			}

			if (is_array($_cond)) {
				$cond = " AND ( " . implode(" OR "  , $_cond ) . ") ";
			}							
		}
		
		

		$items = $this->db->QFetchRowArray(
			"SELECT * FROM {$this->tables['plugin:testimonials_items']} as t , {$this->tables['plugin:testimonials_items_lang']} as l 
			WHERE t.module_id = {$widget[mod_id]} AND l.lang_id={$widget[lang_id]} AND t.item_id=l.item_id 
			{$cond}
			ORDER BY {$sort}
			LIMIT $cnt
			"
		);

		if (!is_array($items)) {
			return "";
		}

		switch ($widget["settings"]["set_style"]) {
			default:  $block = "Small"; break;
			case "1":  $block = "Large"; break;

		}


		global $base;

		$elements = explode("," , $widget["settings"]["set_elements"]);

		foreach ($items as $key => $val) {
			$val["link"] = 	$this->plugins["modules"]->BuildLink(array(
				"type"	=> $val["item_link_type"],
				"link"	=> $val["item_link_link"],
				"module"=> $val["item_link_module"],
				"global"=> $val["item_link_global"]
			));

			$items[$key]["image"] = in_array("image" , $elements) ? $this->tpl->blockReplace($block . ($val["item_image"] ? "Image" : "NoImage"), $val) : "";				
			$items[$key]["_name"] = $this->tpl->blockReplace($val["link"] ? "NameLink" : "NameNoLink" , $val);
			$items[$key]["_company"] = $val["item_company"] ? $this->tpl->blockReplace("Company" , $val) : "";
		}
		
		return $this->tpl->blockReplace(
			$block ."Main" , 
			array(
				"items"	=> $base->html->table(
					$this->tpl , 
					$block , 
					$items
				),

				"class_2" => in_array("image" , $elements) ? "" : "no-avatar",

				"class"	=> $this->tpl->blockreplace($widget["settings"]["set_carousel"] ? "Carousel" :"List"),
			),
			$widget,
			$widget["settings"],
			$widget["mod_settings"]

		);
	}
	
}


?>