<?php
/**
* @version	$Id: visits_tag_processor.php 12734 2009-10-20 19:28:11Z alex $
* @package	In-Portal
* @copyright	Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license      GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/

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

	class VisitsTagProcessor extends kDBTagProcessor {

		function UserFound($params)
		{
			$virtual_users = Array(-1,-2, 0);
			$object =& $this->Application->recallObject( $this->getPrefixSpecial(), $this->Prefix, $params );
			return !in_array( $object->GetDBField( $params['user_field'] ) , $virtual_users );
		}


		function UserLink($params)
		{
			$object =& $this->Application->recallObject( $this->getPrefixSpecial(), $this->Prefix, $params );
			$user_id = $object->GetDBField( $params['user_field'] );

			if ($user_id) {
				$url_params =  Array (
					'm_opener' => 'd',
					'u_mode' => 't',
					'u_event' => 'OnEdit',
					'u_id' => $user_id,
					'pass' => 'all,u'
				);

				return $this->Application->HREF($params['edit_template'], '', $url_params);
			}
		}

		function getDateLimitClause($field)
		{
			$search_filter = $this->Application->RecallVar( $this->getPrefixSpecial().'_search_filter');
			if($search_filter)
			{
				$search_filter = unserialize($search_filter);
				return $search_filter[$field]['value'];
			}
			return '';
		}

		function AffiliateOrderInfo($params)
		{
			$list =& $this->GetList($params);

			$date_limit = str_replace($list->TableName, 'vis', $this->getDateLimitClause('VisitDate') );

			$affil_table = $this->Application->getUnitOption('affil', 'TableName');
			$affil_idfield = $this->Application->getUnitOption('affil', 'IDField');
			$sql = 'SELECT '.$affil_idfield.' FROM '.$affil_table.' WHERE PortalUserId = '.$this->Application->RecallVar('user_id');
			$affiliate_id = $this->Conn->GetOne($sql);

			$sql = 'SELECT COUNT(ord.OrderId) AS OrderCount
					FROM '.$list->TableName.' vis
					LEFT JOIN '.TABLE_PREFIX.'Orders ord ON ord.VisitId = vis.VisitId
					WHERE (vis.AffiliateId = '.$affiliate_id.') AND (ord.Status = '.ORDER_STATUS_PROCESSED.')'.($date_limit ? ' AND '.$date_limit : '');

			$result = $this->Conn->GetRow($sql);

			$sql = 'SELECT COUNT(*) FROM '.$list->TableName.' vis
					WHERE AffiliateId = '.$affiliate_id.($date_limit ? ' AND '.$date_limit : '');

			$result['TotalVisitors'] = $this->Conn->GetOne($sql);
			$result['OrderTotalAmount'] = $list->getTotal('OrderTotalAmount', 'SUM');
			$result['OrderAffiliateCommission'] = $list->getTotal('OrderAffiliateCommission', 'SUM');

			$block_params = $this->prepareTagParams($params);
			$block_params['name'] = $params['render_as'];

			$format_fields = Array('OrderTotalAmount', 'OrderAffiliateCommission');

			if (array_key_exists('currency', $params) && $params['currency']) {
				$iso = $this->GetISO($params['currency']);
				foreach($format_fields as $format_field)
				{
					$value =  sprintf($list->Fields[$format_field]['format'], $result[$format_field]);
					$value = $this->ConvertCurrency($value, $iso);
					$value = $this->AddCurrencySymbol($value, $iso);
					$result[$format_field] = $value;
				}
			}

			$block_params = array_merge_recursive2($block_params, $result);

			return $this->Application->ParseBlock($block_params);
		}


		/*function ShowTotals($params)
		{
			$list =& $this->GetList($params);
			$block_params = $this->prepareTagParams($params);
			$block_params = array_merge_recursive2($block_params, $list->Totals);
			$block_params['name'] = $this->SelectParam($params, 'render_as,block');
			return $this->Application->ParseBlock($block_params);
		}*/

		function ListVisitors($params)
		{
			$o = '';

			$params['render_as'] = $params['item_render_as'];

			$o_visitors = $this->PrintList2($params);

			if($o_visitors)
			{
				$header = '';
				$footer = '';

				$block_params = $this->prepareTagParams($params);

				$header_block = getArrayValue($params, 'header_render_as');
				if($header_block)
				{
					$block_params['name'] = $header_block;
					$header = $this->Application->ParseBlock($block_params);
				}

				$footer_block = getArrayValue($params, 'footer_render_as');
				if($footer_block)
				{
					$block_params['name'] = $footer_block;
					$footer = $this->Application->ParseBlock($block_params);
				}

				$o = $header.$o_visitors.$footer;
			}
			else
			{
				$visitors_params = array('name' => $params['empty_myvisitors_render_as']);
				$o = $this->Application->ParseBlock($visitors_params);
			}

			return $o;

		}
		/*
		function OrderLink($params){
			$object = &$this->Application->recallObject($this->getPrefixSpecial());
			$params['ord_id'] = $object->GetDBField('OrderId');
			$main_processor =& $this->Application->recallObject('m_TagProcessor');
			return $main_processor->T($params);
		}
		*/

		/**
		 * Enter description here...
		 *
		 * @param unknown_type $params
		 * @return kDBList
		 */

		function &GetList($params)
		{
			$list_name = $this->SelectParam($params, 'list_name,name');
			if (!$list_name) {
				$list_name = $this->Application->Parser->GetParam('list_name');
			}
			$types = $this->SelectParam($params, 'types');
			$special='';
			if ($types=='myvisitororders' || $types=='myvisitors'){
				$special = 'incommerce';
				$names_mapping  = $this->Application->GetVar('NamesToSpecialMapping');
				$names_mapping[$this->Prefix][$list_name] = $special;
				$this->Application->SetVar('NamesToSpecialMapping', $names_mapping);

			}

			return parent::GetList($params);
		}

	}