Current File : /home/pacjaorg/public_html/kmm/administrator/components/com_djclassifieds/controllers/items.php
<?php
/**
 * @package DJ-Classifieds
 * @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
 * @license http://www.gnu.org/licenses GNU/GPL
 * @author url: http://dj-extensions.com
 * @author email: contact@dj-extensions.com
 */

defined('_JEXEC') or die;

class DJClassifiedsControllerItems extends JControllerAdmin
{
	public function getModel($name = 'Item', $prefix = 'DJClassifiedsModel', $config = array('ignore_request' => true))
	{
		$model = parent::getModel($name, $prefix, $config);

		return $model;
	}

	function recreateThumbnails()
	{
		$app = JFactory::getApplication();
		$cid = $app->input->get('cid', array());

		if (!$cid) {
			$this->recreateThumbnailsAll();
			return true;
		}

		$item_id = $cid[0];
		unset($cid[0]);		

		$this->_recreateItemThumbnails($item_id);
	    
		if (!$cid) {
			$this->setRedirect( 'index.php?option=com_djclassifieds&view=items', JText::_('COM_DJCLASSIFIEDS_THUMBNAILS_RECREATED') );	
		} else {	        
			$cids = '';
			foreach ($cid as $value) {
				$cids .= '&cid[]='.$value; 
			}
			header("refresh: 0; url=".JURI::base().'index.php?option=com_djclassifieds&task=items.recreateThumbnails'.$cids);				        
	    }
	}

	function recreateThumbnailsAll()
	{
		$app = JFactory::getApplication();
		$db = JFactory::getDBO();
		$prev_item_id = $app->input->get('item_id', '0');

		$query = "SELECT item_id FROM #__djcf_images WHERE type='item' AND item_id > ".$prev_item_id." ORDER BY item_id ASC LIMIT 1";
		$db->setQuery($query);
		$item_id = $db->loadResult();

		if(!$item_id){
			$this->setRedirect('index.php?option=com_djclassifieds&view=items', JText::_('COM_DJCLASSIFIEDS_THUMBNAILS_RECREATED'));
		}else{
			$this->_recreateItemThumbnails($item_id);
			header("refresh: 0; url=".JURI::base().'index.php?option=com_djclassifieds&task=items.recreateThumbnailsAll&item_id='.$item_id);	
		}
	}

	function _recreateItemThumbnails($item_id)
	{
		JToolBarHelper::title(JText::_('COM_DJCLASSIFIEDS_RECREATING_THUMBNAILS'), 'generic.png');
		echo '<h3>'.JTEXT::_('COM_DJCLASSIFIEDS_RESIZING_ITEM').' '.$item_id.'... '.JTEXT::_('COM_DJCLASSIFIEDS_PLEASE_WAIT').'</h3>';

		$par = JComponentHelper::getParams('com_djclassifieds');
		$db = JFactory::getDBO();

		$query = "SELECT * FROM #__djcf_images WHERE item_id = ".$item_id." AND type='item'";
		$db->setQuery($query);
		$images = $db->loadObjectList();

		if($images){
			$nws = $par->get('smallth_width','56');
			$nhs = $par->get('smallth_height','32');
			$nwm = $par->get('middleth_width','150');
			$nhm = $par->get('middleth_height','110');
			$nwb = $par->get('bigth_width','600');
			$nhb = $par->get('bigth_height','0');

			foreach($images as $image){
				$path = JPATH_SITE.$image->path.$image->name;	
				if (JFile::exists($path.'.'.$image->ext)){ 
					if (JFile::exists($path.'_thb.'.$image->ext)){
						JFile::delete($path.'_thb.'.$image->ext);
					}
					if (JFile::exists($path.'_thm.'.$image->ext)){
						JFile::delete($path.'_thm.'.$image->ext);
					}
					if (JFile::exists($path.'_ths.'.$image->ext)){
						JFile::delete($path.'_ths.'.$image->ext);
					}
					
					DJClassifiedsImage::makeThumb($path.'.'.$image->ext,$path.'_ths.'.$image->ext, $nws, $nhs);
					echo '<img src="'.JUri::root(true).$image->path.$image->name.'_ths.'.$image->ext.'" style="margin-right:3px">';
					DJClassifiedsImage::makeThumb($path.'.'.$image->ext,$path.'_thm.'.$image->ext, $nwm, $nhm);
					DJClassifiedsImage::makeThumb($path.'.'.$image->ext,$path.'_thb.'.$image->ext, $nwb, $nhb);
				}
			}
		}
	}
	
