<?php
/**
* @version	$Id: payment_type_currencies_event_handler.php 16516 2017-01-20 14:12:22Z alex $
* @package	In-Commerce
* @copyright	Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license	Commercial License
* This software is protected by copyright law and international treaties.
* Unauthorized reproduction or unlicensed usage of the code of this program,
* or any portion of it may result in severe civil and criminal penalties,
* and will be prosecuted to the maximum extent possible under the law
* See http://www.in-portal.org/commercial-license for copyright notices and details.
*/

	defined('FULL_PATH') or die('restricted access!');

	class PaymentTypeCurrenciesEventHandler extends kDBEventHandler {

		/**
		 * Creates new kDBItem
		 *
		 * @param kEvent $event
		 * @return void
		 * @access protected
		 */
		protected function OnCreate(kEvent $event)
		{
			$currency_id_list = $this->Application->GetVar('currency_list');

			if ( !$currency_id_list ) {
				return;
			}

			/** @var kDBItem $object */
			$object = $event->getObject(Array ('skip_autoload' => true));

			$pt_id = $this->Application->GetVar('pt_id');

			if ( $pt_id === false ) {
				return;
			}

			$sql = 'DELETE FROM ' . $object->TableName . '
					WHERE PaymentTypeId = ' . $pt_id;
			$this->Conn->Query($sql);

			foreach ($currency_id_list as $id) {
				$object->SetDBField('CurrencyId', $id);
				$object->SetDBField('PaymentTypeId', $pt_id);
				$this->customProcessing($event, 'before');

				if ( $object->Create() ) {
					$this->customProcessing($event, 'after');
					$event->status = kEvent::erSUCCESS;
				}
				else {
					$event->status = kEvent::erFAIL;
					$event->redirect = false;
					$this->Application->SetVar($event->getPrefixSpecial() . '_SaveEvent', 'OnCreate');
					$object->setID(0);
				}
			}
		}
	}