diff --git a/intaro.intarocrm/classes/general/ICMLLoader.php b/intaro.intarocrm/classes/general/ICMLLoader.php
new file mode 100644
index 00000000..c2ee5c29
--- /dev/null
+++ b/intaro.intarocrm/classes/general/ICMLLoader.php
@@ -0,0 +1,331 @@
+iblocks = json_decode($iblocks, true);
+ $loader->filename = $filename;
+ $loader->application = $APPLICATION;
+ $loader->Load();
+ $arIblocks = "'" . $iblocks . "'";
+ $arFilename = "'" . $filename . "'";
+ return "ICMLLoader::AgentLoad(" . $arIblocks . ", " . $arFilename . ");";
+ }
+ public function Load()
+ {
+ $categories = $this->GetCategories();
+
+ $offers = $this->GetOffers();
+
+ /*foreach ($offers as $obj)
+ if (is_array($obj))
+ foreach ($obj as $obj2)
+ print(htmlspecialcharsbx($obj2) . "
");
+ else
+ print(htmlspecialcharsbx($obj) . "
");
+ */
+
+
+ $this->PrepareFile();
+
+ $this->PreWriteCatalog();
+
+ $this->WriteCategories($categories);
+ $this->WriteOffers($offers);
+
+ $this->PostWriteCatalog();
+
+ $this->CloseFile();
+
+ }
+
+ protected function PrepareValue($text)
+ {
+
+ //$text = htmlspecialcharsbx($text);
+ //$text = str_replace('"', '"', $text);
+ //$text = preg_replace("/[\x1-\x8\xB-\xC\xE-\x1F]/", "", $text);
+ //$text = str_replace("'", "'", $text);
+ $text = $this->application->ConvertCharset($text, LANG_CHARSET, $this->encoding);
+ return $text;
+ }
+
+ protected function PrepareFile()
+ {
+ $fullFilename = $_SERVER["DOCUMENT_ROOT"] . $this->filename;
+ CheckDirPath($fullFilename);
+
+ if (!$this->fp = @fopen($fullFilename, "w"))
+ return false;
+ else
+ return true;
+ }
+
+ protected function PreWriteCatalog()
+ {
+ @fwrite($this->fp, "\n");
+ @fwrite($this->fp, "\n");
+
+ @fwrite($this->fp, "".$this->application->ConvertCharset(htmlspecialcharsbx(COption::GetOptionString("main", "site_name", "")), LANG_CHARSET, $encoding)."\n");
+
+ @fwrite($this->fp, "".$this->application->ConvertCharset(htmlspecialcharsbx(COption::GetOptionString("main", "site_name", "")), LANG_CHARSET, $encoding)."\n");
+
+ }
+
+ protected function WriteCategories($categories)
+ {
+ @fwrite($this->fp, "\n");
+ foreach ($categories as $category) {
+ @fwrite($this->fp, $category . "\n");
+ }
+ @fwrite($this->fp, "\n");
+ }
+ protected function WriteOffers($offers)
+ {
+ @fwrite($this->fp, "\n");
+ foreach ($offers as $offer) {
+ @fwrite($this->fp, $offer . "\n");
+ }
+ @fwrite($this->fp, "\n");
+ }
+
+ protected function PostWriteCatalog()
+ {
+ @fwrite($this->fp, "\n");
+ @fwrite($this->fp, "\n");
+ }
+
+ protected function CloseFile()
+ {
+ @fclose($this->fp);
+ }
+
+
+ protected function GetCategories()
+ {
+ $categories = array();
+ foreach ($this->iblocks as $id)
+ {
+ $filter = Array(
+ "IBLOCK_ID" => $id,
+ "ACTIVE" => "Y",
+ "IBLOCK_ACTIVE" => "Y",
+ "GLOBAL_ACTIVE" => "Y"
+ );
+
+
+ $dbRes = CIBlockSection::GetList(array("left_margin" => "asc"), $filter);
+ while ($arRes = $dbRes->Fetch())
+ {
+ $categories[] = $this->BuildCategory($arRes);
+ }
+ }
+ return $categories;
+
+ }
+
+ protected function BuildCategory($arCategory)
+ {
+ return "
+ 0 ?
+ " parentId=\"" . $arCategory["IBLOCK_SECTION_ID"] . "\""
+ :"")
+ . ">"
+ . $arCategory["NAME"]
+ . "";
+
+ }
+
+ protected function GetOffers()
+ {
+ $offers = Array();
+ foreach ($this->iblocks as $key => $id)
+ {
+
+ $iblock['IBLOCK_DB'] = CIBlock::GetByID($id)->Fetch();
+ $iblockOffer = CCatalogSKU::GetInfoByProductIBlock($id);
+
+
+ $arSelect = Array (
+ "ID",
+ "LID",
+ "IBLOCK_ID",
+ "IBLOCK_SECTION_ID",
+ "ACTIVE",
+ "ACTIVE_FROM",
+ "ACTIVE_TO",
+ "NAME",
+ "DETAIL_PICTURE",
+ "DETAIL_TEXT",
+ "DETAIL_PICTURE",
+ "LANG_DIR",
+ "DETAIL_PAGE_URL",
+ "PROPERTY_" . $this->articleProperties[$key]
+ );
+
+ $filter = Array (
+ "IBLOCK_ID" => $id,
+ "ACTIVE_DATE" => "Y",
+ "ACTIVE" => "Y",
+ "INCLUDE_SUBSECTIONS" => "Y"
+ );
+ $counter = 0;
+ $dbResProducts = CIBlockElement::GetList(array(), $filter, false, false, $arSelect);
+ while ($product = $dbResProducts->GetNextElement()) {
+
+ $product = $product->GetFields();
+
+ $categoriesString = "";
+
+
+ $existOffer = false;
+ if (!empty($iblockOffer['IBLOCK_ID'])) {
+ $arFilterOffer = Array (
+ 'IBLOCK_ID' => $iblockOffer['IBLOCK_ID'],
+ 'PROPERTY_'.$iblockOffer['SKU_PROPERTY_ID'] => $product["ID"]
+ );
+ $arSelectOffer = Array (
+ 'ID',
+ "NAME",
+ "DETAIL_TEXT",
+ "DETAIL_PAGE_URL",
+ "DETAIL_PICTURE",
+ "PROPERTY_" . $this->articleProperties[$key]
+ );
+
+ $rsOffers = CIBlockElement::GetList(array(), $arFilterOffer, false, false, $arSelectOffer);
+ while ($arOffer = $rsOffers->GetNext()) {
+
+ $dbResCategories = CIBlockElement::GetElementGroups($arOffer['ID'], true);
+ while ($arResCategory = $dbResCategories->Fetch()) {
+ $categoriesString .= "" . $arResCategory["ID"] . "\n";
+ }
+ $offer = CCatalogProduct::GetByID($arOffer['ID']);
+ $arOffer['QUANTITY'] = $offer["QUANTITY"];
+
+ $arOffer['PRODUCT_ID'] = $product["ID"];
+ $arOffer['DETAIL_PAGE_URL'] = $product["DETAIL_PAGE_URL"];
+ $arOffer['DETAIL_PICTURE'] = $product["DETAIL_PICTURE"];
+ $arOffer['PREVIEW_PICTURE'] = $product["PREVIEW_PICTURE"];
+ $arOffer['PRODUCT_NAME'] = $product["NAME"];
+ $arOffer['ARTICLE'] = $arOffer["PROPERTY_" . $this->articleProperties[$key] . "_VALUE"];
+
+ $dbPrice = GetCatalogProductPrice($arOffer["ID"],1);
+ $arOffer['PRICE'] = $dbPrice['PRICE'];
+
+
+
+ $offers[] = $this->BuildOffer($arOffer, $categoriesString, $iblock);
+ $existOffer = true;
+ }
+ }
+ if (!$existOffer) {
+ $dbResCategories = CIBlockElement::GetElementGroups($product["ID"], true);
+ while ($arResCategory = $dbResCategories->Fetch()) {
+ $categoriesString .= "" . $arResCategory["ID"] . "\n";
+ }
+
+
+ $offer = CCatalogProduct::GetByID($product['ID']);
+ $product['QUANTITY'] = $offer["QUANTITY"];
+
+ $product['PRODUCT_ID'] = $product["ID"];
+ $product['PRODUCT_NAME'] = $product["NAME"];
+ $product['ARTICLE'] = $product["PROPERTY_" . $this->articleProperties[$key] . "_VALUE"];
+
+ $dbPrice = GetCatalogProductPrice($product["ID"],1);
+ $product['PRICE'] = $dbPrice['PRICE'];
+
+ $offers[] = $this->BuildOffer($product, $categoriesString, $iblock);
+ }
+ }
+ }
+ return $offers;
+ }
+
+
+ protected function BuildOffer($arOffer, $categoriesString, $iblock)
+ {
+ $offer = "";
+ $offer .= "\n";
+ $offer .= "http://" . $iblock['IBLOCK_DB']['SERVER_NAME'] . $arOffer['DETAIL_PAGE_URL'] . "\n";
+
+ $offer .= "" . $arOffer['PRICE'] . "\n";
+ $offer .= $categoriesString;
+
+ $detailPicture = intval($arOffer["DETAIL_PICTURE"]);
+ $previewPicture = intval($arOffer["PREVIEW_PICTURE"]);
+
+ if ($detailPicture > 0 || $previewPicture > 0)
+ {
+ $picture = $detailPicture;
+ if ($picture <= 0) {
+ $picture = $previewPicture;
+ }
+
+ if ($arFile = CFile::GetFileArray($picture))
+ {
+ if(substr($arFile["SRC"], 0, 1) == "/")
+ $strFile = "http://" . $iblock['IBLOCK_DB']['SERVER_NAME'] . implode("/", array_map("rawurlencode", explode("/", $arFile["SRC"])));
+ elseif(preg_match("/^(http|https):\\/\\/(.*?)\\/(.*)\$/", $arFile["SRC"], $match))
+ $strFile = "http://" . $match[2] . '/' . implode("/", array_map("rawurlencode", explode("/", $match[3])));
+ else
+ $strFile = $arFile["SRC"];
+ $offer .= "\n";
+ }
+ }
+
+ $offer .= "" . $this->PrepareValue($arOffer["NAME"]) . "\n";
+ $offer .= "" . (strip_tags( html_entity_decode(str_replace(" ", ' ', $this->PrepareValue($arOffer["DETAIL_TEXT"]))))) .
+ "\n";
+
+
+ $offer .= "" . $arOffer["EXTERNAL_ID"] . "\n";
+ $offer .= "" . $arOffer["PRODUCT_NAME"] . "\n";
+ $offer .= "" . $arOffer["ARTICLE"] . "\n";
+
+ $offer.= "\n";
+ return $offer;
+ }
+
+
+
+}
\ No newline at end of file
diff --git a/intaro.intarocrm/include.php b/intaro.intarocrm/include.php
index 0851bb33..34c55087 100755
--- a/intaro.intarocrm/include.php
+++ b/intaro.intarocrm/include.php
@@ -4,6 +4,7 @@ CModule::AddAutoloadClasses(
array (
'IntaroCrm\RestApi' => 'classes/general/RestApi.php',
'ICrmOrderActions' => 'classes/general/ICrmOrderActions.php',
+ 'ICMLLoader' => 'classes/general/ICMLLoader.php',
'ICrmOrderEvent' => 'classes/general/events/ICrmOrderEvent.php'
)
);
\ No newline at end of file
diff --git a/intaro.intarocrm/install/index.php b/intaro.intarocrm/install/index.php
index 334ec133..f7816273 100755
--- a/intaro.intarocrm/install/index.php
+++ b/intaro.intarocrm/install/index.php
@@ -21,6 +21,7 @@ class intaro_intarocrm extends CModule
var $PARTNER_NAME;
var $PARTNER_URI;
var $INTARO_CRM_API;
+ var $INTARO_CRM_EXPORT = 'intarocrm';
var $CRM_API_HOST_OPTION = 'api_host';
var $CRM_API_KEY_OPTION = 'api_key';
@@ -65,6 +66,7 @@ class intaro_intarocrm extends CModule
include($this->INSTALL_PATH . '/../classes/general/RestApi.php');
include($this->INSTALL_PATH . '/../classes/general/ICrmOrderActions.php');
+ include($this->INSTALL_PATH . '/../classes/general/ICMLLoader.php');
$step = intval($_REQUEST['step']);
@@ -370,11 +372,193 @@ class intaro_intarocrm extends CModule
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step3.php'
);
} else if ($step == 4) {
+
+ if(!CModule::IncludeModule("iblock")) {
+ $arResult['errCode'] = 'ERR_IBLOCK';
+ }
+ if(!CModule::IncludeModule("catalog")) {
+ $arResult['errCode'] = 'ERR_CATALOG';
+ }
+ $APPLICATION->IncludeAdminFile(
+ GetMessage('MODULE_INSTALL_TITLE'),
+ $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step4.php'
+ );
+ } else if ($step == 5) {
+
+ if(isset($arResult['errCode']) && $arResult['errCode']) {
+ $APPLICATION->IncludeAdminFile(
+ GetMessage('MODULE_INSTALL_TITLE'),
+ $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step4.php'
+ );
+ return;
+ }
+
+ if (isset($_POST['back']) && $_POST['back']) {
+ $APPLICATION->IncludeAdminFile(
+ GetMessage('MODULE_INSTALL_TITLE'),
+ $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step3.php'
+ );
+ }
+
RegisterModule($this->MODULE_ID);
RegisterModuleDependences("sale", "OnSaleCancelOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSaleCancelOrder");
+
+ if(!CModule::IncludeModule("iblock")) {
+ $arResult['errCode'] = 'ERR_IBLOCK';
+ }
+
+ if(!CModule::IncludeModule("catalog")) {
+ $arResult['errCode'] = 'ERR_CATALOG';
+ }
+
+ if(!isset($_POST['IBLOCK_EXPORT']))
+ $iblocks = 0;
+ else
+ $iblocks = $_POST['IBLOCK_EXPORT'];
+
+ if(!isset($_POST['IBLOCK_PROPERTY_ARTICLE']))
+ $articleProperties = 0;
+ else
+ $articleProperties = $_POST['IBLOCK_PROPERTY_ARTICLE'];
+
+ if(!isset($_POST['SETUP_PROFILE_NAME']))
+ $profileName = 0;
+ else
+ $profileName = $_POST['SETUP_PROFILE_NAME'];
+
+ if(!isset($_POST['SETUP_FILE_NAME']))
+ $filename = 0;
+ else
+ $filename = $_POST['SETUP_FILE_NAME'];
+
+ if(!isset($_POST['TYPE_LOADING']))
+ $typeLoading = 0;
+ else
+ $typeLoading = $_POST['TYPE_LOADING'];
+
+ if (isset($_POST['LOAD_NOW'])) {
+
+ $loader = new ICMLLoader();
+ $loader->iblocks = $iblocks;
+ $loader->articleProperties = $articleProperties;
+ $loader->filename = $filename;
+ $loader->application = $APPLICATION;
+ $loader->Load();
+
+ }
+ if(!isset($_POST['TYPE_LOADING']))
+ $typeLoading = 0;
+ else
+ $typeLoading = $_POST['TYPE_LOADING'];
+
+ if ($typeLoading == 'agent' || $typeLoading == 'cron') {
+ $dbProfile = CCatalogExport::GetList(array(), array("FILE_NAME" => $this->INTARO_CRM_EXPORT));
+
+ while ($arProfile = $dbProfile->Fetch()) {
+ if ($arProfile["DEFAULT_PROFILE"]!="Y")
+ CAgent::RemoveAgent("CCatalogExport::PreGenerateExport(".$arProfile['ID'].");", "catalog");
+ }
+ $ar = $this->GetProfileSetupVars($iblocks, $articleProperties, $filename);
+ $PROFILE_ID = CCatalogExport::Add(array(
+ "LAST_USE" => false,
+ "FILE_NAME" => $this->INTARO_CRM_EXPORT,
+ "NAME" => $profileName,
+ "DEFAULT_PROFILE" => "N",
+ "IN_MENU" => "N",
+ "IN_AGENT" => "N",
+ "IN_CRON" => "N",
+ "NEED_EDIT" => "N",
+ "SETUP_VARS" => $ar
+ ));
+ if (intval($PROFILE_ID) <= 0) {
+ $arResult['errCode'] = 'ERR_IBLOCK';
+ return;
+ }
+ if ($typeLoading == 'agent') {
+
+ $dateAgent = new DateTime();
+ $intAgent = new DateInterval('PT60S'); // PT60S - 60 sec;
+ $dateAgent->add($intAgent);
+ CAgent::AddAgent(
+ "CCatalogExport::PreGenerateExport(" . $PROFILE_ID . ");",
+ "catalog",
+ "N",
+ 86400,
+ $dateAgent->format('d.m.Y H:i:s'), // date of first check
+ "Y", // агент активен
+ $dateAgent->format('d.m.Y H:i:s'), // date of first start
+ 30
+ );
+
+ CCatalogExport::Update($PROFILE_ID, array(
+ "IN_CRON" => ($arProfile["IN_AGENT"]=="Y" ? "N" : "Y")
+ ));
+ } else {
+ $agent_period = 24;
+ $agent_php_path = "/usr/local/php/bin/php";
+
+ if (!file_exists($_SERVER["DOCUMENT_ROOT"].CATALOG_PATH2EXPORTS."cron_frame.php"))
+ {
+ CheckDirPath($_SERVER["DOCUMENT_ROOT"].CATALOG_PATH2EXPORTS);
+ $tmp_file_size = filesize($_SERVER["DOCUMENT_ROOT"].CATALOG_PATH2EXPORTS_DEF."cron_frame.php");
+ $fp = fopen($_SERVER["DOCUMENT_ROOT"].CATALOG_PATH2EXPORTS_DEF."cron_frame.php", "rb");
+ $tmp_data = fread($fp, $tmp_file_size);
+ fclose($fp);
+
+ $tmp_data = str_replace("#DOCUMENT_ROOT#", $_SERVER["DOCUMENT_ROOT"], $tmp_data);
+ $tmp_data = str_replace("#PHP_PATH#", $agent_php_path, $tmp_data);
+
+ $fp = fopen($_SERVER["DOCUMENT_ROOT"].CATALOG_PATH2EXPORTS."cron_frame.php", "ab");
+ fwrite($fp, $tmp_data);
+ fclose($fp);
+ }
+
+ $cfg_data = "";
+ if (file_exists($_SERVER["DOCUMENT_ROOT"]."/bitrix/crontab/crontab.cfg"))
+ {
+ $cfg_file_size = filesize($_SERVER["DOCUMENT_ROOT"]."/bitrix/crontab/crontab.cfg");
+ $fp = fopen($_SERVER["DOCUMENT_ROOT"]."/bitrix/crontab/crontab.cfg", "rb");
+ $cfg_data = fread($fp, $cfg_file_size);
+ fclose($fp);
+ }
+
+ CheckDirPath($_SERVER["DOCUMENT_ROOT"].CATALOG_PATH2EXPORTS."logs/");
+
+ if ($arProfile["IN_CRON"]=="Y")
+ {
+ // remove
+ $cfg_data = preg_replace("#^.*?".preg_quote(CATALOG_PATH2EXPORTS)."cron_frame.php +".$PROFILE_ID." *>.*?$#im", "", $cfg_data);
+ }
+ else
+ {
+ $strTime = "0 */".$agent_period." * * * ";
+ if (strlen($cfg_data)>0)
+ $cfg_data .= "\n";
+
+ $cfg_data .= $strTime.$agent_php_path." -f ".$_SERVER["DOCUMENT_ROOT"].CATALOG_PATH2EXPORTS."cron_frame.php ".$PROFILE_ID." >".$_SERVER["DOCUMENT_ROOT"].CATALOG_PATH2EXPORTS."logs/".$PROFILE_ID.".txt\n";
+ }
+
+ CCatalogExport::Update($PROFILE_ID, array(
+ "IN_CRON" => ($arProfile["IN_CRON"]=="Y" ? "N" : "Y")
+ ));
+
+ CheckDirPath($_SERVER["DOCUMENT_ROOT"]."/bitrix/crontab/");
+ $cfg_data = preg_replace("#[\r\n]{2,}#im", "\n", $cfg_data);
+ $fp = fopen($_SERVER["DOCUMENT_ROOT"]."/bitrix/crontab/crontab.cfg", "wb");
+ fwrite($fp, $cfg_data);
+ fclose($fp);
+
+ $arRetval = array();
+ @exec("crontab ".$_SERVER["DOCUMENT_ROOT"]."/bitrix/crontab/crontab.cfg", $arRetval, $return_var);
+
+ }
+ }
+
+ //
//agent
+
$dateAgent = new DateTime();
$intAgent = new DateInterval('PT60S'); // PT60S - 60 sec;
$dateAgent->add($intAgent);
@@ -391,16 +575,10 @@ class intaro_intarocrm extends CModule
);
$this->CopyFiles();
-
- // statistic update
- $api_host = COption::GetOptionString($this->MODULE_ID, $this->CRM_API_HOST_OPTION, 0);
- $api_key = COption::GetOptionString($this->MODULE_ID, $this->CRM_API_KEY_OPTION, 0);
- $this->INTARO_CRM_API = new \IntaroCrm\RestApi($api_host, $api_key);
- $this->INTARO_CRM_API->statisticUpdate();
-
+
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
- $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step4.php'
+ $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step5.php'
);
}
}
@@ -417,10 +595,9 @@ class intaro_intarocrm extends CModule
COption::RemoveOption($this->MODULE_ID, $this->CRM_PAYMENT_STATUSES);
COption::RemoveOption($this->MODULE_ID, $this->CRM_PAYMENT);
COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_LAST_ID);
- UnRegisterModuleDependences("sale", "OnSaleCancelOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSaleCancelOrder");
-
+
$this->DeleteFiles();
-
+
UnRegisterModule($this->MODULE_ID);
$APPLICATION->IncludeAdminFile(
@@ -442,4 +619,23 @@ class intaro_intarocrm extends CModule
unlink($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/intarocrm_run.php');
unlink($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/intarocrm_setup.php');
}
-}
\ No newline at end of file
+
+ function GetProfileSetupVars($iblocks, $articleProperties, $filename) {
+ // Get string like IBLOCK_EXPORT[0]=3&
+ // IBLOCK_EXPORT[1]=6&
+ // IBLOCK_PROPERTY_ARTICLE[0]=ARTICLE&
+ // IBLOCK_PROPERTY_ARTICLE[1]=ARTNUMBER&
+ // SETUP_FILE_NAME=%2Fbitrix%2Fcatalog_export%2Ftestintarocrm.xml
+
+ //$arProfileFields = explode(",", $SETUP_FIELDS_LIST);
+ $strVars = "";
+ foreach ($iblocks as $key => $val)
+ $strVars .= 'IBLOCK_EXPORT[' . $key . ']=' . $val . '&';
+ foreach ($articleProperties as $key => $val)
+ $strVars .= 'IBLOCK_PROPERTY_ARTICLE[' . $key . ']=' . $val . '&';
+
+ $strVars .= 'SETUP_FILE_NAME=' . urlencode($filename);
+
+ return $strVars;
+ }
+}
diff --git a/intaro.intarocrm/install/step4.php b/intaro.intarocrm/install/step4.php
index 931fdfcc..81178e93 100644
--- a/intaro.intarocrm/install/step4.php
+++ b/intaro.intarocrm/install/step4.php
@@ -1,10 +1,228 @@
-
+
+
diff --git a/intaro.intarocrm/install/step5.php b/intaro.intarocrm/install/step5.php
new file mode 100644
index 00000000..931fdfcc
--- /dev/null
+++ b/intaro.intarocrm/install/step5.php
@@ -0,0 +1,10 @@
+
+
+