<?
class WizardServices
{
	function GetTemplates($relativePath,$arWizardTemplates = Array())
	{
		$absolutePath = $_SERVER["DOCUMENT_ROOT"].$relativePath;
		$absolutePath = str_replace("\\", "/", $absolutePath);

		if (!$handle  = @opendir($absolutePath))
			return $arWizardTemplates;

		while(($dirName = @readdir($handle)) !== false)
		{
			if ($dirName == "." || $dirName == ".." || !is_dir($absolutePath."/".$dirName)) 
				continue;

			$arTemplate = Array(
				"DESCRIPTION"=>"",
				"NAME" => $dirName,
			);

			if (file_exists($absolutePath."/".$dirName."/description.php"))
			{
				if (LANGUAGE_ID != "en" && LANGUAGE_ID != "ru")
				{
					if (file_exists($absolutePath."/".$dirName."/lang/en/description.php"))
						__IncludeLang($absolutePath."/".$dirName."/lang/en/description.php");
				}

				if (file_exists($absolutePath."/".$dirName."/lang/".LANGUAGE_ID."/description.php"))
						__IncludeLang($absolutePath."/".$dirName."/lang/".LANGUAGE_ID."/description.php");

				include($absolutePath."/".$dirName."/description.php");
			}

			$arTemplate["ID"] = $dirName;
			if (isset($arTemplate["SORT"]) && intval($arTemplate["SORT"]) > 0)
				$arTemplate["SORT"] = intval($arTemplate["SORT"]);
			else
				$arTemplate["SORT"] = 0;

			if (file_exists($absolutePath."/".$dirName."/screen.png"))
				$arTemplate["SCREENSHOT"] = $relativePath."/".$dirName."/screen.png";
			else
				$arTemplate["SCREENSHOT"] = false;

			if (file_exists($absolutePath."/".$dirName."/preview.png"))
				$arTemplate["PREVIEW"] = $relativePath."/".$dirName."/preview.png";
			elseif (file_exists($absolutePath."/".$dirName."/preview.gif"))
				$arTemplate["PREVIEW"] = $relativePath."/".$dirName."/preview.gif";
			else
				$arTemplate["PREVIEW"] = false;

			if (!isset($arWizardTemplates[$arTemplate["ID"]]))
				$arWizardTemplates[$arTemplate["ID"]] = $arTemplate;
			else {
				$arWizardTemplates[$arTemplate["ID"]]["SORT"] = (isset($arTemplate["SORT"]) && intval($arTemplate["SORT"]) > 0 ? intval($arTemplate["SORT"]) : $arWizardTemplates[$arTemplate["ID"]]["SORT"]);
				$arWizardTemplates[$arTemplate["ID"]]["SCREENSHOT"] = (!empty($arTemplate["SCREENSHOT"]) ? $arTemplate["SCREENSHOT"] : $arWizardTemplates[$arTemplate["ID"]]["SCREENSHOT"]);
				$arWizardTemplates[$arTemplate["ID"]]["PREVIEW"] = (!empty($arTemplate["PREVIEW"]) ? $arTemplate["PREVIEW"] : $arWizardTemplates[$arTemplate["ID"]]["PREVIEW"]);
				$arWizardTemplates[$arTemplate["ID"]]["DESCRIPTION"] = (!empty($arTemplate["DESCRIPTION"]) ? $arTemplate["DESCRIPTION"] : $arWizardTemplates[$arTemplate["ID"]]["DESCRIPTION"]);
				$arWizardTemplates[$arTemplate["ID"]]["NAME"] = ($arTemplate["NAME"]!=$arTemplate["ID"] ? $arTemplate["NAME"] : $arWizardTemplates[$arTemplate["ID"]]["NAME"]);
			}
		}

		closedir($handle);
		uasort($arWizardTemplates, create_function('$a, $b', 'return $a["SORT"] > $b["SORT"];'));
		return $arWizardTemplates;
	}