	function resmushitThumbnails()
	{
		$app = JFactory::getApplication();
		$db = JFactory::getDBO();

		JToolBarHelper::title(JText::_('COM_DJCLASSIFIEDS_OPTIMIZING_THUMBNAILS'), 'generic.png');
		 
		$cid = $app->input->getVar('cid', array());
	
		if(!$cid){
			$query = "SELECT item_id FROM #__djcf_images where type='item' AND optimized=0 GROUP BY item_id";
			$db->setQuery($query);
			$cid = $db->loadColumn();
		}
	
		if($cid){
			$item_id = $cid[0];
			unset($cid[0]);

			echo '<h3>'.JTEXT::_('COM_DJCLASSIFIEDS_OPTIMALIZING_ITEM').' '.$item_id.'... '.JTEXT::_('COM_DJCLASSIFIEDS_PLEASE_WAIT').'</h3>';
			
			$query = "SELECT * FROM #__djcf_images WHERE item_id=".$item_id." AND type='item' AND optimized=0";
			$db->setQuery($query);
			$images = $db->loadObjectList();

			foreach($images as $image){
				$path = JPATH_SITE.$image->path.$image->name;
				if(JFile::exists($path.'.'.$image->ext)){
					if (JFile::exists($path.'_thb.'.$image->ext)){
						DJClassifiedsImage::resmushitThumbnails($path.'_thb.'.$image->ext);
					}
					if (JFile::exists($path.'_th.'.$image->ext)){
						DJClassifiedsImage::resmushitThumbnails($path.'_th.'.$image->ext);
					}
					if (JFile::exists($path.'_thm.'.$image->ext)){
						DJClassifiedsImage::resmushitThumbnails($path.'_thm.'.$image->ext);
					}
					if (JFile::exists($path.'_ths.'.$image->ext)){
						DJClassifiedsImage::resmushitThumbnails($path.'_ths.'.$image->ext);
					}	
					/*
					if (JFile::exists($path.'.'.$image->ext)){
						DJClassifiedsImage::resmushitThumbnails($path.'.'.$image->ext);
					}
					*/
				}
				$query = "UPDATE #__djcf_images SET optimized=1 WHERE id=".$image->id;
				$db->setQuery($query);
				$db->execute();
			}
		}
		
		if(!$cid){
			$this->setRedirect('index.php?option=com_djclassifieds&view=items', JText::_('COM_DJCLASSIFIEDS_THUMBNAILS_OPTIMIZED'));
		}else{
			$cids = null;
			foreach ($cid as $value) {
				$cids .= '&cid[]='.$value;
			}
			header("refresh: 0; url=".JURI::base().'index.php?option=com_djclassifieds&task=items.resmushitThumbnails'.$cids);
		}
	}

	function generateCoordinatesAll()
	{	
		JToolBarHelper::title(JText::_('COM_DJCLASSIFIEDS_GENERATING_COORDINATES'), 'generic.png');

		$app = JFactory::getApplication();
		$db  = JFactory::getDBO();

		$id_checked = $app->input->getInt('idc', 0);
			
	    $query = "SELECT * FROM #__djcf_items WHERE (region_id>0 OR address!='' OR post_code!='') "
	    		."AND latitude=0.000000000000000 AND longitude=0.000000000000000 "
	    		."AND id > ".$id_checked." ORDER BY id LIMIT 1";
		$db->setQuery($query);
		$item = $db->loadObject();	
		
		if($item){
			$id_checked = $item->id;
			
			$this->_generateItemCoordinates($item);

			header("refresh: 0; url=".JURI::base().'index.php?option=com_djclassifieds&task=items.generateCoordinatesAll&idc='.$id_checked);			
		}else{
			$redirect = 'index.php?option=com_djclassifieds&view=items';
			$app->enqueueMessage(JText::_('COM_DJCLASSIFIEDS_COORDINATES_GENERATED'), 'success');
	    	$app->redirect($redirect);
		}
	}

