Current File : /home/pacjaorg/public_html/km/components/com_djclassifieds/controllers/renewitem.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 DJClassifiedsControllerRenewItem extends JControllerLegacy
{
	function save()
	{
		JTable::addIncludePath(JPATH_ROOT.'/administrator/components/com_djclassifieds/tables');
		$app = JFactory::getApplication();
		$par = $app->getParams('com_djclassifieds');		
		$user = JFactory::getUser();
		$db = JFactory::getDBO();
		$model = $this->getModel('renewitem');
		$row = JTable::getInstance('Items', 'DJClassifiedsTable');

		$id = $app->input->getInt('id', 0);
		$it = $app->input->getInt('Itemid', 0);
		$order = $app->input->getCmd('order', $par->get('useritems_ordering','date_a'));		
		$ord_t = $app->input->getCmd('ord_t', $par->get('useritems_ordering_dir','desc'));

		if(!$user->id){
			DJClassifiedsSEO::redirectLogIn(DJClassifiedsSEO::getCategoryRoute('0:all'));
		}

		$old_row = $model->getItemById($id);
		
		$app->triggerEvent('onBeforeInitialiseDJClassifiedsRenewAdvert', array(&$row, &$par));
		
		if($user->id != $old_row->user_id){
			DJClassifiedsSEO::redirectWrongItem();
		}

		$query = "SELECT cat_id FROM #__djcf_items WHERE id=".$id." "
				.(JPluginHelper::isEnabled('djclassifieds', 'multicategories') ? "UNION SELECT cat_id FROM #__djcf_items_categories WHERE item_id=".$id." " : "");
		$db->setQuery($query);
		$cats_to_check = $db->loadColumn();
		if(!DJClassifiedsAccess::checkCatAdsLimits($cats_to_check, $id)){
			$app->redirect(JRoute::_(DJClassifiedsSEO::getViewUri('useritems'), false));
		}

		$row->load($id);		
		
		$app->triggerEvent('onAfterInitialiseDJClassifiedsRenewAdvert', array(&$row, &$par));

		$cat = $model->getCategory($row->cat_id);

		$row->notify = 0;
		if($app->input->exists('exp_days')){
			$row->exp_days = $app->input->getInt('exp_days', $par->get('exp_days'));
		}
		if($app->input->exists('type_id')){
			$row->type_id = $app->input->getInt('type_id', 0);
		}

		if($row->new == 2){ // after 'back to renew'
			$new_date_exp = JFactory::getDate($row->date_exp);
			if($old_row->exp_days > 0){
				$new_date_exp->modify('-'.$old_row->exp_days.'day'); // removing already added prev exp days
			}
			$new_date_exp->add(date_diff(JFactory::getDate($row->date_renew), JFactory::getDate())); // adding the time between last_renew and now (date_exp at this point is based on prev now)
		}else{
			$renew_date_limit = JFactory::getDate('now +'.$par->get('renew_days','3').'day')->toSQL();

			if($renew_date_limit <= $row->date_exp){
				DJClassifiedsSEO::redirectWrongItem('index.php?option=com_djclassifieds&view=useritems&Itemid='.$it);
			}

			if(JFactory::getDate()->toSQL() < $row->date_exp){
				$time_left = date_diff(JFactory::getDate(), JFactory::getDate($row->date_exp));
			}
			$new_date_exp = JFactory::getDate();
			if(!empty($time_left)){
				$new_date_exp->add($time_left);
			}
		}

		if($row->exp_days == 0){
			$row->date_exp = '2038-01-01 00:00:00';
		}elseif($row->exp_days > 0){
			$new_date_exp->modify('+'.$row->exp_days.' day');
			$row->date_exp = $new_date_exp->toSQL();
		}

		$row->date_sort = JFactory::getDate()->toSQL();
		$row->new = 2;
		$row->date_renew = JFactory::getDate()->toSQL();
					
		$duration_price = 0;
		$duration_points = 0;
		if($par->get('durations_list','')){
			$day = $model->getDuration($row->exp_days, $row->cat_id, $row->user_id);
			if($day){
				$duration_price = $day->price_renew;
				$duration_points = $day->points_renew;
			}
		}
		
		$type = null;
		$type_price = 0;
		$type_points = 0;
		if($row->type_id){
			$type = DJClassifiedsPayment::getTypePrice($user->id, $row->type_id);
			if($type){
				$type_price = $type->price;
				$type_points = $type->points;
			}
		}

		$row->extra_images = 0; // resetting the paid images number
		$row->extra_images_to_pay = 0;
		
		$query = "SELECT * FROM #__djcf_images WHERE item_id=".$row->id." AND type='item'";
		$db->setQuery($query);
		$item_images = $db->loadObjectList('id');
		$images_c = count($item_images);
		
		$imgfreelimit = $par->get('img_free_limit','-1');
		$images_to_pay = 0;
		if($imgfreelimit >- 1 && $images_c > $imgfreelimit){
			$images_to_pay = $images_c - $imgfreelimit;
		}

		$row->extra_chars = 0; // resetting the paid chars number
		$row->extra_chars_to_pay = 0;
		
		$desc_chars_limit = $par->get('pay_desc_chars_free_limit',0);
		$desc_c = mb_strlen(str_ireplace(array('<br /> ','<br />','<br>','<br/>'), "\n", $row->description));
		$chars_to_pay = 0;
		if($par->get('pay_desc_chars',0) && $desc_c > $desc_chars_limit){
			$chars_to_pay = $desc_c - $desc_chars_limit;				
		}

		$row->promotions = '';
		$promotions = $par->get('promotion','1')=='1' ? $model->getPromotionsPrices() : array();

		$mcat_list = $model->getItemCategories($row->id);

		$app->triggerEvent('onBeforePaymentsDJClassifiedsRenewAdvert', array(&$row, &$cat, &$promotions, &$type_price, &$duration_price, &$images_to_pay, &$chars_to_pay, &$type_points, &$duration_points, &$mcat_list));

		$pay_redirect = false;
		$row->pay_type = '';
		$pay_types = array();

		if(!DJClassifiedsPayment::isFree($cat->price, $cat->points, $par)){
			$pay_types[] = 'cat';
		}
		if(!DJClassifiedsPayment::isFree($duration_price, $duration_points, $par)){
			$pay_types[] = 'duration_renew';
		}
		if(!DJClassifiedsPayment::isFree($type_price, $type_points, $par)){
			$pay_types[] = 'type';
		}
		if($images_to_pay > 0){
			$row->extra_images_to_pay = $images_to_pay;
			$pay_types[] = 'extra_img';
		}
		if($chars_to_pay > 0){
			$row->extra_chars_to_pay = $chars_to_pay;
			$pay_types[] = 'extra_chars';
		}
		foreach($mcat_list as $mc){
			if(!DJClassifiedsPayment::isFree($mc->price, $mc->points, $par)){
				$pay_types[] = 'mc'.$mc->id;
			}
		}

		$row->pay_type = implode(',', $pay_types).($pay_types ? ',' : '');

		if($par->get('promotion','1') == '1'){
			DJClassifiedsPayment::checkApplyPromotions($row, $old_row, $promotions, $par);
		}

		$app->triggerEvent('onAfterPaymentsDJClassifiedsRenewAdvert', array(&$row));

		DJClassifiedsAccess::overrideUserAutopublish($par);

		if($row->pay_type){
			$pay_redirect = true;
			if(!DJClassifiedsPayment::isPayTypePromsOnly($row->pay_type)){ // if non-prom pay types
				$row->published = 0;
			}
		}else{
			if($old_row->c_autopublish == '0'){
				if($par->get('autopublish','1') == '1' || $par->get('autopublish','1') == '2'){ // ignore 'edit only' on renew
					$row->published = 1;
				}else{
					$row->published = 0;
				}
			}elseif($old_row->c_autopublish == '1' || $old_row->c_autopublish == '3'){ // ignore 'edit only' on renew
				$row->published = 1;
			}elseif($old_row->c_autopublish == '2'){
				$row->published = 0;
			}
		}

		if($row->buynow == 1 && $row->quantity == 0){
			$row->quantity = 1;
		}
		
		$app->triggerEvent('onBeforeDJClassifiedsRenewAdvert', array(&$row, &$par));
		
		if(!$row->store()){
			throw new Exception($row->getError());
		}
		
		$app->triggerEvent('onAfterDJClassifiedsRenewAdvert', array(&$row, &$par));

		if($par->get('notify_renew_admin','0')){
			DJClassifiedsNotify::notifyAdminRenewAdvert($row);
		}
	
		if($pay_redirect){
			if($row->pay_type != $old_row->pay_type){
				DJClassifiedsPayment::deletePrevStartPayment($row->id);
			}
			$redirect = 'index.php?option=com_djclassifieds&view=payment&id='.$row->id.'&Itemid='.$it;
			$message = JTExt::_('COM_DJCLASSIFIEDS_AD_RENEWED_SUCCESSFULLY_CHOOSE_PAYMENT');
			$msg_type = 'notice';
		}else{
			$redirect = 'index.php?option=com_djclassifieds&view=useritems&Itemid='.$it.'&order='.$order.'&ord_t='.$ord_t;
			$message = JText::_('COM_DJCLASSIFIEDS_AD_RENEWED_SUCCESSFULLY');
			$msg_type = 'success';
			$row->new = 0;
			if(!$row->store()){
				throw new Exception($row->getError());
			}
		}

		$app->enqueueMessage($message, $msg_type);
		$app->redirect(JRoute::_($redirect, false));
	}
}
Site is undergoing maintenance

PACJA Events

Maintenance mode is on

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