<?php
/**
 * @package DJ-Catalog2
 * @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;

jimport('joomla.application.component.controllerform');

class Djcatalog2ControllerOrder extends JControllerForm {
	public function save($key = null, $urlVar = null) {
		return parent::save($key, $urlVar);
	}
	protected function allowAdd($data = array())
	{
		$user = JFactory::getUser();
		return $user->authorise('core.create', $this->option) || $user->authorise('djcatalog2.admin.orders', $this->option);
	}
	
	protected function allowEdit($data = array(), $key = 'id')
	{
		$canDo = JFactory::getUser()->authorise('core.edit', $this->option) || JFactory::getUser()->authorise('djcatalog2.admin.orders', $this->option);
		
		if (!$canDo) {
			$task = JFactory::getApplication()->input->getCmd('task');
			if ($task == 'edit' && JFactory::getUser()->authorise('djcatalog2.access.orders', $this->option)) {
				return true;
			} else if ($task == 'cancel') {
				return true;
			}
		}
		
		return $canDo;
	}
	
	public function change_status() {
		// Check for request forgeries.
		$this->checkToken();
		
		$model = $this->getModel();
		$data  = $this->input->post->get('jform', array(), 'array');
		
		
		$recordId = $this->input->getInt('id');
		
		// Populate the row id from the session.
		$data['id'] = $recordId;
		
		// Access check.
		if (!$this->allowSave($data, 'id') || !$recordId)
		{
			$this->setError(\JText::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'));
			$this->setMessage($this->getError(), 'error');
			
			$this->setRedirect(
				\JRoute::_(
					'index.php?option=' . $this->option . '&view=' . $this->view_list
					. $this->getRedirectToListAppend(), false
					)
				);
			
			return false;
		}
		
		$comment = $this->input->post->getString('history_comment');
		$notify = $this->input->post->getInt('history_notify');
		$status = $this->input->post->getString('history_status');
		
		if (!$model->set_status($recordId, $status, $notify, $comment))
		{
			// Redirect back to the edit screen.
			$this->setError(\JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()));
			$this->setMessage($this->getError(), 'error');
			
			$this->setRedirect(
				\JRoute::_(
					'index.php?option=' . $this->option . '&view=' . $this->view_item
					. $this->getRedirectToItemAppend($recordId, 'id'), false
					)
				);
			
			return false;
		}
		
		$this->setRedirect(
			\JRoute::_(
				'index.php?option=' . $this->option . '&view=' . $this->view_item
				. $this->getRedirectToItemAppend($recordId, 'id'), false
				)
			);
		
		return true;
	}

	public function createShipment() {

		$app = JFactory::getApplication();
		$jinput = $app->input;


		$model = $this->getModel();

		$order_id = $jinput->getInt('id');
		$delivery_method_id = $jinput->getInt('delivery_method_id');


		$order = $model->getItem($order_id);
		$deliveryData = $jinput->get('createShipments', array(), 'array');
		$deliveryMethod = $model->getDeliveryMethod($delivery_method_id);


		JPluginHelper::importPlugin('djcatalog2delivery');
		$dispatcher = Joomla\CMS\Factory::getApplication()->getDispatcher();

		Joomla\CMS\Factory::getApplication()->triggerEvent('onDJC2ProcessCreateShipments', array('com_djcatalog2.order.delivery', $order, $deliveryData, $deliveryMethod));
	}

	public function displayDeliveryForm() {
		$app = JFactory::getApplication();
		$jinput = $app->input;

		$model = $this->getModel();

		$order_id = $jinput->getInt('id');
		$delivery_method_id = $jinput->getInt('delivery_method_id');

		$order = $model->getItem($order_id);
		$deliveryMethod = $model->getDeliveryMethod($delivery_method_id);

		JPluginHelper::importPlugin('djcatalog2delivery');
		$dispatcher = Joomla\CMS\Factory::getApplication()->getDispatcher();

	 	$deliveryForm = Joomla\CMS\Factory::getApplication()->triggerEvent('renderShippmentForm', array('com_djcatalog2.order.delivery', $order, $deliveryMethod));
		echo implode("", $deliveryForm);
	}

	public function downloadDeliveryLabel() {
		$app = JFactory::getApplication();
		$jinput = $app->input;

		$model = $this->getModel();
		$order_id = $jinput->getInt('oid');
		$order = $model->getItem($order_id);
		$deliveryMethod = $model->getDeliveryMethod($order->delivery_method_id);

		JPluginHelper::importPlugin('djcatalog2delivery');
		$dispatcher = Joomla\CMS\Factory::getApplication()->getDispatcher();

		Joomla\CMS\Factory::getApplication()->triggerEvent('onDJC2DownloadDeliveryLabel', array('com_djcatalog2.order.delivery', $order, $deliveryMethod));
	}
}
?>
