Current File : /home/pacjaorg/public_html/km/administrator/components/com_djclassifieds/models/item.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 DJClassifiedsModelItem extends DJClassifiedsAdminModel
{
	protected $form_name = 'item';

	public function __construct($config = array())
	{
		$config['event_after_save'] = 'onDJClassifiedsModelAdminAfterSave';
		$config['event_after_delete'] = 'onDJClassifiedsModelAdminAfterDelete';
		parent::__construct($config);
	}

	public function getTable($type = 'Items', $prefix = 'DJClassifiedsTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}

	public function getForm($data = array(), $loadData = true)
	{
		// Get the form.
		$form = $this->loadForm('com_djclassifieds.'.$this->form_name, $this->form_name, array('control' => 'jform', 'load_data' => $loadData));
		if (empty($form)) {
			return false;
		}

		return $form;
	}

	protected function loadFormData()
	{
		$app = JFactory::getApplication();
		$par = JComponentHelper::getParams('com_djclassifieds');
		$data = JFactory::getApplication()->getUserState('com_djclassifieds.edit.'.$this->form_name.'.data', array());

		if (empty($data)) {
			$data = $this->getItem();

			$proms = $this->getPromotions();
			$proms_subform_arr = array();
			$active_proms_count = 0;
			foreach($proms as $key => $prom){
				$proms_subform_arr['proms'.$key] = array(
					'prom_id' => $prom->id,
					'prom_label' => JText::_($prom->label)
				);

				if($data->id){
					$active_prom_days = $this->getActivePromotionDays($data->id, $prom->id);
					if($active_prom_days){
						$proms_subform_arr['proms'.$key]['prom_days'] = $active_prom_days->days;
						$proms_subform_arr['proms'.$key]['prom_date_exp'] = $active_prom_days->date_exp;
						$active_proms_count++;
					}
				}
			}
			$data->set('proms', $proms_subform_arr);
			$data->set('active_proms_count', $active_proms_count);

			if($data->id){
				if($data->exp_days == '0' && $data->date_exp != '2038-01-01 00:00:00'){
					$data->set('exp_days', ''); // setting empty option instead of unlimited
				}

				if($data->date_mod == '0000-00-00 00:00:00'){
					$data->date_mod = null;
				}

				$data->contact = str_ireplace(array('<br />','<br>','<br/>'), '', $data->contact);

				$images_count = $this->getImagesCount($data->id);
				$data->set('images_count', $images_count);

				$bids = $this->getBids($data->id);
				$bids_subform_arr = array();
				foreach($bids as $key => $bid){
					$bids_subform_arr['bids'.$key] = array(
						'bid_id' => $bid->id,
						'bid_name' => $bid->u_name,
						'bid_date' => $bid->date,
						'bid_value' => strip_tags(DJClassifiedsTheme::priceFormat($bid->price, $data->currency))
					);
				}
				$data->set('bids', $bids_subform_arr);

				$orders = $this->getOrders($data->id);
				$orders_subform_arr = array();
				foreach($orders as $key => $order){
					$orders_subform_arr['orders'.$key] = array(
						'order_id' => $order->id,
						'order_name' => $order->u_name,
						'order_date' => $order->date,
						'order_quantity' => $order->quantity
					);
				}
				$data->set('orders', $orders_subform_arr);

				$askmessages = $this->getAskMessages($data->id);
				$askmessages_subform_arr = array();
				foreach($askmessages as $key => $askmessage){
					$askmessages_subform_arr['askmessages'.$key] = array(
						'askmessage_id' => $askmessage->id,
						'askmessage_user' => $askmessage->user_id ? ($askmessage->u_name ? $askmessage->u_name.' ('.$askmessage->user_id.')' : $askmessage->user_id) : JText::_('COM_DJCLASSIFIEDS_OPTION_GUEST'),
						'askmessage_date' => $askmessage->date,
						'askmessage_message' => $askmessage->message.($askmessage->custom_fields ? PHP_EOL.PHP_EOL.str_replace('<br />', PHP_EOL, $askmessage->custom_fields) : '')
					);
				}
				$data->set('askmessages', $askmessages_subform_arr);

				$abusereports = $this->getAbuseRaports($data->id);
				$abusereports_subform_arr = array();
				foreach($abusereports as $key => $abusereport){
					$abusereports_subform_arr['abusereports'.$key] = array(
						'abusereport_id' => $abusereport->id,
						'abusereport_user' => $abusereport->user_id ? ($abusereport->u_name ? $abusereport->u_name.' ('.$abusereport->user_id.')' : $abusereport->user_id) : JText::_('COM_DJCLASSIFIEDS_OPTION_GUEST'),
						'abusereport_date' => $abusereport->date,
						'abusereport_message' => $abusereport->message
					);
				}
				$data->set('abusereports', $abusereports_subform_arr);
			}else{
				if($par->get('exp_days','7')){
					$data->set('date_exp', JFactory::getDate('now +'.$par->get('exp_days','7').' day')->toSQL());
				}else{
					$data->set('date_exp', '2038-01-01 00:00:00');
					$data->set('exp_days', '0');
				}
			}
		}

		return $data;
	}

	protected function prepareTable($table)
	{
		$app = JFactory::getApplication();

		$is_new = $table->id ? false : true;

		$table->contact = nl2br($table->contact);

		if($table->alias){
			$table->alias = DJClassifiedsSEO::getAliasName($table->alias);
		}else{
			$table->alias = DJClassifiedsSEO::getAliasName($table->name);
		}

		if(!$table->cat_id){
			$table->cat_id = 0;
		}
		
		if(!$table->id){
			$table->date_start = JFactory::getDate()->toSQL();
			$table->date_sort = JFactory::getDate()->toSQL();
			$table->date_mod = '0000-00-00 00:00:00';

			if(!$table->user_id){
				$user = JFactory::getUser();
				$table->user_id = $user->id;
				$table->ip_address = $app->input->server->getStr('REMOTE_ADDR');
			}
		}

		if(strstr(($table->promotions ? $table->promotions : ''), 'p_first')){
			$table->special = 1;
		}else{
			$table->special = 0;
		}

		$mcat_ids = $app->input->get('mcat_ids', array(), 'ARRAY');
		if(!$table->cat_id && $mcat_ids){
			$table->cat_id = $mcat_ids[0];
			unset($mcat_ids[0]);
			$mcat_ids = array_values($mcat_ids);
			$app->input->set('mcat_ids', $mcat_ids);
		}

		if(($table->region_id || $table->address) && (($table->latitude=='0.000000000000000' && $table->longitude=='0.000000000000000') || (!$table->latitude && !$table->longitude))){
			if($table->post_code){
				$loc_coord = DJClassifiedsGeocode::getLocationPostCode($table->post_code, $table->address, $table->region_id);
			}else{
				$loc_coord = DJClassifiedsGeocode::getLocation($table->address, $table->region_id);
			}
							
			if(!empty($loc_coord)){
				$table->latitude = $loc_coord['lat'];
				$table->longitude = $loc_coord['lng'];
			}
		}

		$table->date_renew = $table->date_renew ? $table->date_renew : NULL;
		$table->exp_days = $table->exp_days ? $table->exp_days : 0;
		$table->promotions = $table->promotions ? $table->promotions : '';
		
		$app->triggerEvent('onBeforeAdminDJClassifiedsSaveAdvert', array(&$table, $is_new));
	}

	protected function getReorderConditions($table)
	{
		$condition = array();
		$condition[] = 'cat_id = '.(int) $table->cat_id;
		return $condition;
	}

	function getBids($item_id)
	{
		$db = JFactory::getDBO();
		$query = "SELECT a.*, u.name as u_name FROM #__djcf_auctions a "
				."LEFT JOIN #__users u ON a.user_id=u.id "
				."WHERE a.item_id=".$item_id." ORDER BY a.date DESC";
		$db->setQuery($query);
		$bids = $db->loadObjectList();

    	return $bids;
    }

	function getOrders($item_id)
	{
		$db = JFactory::getDBO();
		$query = "SELECT a.*, u.name as u_name "
				."FROM #__djcf_orders a "
				."LEFT JOIN #__users u ON a.user_id=u.id "
				."WHERE a.item_id=".$item_id." AND a.status=1 "
				."ORDER BY a.date DESC";
		$db->setQuery($query);
		$orders = $db->loadObjectList();

    	return $orders;
    }

	function getAskMessages($item_id)
	{
		$db = JFactory::getDBO();
		$query = "SELECT a.*, u.name as u_name "
				."FROM #__djcf_itemsask a "
				."LEFT JOIN #__users u ON a.user_id=u.id "
				."WHERE a.item_id=".$item_id." "
				."ORDER BY a.date DESC";
		$db->setQuery($query);
		$askmessages = $db->loadObjectList();

		return $askmessages;
	}

	function getAbuseRaports($item_id)
	{
		$db = JFactory::getDBO();
		$query = "SELECT a.*, u.name as u_name "
				."FROM #__djcf_items_abuse a "
				."LEFT JOIN #__users u ON a.user_id=u.id "
				."WHERE a.item_id=".$item_id." "
				."ORDER BY a.date DESC";
		$db->setQuery($query);
		$abusereports = $db->loadObjectList();

		return $abusereports;
	}

	function getActivePromotionDays($item_id, $prom_id)
	{
		$db = JFactory::getDBO();
		$query = "SELECT * FROM #__djcf_items_promotions WHERE date_exp>".$db->q(JFactory::getDate()->toSQL())." AND prom_id=".$prom_id." AND item_id=".$item_id;
		$db->setQuery($query);
		$active_prom_day = $db->loadObject();

		return $active_prom_day;
	}

	function getPromotions()
	{
		$db = JFactory::getDBO();
		$query = "SELECT * FROM #__djcf_promotions WHERE published=1 ORDER BY ordering, id";
		$db->setQuery($query);
		$proms = $db->loadObjectList();

		return $proms;
	}

	function getImagesCount($item_id)
	{
		$db = JFactory::getDBO();
		$query = "SELECT count(id) FROM #__djcf_images WHERE type='item' AND item_id=".$item_id;
		$db->setQuery($query);
		$images_count = $db->loadResult();

		return $images_count;
	}
}
Site is undergoing maintenance

PACJA Events

Maintenance mode is on

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