<?php

/**

* @version		1.1

* @package		DJ Classifieds

* @subpackage	DJ Classifieds Component

* @copyright	Copyright (C) 2010 Blue Constant Media LTD, All rights reserved.

* @license		http://www.gnu.org/licenses GNU/GPL

* @autor url    http://design-joomla.eu

* @autor email  contact@design-joomla.eu

* @Developer    Lukasz Ciastek - lukasz.ciastek@design-joomla.eu

* 

* 

* DJ Classifieds is free software: you can redistribute it and/or modify

* it under the terms of the GNU General Public License as published by

* the Free Software Foundation, either version 3 of the License, or

* (at your option) any later version.

*

* DJ Classifieds is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

* GNU General Public License for more details.

*

* You should have received a copy of the GNU General Public License

* along with DJ Classifieds. If not, see <http://www.gnu.org/licenses/>.

* 

*/

defined ('_JEXEC') or die('Restricted access');



jimport('joomla.application.component.model');



JTable::addIncludePath(JPATH_COMPONENT.DS.'tables');



class DjclassifiedsModelShow extends JModel

{	

	function getCategories(){

			

			$db= &JFactory::getDBO();

			$query = "SELECT * FROM #__djcf_categories WHERE published = 1 ORDER BY ordering";

			$db->setQuery($query);

			$categories=$db->loadObjectList();

		

		return $categories;

	}

	

	

	function getCountCategories($id){

			$list = implode(",", $id);



			$date_time =& JFactory::getDate();

			$date_all=$date_time->toMySQL();

			$date = explode(' ',$date_all);

			

			$db= &JFactory::getDBO();

			$query = "SELECT COUNT(*) FROM #__djcf_items WHERE date_exp > '".$date[0]."' AND published = 1 AND cat_id IN (".$list.")";



			$db->setQuery($query);

			$allelems=$db->loadResult();

			



			return $allelems;

	}

	

	

	

	function getAllElements(){

			

			global $mainframe;

			$par =& $mainframe->getParams('com_djclassifieds');

		

			$limit	= JRequest::getVar('limit', $par->get('limit_djitem_show'), '', 'int');

			$limitstart	= JRequest::getVar('limitstart', 0, '', 'int');



			$search = '';

			if(JRequest::getVar('search','0','','string')!='0'){

				$search = " AND CONCAT(i.name,i.intro_desc,i.description) LIKE '%".JRequest::getVar('search','0','string')."%' ";

			}

			

			$user_items = '';

			if(JRequest::getVar('uid','0','','int')!='0'){

				$user_items= " AND i.user_id=".JRequest::getVar('uid','0','','int')." ";

			}

			

			$db= &JFactory::getDBO();		

			$order = JRequest::getVar('order');		

			

			$ord="i.id DESC";

			if($order=="date"){

				$ord="i.date_start DESC, i.id DESC ";

			}elseif($order=="display"){

				$ord="i.display DESC ";

			}elseif($order=="description"){

				$ord="i.intro_desc ";

			}elseif($order=="category"){

				$ord="c.name ";

			}elseif($order=="title"){

				$ord="i.name ";

			}

			

			$date_time =& JFactory::getDate();

			$date_all=$date_time->toMySQL();

			$date = explode(' ',$date_all);

			$query = "(SELECT i.*, c.name AS c_name FROM #__djcf_items i, #__djcf_categories c WHERE i.cat_id = c.id AND i.date_exp > '".$date[0]."' AND i.published = 1 ".$search.$user_items." ORDER BY i.special DESC, ".$ord.")";



			$tab = $this->_getList($query, $limitstart, $limit);	

			return $tab;

	}

	

	function getCountAllElements(){

			

			global $mainframe;

			$par =& $mainframe->getParams('com_djclassifieds');



			$search = '';

			if(JRequest::getVar('search','0','string')!='0'){

				$search = " AND name LIKE '%".JRequest::getVar('search','0','string')."%' ";

			}

			$user_items = '';

			if(JRequest::getVar('uid','0','','int')!='0'){

				$user_items= " AND user_id=".JRequest::getVar('uid','0','','int')." ";

			}

			

			$date_time =& JFactory::getDate();

			$date_all=$date_time->toMySQL();

			$date = explode(' ',$date_all);

			

			$db= &JFactory::getDBO();		

			$query = "SELECT COUNT(*) FROM #__djcf_items WHERE date_exp > '".$date[0]."' AND published = 1 ".$search.$user_items." ";



			$db->setQuery($query);

			$res=$db->loadResult();



			return $res;

	}

	

	

		function getElements($id){

			

			global $mainframe;

			$list = implode(",", $id);			

			$par =& $mainframe->getParams('com_djclassifieds');

		

			$limit	= JRequest::getVar('limit', $par->get('limit_djitem_show'), '', 'int');

			$limitstart	= JRequest::getVar('limitstart', 0, '', 'int');



			$search = '';

			if(JRequest::getVar('search','0','string')!='0'){

				$search = " AND i.name LIKE '%".JRequest::getVar('search','0','string')."%' ";

			}

			$db= &JFactory::getDBO();		

			$order = JRequest::getVar('order');		

			

			$ord="i.id DESC";

			if($order=="date"){

				$ord="i.date_start DESC, i.id DESC ";

			}elseif($order=="display"){

				$ord="i.display DESC ";

			}elseif($order=="description"){

				$ord="i.intro_desc ";

			}elseif($order=="category"){

				$ord="c.name ";

			}elseif($order=="title"){

				$ord="i.name ";

			}

			

			$date_time =& JFactory::getDate();

			$date_all=$date_time->toMySQL();

			$date = explode(' ',$date_all);

			$query = "(SELECT i.*, c.name AS c_name FROM #__djcf_items i, #__djcf_categories c WHERE i.cat_id = c.id AND i.cat_id IN (".$list.") AND i.date_exp > '".$date[0]."' AND i.published = 1 ORDER BY i.special DESC, ".$ord.")";

		

			$tab = $this->_getList($query, $limitstart, $limit);		

			return $tab;

	}

	

	function getUserName($uid){						

			$db= &JFactory::getDBO();

			$query = "SELECT username FROM #__users WHERE id=".$uid." LIMIT 1";

			$db->setQuery($query);

			$username=$db->loadResult();

			return $username;

	}	

	

	function makePathway($list){

		global $mainframe;

		$document	=& JFactory::getDocument();

		$cid	= JRequest::getVar('cid', 0, '', 'int');

		$pathway =& $mainframe->getPathway();

		$id = Array();

		$name = Array();

		

		if($cid!=0){

			while($cid!=0){	

				foreach($list as $li){

					if($li->id==$cid){

						$cid=$li->parent_id;

						$id[]=$li->id;

						$name[]=$li->name;

						break;

					}

				}

			}

		

		$document->setTitle($name[0]);		

		for($i=count($id)-1;$i>-1;$i--){

			$pathway->addItem($name[$i], 'index.php?option=com_djclassifieds&view=show&cid='.$id[$i]);	

		}

		return $name[0];

		}

		

		return null;

		

	}

	

}