	function GetTemplatesPath($path,$common=false)
	{
		if (WIZARD_TRADE)
			$templatesPath = $path."/templates_biz";
		else
			$templatesPath = $path."/templates_std";

		if (file_exists($_SERVER["DOCUMENT_ROOT"].$templatesPath."/".LANGUAGE_ID))
			$templatesPath .= "/".LANGUAGE_ID;

		if ($common) {
			$arTemplatesPath=array();
			$arTemplatesPath['self']=$templatesPath;
			$arTemplatesPath['common'] = $path."/templates_common";
			if (file_exists($_SERVER["DOCUMENT_ROOT"].$templatesPath['common']."/".LANGUAGE_ID))
				$templatesPath['common'] .= "/".LANGUAGE_ID;
			return $arTemplatesPath;
		}

		return $templatesPath;
	}

	function GetServices($wizardPath, $serviceFolder = "", $arFilter = Array(), $sitePath = "/")
	{
		$arServices = Array();
		$sitePath=$_SERVER["DOCUMENT_ROOT"].$sitePath;
		$wizardPath = rtrim($wizardPath, "/");
		$serviceFolder = rtrim($serviceFolder, "/");
		$servicePath = $wizardPath."/".$serviceFolder;
		
		if (LANGUAGE_ID != "en" && LANGUAGE_ID != "ru")
		{
			if (file_exists($wizardPath."/lang/en".$serviceFolder."/.services.php"))
				__IncludeLang($wizardPath."/lang/en".$serviceFolder."/.services.php");
		}
		if (file_exists($wizardPath."/lang/".LANGUAGE_ID.$serviceFolder."/.services.php"))
			__IncludeLang($wizardPath."/lang/".LANGUAGE_ID.$serviceFolder."/.services.php");
		
		include($servicePath."/.services.php");
		
		if (empty($arServices))
			return $arServices;
		
		
		$servicePosition = 1;
		foreach ($arServices as $serviceID => $arService)
		{
			if (isset($arFilter["SKIP_INSTALL_ONLY"]) && array_key_exists("INSTALL_ONLY", $arService) && $arService["INSTALL_ONLY"] == $arFilter["SKIP_INSTALL_ONLY"])
			{
				unset($arServices[$serviceID]);
				continue;
			}
						
			if (isset($arFilter["SHOW_IN_FORM"]) && $arService["IN_FORM"] != $arFilter["SHOW_IN_FORM"])
			{				
				unset($arServices[$serviceID]);
				continue;
			}

			if (isset($arFilter["SERVICES"]) && is_array($arFilter["SERVICES"]) && !in_array($serviceID, $arFilter["SERVICES"]) && $arService["IN_FORM"]=="Y")
			{
				unset($arServices[$serviceID]);
				continue;
			}
			
			if($arService["CHECK_FILE"])
			{				
				if (file_exists($sitePath.$arService["CHECK_FILE"]))
				{
					unset($arServices[$serviceID]);
					continue;
				}
			}
			
			//Check service dependencies
			$modulesCheck = Array();
			if (array_key_exists("MODULE_ID", $arService))
				$modulesCheck = (is_array($arService["MODULE_ID"]) ? $arService["MODULE_ID"] : Array($arService["MODULE_ID"]));
			
			foreach ($modulesCheck as $moduleID)
			{
				if (!IsModuleInstalled($moduleID))
				{
					unset($arServices[$serviceID]);
					continue 2;
				}
			}
			
			$arServices[$serviceID]["POSITION"] = $servicePosition;
			$servicePosition += (isset($arService["STAGES"]) && !empty($arService["STAGES"]) ? count($arService["STAGES"]) : 1);
		}

		return $arServices;
	}

	function IncludeServiceLang($relativePath, $lang = false, $bReturnArray = false)
	{
		if($lang === false)
			$lang = LANGUAGE_ID;

		$arMessages = Array();
		if ($lang != "en" && $lang != "ru")
		{
			if (file_exists(WIZARD_SERVICE_ABSOLUTE_PATH."/lang/en/".$relativePath))
			{
				if ($bReturnArray)
					$arMessages = __IncludeLang(WIZARD_SERVICE_ABSOLUTE_PATH."/lang/en/".$relativePath, true);
				else
					__IncludeLang(WIZARD_SERVICE_ABSOLUTE_PATH."/lang/en/".$relativePath);
			}
		}

		if (file_exists(WIZARD_SERVICE_ABSOLUTE_PATH."/lang/".$lang."/".$relativePath))
		{
			if ($bReturnArray)
				$arMessages = array_merge($arMessages, __IncludeLang(WIZARD_SERVICE_ABSOLUTE_PATH."/lang/".$lang."/".$relativePath, true));
			else
				__IncludeLang(WIZARD_SERVICE_ABSOLUTE_PATH."/lang/".$lang."/".$relativePath);
		}

		return $arMessages;
	}