	function generateCoordinates()
	{	
		JToolBarHelper::title(JText::_('COM_DJCLASSIFIEDS_GENERATING_COORDINATES'), 'generic.png');

		$app = JFactory::getApplication();
		$db = JFactory::getDBO();

		$app = JFactory::getApplication();
		$cid = $app->input->get('cid', array());

		$item_id = $cid[0];
		unset($cid[0]);	

		$query = "SELECT * FROM #__djcf_items WHERE id=".$item_id;
		$db->setQuery($query);
		$item = $db->loadObject();	

		$this->_generateItemCoordinates($item);
	    
		if(!$cid){
			$this->setRedirect( 'index.php?option=com_djclassifieds&view=items', JText::_('COM_DJCLASSIFIEDS_COORDINATES_REGENERATED') );	
		}else{	        
			$cids = '';
			foreach($cid as $value){
				$cids .= '&cid[]='.$value; 
			}
			header("refresh: 0; url=".JURI::base().'index.php?option=com_djclassifieds&task=items.generateCoordinates'.$cids);				        
	    }
	}
	
	function _generateItemCoordinates($item)
	{
		echo '<h3>'.JTEXT::_('COM_DJCLASSIFIEDS_GENERATING_COORDINATES').' '.$item->id.'... '.JTEXT::_('COM_DJCLASSIFIEDS_PLEASE_WAIT').'</h3>';			

		$db = JFactory::getDBO();

		if($item->post_code){
			$loc_coord = DJClassifiedsGeocode::getLocationPostCode($item->post_code, $item->address, $item->region_id);
		}else{
			$loc_coord = DJClassifiedsGeocode::getLocation($item->address, $item->region_id);
		}

		$loc_coord = isset($loc_coord[0]) && is_array($loc_coord[0]) ? $loc_coord[0] : $loc_coord;
		if(!empty($loc_coord)){
			$query = "UPDATE #__djcf_items SET latitude='".$loc_coord['lat']."',longitude='".$loc_coord['lng']."'  WHERE id=".$item->id;
			$db->setQuery($query);
			$db->execute();
		}
	}

