<?php
/**
* @version	$Id: affiliate_plans_brackets_tag_processor.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 AffiliatePlansBracketsTagProcessor extends kDBTagProcessor {


		/*function Product_ListPriceBrackets($params)
		{
			return $this->PrintList2($params);
		}*/

		function Field($params)
		{
			$value = parent::Field($params);
			if ( ($params['field'] == 'FromAmount' || $params['name'] == 'ToAmount') && $value == -1)
				$value = '&infin;';
			return $value;
		}

		function ShowPricingForm($params)
		{
			/** @var kDBItem $br_object */
			$br_object = $this->Application->recallObject( $this->getPrefixSpecial() );

			$br_data = $this->Application->GetVar( $this->getPrefixSpecial(true) );
			$linked_info = $br_object->getLinkedInfo($this->Special);

			if(!$br_data)
			{
				$sql = 	'SELECT * FROM '.$br_object->TableName.' WHERE '.$linked_info['ParentTableKey'].' = '.$linked_info['ParentId'];
				$brackets = $this->Conn->Query($sql, $br_object->IDField);

				usort($brackets, 'ap_bracket_comp');

				$br_data = $brackets;
				$this->Application->SetVar($this->getPrefixSpecial(true), $brackets);
			}
			else
			{
				usort($br_data , 'ap_bracket_comp');
			}

			$ret = '';
			if( is_array($br_data) )
			{

				$block_params = $this->prepareTagParams($params);
				$block_params['IdField'] = $br_object->IDField;
				$block_params['name'] = $params['block'];
				$first = true;

				$main_object = $this->Application->recallObject($linked_info['ParentPrefix'].'.'.$this->Special);
				$plan_type = $main_object->GetDBField('PlanType');
				$limits_format = ($plan_type == 2) ? '%d' : $br_object->GetFieldOption('FromAmount', 'format');

				// this is needed to find next id
				$br_data_copy = $br_data;
				foreach($br_data as $id => $values)
				{

					foreach($values as $value_key => $value_val)
					{
						$block_params[$value_key] = $value_val;
					}
					reset($values);

					next($br_data_copy);
					$next_bracket = current($br_data_copy);

					$values['FromAmount'] = sprintf($limits_format, $values['FromAmount']);
					$values['ToAmount'] = sprintf($limits_format, $values['ToAmount']);

					$block_params['id']	= $values[$br_object->IDField];
					$block_params['min'] = ($id == -1) ? ($values['FromAmount'] ? $values['FromAmount'] : 0) : $values['FromAmount'];
					$block_params['max'] = ($values['ToAmount'] == -1) ? '&infin;' : $values['ToAmount'];
					$block_params['next_min_id'] = $next_bracket[$br_object->IDField];

					if($first)
					{
						$block_params['first'] = 1;
						$first = false;
					}
					else
					{
						$block_params['first'] = 0;
					}
					$ret .= $this->Application->ParseBlock($block_params, 1);
				}
			}
			return $ret;
		}

	}