<?php
/**
 * @version		3.0.0
 * @package		JomLand TicketCenter
 * @copyright	Copyright (C) 2010 JomLand.com. All rights reserved.
 * @link 		www.jomland.com
 * @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.model' );

class JLTicketCenterModelEventsTicketsType extends JModelLegacy
{
	public function getLines($event_id){
		$db = $this->getDBO();

		$query = "SELECT *
				FROM #__jltcenter_events_ticket_type
				WHERE `event_id`=".$event_id
		." ORDER BY `ticket_type`";

		$db->setQuery($query);
		$results = $db->loadObjectList();

		return $results;
	}

	public function saveLines($event_id,$tickets_lines){
		$db = $this->getDBO();
		//$event_id= array('id'=>$event_id);

        $query = null;
		foreach ($tickets_lines as $line){

            $query = null;

			$line['ticket_desc'] = "'".addslashes($line['ticket_desc'])."'";
			if($line['ticket_type']==''){
				$line['ticket_type'] =0;
			}
			if(empty($line['ticket_price'])){
				$line['ticket_price']=0;
			}
				
			if($line['ticket_type']>0){

                echo "111";
				//Update
				$query = "UPDATE #__jltcenter_events_ticket_type
						SET `ticket_desc`=".$line['ticket_desc'].", 
						`ticket_price`=".$line['ticket_price'].",
						`tickets`=".$line['tickets'].",
			      		`tickets_ordered`=".$line['tickets_ordered'].",
			      		`tickets_sold`=".$line['ticktes_sold'] 
				." WHERE `ticket_type`=".$line['ticket_type'];
			}else{
				// New line
				if($line['ticket_price']>0 || $line['tickets']>0) {
					//$insert_line = array_merge($event_id, $line);
						
					$query = "INSERT INTO  #__jltcenter_events_ticket_type
						SET`event_id` =".$event_id.",
						`ticket_desc`=".$line['ticket_desc'].", 
						`ticket_price`=".$line['ticket_price'].",
						`tickets`=".(int)$line['tickets'].",
			      		`tickets_ordered`=".(int)$line['tickets_ordered'].",
			      		`tickets_sold`=".(int)$line['ticktes_sold'];
						
				}
			}

            if (!is_null($query)){
                $db->setQuery($query);
                $db->query();
            }

		}

		return;
	}//EOF save
	
	function deleteLine($id){
		if ((int)$id == 0){
			return;
		}

		$db = $this->getDBO();
		$query = "DELETE FROM   #__jltcenter_events_ticket_type
				WHERE `ticket_type`=".$id;
						
		$db->setQuery($query);
		if ($db->query()){
			return true;
		}else{
			return false;
		}
	}//EOF deleteLine
} // EOC