	function GetCurrentSiteID($selectedSiteID = null)
	{
		if (strlen($selectedSiteID) > 0)
		{
			$obSite = CSite::GetList($by = "def", $order = "desc", Array("LID" => $selectedSiteID));
			if (!$arSite = $obSite->Fetch())
				$selectedSiteID = null;
		}

		$currentSiteID = $selectedSiteID;
		if ($currentSiteID == null)
		{
			$currentSiteID = SITE_ID;
			if (defined("ADMIN_SECTION"))
			{
				$obSite = CSite::GetList($by = "def", $order = "desc", Array("ACTIVE" => "Y"));
				if ($arSite = $obSite->Fetch())
					$currentSiteID = $arSite["LID"];
			}
		}
		return $currentSiteID;
	}

	function GetThemes($relativePath,$arThemes = Array())
	{
		if (!is_dir($_SERVER["DOCUMENT_ROOT"].$relativePath))
			return $arThemes;

		$themePath = $_SERVER["DOCUMENT_ROOT"].$relativePath;
		$themePath = str_replace("\\", "/", $themePath);

		if ($handle = @opendir($themePath))
		{
			while (($file = readdir($handle)) !== false)
			{
				if ($file == "." || $file == ".." || !is_dir($themePath."/".$file))
					continue;

				$arTemplate = Array();
				if (is_file($themePath."/".$file."/description.php"))
				{
					if (LANGUAGE_ID != "en" && LANGUAGE_ID != "ru")
					{
						if (file_exists($themePath."/".$file."/lang/en/description.php"))
							__IncludeLang($themePath."/".$file."/lang/en/description.php");
					}

					if (file_exists($themePath."/".$file."/lang/".LANGUAGE_ID."/description.php"))
							__IncludeLang($themePath."/".$file."/lang/".LANGUAGE_ID."/description.php");

					@include($themePath."/".$file."/description.php");
				}
				if (!isset ($arThemes[$file])) {
					$arThemes[$file] = $arTemplate + Array(
						"ID" => $file,
						"SORT" => (isset($arTemplate["SORT"]) && intval($arTemplate["SORT"]) > 0 ? intval($arTemplate["SORT"]) : 10),
						"NAME" => (isset($arTemplate["NAME"]) ? $arTemplate["NAME"] : $file),
						"SCREENSHOT" => (file_exists($themePath."/".$file."/screen.png") ? $relativePath."/".$file."/screen.png" : false),
					);
					if (file_exists($themePath."/".$file."/preview.png"))
						$arThemes[$file]["PREVIEW"] = $relativePath."/".$file."/preview.png";
					elseif (file_exists($themePath."/".$file."/preview.gif"))
						$arThemes[$file]["PREVIEW"] = $relativePath."/".$file."/preview.gif";
					else
						$arThemes[$file]["PREVIEW"] = false;
				}
				else {
						$arThemes[$file]["SORT"] = (isset($arTemplate["SORT"]) && intval($arTemplate["SORT"]) > 0 ? intval($arTemplate["SORT"]) : $arThemes[$file]["SORT"]);
						$arThemes[$file]["NAME"] = (isset($arTemplate["NAME"]) ? $arTemplate["NAME"] : $arThemes[$file]["NAME"]);
						$arThemes[$file]["DESCRIPTION"] = (isset($arTemplate["DESCRIPTION"]) ? $arTemplate["DESCRIPTION"] : $arThemes[$file]["DESCRIPTION"]);
						if (file_exists($themePath."/".$file."/preview.png"))
							$arThemes[$file]["PREVIEW"] = $relativePath."/".$file."/preview.png";
						elseif (file_exists($themePath."/".$file."/preview.gif"))
							$arThemes[$file]["PREVIEW"] = $relativePath."/".$file."/preview.gif";
						$arThemes[$file]["SCREENSHOT"] = (file_exists($themePath."/".$file."/screen.png") ? $relativePath."/".$file."/screen.png" : $arThemes[$file]["SCREENSHOT"]);
				}

			}
			@closedir($handle);
		}

		uasort($arThemes, create_function('$a, $b', 'return strcmp($a["SORT"], $b["SORT"]);'));
		return $arThemes;
	}

