<?php
/**
 * @package BILLETS
 * @link 	http://www.dioscouri.com
 * @license GNU/GPLv2
 */
 
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

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

/**
* 	BilletsController
*
*/
// ************************************************************************
class BilletsControllerTicket extends BilletsController {

	var $_type;

	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	function __construct() {
		parent::__construct();

		// Register Extra tasks
		$this->registerTask( 'view', 'view' );
		$this->registerTask( 'new', 'edit' );
		$this->registerTask( 'savenew', 'savenew' );
		$this->registerTask( 'addcomment', 'addcomment' );
		$this->registerTask( 'closeunsatisfactory', 'closeticket' );
		$this->registerTask( 'closepoor', 'closeticket' );
		$this->registerTask( 'closeaverage', 'closeticket' );
		$this->registerTask( 'closegood', 'closeticket' );
		$this->registerTask( 'closegreat', 'closeticket' );
	}

	/**
	 * display 
	 * @return void
	 */
	function display() {
		JRequest::setVar( 'view', 'ticket' );
		JRequest::setVar( 'layout', 'default'  );
		// JRequest::setVar('hidemainmenu', 1);

		parent::display();

	}

	/**
	 * display the ticket
	 * @return void
	 */
	function view() {	
		global $mainframe, $Itemid;
		
		JRequest::setVar( 'view', 'ticket' );
		JRequest::setVar( 'layout', 'default' );

		$model 	= &$this->getModel( 'ticket' );
		$row 	= &$model->getData();
		$user 	= &JFactory::getUser();
		
		// ticket not assoc with user
		if ( ($row->sender_userid != $user->id) ) {
			$link = 'index.php?option='._THISCOMPONENT.'&controller=tickets&task=list&Itemid='.$Itemid;
			$link = JRoute::_( $link );
			$msg = JText::_( _BILLETS_INVALIDSUPPORTTICKET_MESSAGE );
			$this->setRedirect( $link, $msg, 'notice' );
        	return false;
		}	
		
		parent::display();
	}

	/**
	 * display the edit form
	 * @return void
	 */
	function edit() {
		JRequest::setVar( 'view', 'ticket' );
		JRequest::setVar( 'layout', 'form' );

		$model 	= &$this->getModel( 'ticket' );
		$row 	= &$model->getData();
		$user 	= &JFactory::getUser();
		
		// ticket not assoc with user
		if ( $row->id && ($row->sender_userid != $user->id) ) {
			$link = 'index.php?option='._THISCOMPONENT.'&controller=tickets&task=list&Itemid='.$Itemid;
			$link = JRoute::_( $link );
			$msg = JText::_( _BILLETS_INVALIDSUPPORTTICKET_MESSAGE );
			$this->setRedirect( $link, $msg, 'notice' );
        	return false;
		}

		parent::display();
	}

	/**
	 * save a record (and redirect to main page)
	 * @return void
	 */
	function savenew() {
		global $mainframe, $Itemid;
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
		if ($Itemid == 0) { $Itemid = billetsGetItemid(); }
		
		$model = &$this->getModel( 'ticket' );
		
		if ( $ticket = $model->savenew() ) {
			//	email users involved

				$link = $mainframe->getCfg('live_site')."/index.php?option="._THISCOMPONENT."&controller=ticket&task=view&id=".$ticket->id;
				$link = JRoute::_( $link, false );
				
				$emailBody = sprintf(_BILLETS_NEWSUPPORTTICKETCREATED_MESSAGE, $ticket->sender_email, $link);
				$emailSubject = _BILLETS_NEWSUPPORTTICKETCREATED;
			
			//	send email to sender
				$mailUser = billetsMail ( $mainframe->getCfg('mailfrom'), $mainframe->getCfg('fromname'), $ticket->sender_email, $emailSubject, $emailBody );	
			//	send email to admin
				$mailAdmin = billetsMail ( $mainframe->getCfg('mailfrom'), $mainframe->getCfg('fromname'), $mainframe->getCfg('mailfrom'), $emailSubject, $emailBody );	
			
			if ($mailUser && $mailAdmin) {
				// savenew success
				$link = 'index.php?option='._THISCOMPONENT.'&controller=tickets&task=list&Itemid='.$Itemid;
				$link = JRoute::_( $link );				
				$msg = JText::_( _BILLETS_NEW." "._BILLETS_TICKET." "._BILLETS_CREATED );
				$this->setRedirect( $link, $msg, 'message' );
			} else {
				// savenew but no email
				$link = 'index.php?option='._THISCOMPONENT.'&controller=tickets&task=list&Itemid='.$Itemid;
				$link = JRoute::_( $link );				
				$msg = JText::_( _BILLETS_NEW." "._BILLETS_TICKET." "._BILLETS_CREATED." - "._BILLETS_EMAIL." "._BILLETS_FAILED );
				$this->setRedirect( $link, $msg, 'notice' );
			}
		} else {
			// savenew failed
			$link = 'index.php?option='._THISCOMPONENT.'&controller=tickets&task=list&Itemid='.$Itemid;
			$link = JRoute::_( $link );			
			$msg = JText::_( _BILLETS_NEW." "._BILLETS_TICKET." "._BILLETS_SAVE." "._BILLETS_FAILED." - ".$model->getError() );
			$this->setRedirect( $link, $msg, 'notice' );
		}
	}

