<?
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/start.php");
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/franchise/lang/ru/include.php");
require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/franchise/tools/Mobile_Detect.php');
global $MESS; 
class FranchiseEvents{
	function OnCreatePanel()
	{
		if($GLOBALS["USER"]->IsAdmin())
		{
			if($GLOBALS["USER"]->IsAdmin())
				$GLOBALS["APPLICATION"]->AddPanelButton(array(
					"HREF"=>"/bitrix/admin/wizard_install.php?lang=ru&wizardName=bitrix:franchise&".bitrix_sessid_get(),
					"ID"=>"portal_wizard",
					'ICON' => 'bx-panel-site-wizard-icon',
					"ALT" => GetMessage('BUTTON_ALT'),
					"TEXT" => GetMessage('BUTTON_TEXT'),
					"MAIN_SORT"=>"325",
					"SORT"=>20,
					'TYPE'=>'BIG'
				));
		}
		global $APPLICATION;
		$page = $APPLICATION->GetCurPage();
		$checkType='';
		if (isset($_COOKIE['siteType'])) $checkType=$_COOKIE['siteType'];
		$detect = new Mobile_Detect();
		if ($checkType!=='original'&&$checkType!=='special'){
			if ($detect->isMobile() || $detect->isTablet())
				$checkType='pda';
		}
		if ($page==SITE_DIR."index_mobile.php"){
			if ($checkType=='original')
				LocalRedirect("/index.php");
		}
	}
	function DetectMobile(){
		global $APPLICATION;
		$checkType='';
		if (isset ($_GET['type'])){
			if ($_GET['type']=='pda' || $_GET['type']=='special' || $_GET['type']=='original')
				$checkType = $_GET['type'];
		}
		else{
			if (isset($_COOKIE['siteType']))
				$checkType=$_COOKIE['siteType'];
		}

		$page = $APPLICATION->GetCurPage();

		$detect = new Mobile_Detect();
		if ($checkType!=='original'&&$checkType!=='special'){
			if ($detect->isMobile() || $detect->isTablet())
				$checkType='pda';
		}
		setcookie('siteType', $checkType, time()+3600*24*30,'/');
		define('siteType',$checkType);
		if ($page==SITE_DIR."index_mobile.php"){
			if ($checkType=='original')
				LocalRedirect("/index.php");
		}
	}
	
	function GetNewsOneC()
	{		
		CModule::IncludeModule("iblock");
		$IBLOCK_ID=COption::GetOptionString("franchise","1c_news_iblock_id");
		$section_id=COption::GetOptionString("franchise","1c_news_section_id");
		$xmlUrl=COption::GetOptionString("franchise","1c_news_url", "http://www.1c.ru/news/rss-2.0.jsp");
		$arTranslitSection = array(
						"max_len" => '100',
						"change_case" => 'L',
						"replace_space" => "_",
						"replace_other" => "_",
						"delete_repeat_replace" => 'Y',
						"use_google" => 'N',
					);
		if($IBLOCK_ID && $section_id && $xmlUrl)
		{
			$xmlNews = simplexml_load_file($xmlUrl);
			foreach($xmlNews->channel->item as $item)
			{				
				$arSelect = Array("ID");
				$arFilter = Array("IBLOCK_ID"=>$IBLOCK_ID, "PROPERTY_ORIGINAL_URL"=>$item->link);
				$resNews = CIBlockElement::GetList(Array(), $arFilter, false, false, $arSelect);
				if($arNews = $resNews->GetNext())
				{
					//echo $obBlog['ID'];
					break;
				}
				$el = new CIBlockElement;	
				$dt = FranchiseEvents::date2sql($item->pubDate);
				if(defined("BX_UTF"))
					$title=$item->title;
				else
					$title=iconv('UTF-8','windows-1251', $item->title);
				//$tmp_desc=strip_tags($item->description);
				$arLoadProductArray = Array(					   
					  "IBLOCK_SECTION_ID" => $section_id,          // элемент лежит в корне раздела
					  "IBLOCK_ID"      => $IBLOCK_ID,					  
					  "NAME"           => $title,
					  "ACTIVE"         => "Y",            // активен
					  "DATE_ACTIVE_FROM" => $dt,
					  "CODE"=>$propCode=CUtil::translit($title, 'ru', $arTranslitSection),
					  "PROPERTY_VALUES"	=>array("ORIGINAL_URL"=>strval($item->link)),				  		  
				);					
				$PRODUCT_ID = $el->Add($arLoadProductArray);
				echo "Error: ".$el->LAST_ERROR;			
			}
		}
		return 'FranchiseEvents::GetNewsOneC()';
	}
	
	function date2sql($str = "")
	{			
		$tmst=strtotime($str);		
		return ConvertTimeStamp($tmst, "FULL", "ru");
	}
}
?>