	function delete()
	{
		$app = JFactory::getApplication();
	    $db = JFactory::getDBO();
	    $user = JFactory::getUser();
	    $par = JComponentHelper::getParams('com_djclassifieds');
		$cid = $app->input->getVar('cid', array());
	    
	    if (!$user->authorise('core.delete', 'com_djclassifieds')) {
	    	$this->setMessage(JText::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED'), 'error');
	    	$this->setRedirect( 'index.php?option=com_djclassifieds&view=items' );
	    	return false;
	    }
	    
	    if (count($cid))
	    {
	        $cids = implode(',', $cid);
	        $query = "SELECT * FROM #__djcf_items WHERE id IN ( ".$cids." )";
			$db->setQuery($query);
			$items = $db->loadObjectList();
			
			foreach($items as $item){
				$app->triggerEvent('onBeforeDJClassifiedsDeleteAdvert', array($item));
			}
			
			$query = "SELECT * FROM #__djcf_images WHERE item_id IN ( ".$cids." ) AND type='item' ";
			$db->setQuery($query);
			$items_images = $db->loadObjectList('id');
			
			if($items_images){
				foreach($items_images as $item_img){
					$path_to_delete = JPATH_ROOT.$item_img->path.$item_img->name;
					if (JFile::exists($path_to_delete.'.'.$item_img->ext)){
						JFile::delete($path_to_delete.'.'.$item_img->ext);
					}
					
					if($par->get('leave_small_th','0')==0){
						if (JFile::exists($path_to_delete.'_ths.'.$item_img->ext)){
							JFile::delete($path_to_delete.'_ths.'.$item_img->ext);
						}
					}
					if (JFile::exists($path_to_delete.'_thm.'.$item_img->ext)){
						JFile::delete($path_to_delete.'_thm.'.$item_img->ext);
					}
					if (JFile::exists($path_to_delete.'_thb.'.$item_img->ext)){
						JFile::delete($path_to_delete.'_thb.'.$item_img->ext);
					}
				}
			}

			$query = "SELECT * FROM #__djcf_images WHERE item_id IN ( ".$cids." ) AND type LIKE 'item_field_%'";
			$db->setQuery($query);
			$field_images = $db->loadObjectList('id');
			
			foreach($field_images as $f_img){
				$path_to_delete = JPATH_ROOT.$f_img->path.$f_img->name;
				if (JFile::exists($path_to_delete.'.'.$f_img->ext)){
					JFile::delete($path_to_delete.'.'.$f_img->ext);
				}
				if (JFile::exists($path_to_delete.'_th.'.$f_img->ext)){
					JFile::delete($path_to_delete.'_th.'.$f_img->ext);
				}
			}
			
	        $cids = implode(',', $cid);
	        $query = "DELETE FROM #__djcf_items WHERE id IN ( ".$cids." )";
	        $db->setQuery($query);
	        if (!$db->execute())
	        {
				echo $db->getErrorMsg();
        		exit ();	
	        }
			
			$query = "DELETE FROM #__djcf_fields_values WHERE item_id IN ( ".$cids." )";
	        $db->setQuery($query);
	        $db->execute();
			
			$query = "DELETE FROM #__djcf_payments WHERE item_id IN ( ".$cids." )";
	        $db->setQuery($query);
	        $db->execute();
	        
	        $query = "DELETE FROM #__djcf_images WHERE item_id IN ( ".$cids." ) AND type='item' ";
	        $db->setQuery($query);
			$db->execute();
			
			$query = "DELETE FROM #__djcf_images WHERE item_id IN ( ".$cids." ) AND type LIKE 'item_field_%'";
	        $db->setQuery($query);
	        $db->execute();
	        
	        foreach($items as $item){
	        	$app->triggerEvent('onAfterDJClassifiedsDeleteAdvert', array($item));
	        }
	    }

		$app->enqueueMessage(JText::_('COM_DJCLASSIFIEDS_ITEMS_DELETED'), 'success');
	    $app->redirect('index.php?option=com_djclassifieds&view=items');
	}
	
	public function publish()
	{
		$app = JFactory::getApplication();		
		$par = JComponentHelper::getParams( 'com_djclassifieds' );
		$data = array('publish' => 1, 'unpublish' => 0, 'archive' => 2, 'trash' => -2, 'report' => -3);		
		$task = $this->getTask();
		$new_status = isset($data[$task]) ? $data[$task] : 0;
		$cid = $app->input->getVar('cid', array());						
		
		if($par->get('notify_status_change','0')=='2'){
			foreach($cid as $id){					
				DJClassifiedsNotify::notifyUserPublication($id,$new_status);					
			}
		}
				
		$publish = parent::publish(); 		
	
		return $publish;
	}
	
	function migratePromotions($exit = true)
	{
		$app = JFactory::getApplication();
		$db = JFactory::getDBO();
		$par = JComponentHelper::getParams('com_djclassifieds');
		$exp_days = $par->get('exp_days','7');
	
		$query = "SELECT * FROM #__djcf_promotions ORDER BY id";
		$db->setQuery($query);
		$proms = $db->loadObjectList('name');
	
		$ins_arr = array();
		foreach($proms as $prom){
			$ins_arr[] = "(".$prom->id.",'".$exp_days."','".$prom->price."',".$prom->points.")";
		}
		if($ins_arr){
			$query = "INSERT INTO #__djcf_promotions_prices(`prom_id`,`days`,`price`,`points`) VALUES "
					.implode(', ', $ins_arr);
			$db->setQuery($query);
			$db->execute();
		}
	
		$query = "SELECT * FROM #__djcf_items WHERE promotions != '' ORDER BY id";
		$db->setQuery($query);
		$items = $db->loadObjectList();
	
		$ins_arr = array();
		foreach($items as $item){
			$item_proms = explode(',', $item->promotions);
			foreach($item_proms as $item_p){
				if(isset($proms[$item_p])){
					$ins_arr[] = "(".$item->id.",".$proms[$item_p]->id.",'".$item->date_start."','".$item->date_exp."','".$exp_days."')";
				}
			}
		}
		if($ins_arr){
			$query = "INSERT INTO #__djcf_items_promotions(`item_id`,`prom_id`,`date_start`,`date_exp`,`days`) VALUES "
					.implode(', ', $ins_arr);
			$db->setQuery($query);
			$db->execute();
		}
	
		if($exit){
			echo JText::_('COM_DJCLASSIFIEDS_PROMOTIONS_MIGRATED_SUCCESFULLY');
			$app->close();
		}
	}
	
	public function exportItems($cron = false)
	{
		$app = JFactory::getApplication();
		$db = JFactory::getDBO();
		$cid = $app->input->get('cid', array(), 'array');

		require_once(JPATH_ROOT.'/administrator/components/com_djclassifieds/lib/djtheme.php');
		DJClassifiedsTheme::loadAdminLanguage();
					
		JLoader::import('items', JPATH_ROOT.'/administrator/components/com_djclassifieds/models/');
		$model = JModelList::getInstance('Items', 'DjclassifiedsModel');
		//$model = $app->bootComponent('com_djclassifieds')->getMVCFactory()->createModel('Items', 'DjClassifiedsModel', ['ignore_request' => true]);
		
		if (count($cid) > 0) {
			$model->setState('filter.ids', implode(',',$cid));
		}
		
		$items = $model->getItems();
		if($items){
			$query = "SELECT * FROM #__djcf_types";
			$db->setQuery($query);
			$types = $db->loadObjectList('id');
			
			$query = "SELECT * FROM #__djcf_fields";
			$db->setQuery($query);
			$fields = $db->loadObjectList();
			
			$id_list = '';
			foreach($items as $item){
				$id_list .= ($id_list) ? ','.$item->id : $item->id;
			}
			
			$query = "SELECT * FROM #__djcf_fields_values WHERE item_id IN (".$id_list.")";
			$db->setQuery($query);
			$fields_values = $db->loadObjectList();

			$items_img = DJClassifiedsImage::getAdsImages($id_list, true);
			
			$itemsXML = new SimpleXMLElement("<items></items>");
			foreach($items as $item){
				$itemXML = $itemsXML->addChild('item');
				$itemXML->addChild('id',$item->id);
				$itemXML->addChild('start_date',$item->date_start);
				$itemXML->addChild('end_date',$item->date_exp);
				$itemXML->addChild('title',$item->name);
				$itemXML->addChild('short_description',$item->intro_desc);
				$itemXML->addChild('description',htmlspecialchars($item->description));
				$itemXML->addChild('price',$item->price);
				$itemXML->addChild('currency',$item->currency);
				$itemXML->addChild('video',$item->video);
				$itemXML->addChild('website',$item->website);
				$itemXML->addChild('hits',$item->display);
				
				if($item->type_id){
					$type = $itemXML->addChild('type',$types[$item->type_id]->name);
					$type->addAttribute('id',$item->type_id);
				}else{
					$itemXML->addChild('type');
				}
				
				$location = $itemXML->addChild('location');
				$location->addChild('address',$item->address);
				$location->addChild('postal_code',$item->post_code);
				
				if($item->region_id){
					$reg_path = DJClassifiedsRegion::getParentPath($item->region_id);
					$reg_path = array_reverse($reg_path);
				
					for($ri=0;$ri<5;$ri++){
						$rii = $ri+1;
						if(isset($reg_path[$ri])){
							$region = $location->addChild('region'.$rii, $reg_path[$ri]->name);
							$region->addAttribute('id',$reg_path[$ri]->id);
							$region->addAttribute('country',$reg_path[$ri]->country);
							$region->addAttribute('city',$reg_path[$ri]->city);
						}else{
							$location->addChild('region'.$rii);
						}
					}
				
				}else{
					for($ri=1;$ri<6;$ri++){
						$location->addChild('region'.$ri);
					}
				}												
				$itemXML->addChild('contact',$item->contact);
				$contact_fields = $itemXML->addChild('contact_specification');
				foreach ($fields as $field){
					if($field->source==1){					
						foreach($fields_values as $fv){
							if($fv->item_id==$item->id && $fv->field_id==$field->id){
								$f_value = $fv->value;
								if($field->type=='date'){
									$f_value = $fv->value_date;
								}
								if(substr($f_value, 0,1)==';'){
									$f_value = substr($f_value, 1);
								}
								if(substr($f_value, -1,1)==';'){
									$f_value = substr($f_value, 0,-1);
								}
								$f_value = str_ireplace(';', ', ', $f_value);
								$contact_fields->addChild($field->name,$f_value);
								break;
							}
						}
					}
				}
				
				if($item->cat_id){
					$cat_path = DJClassifiedsCategory::getParentPath(0,$item->cat_id);
					$cat_path = array_reverse($cat_path);
				
					for($ci=0;$ci<5;$ci++){
						$cii= $ci+1;
						if(isset($cat_path[$ci])){
							$category = $itemXML->addChild('category'.$cii, $cat_path[$ci]->name);
							$category->addAttribute('id',$cat_path[$ci]->id);
						}else{
							$itemXML->addChild('category'.$cii);
						}
					}
				}else{
					for($ci=1;$ci<6;$ci++){
						$itemXML->addChild('category'.$ci);
					}
				}
				
				$cat_fields = $itemXML->addChild('specification');
				foreach ($fields as $field){
					if($field->source==0){
						foreach($fields_values as $fv){
							if($fv->item_id==$item->id && $fv->field_id==$field->id){
								$f_value = $fv->value;
								if($field->type=='date'){
									$f_value = $fv->value_date;
								}
								if(substr($f_value, 0,1)==';'){
									$f_value = substr($f_value, 1);
								}
								if(substr($f_value, -1,1)==';'){
									$f_value = substr($f_value, 0,-1);
								}
								$f_value = str_ireplace(';', ', ', $f_value);
								$cat_fields->addChild($field->name,$f_value);
								break;
							}
						}
					}
				}
				
				$profileXML = $itemXML->addChild('profile');
				if($item->user_id){
					$profileXML->addChild('user_id',$item->user_id);
					$profileXML->addChild('name',$item->user_name);
					$profileXML->addChild('email',$item->u_email);
				}else{
					$profileXML->addChild('user_id','0');
					$profileXML->addChild('name','');
					$profileXML->addChild('email',$item->email);
				}
				
				$imagesXML = $itemXML->addChild('images');
				if(!empty($items_img[$item->id])){
					foreach($items_img[$item->id] as $img){				
						$imagesXML->addChild('image',JURI::root().ltrim($img->thumb_b,'/'));
					}
				}
			}

			$xml_file_name = 'Export_items_'.JFactory::getDate()->format('Y-m-d_H_i_s').'.xml';
			$xml_file = JPATH_ROOT.'/administrator/components/com_djclassifieds/export/'.$xml_file_name;
			$itemsXML->saveXML($xml_file);
			
			$xml_link = '<a target="_blank" href="'.JUri::root().'administrator/components/com_djclassifieds/export/'.$xml_file_name.'">'.$xml_file_name.'</a>';
			if($cron){
				return JText::_('COM_DJCLASSIFIEDS_XML_GENERATED').': '.$xml_link;
			}else{
				$this->setRedirect('index.php?option=com_djclassifieds&view=items', JText::_('COM_DJCLASSIFIEDS_XML_GENERATED').': '.$xml_link);
			}
		}else{
			if($cron){
				return JText::_('COM_DJCLASSIFIEDS_NO_ADVERTS_FOR_EXPORT');
			}else{
				$this->setRedirect('index.php?option=com_djclassifieds&view=items', JText::_('COM_DJCLASSIFIEDS_NO_ADVERTS_FOR_EXPORT'));
			}
		}
	}

	function archiveExpired($days = '0')
	{
		if(!is_numeric($days)){
			return false;
		}

		$app = JFactory::getApplication();
		$db = JFactory::getDBO();
		$now = JFactory::getDate()->toSQL();

		$query = "SELECT * FROM #__djcf_items WHERE published=1 AND DATE_ADD(date_exp, INTERVAL ".$days." DAY) < ".$db->quote($now);
		$db->setQuery($query);
		$items = $db->loadObjectList();
		
		if($items){
			$ids = implode(',', array_column($items, 'id'));
			
			foreach($items as $item){
				$app->triggerEvent('onBeforeDJClassifiedsItemArchive', array($item));
			}
			
			$query = "UPDATE #__djcf_items SET published=2 WHERE id IN (".$ids.")";
			$db->setQuery($query);
			$db->execute();

			return JText::sprintf('COM_DJCLASSIFIEDS_EXPIRED_ADS_ARCHIVED', count($items));
		}
	}
	
	function deleteExpired($days = '0')
	{
		if(!is_numeric($days)){
			return false;
		}

		$app  = JFactory::getApplication();
		$db   = JFactory::getDBO();
		$par = JComponentHelper::getParams( 'com_djclassifieds' );
		
		$now = JFactory::getDate()->toSQL();

		$query = "SELECT * FROM #__djcf_items WHERE DATE_ADD(date_exp, INTERVAL ".$days." DAY) < ".$db->quote($now);
		$db->setQuery($query);
		$items = $db->loadObjectList();
		
		if($items){
			$ids = implode(',', array_column($items, 'id'));
			
			foreach($items as $item){
				$app->triggerEvent('onBeforeDJClassifiedsDeleteAdvert', array($item));
			}
			
			$query = "SELECT * FROM #__djcf_images WHERE item_id IN (".$ids.") AND type='item'";
			$db->setQuery($query);
			$items_images = $db->loadObjectList('id');
			
			if($items_images){
				foreach($items_images as $item_img){
					$path_to_delete = JPATH_ROOT.$item_img->path.$item_img->name;
					if (JFile::exists($path_to_delete.'.'.$item_img->ext)){
						JFile::delete($path_to_delete.'.'.$item_img->ext);
					}
					
					if($par->get('leave_small_th','0')==0){
						if (JFile::exists($path_to_delete.'_ths.'.$item_img->ext)){
							JFile::delete($path_to_delete.'_ths.'.$item_img->ext);
						}
					}
					if (JFile::exists($path_to_delete.'_thm.'.$item_img->ext)){
						JFile::delete($path_to_delete.'_thm.'.$item_img->ext);
					}
					if (JFile::exists($path_to_delete.'_thb.'.$item_img->ext)){
						JFile::delete($path_to_delete.'_thb.'.$item_img->ext);
					}
				}
			}

			$query = "DELETE FROM #__djcf_items WHERE id IN (".$ids.")";
			$db->setQuery($query);
			if (!$db->execute())
			{
				echo $db->getErrorMsg();
				exit();	
			}
			
			$query = "DELETE FROM #__djcf_fields_values WHERE item_id IN (".$ids.")";
			$db->setQuery($query);
			$db->execute();
			
			$query = "DELETE FROM #__djcf_payments WHERE item_id IN (".$ids.")";
			$db->setQuery($query);
			$db->execute();
			
			$query = "DELETE FROM #__djcf_images WHERE item_id IN (".$ids.") AND type='item'";
			$db->setQuery($query);
			$db->execute();
			
			foreach($items as $item){
				$app->triggerEvent('onAfterDJClassifiedsDeleteAdvert', array($item));
			}

			return JText::sprintf('COM_DJCLASSIFIEDS_EXPIRED_ADS_DELETED', count($items));
		}
	}

	public function batch()
	{
		$app  = JFactory::getApplication();
	    $db   = JFactory::getDBO();
		$user = JFactory::getUser();
		$redirect = 'index.php?option=com_djclassifieds&view=items';

		if (!$user->authorise('core.admin', 'com_djclassifieds')) {
	    	$this->setMessage(JText::_('JGLOBAL_BATCH_CANNOT_EDIT'), 'error');
	    	$this->setRedirect($redirect);
	    	return false;
	    }
		
		$id_arr = $app->input->get('cid', array());
		$cat_id = $app->input->getInt('batch_cid', '0');
		$user_id = $app->input->get('batch_uid', '');
		$exp_date = $app->input->getStr('batch_exp', '');
		$exp_date = $exp_date != '0000-00-00 00:00:00' ? $exp_date : '';
		
		$items_updated = '0';

	    if ($id_arr && ($cat_id || ($user_id != '' && is_numeric($user_id)) || $exp_date)){
	        $ids = implode(',', $id_arr);
			
			if($cat_id){
				$query = "UPDATE #__djcf_items SET cat_id=".$cat_id." WHERE id IN (".$ids.")";
				$db->setQuery($query);
				$db->execute();
			}

			if($user_id != '' && is_numeric($user_id)){
				$query = "UPDATE #__djcf_items SET user_id=".intval($user_id)." WHERE id IN (".$ids.")";
				$db->setQuery($query);
				$db->execute();
			}
			
			if($exp_date){
				$query = "UPDATE #__djcf_items SET date_exp=".$db->q($exp_date)." WHERE id IN (".$ids.")";
				$db->setQuery($query);
				$db->execute();
			}

			$items_updated = count($id_arr);
		}		

		$message = JText::sprintf('COM_DJCLASSIFIEDS_BATCH_ITEMS_UPDATED', $items_updated);
		$app->enqueueMessage($message, 'success');
	    $app->redirect($redirect);
	}

	function unblock()
	{
		$app = JFactory::getApplication();
		$db = JFactory::getDbo();
		$cid = $app->input->get('cid', array(), 'array');
		
		$cids = implode(',', $cid);

		$query = 'UPDATE #__djcf_items SET blocked=0 WHERE id IN ('.$cids.')';
		$db->setQuery($query);
		$db->execute();

		$app->enqueueMessage(JText::_('COM_DJCLASSIFIEDS_ITEMS_ITEM_UNBLOCKED'), 'success');
		$app->redirect('index.php?option=com_djclassifieds&view=items');
	}

}
Site is undergoing maintenance

PACJA Events

Maintenance mode is on

Site will be available soon. Thank you for your patience!