	/**
	 * cancel and redirect to main page
	 * @return void
	 */
	function cancel() {
		global $Itemid;
		$link = 'index.php?option='._THISCOMPONENT.'&controller=tickets&task=list&status=open&type=&Itemid='.$Itemid;
		$link = JRoute::_( $link );		
	    $msg = JText::_( _BILLETS_OPERATIONCANCELLED );
	    $this->setRedirect( $link, $msg, 'notice' );		
	}

	/**
	 * addcomment
	 * @return void
	 */
	function addcomment() {
		global $mainframe, $Itemid;
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$model = &$this->getModel( 'ticket' );
		
		if ( $ticket = $model->addcomment() ) {
			//	email users involved
				$user =& JFactory::getUser();
				$link = $mainframe->getCfg('live_site')."/index.php?option="._THISCOMPONENT."&controller=ticket&task=view&id=".$ticket->id;
				$link = JRoute::_( $link );
				
				$emailBody = sprintf(_BILLETS_NEWSUPPORTTICKETCOMMENT_MESSAGE, $user->email, $link);
				$emailSubject = _BILLETS_NEWSUPPORTTICKETCOMMENT;
			
			//	send email to sender
				$mailUser = billetsMail ( $mainframe->getCfg('mailfrom'), $mainframe->getCfg('fromname'), $ticket->sender_email, $emailSubject, $emailBody );	
			//	send email to admin
				$mailAdmin = billetsMail ( $mainframe->getCfg('mailfrom'), $mainframe->getCfg('fromname'), $mainframe->getCfg('mailfrom'), $emailSubject, $emailBody );
				// $mailAdmin = true;
			
			if ($mailUser && $mailAdmin) {
				// addcomment success
				$link = 'index.php?option='._THISCOMPONENT.'&controller=ticket&task=view&id='.$ticket->id.'&Itemid='.$Itemid;
				$link = JRoute::_( $link );
				$msg = JText::_( _BILLETS_NEW." "._BILLETS_COMMENT." "._BILLETS_ADDED );
				$this->setRedirect( $link, $msg, 'message' );
			} else {
				// addcomment success but no email
				$link = 'index.php?option='._THISCOMPONENT.'&controller=ticket&task=view&id='.$ticket->id.'&Itemid='.$Itemid;
				$link = JRoute::_( $link );
				$msg = JText::_( _BILLETS_NEW." "._BILLETS_COMMENT." "._BILLETS_ADDED." - "._BILLETS_EMAIL." "._BILLETS_FAILED );
				$this->setRedirect( $link, $msg, 'notice' );
			}
		} else {
			// addcomment failed
			$link = 'index.php?option='._THISCOMPONENT.'&controller=ticket&task=view&id='.$ticket->id.'&Itemid='.$Itemid;
			$link = JRoute::_( $link );
			$msg = JText::_( _BILLETS_NEW." "._BILLETS_COMMENT." "._BILLETS_FAILED." - ".$model->getError() );
			$this->setRedirect( $link, $msg, 'notice' );
		}
	}

	/**
	 * display the edit form
	 * @return void
	 */
	function closeticket() {
		global $mainframe, $Itemid;
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$model 	= &$this->getModel( 'ticket' );
		
		if ( $ticket = $model->closeticket() ) {
			// ticket closed
			$link = 'index.php?option='._THISCOMPONENT.'&Itemid='.$Itemid.'&controller=tickets&task=list&Itemid='.$Itemid;
			$link = JRoute::_( $link );
			$msg = JText::_( _BILLETS_TICKET." "._BILLETS_CLOSED );
			$this->setRedirect( $link, $msg, 'message' );
		} else {
			// addcomment failed
			$link = 'index.php?option='._THISCOMPONENT.'&Itemid='.$Itemid.'&controller=tickets&task=list&Itemid='.$Itemid;
			$link = JRoute::_( $link );
			$msg = JText::_( _BILLETS_CLOSE." "._BILLETS_TICKET." "._BILLETS_FAILED." - ".$model->getError() );
			$this->setRedirect( $link, $msg, 'notice' );		
		}
	}
	
}


?>