<?php
/**
 * @version		$Id$
 * @package		ARTIO Booking
 * @copyright	Copyright (C) 2010 ARTIO s.r.o.. All rights reserved.
 * @author 		ARTIO s.r.o., http://www.artio.net
 * @license     GNU/GPL http://www.gnu.org/copyleft/gpl.html
 * @link        http://www.artio.net Official website
 */

defined('_JEXEC') or die('Restricted access');

include_once (($dir = dirname(__FILE__) . DS) . 'reinstall.php');

/**
 * Create new table with reserved items and migrate old reservations to it.
 */
if (file_exists(($installer = ($admin = dirname(__FILE__) . DS . '..' . DS . 'admin' . DS) . 'install.booking.php'))) {
    include_once ($installer);
   
    //DB update script which add new items table and extrapolate old reservations to it
    
	include_once ($dir . 'dbupdate.php');
		
	//update config with e-mails body (to support multiple items)
	
	$params = AParameter::loadComponentParams();
	
    $mailCustomer='&lt;p&gt;Dear Customer,&lt;br /&gt;&lt;br /&gt;your Reservation was successfully saved.&lt;br /&gt;&lt;br /&gt;{OBJECTS}&lt;br /&gt;Object: {OBJECT TITLE}&lt;br /&gt;Reservation Date: {DATE}&lt;br /&gt;Quantity: {QUANTITY}&lt;br /&gt;Price: {PRICE}&lt;br /&gt;Deposit: {DEPOSIT}&lt;br /&gt;{SUPPLEMENTS}&lt;br /&gt;{/OBJECTS}&lt;br /&gt;Full Price: {FULLPRICE}&lt;br /&gt;Full Deposit: {FULLDEPOSIT}&lt;br /&gt;&lt;br /&gt;Customer: {CUSTOMER}&lt;br /&gt;Address: {ADDRESS}&lt;br /&gt;Company: {COMPANY}&lt;br /&gt;E-mail: {EMAIL}&lt;br /&gt;Telephone: {TELEPHONE}&lt;br /&gt;Fax: {FAX}&lt;br /&gt;&lt;br /&gt;Best regards.&lt;br /&gt;&lt;br /&gt;This E-mail was generated automatically.&lt;/p&gt;';
    $mailAdmin='&lt;p&gt;New Reservation received.&lt;br /&gt;&lt;br /&gt;{OBJECTS}&lt;br /&gt;Object: {OBJECT TITLE}&lt;br /&gt;Reservation Date: {DATE}&lt;br /&gt;Quantity: {QUANTITY}&lt;br /&gt;Price: {PRICE}&lt;br /&gt;Deposit: {DEPOSIT}&lt;br /&gt;{SUPPLEMENTS}&lt;br /&gt;{/OBJECTS}&lt;br /&gt;Full Price: {FULLPRICE}&lt;br /&gt;Full Deposit: {FULLDEPOSIT}&lt;br /&gt;&lt;br /&gt;Customer: {CUSTOMER}&lt;br /&gt;Address: {ADDRESS}&lt;br /&gt;Company: {COMPANY}&lt;br /&gt;E-mail: {EMAIL}&lt;br /&gt;Telephone: {TELEPHONE}&lt;br /&gt;Fax: {FAX}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This E-mail was generated automatically.&lt;/p&gt;';
    $mailStatus='&lt;p&gt;Dear Customer,&lt;br /&gt;&lt;br /&gt;status of your reservation was  changed to {STATUS}&lt;br /&gt;&lt;br /&gt;Reservation info:&lt;br /&gt;&lt;br /&gt;{OBJECTS}&lt;br /&gt;Object: {OBJECT TITLE}&lt;br /&gt;Reservation Date: {DATE}&lt;br /&gt;Quantity: {QUANTITY}&lt;br /&gt;Price: {PRICE}&lt;br /&gt;Deposit: {DEPOSIT}&lt;br /&gt;{SUPPLEMENTS}&lt;br /&gt;{/OBJECTS}&lt;br /&gt;Full Price: {FULLPRICE}&lt;br /&gt;Full Deposit: {FULLDEPOSIT}&lt;br /&gt;&lt;br /&gt;Customer: {CUSTOMER}&lt;br /&gt;Address: {ADDRESS}&lt;br /&gt;Company: {COMPANY}&lt;br /&gt;E-mail: {EMAIL}&lt;br /&gt;Telephone: {TELEPHONE}&lt;br /&gt;Fax: {FAX}&lt;br /&gt;&lt;br /&gt;Best regards.&lt;br /&gt;&lt;br /&gt;This E-mail was generated automatically.&lt;/p&gt;';

    $changed=false;
    
    if (strpos($params->get('send_notifications_body_customer'),'{OBJECTS}')===false){
    	$params->setValue('send_notifications_body_customer', $mailCustomer);
    	$changed=true;}
    	
    if (strpos($params->get('send_notifications_body_admin'),'{OBJECTS}')===false){
    	$params->setValue('send_notifications_body_admin', $mailAdmin);
    	$changed=true;}
    	
    if (strpos($params->get('send_change_status_body_customer'),'{OBJECTS}')===false){
    	$params->setValue('send_change_status_body_customer', $mailStatus);
		$changed=true;}
		
	if ($changed){
    	$params = $params->toArray();
    	$data['params'] = $params;
    	$model = new BookingModelConfig();
    	$model->store($data);
	}
	
    unset($changed);
    
    $db = JFactory::getDBO();
    $db->setQuery("INSERT INTO `#__booking_payment` (`id`, `title`, `alias`, `state`, `params`, `checked_out`, `checked_out_time`) VALUES
(6, 'cciDEAL Platform', 'ccideal', 0, '', 0, '0000-00-00 00:00:00') ON DUPLICATE KEY UPDATE `id` = 6");
    $db->query();
}
else
	die('installer class could not be found. Db not updated.');
	
?>
