Your IP : 216.73.216.54


Current Path : /var/www/html/components/com_jvld/models/
Upload File :
Current File : /var/www/html/components/com_jvld/models/editcoupon.php

<?php
/**
 * @version		$Id$
 * @package		JV-LinkDirectory
 * @subpackage	com_jvld
 * @copyright	Copyright 2008-2013 JV-Extensions. All rights reserved
 * @license		GNU General Public License version 3 or later
 * @author		JV-Extensions
 * @link		http://www.jv-extensions.com
 */

// No direct access
defined('_JEXEC') or die('Restricted access');

class JvldModelEditcoupon extends JvldFendModel
{
	public function getForm($data = array(), $loadData = true)
	{
		$form = $this->loadForm('com_jvld.editcoupon', 'editcoupon', array('control' => 'jform', 'load_data' => $loadData), true);
		if (empty($form))
			return false;

		return $form;
	}

	public function getItem()
	{
		$lid = JFactory::getApplication()->input->getInt('lid', 0);
		$id = JFactory::getApplication()->input->getInt('id', 0);

		if ($id)
			return JvldDb::getRow("select * from #__jvld_liscoupons where id = ".(int)$id." and lid = ".(int)$lid);
		else
			return null;
	}

	protected function loadFormData()
	{
		$item = $this->getItem();
		if ($item)
		{
			; // nothing to do
		}

		return $item;
	}

	public function process()
	{
		try
		{
			parent::preProcess();

			if (!$this->cfg->get('coupons_en'))
				throw new Exception(JText::_("COM_JVLD_FEATURED_DISABLED"));

			if (!$this->lid)
				throw new Exception(JText::_("COM_JVLD_ERR_INVALID_REQ"));

			if (($this->title == '') || ($this->code == '') || ($this->discount == '') || ($this->expires_on == ''))
				throw new Exception(JText::_("COM_JVLD_ERR_INSUFFINFO"));

			$link = new JvldLinkinfo($this->lid);
			if ($link->getInfo('uid') != $this->user->get('id'))
				throw new Exception(JText::_("COM_JVLD_UNAUTHORIZED"));

			$linkplan = new JvldLinkPlan();
			$linkplan->setPlanCode($link->getInfo('oway_plan'));

			$coupons = JvldPostinit::getLinkCoupons($this->lid, -1, 0, 0);

			if ($this->id)
			{
				// edit existing coupon
				$ecoupon = JvldDb::getRow("select * from #__jvld_liscoupons where id = ".(int)$this->id);

				foreach ($coupons as $coupon)
				{
					if ($coupon->id == $ecoupon->id)
						continue;

					if (($this->title == $coupon->title) || ($this->code == $coupon->code))
						throw new Exception(JText::_('COM_JVLD_COUPON_DUPLICATE'));
				}

				JvldDb::update("update #__jvld_liscoupons set title = '".JvldSecure::defendSQL($this->title)."', description = '".JvldSecure::defendSQL($this->description)."', code = '".JvldSecure::defendSQL($this->code)."', discount = '".JvldSecure::defendSQL($this->discount)."', discount_unit = '".JvldSecure::defendSQL($this->discount_unit)."', expires_on = '".JvldSecure::defendSQL($this->expires_on)."', onclick_url = '".JvldSecure::defendSQL($this->onclick_url)."', ordering = '".(int)$this->ordering."' where id = ".(int)$this->id);

				$oktext = JText::_('COM_JVLD_COUPON_EDITED_OK');
			}
			else
			{
				// new coupon
				$maxcoupons = $linkplan->getMaxCoupons();
				if (count($coupons) >= $maxcoupons)
					throw new Exception(JText::_("COM_JVLD_LISCOUPON_LIMIT_REACHED"));

				foreach ($coupons as $coupon)
				{
					if (($this->title == $coupon->title) || ($this->code == $coupon->code))
						throw new Exception(JText::_('COM_JVLD_COUPON_DUPLICATE'));
				}

				// get max ordering
				$maxOrdering = JvldDb::getValue("select max(ordering) from #__jvld_liscoupons where lid = ".(int)$this->lid);
				$newOrdering = 1 + $maxOrdering;

				$created_on = JvldDate::getDateTimeInUTCInMySqlFormat();

				$id = JvldDb::update("insert into #__jvld_liscoupons (lid, title, description, code, discount, discount_unit, expires_on, onclick_url, ordering, created_on, updated_on) values('".(int)$this->lid."', '".JvldSecure::defendSQL($this->title)."', '".JvldSecure::defendSQL($this->description)."', '".JvldSecure::defendSQL($this->code)."', '".JvldSecure::defendSQL($this->discount)."', '".JvldSecure::defendSQL($this->discount_unit)."', '".JvldSecure::defendSQL($this->expires_on)."', '".JvldSecure::defendSQL($this->onclick_url)."', '".(int)$newOrdering."', '".JvldSecure::defendSQL($created_on)."', '".JvldSecure::defendSQL($created_on)."')");

				// Trigger OnNewFileUpload plugin
				JvldInit::triggerPlugins('OnNewCouponUpload', array($this->lid, $id));

				// get text
				$oktext = JText::_('COM_JVLD_COUPON_ADDED_OK');
			}

			JvldLinkinfo::updateLinkUpdateDate($this->lid);

			JvldMsg::display($oktext, _JVLD_NEXT_PAGE_GO_TO_RETURN_URL);
			return;
		}
		catch (Exception $ex)
		{
			JvldMsg::display($ex->getMessage(), _JVLD_NEXT_PAGE_GO_BACK);
			return;
		}
	}
}