	function SetFilePermission($path, $permissions)
	{
		$originalPath = $path;

		CMain::InitPathVars($site, $path);
		$documentRoot = CSite::GetSiteDocRoot($site);

		$path = rtrim($path, "/");

		if (strlen($path) <= 0)
			$path = "/";

		if( ($position = strrpos($path, "/")) !== false)
		{
			$pathFile = substr($path, $position+1);
			$pathDir = substr($path, 0, $position);
		}
		else
			return false;

		if ($pathFile == "" && $pathDir == "")
			$pathFile = "/";

		$PERM = Array();
		if(file_exists($documentRoot.$pathDir."/.access.php"))
			@include($documentRoot.$pathDir."/.access.php");

		if (!isset($PERM[$pathFile]) || !is_array($PERM[$pathFile]))
			$arPermisson = $permissions;
		else
			$arPermisson = $permissions + $PERM[$pathFile];

		return $GLOBALS["APPLICATION"]->SetFileAccessPermission($originalPath, $arPermisson);
	}

	function AddMenuItem($menuFile, $menuItem,  $siteID)
	{
		if (CModule::IncludeModule('fileman'))
		{
			$arResult = CFileMan::GetMenuArray($_SERVER["DOCUMENT_ROOT"].$menuFile);
			$arMenuItems = $arResult["aMenuLinks"];
			$menuTemplate = $arResult["sMenuTemplate"];

			$bFound = false;
			foreach($arMenuItems as $item)
				if($item[1] == $menuItem[1])
					$bFound = true;

			if(!$bFound)
			{
				$arMenuItems[] = $menuItem;
				CFileMan::SaveMenu(Array($siteID, $menuFile), $arMenuItems, $menuTemplate);
			}
		}
	}

	
	function ImportIBlockFromXML($xmlFile, $iblockCode, $iblockType, $siteID, $permissions = Array(),$upd=false)
	{
		if (!CModule::IncludeModule('iblock'))
			return false;

		if (!$upd) {
		$rsIBlock = CIBlock::GetList(array(), array('SITE_ID' => $siteID, 'CODE' => $iblockCode, 'TYPE' => $iblockType));
		if ($arIBlock = $rsIBlock->Fetch())
			return false;
		}

		if (!is_array($siteID))
			$siteID = Array($siteID);

		require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/iblock/classes/'.strtolower($GLOBALS['DB']->type).'/cml2.php');
		$resEr=ImportXMLFile($xmlFile, $iblockType, $siteID, $section_action = 'N', $element_action = 'N',false,false,false,true);
				
		$iblockID = false;
		$rsIBlock = CIBlock::GetList(array(), array('SITE_ID' => $siteID, 'CODE' => $iblockCode, 'TYPE' => $iblockType));
		if ($arIBlock = $rsIBlock->Fetch())
		{
			$iblockID = $arIBlock['ID'];

			if (empty($permissions))
				$permissions = Array(1 => 'X', 2 => 'R');

			CIBlock::SetPermission($iblockID, $permissions);
		}

		return $iblockID;
	}


	function SetIBlockFormSettings($iblockID, $settings)
	{
		global $DBType;
		require_once($_SERVER['DOCUMENT_ROOT']."/bitrix/modules/main/classes/".strtolower($DBType)."/favorites.php");

		CUserOptions::SetOption(
			"form", 
			"form_element_".$iblockID,
			$settings,
			$common = true
		);
	}

	function SetUserOption($category, $option, $settings, $common = false, $userID = false)
	{
		global $DBType;
		require_once($_SERVER['DOCUMENT_ROOT']."/bitrix/modules/main/classes/".strtolower($DBType)."/favorites.php");

		CUserOptions::SetOption(
			$category, 
			$option, 
			$settings, 
			$common, 
			$userID
		);
	}

