<?php
/**
 * @version		3.0.0
 * @package		JomLand TicketCenter
 * @copyright	Copyright (C) 2010 JomLand.com. All rights reserved.
 * @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */

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

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


class JLTicketCenterViewReferrals extends JViewLegacy
{
	function display($tpl = null)
	{
		$mainframe = JFactory::getApplication();

		$task =  JRequest::getVar('task');

		if ($task=='add' ||$task=='edit' ){
			$this->edit($tpl);
			return;
		}

		JToolBarHelper::title(JText::_('Referrals - JomLand TicketCenter'), 'generic.png' );

		JLTicketCenterController::displayToolBar();

		$pparams = new JLTicketCenterHelperSetup;

		$context			= 'com_jlticketcenter.referrals.list.';
		$filter_order		= $mainframe->getUserStateFromRequest( $context.'filter_order',		'filter_order',		'cc.title',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $context.'filter_order_Dir',	'filter_order_Dir',	'',			'word' );
		$filter_state		= $mainframe->getUserStateFromRequest( $context.'filter_state',		'filter_state',		'',			'word');
		$search				= $mainframe->getUserStateFromRequest( $context.'search',			'search',			'',			'string');
		if (strpos($search, '"') !== false) {
			$search = str_replace(array('=', '<'), '', $search);
		}
		$search = JString::strtolower($search);

		$filter['search']	= $search;

		JRequest::setVar('filter', $filter);
		JRequest::setVar('list',true);

		// Get data from the model
		$items		= $this->get('Data');
		$pagination = $this->get('Pagination');


		$lists['state']	= JHTML::_('grid.state',  $filter_state );
		$lists['search']	= $search;

		/* Call the state object */
		$state = $this->get('state');
		$lists['order_Dir'] = $state->get('filter_order_Dir');
		$lists['order'] 	= $state->get('filter_order' );

		// push data into the template
		$this->assignRef('items',$items);
		$this->assignRef('pagination', $pagination);
		$this->assignRef('lists', $lists);
		$this->assignRef('params', $pparams);


		parent::display($tpl);

		JLTicketCenterController::renderinfo();
	}

	

	function edit($tpl = null){
		$user	= JFactory::getUser();

		$task = JRequest::getVar( 'task', '', 'method', 'string');
                
                $selectedHearAbout = null;

		JToolBarHelper::title( $task == 'add' ? JText::_('Referral') . ': <small><small>[ '. JText::_( 'New' ) .' ]</small></small>' : JText::_( 'Referral' ) . ': <small><small>[ '. JText::_( 'Edit' ) .' ]</small></small>', 'generic.png' );
		JToolBarHelper::apply('apply');
		JToolBarHelper::save('save');
		JToolBarHelper::cancel('cancel');
		
		JHTML::_('behavior.framework');
		JHTML::_('behavior.framework',true);
		JHtml::_('behavior.modal');
		
		$doc = JFactory::getDocument();
		$jInsertFieldValue =
			'function jInsertFieldValue(value, id) {
				var old_id = document.id(id).value;
				if (old_id != id) {
					var elem = document.id(id)
					elem.value = value;
					elem.fireEvent("change");
				}
			}';
		$doc->addScriptDeclaration($jInsertFieldValue);

		$pparams = new JLTicketCenterHelperSetup;

		$row = JTable::getInstance('referrals', 'Table');

		if ($task == 'edit') {
                    $cid	= JRequest::getVar( 'cid', array(), 'get', 'array' );

                    if (!$cid){
                            $cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
                    }

                    JArrayHelper::toInteger($cid);
                    $cids = implode(',',$cid);

                    $row->load($cid[0]);
                    
                    $selectedHearAbout = $row->hear_about_value;

			
		}else{
                    $context    = 'com_jlticketcenter.referrals.list.';
		}

		if (!empty($cid[0])){
			$row->checkout($user->get('id') );
		} else {
			$row->load(0);
			$row->published = 1;
		}
                

		$published =  JHTML::_('select.genericlist', array(0=>JText::_('JNO'),1=>JText::_('JYES')), 'published', null, $optKey = 'value', $optText = 'text',$row->published);

                JLoader::import('components.com_jlticketcenter.helpers.helper_pro',JPATH_SITE);
                $hearAbout = JLTicketCenterProHelper::hearAbout();

                $hearAbout  = JHtml::_('select.genericlist',$hearAbout , 'hear_about_value', $attribs = null, $optKey = 'value', $optText = 'text', $selectedHearAbout , $idtag = false, $translate = false);

                $this->assignRef('hearAbout',$hearAbout);	

                		
		$this->assignRef('published',$published);
		$this->assignRef('referral',$row);
		$this->assignRef('params', $pparams);

		parent::display($tpl);

		JLTicketCenterController::renderinfo();
	} //EOF edit
}//EOC