<?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 DjclassifiedsModelShowlist extends JModel

{	



	function getCategory(){

		global $mainframe;

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



		$db= &JFactory::getDBO();

		$query = "SELECT * FROM #__djcf_categories WHERE published = 1 AND id = ".$main_id."  ORDER BY ordering LIMIT 1";

		$db->setQuery($query);

		$cat=$db->loadObject();

		if(count($cat)==0){

			$redirect= 'index.php';				

			$message = JText::_("This category is not available");	

			$mainframe->redirect($redirect,$message);

		}

		return $cat;

	}







	function getCountElements(){

			$main_id= JRequest::getVar('cid','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 cat_id = (".$main_id.")";



			$db->setQuery($query);

			$allelems=$db->loadResult();

			



			return $allelems;

	}

	

	

		function getElements(){

			

			global $mainframe;

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

			$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 name LIKE '%".JRequest::getVar('search','0','string')."%' ";

			}

			$db= &JFactory::getDBO();		

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

			

			$ord="id DESC";

			if($order=="date"){

				$ord="date_start DESC, id DESC ";

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

				$ord="display DESC ";

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

				$ord="intro_desc ";

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

				$ord="name ";

			}

			

			$date_time =& JFactory::getDate();

			$date_all=$date_time->toMySQL();

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

			$query = "(SELECT * FROM #__djcf_items WHERE cat_id = (".$main_id.") AND date_exp > '".$date[0]."' AND published = 1 ORDER BY special DESC, ".$ord.")";

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

			return $tab;

	}

	

	function makePathway(){

		global $mainframe;

			$db= &JFactory::getDBO();

			$query = "SELECT * FROM #__djcf_categories";

			$db->setQuery($query);

			$list=$db->loadObjectList();

					

		$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]);	

		}

		

	}

	

	

	

}