	function CreateSectionProperty($iblockID, $fieldCode, $arFieldName = Array())
	{
		$entityID = "IBLOCK_".$iblockID."_SECTION";
		
		$dbField = CUserTypeEntity::GetList(Array(), array("ENTITY_ID" => $entityID, "FIELD_NAME" => $fieldCode));
		if ($arField = $dbField->Fetch())
			return $arField["ID"];

		$arFields = Array(
			"ENTITY_ID" => $entityID,
			"FIELD_NAME" => $fieldCode,
			"USER_TYPE_ID" => "string",
			"MULTIPLE" => "N",
			"MANDATORY" => "N",
			"EDIT_FORM_LABEL" => $arFieldName
		);

		$obUserField = new CUserTypeEntity;
		$fieldID = $obUserField->Add($arFields);
		$GLOBALS["USER_FIELD_MANAGER"]->arFieldsCache = array();
		return $fieldID;
	}
	
	function MySetMailTemplate($WEB_FORM_ID, $ADD_NEW_TEMPLATE="Y", $old_SID="", $bReturnFullInfo = false)
        {
		if(!CModule::IncludeModule("form"))
			return;
                global $DB, $MESS, $strError;
                $err_mess = (CAllForm::err_mess())."<br>Function: SetMailTemplates<br>Line: ";
                $arrReturn = array();
                $WEB_FORM_ID = intval($WEB_FORM_ID);
                $q = CForm::GetByID($WEB_FORM_ID);
                if ($arrForm = $q->Fetch())
                {
                        $MAIL_EVENT_TYPE = "FORM_FILLING_".$arrForm["SID"];
                        if (strlen($old_SID)>0) $old_MAIL_EVENT_TYPE = "FORM_FILLING_".$old_SID;

                        $et = new CEventType;
                        $em = new CEventMessage;

                        if (strlen($MAIL_EVENT_TYPE)>0)
                                $et->Delete($MAIL_EVENT_TYPE);

                        $z = CLanguage::GetList($v1, $v2);
                        $OLD_MESS = $MESS;
                        $MESS = array();
                        while ($arLang = $z->Fetch())
                        {
                                IncludeModuleLangFile($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/form/admin/form_mail.php", $arLang["LID"]);

                                $str = "";
                                $str .= "#RS_FORM_ID# - ".GetMessage("FORM_L_FORM_ID")."\n";
                                $str .= "#RS_FORM_NAME# - ".GetMessage("FORM_L_NAME")."\n";
                                $str .= "#RS_FORM_SID# - ".GetMessage("FORM_L_SID")."\n";
                                $str .= "#RS_RESULT_ID# - ".GetMessage("FORM_L_RESULT_ID")."\n";
                                $str .= "#RS_DATE_CREATE# - ".GetMessage("FORM_L_DATE_CREATE")."\n";
                                $str .= "#RS_USER_ID# - ".GetMessage("FORM_L_USER_ID")."\n";
                                $str .= "#RS_USER_EMAIL# - ".GetMessage("FORM_L_USER_EMAIL")."\n";
                                $str .= "#RS_USER_NAME# - ".GetMessage("FORM_L_USER_NAME")."\n";
                                $str .= "#RS_USER_AUTH# - ".GetMessage("FORM_L_USER_AUTH")."\n";
                                $str .= "#RS_STAT_GUEST_ID# - ".GetMessage("FORM_L_STAT_GUEST_ID")."\n";
                                $str .= "#RS_STAT_SESSION_ID# - ".GetMessage("FORM_L_STAT_SESSION_ID")."\n";

                                $strFIELDS = "";
                                $w = CFormField::GetList($WEB_FORM_ID,"ALL", $by, $order, array(), $is_filtered);
                                while ($wr=$w->Fetch())
                                {
                                        if (strlen($wr["RESULTS_TABLE_TITLE"])>0)
                                        {
                                                $FIELD_TITLE = $wr["RESULTS_TABLE_TITLE"];
                                        }
                                        elseif (strlen($wr["TITLE"])>0)
                                        {
                                                $FIELD_TITLE = $wr["TITLE_TYPE"]=="html" ? htmlspecialcharsback(strip_tags($wr["TITLE"])) : $wr["TITLE"];
                                        }
                                        else
                                        {
                                                $FIELD_TITLE = TrimEx($wr["FILTER_TITLE"],":");
                                        }

                                        $str .= "#".$wr["SID"]."# - ".$FIELD_TITLE."\n";
                                        $strFIELDS .= $FIELD_TITLE."\n*******************************\n#".$wr["SID"]."#\n\n";
                                }
                                $et->Add(
                                                Array(
                                                "LID"                   => $arLang["LID"],
                                                "EVENT_NAME"    => $MAIL_EVENT_TYPE,
                                                "NAME"                  => GetMessage("FORM_FILLING")." \"".$arrForm["SID"]."\"",
                                                "DESCRIPTION"   => $str
                                                )
                                        );
                        }
                        // задаем новый тип события для старых шаблонов
                        if (strlen($old_MAIL_EVENT_TYPE)>0 && $old_MAIL_EVENT_TYPE!=$MAIL_EVENT_TYPE)
                        {
                                $e = $em->GetList($by="id",$order="desc",array("EVENT_NAME"=>$old_MAIL_EVENT_TYPE));
                                while ($er=$e->Fetch())
                                {
                                        $em->Update($er["ID"],array("EVENT_NAME"=>$MAIL_EVENT_TYPE));
                                }
                                if (strlen($old_MAIL_EVENT_TYPE)>0)
                                        $et->Delete($old_MAIL_EVENT_TYPE);
                        }

                        if ($ADD_NEW_TEMPLATE=="Y")
                        {
                                $z = CSite::GetList($v1, $v2);
                                while ($arSite = $z->Fetch()) $arrSiteLang[$arSite["ID"]] = $arSite["LANGUAGE_ID"];

                                $arrFormSite = CForm::GetSiteArray($WEB_FORM_ID);
                                if (is_array($arrFormSite) && count($arrFormSite)>0)
                                {
                                        foreach($arrFormSite as $sid)
                                        {
                                                IncludeModuleLangFile($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/form/admin/form_mail.php", $arrSiteLang[$sid]);

//                                                $SUBJECT = "#SERVER_NAME#: ".GetMessage("FORM_ZAYAVKA");
                                                $SUBJECT = "#SERVER_NAME#: ".$arrForm["NAME"];
                                                $MESSAGE = "$strFIELDS
".GetMessage("FORM_VIEW")."
http://#SERVER_NAME#/bitrix/admin/form_result_view.php?lang=".$arrSiteLang[$sid]."&WEB_FORM_ID=#RS_FORM_ID#&RESULT_ID=#RS_RESULT_ID#

-------------------------------------------------------
".GetMessage("FORM_GENERATED_AUTOMATICALLY")."
                                                ";
                                                // добавляем новый шаблон
                                                $arFields = Array(
                                                        "ACTIVE"                => "Y",
                                                        "EVENT_NAME"    => $MAIL_EVENT_TYPE,
                                                        "LID"                   => $sid,
                                                        "EMAIL_FROM"    => "#DEFAULT_EMAIL_FROM#",
                                                        "EMAIL_TO"              => "#DEFAULT_EMAIL_FROM#",
                                                        "SUBJECT"               => $SUBJECT,
                                                        "MESSAGE"               => $MESSAGE,
                                                        "BODY_TYPE"             => "text"
                                                        );
                                                $TEMPLATE_ID = $em->Add($arFields);
                                                if ($bReturnFullInfo)
                                                        $arrReturn[] = array(
                                                                'ID' => $TEMPLATE_ID,
                                                                'FIELDS' => $arFields,
                                                        );
                                                else
                                                        $arrReturn[] = $TEMPLATE_ID;
                                        }
                                }
                        }
                        $MESS = $OLD_MESS;
                }
                return $arrReturn;
        }
        
        function printArray($array, $tabFirst, $tabToAdd, $depLevel = 0)
	{		
		if($depLevel==0)
		{
			$resStr=$tabFirst."Array(\n";
			$tabFirst.=$tabToAdd;			
		}
		else
			$resStr="";
		foreach($array as $key => $value)
		{
			 if(is_array($value))
	                {
	                        $resStr.=$tabFirst."'".$key."' => array(\n".WizardServices::printArray($value, $tabFirst.$tabToAdd, $tabToAdd, $depLevel+1);
	                }
	                else
	                {
	                      $resStr.=$tabFirst."'".$key."' => '".$value."',\n";	                       
	                }
		}
		$resStr.=$tabFirst."),\n";
		return $resStr;
	}
}
?>