<?php

//=====================================================
// Автор: REZER (http://rezer.net)
//=====================================================
// Данный код защищен авторскими правами
//=====================================================

if( !defined( "DATALIFEENGINE" ) ) die( "Hacking attempt!" );

class dle_boardApiPlagin {
	
	public $PlaginOn = false;
	public $PlaginID = null;
	public $Options = array();
	public $BoardConfig = array();
	public $ModOn = false;
	public $TypeToName = array( "color" => "Выделение цветом", "super_vip" => "Super Vip", "vip" => "Vip" );
	public $AllowType = array();
	public $TypePrice = array();
	public $Currency = "rur";
	public $TextComplited = "";
	
	//------------------------------------------=-=-=-=-=-=-=-=-=
	//	Автоопределение настроек
	//------------------------------------------=-=-=-=-=-=-=-=-=
	
	public function __construct(){
		global $Payments;
		
		if( !file_exists( ENGINE_DIR."/inc/payments/management/init.php" ) ) return "xPay is not exists";
		if( !file_exists( ENGINE_DIR."/inc/board/board.class.php" ) ) return "DLE Board is not exists";
		if( !is_object( $Payments ) ) require( ENGINE_DIR."/inc/payments/management/init.php" );
		$this->PlaginID = $Payments->Cache->PlaginIndex['dle_board'];
		$this->Options = $Payments->Cache->Plagin[ $this->PlaginID ]['options'];
		$this->PlaginOn = $Payments->Cache->Plagin[ $this->PlaginID ]['status'] == 1 ? true : false;
		$this->LoadConfig();
	}
	
	//------------------------------------------=-=-=-=-=-=-=-=-=
	//	Парсер тегов
	//------------------------------------------=-=-=-=-=-=-=-=-=
	
	private function LoadConfig(){
		global $Board;
		
		if( is_object( $Board ) )
			{
				$this->BoardConfig = $Board->Config;	
			}
				else
			{
				if( !file_exists( ENGINE_DIR."/data/board.config.php" ) ) return "DLE Board is not exists";
				require( ENGINE_DIR."/data/board.config.php" );
				$this->BoardConfig = $BoardConfig;
			}
			
		if( $this->BoardConfig['on'] == "yes" ) $this->ModOn = true;
		if( $this->BoardConfig['echo_post_supervip'] == "auto" ) $this->AllowType[] = "super_vip";
		if( $this->BoardConfig['echo_post_vip'] == "auto" ) $this->AllowType[] = "vip";
		if( $this->BoardConfig['echo_post_color'] == "auto" ) $this->AllowType[] = "color";
		
		$this->TypePrice = array(
			"super_vip"		=> $this->BoardConfig['echo_post_supervipprice'],
			"vip"			=> $this->BoardConfig['echo_post_vipprice'],
			"color"			=> $this->BoardConfig['echo_post_colorprice'],
		);
	}
	
	public function TypePrice( $type, $day ){
		if( !in_array( $type, $this->AllowType ) ) return 0;
		if( $day < 1 ) return 0;
		if( $this->TypePrice[ $type ] <= 0 ) return 0;
		
		return $day * $this->TypePrice[ $type ];
	}
	
	public function PaymentComplited( $boardID, $Day, $Type ){
		global $db, $config, $Board;
		
		$boardID = intval( $boardID );
		if( !$boardID ) return "Не верно указан идентификатор";
		if( !in_array( $Type, $this->AllowType ) ) return "Неопознання или выключенная услуга";
		if( $Day < 1 ) return "Не правильно указан срок действия услуги";
		
		$Sec = $Day * 24 * 60 * 60;
		$result = $db->query( "SELECT `title`,`alt_name`,`end_date`,`category`,`vip_date`,`super_vip_date`,`color`,`color_date`,`no_del`,`approve` FROM ".PREFIX."_board_post WHERE `id`='{$boardID}'" );
		if( $db->num_rows( $result ) > 0 )
			{
				if( !is_object( $Board ) ) require( ENGINE_DIR."/inc/board/board.class.php" );
				
				$row = $db->get_row( $result );
				$title = stripslashes( $row['title'] );
				$alt_name = $row['alt_name'];
				$end_date = strtotime( $row['end_date'] );
				$category = $row['category'];
				$vip_date = $row['vip_date'];
				$super_vip_date = $row['super_vip_date'];
				$color_date = $row['color_date'];
				$no_del = $row['no_del'] == 1 ? 1 : 0;
				$linkBoard = $Board->ReturnLinkPost( $boardID, $alt_name, $category );
				$thisDate = time() + ( $config['date_adjust'] * 60 );
				
				switch( $Type ){
					
					case "color":
						$color_date = !$color_date ? ( $thisDate + $Sec ) : ( $color_date + $Sec );
						$color_date_echo = date( "Y-m-d H:i:s", $color_date );
						$color = $db->safesql( $this->BoardConfig['echo_post_color_hex'] );
						if( $no_del == 0 ) $updateDate = ", `end_date`='{$color_date}'";
						
						$db->query( "UPDATE ".PREFIX."_board_post SET `color_date`='{$color_date}', `color`='{$color}'{$updateDate} WHERE `id`='{$boardID}' LIMIT 1" );
						$this->TextComplited = "Услуга {$this->TypeToName[ $Type ]} успешно оплачена для объявления <a href=\"{$linkBoard}\">{$title}</a>. Услуга будет активна до {$color_date_echo}. Изменения вступят в силу в ближайшие {$this->BoardConfig['cache_time']} минут.";
						break;
						
					case "super_vip":
						$super_vip_date = !$super_vip_date ? ( $thisDate + $Sec ) : ( $super_vip_date + $Sec );
						$super_vip_date_echo = date( "Y-m-d H:i:s", $super_vip_date );
						if( $no_del == 0 ) $updateDate = ", `end_date`='{$super_vip_date_echo}'";
						
						$db->query( "UPDATE ".PREFIX."_board_post SET `super_vip_date`='{$super_vip_date}'{$updateDate} WHERE `id`='{$boardID}' LIMIT 1" );
						$this->TextComplited = "Услуга {$this->TypeToName[ $Type ]} успешно оплачена для объявления <a href=\"{$linkBoard}\">{$title}</a>. Услуга будет активна до {$super_vip_date_echo}. Изменения вступят в силу в ближайшие {$this->BoardConfig['cache_time']} минут.";
						break;
						
					case "vip":
						$vip_date = !$vip_date ? ( $thisDate + $Sec ) : ( $vip_date + $Sec );
						$vip_date_echo = date( "Y-m-d H:i:s", $vip_date );
						if( $no_del == 0 ) $updateDate = ", `end_date`='{$vip_date_echo}'";
						
						$db->query( "UPDATE ".PREFIX."_board_post SET `vip_date`='{$vip_date}'{$updateDate} WHERE `id`='{$boardID}' LIMIT 1" );
						$this->TextComplited = "Услуга {$this->TypeToName[ $Type ]} успешно оплачена для объявления <a href=\"{$linkBoard}\">{$title}</a>. Услуга будет активна до {$vip_date_echo}. Изменения вступят в силу в ближайшие {$this->BoardConfig['cache_time']} минут.";
						break;
						
					default:return "Не опознанная услуга";
				}
				
				return true;
			}
				else
			{
				return "Объявление не обнаружено";	
			}
	}
}

?>