コード例 #1
0
ファイル: iblockelement.php プロジェクト: spas-viktor/books
 function Update($ID, $arFields, $bWorkFlow = false, $bUpdateSearch = true, $bResizePictures = false, $bCheckDiskQuota = true)
 {
     global $DB, $USER;
     $ID = intval($ID);
     $db_element = CIBlockElement::GetList(array(), array("ID" => $ID, "SHOW_HISTORY" => "Y"), false, false, array("ID", "TIMESTAMP_X", "MODIFIED_BY", "DATE_CREATE", "CREATED_BY", "IBLOCK_ID", "ACTIVE", "ACTIVE_FROM", "ACTIVE_TO", "SORT", "NAME", "PREVIEW_PICTURE", "PREVIEW_TEXT", "PREVIEW_TEXT_TYPE", "DETAIL_PICTURE", "DETAIL_TEXT", "DETAIL_TEXT_TYPE", "WF_STATUS_ID", "WF_PARENT_ELEMENT_ID", "WF_NEW", "WF_COMMENTS", "IN_SECTIONS", "CODE", "TAGS", "XML_ID", "TMP_ID"));
     if (!($ar_element = $db_element->Fetch())) {
         return false;
     }
     $arIBlock = CIBlock::GetArrayByID($ar_element["IBLOCK_ID"]);
     $bWorkFlow = $bWorkFlow && is_array($arIBlock) && $arIBlock["WORKFLOW"] != "N" && CModule::IncludeModule("workflow");
     $ar_wf_element = $ar_element;
     $LAST_ID = 0;
     if ($bWorkFlow) {
         $LAST_ID = CIBlockElement::WF_GetLast($ID);
         if ($LAST_ID != $ID) {
             $db_element = CIBlockElement::GetByID($LAST_ID);
             if (!($ar_wf_element = $db_element->Fetch())) {
                 return false;
             }
         }
         $arFields["WF_PARENT_ELEMENT_ID"] = $ID;
         if (!array_key_exists("PROPERTY_VALUES", $arFields) || !is_array($arFields["PROPERTY_VALUES"])) {
             $arFields["PROPERTY_VALUES"] = array();
         }
         $bFieldProps = array();
         foreach ($arFields["PROPERTY_VALUES"] as $k => $v) {
             $bFieldProps[$k] = true;
         }
         $arFieldProps =& $arFields['PROPERTY_VALUES'];
         $props = CIBlockElement::GetProperty($ar_element["IBLOCK_ID"], $ar_wf_element["ID"]);
         while ($arProp = $props->Fetch()) {
             $pr_val_id = $arProp['PROPERTY_VALUE_ID'];
             if ($arProp['PROPERTY_TYPE'] == 'F' && strlen($pr_val_id) > 0) {
                 if (strlen($arProp["CODE"]) > 0 && is_set($arFieldProps, $arProp["CODE"])) {
                     $pr_id = $arProp["CODE"];
                 } else {
                     $pr_id = $arProp['ID'];
                 }
                 if (array_key_exists($pr_id, $arFieldProps) && array_key_exists($pr_val_id, $arFieldProps[$pr_id]) && is_array($arFieldProps[$pr_id][$pr_val_id])) {
                     $new_value = $arFieldProps[$pr_id][$pr_val_id];
                     if (strlen($new_value['name']) <= 0 && $new_value['del'] != "Y" && strlen($new_value['VALUE']['name']) <= 0 && $new_value['VALUE']['del'] != "Y") {
                         if (array_key_exists('DESCRIPTION', $new_value) && $new_value['DESCRIPTION'] != $arProp['DESCRIPTION']) {
                             $p = array("VALUE" => CFile::MakeFileArray($arProp['VALUE']));
                             $p["DESCRIPTION"] = $new_value["DESCRIPTION"];
                             $p["MODULE_ID"] = "iblock";
                             $arFieldProps[$pr_id][$pr_val_id] = $p;
                         } elseif ($arProp['VALUE'] > 0) {
                             $arFieldProps[$pr_id][$pr_val_id] = array("VALUE" => $arProp['VALUE'], "DESCRIPTION" => $arProp["DESCRIPTION"]);
                         }
                     }
                 } else {
                     $arFieldProps[$pr_id][$pr_val_id] = array("VALUE" => $arProp['VALUE'], "DESCRIPTION" => $arProp["DESCRIPTION"]);
                 }
                 continue;
             }
             if (strlen($pr_val_id) <= 0 || array_key_exists($arProp["ID"], $bFieldProps) || strlen($arProp["CODE"]) > 0 && array_key_exists($arProp["CODE"], $bFieldProps)) {
                 continue;
             }
             $arFieldProps[$arProp["ID"]][$pr_val_id] = array("VALUE" => $arProp['VALUE'], "DESCRIPTION" => $arProp["DESCRIPTION"]);
         }
         if ($ar_wf_element["IN_SECTIONS"] == "Y") {
             $ar_wf_element["IBLOCK_SECTION"] = array();
             $rsSections = CIBlockElement::GetElementGroups($ar_element["ID"], true, array('ID', 'IBLOCK_ELEMENT_ID'));
             while ($arSection = $rsSections->Fetch()) {
                 $ar_wf_element["IBLOCK_SECTION"][] = $arSection["ID"];
             }
         }
         unset($ar_wf_element["DATE_ACTIVE_FROM"]);
         unset($ar_wf_element["DATE_ACTIVE_TO"]);
         unset($ar_wf_element["EXTERNAL_ID"]);
         unset($ar_wf_element["TIMESTAMP_X"]);
         unset($ar_wf_element["ID"]);
         $arFields = $arFields + $ar_wf_element;
     }
     $arFields["WF"] = $bWorkFlow ? "Y" : "N";
     $bBizProc = is_array($arIBlock) && $arIBlock["BIZPROC"] == "Y" && IsModuleInstalled("bizproc");
     if (array_key_exists("BP_PUBLISHED", $arFields)) {
         if ($bBizProc) {
             if ($arFields["BP_PUBLISHED"] == "Y") {
                 $arFields["WF_STATUS_ID"] = 1;
                 $arFields["WF_NEW"] = false;
             } else {
                 $arFields["WF_STATUS_ID"] = 2;
                 $arFields["WF_NEW"] = "Y";
                 $arFields["BP_PUBLISHED"] = "N";
             }
         } else {
             $arFields["WF_NEW"] = false;
             unset($arFields["BP_PUBLISHED"]);
         }
     } else {
         $arFields["WF_NEW"] = false;
     }
     if (is_set($arFields, "ACTIVE") && $arFields["ACTIVE"] != "Y") {
         $arFields["ACTIVE"] = "N";
     }
     if (is_set($arFields, "PREVIEW_TEXT_TYPE") && $arFields["PREVIEW_TEXT_TYPE"] != "html") {
         $arFields["PREVIEW_TEXT_TYPE"] = "text";
     }
     if (is_set($arFields, "DETAIL_TEXT_TYPE") && $arFields["DETAIL_TEXT_TYPE"] != "html") {
         $arFields["DETAIL_TEXT_TYPE"] = "text";
     }
     $strWarning = "";
     if ($bResizePictures) {
         $arDef = $arIBlock["FIELDS"]["PREVIEW_PICTURE"]["DEFAULT_VALUE"];
         if ($arDef["DELETE_WITH_DETAIL"] === "Y" && $arFields["DETAIL_PICTURE"]["del"] === "Y") {
             $arFields["PREVIEW_PICTURE"]["del"] = "Y";
         }
         if ($arDef["FROM_DETAIL"] === "Y" && ($arFields["PREVIEW_PICTURE"]["size"] <= 0 || $arDef["UPDATE_WITH_DETAIL"] === "Y") && is_array($arFields["DETAIL_PICTURE"]) && $arFields["DETAIL_PICTURE"]["size"] > 0) {
             if ($arFields["PREVIEW_PICTURE"]["del"] !== "Y" && $arDef["UPDATE_WITH_DETAIL"] !== "Y") {
                 $rsElement = CIBlockElement::GetList(array("ID" => "DESC"), array("ID" => $ar_wf_element["ID"], "IBLOCK_ID" => $ar_wf_element["IBLOCK_ID"], "SHOW_HISTORY" => "Y"), false, false, array("ID", "PREVIEW_PICTURE"));
                 $arOldElement = $rsElement->Fetch();
             } else {
                 $arOldElement = false;
             }
             if (!$arOldElement || !$arOldElement["PREVIEW_PICTURE"]) {
                 $arNewPreview = $arFields["DETAIL_PICTURE"];
                 $arNewPreview["COPY_FILE"] = "Y";
                 $arNewPreview["description"] = $arFields["PREVIEW_PICTURE"]["description"];
                 $arFields["PREVIEW_PICTURE"] = $arNewPreview;
             }
         }
         if (array_key_exists("PREVIEW_PICTURE", $arFields) && is_array($arFields["PREVIEW_PICTURE"]) && $arFields["PREVIEW_PICTURE"]["size"] > 0 && $arDef["SCALE"] === "Y") {
             $arNewPicture = CIBlock::ResizePicture($arFields["PREVIEW_PICTURE"], $arDef);
             if (is_array($arNewPicture)) {
                 $arNewPicture["description"] = $arFields["PREVIEW_PICTURE"]["description"];
                 $arFields["PREVIEW_PICTURE"] = $arNewPicture;
             } elseif ($arDef["IGNORE_ERRORS"] !== "Y") {
                 unset($arFields["PREVIEW_PICTURE"]);
                 $strWarning .= GetMessage("IBLOCK_FIELD_PREVIEW_PICTURE") . ": " . $arNewPicture . "<br>";
             }
         }
         if (array_key_exists("PREVIEW_PICTURE", $arFields) && is_array($arFields["PREVIEW_PICTURE"]) && $arDef["USE_WATERMARK_FILE"] === "Y") {
             if (strlen($arFields["PREVIEW_PICTURE"]["tmp_name"]) > 0 && ($arFields["PREVIEW_PICTURE"]["tmp_name"] === $arFields["DETAIL_PICTURE"]["tmp_name"] || $arFields["PREVIEW_PICTURE"]["COPY_FILE"] == "Y" && !$arFields["PREVIEW_PICTURE"]["copy"])) {
                 $tmp_name = CTempFile::GetFileName(basename($arFields["PREVIEW_PICTURE"]["tmp_name"]));
                 CheckDirPath($tmp_name);
                 copy($arFields["PREVIEW_PICTURE"]["tmp_name"], $tmp_name);
                 $arFields["PREVIEW_PICTURE"]["copy"] = true;
                 $arFields["PREVIEW_PICTURE"]["tmp_name"] = $tmp_name;
             }
             CIBLock::FilterPicture($arFields["PREVIEW_PICTURE"]["tmp_name"], array("name" => "watermark", "position" => $arDef["WATERMARK_FILE_POSITION"], "type" => "file", "size" => "real", "alpha_level" => 100 - min(max($arDef["WATERMARK_FILE_ALPHA"], 0), 100), "file" => $_SERVER["DOCUMENT_ROOT"] . Rel2Abs("/", $arDef["WATERMARK_FILE"])));
         }
         if (array_key_exists("PREVIEW_PICTURE", $arFields) && is_array($arFields["PREVIEW_PICTURE"]) && $arDef["USE_WATERMARK_TEXT"] === "Y") {
             if (strlen($arFields["PREVIEW_PICTURE"]["tmp_name"]) > 0 && ($arFields["PREVIEW_PICTURE"]["tmp_name"] === $arFields["DETAIL_PICTURE"]["tmp_name"] || $arFields["PREVIEW_PICTURE"]["COPY_FILE"] == "Y" && !$arFields["PREVIEW_PICTURE"]["copy"])) {
                 $tmp_name = CTempFile::GetFileName(basename($arFields["PREVIEW_PICTURE"]["tmp_name"]));
                 CheckDirPath($tmp_name);
                 copy($arFields["PREVIEW_PICTURE"]["tmp_name"], $tmp_name);
                 $arFields["PREVIEW_PICTURE"]["copy"] = true;
                 $arFields["PREVIEW_PICTURE"]["tmp_name"] = $tmp_name;
             }
             CIBLock::FilterPicture($arFields["PREVIEW_PICTURE"]["tmp_name"], array("name" => "watermark", "position" => $arDef["WATERMARK_TEXT_POSITION"], "type" => "text", "coefficient" => $arDef["WATERMARK_TEXT_SIZE"], "text" => $arDef["WATERMARK_TEXT"], "font" => $_SERVER["DOCUMENT_ROOT"] . Rel2Abs("/", $arDef["WATERMARK_TEXT_FONT"]), "color" => $arDef["WATERMARK_TEXT_COLOR"]));
         }
         $arDef = $arIBlock["FIELDS"]["DETAIL_PICTURE"]["DEFAULT_VALUE"];
         if (array_key_exists("DETAIL_PICTURE", $arFields) && is_array($arFields["DETAIL_PICTURE"]) && $arDef["SCALE"] === "Y") {
             $arNewPicture = CIBlock::ResizePicture($arFields["DETAIL_PICTURE"], $arDef);
             if (is_array($arNewPicture)) {
                 $arNewPicture["description"] = $arFields["DETAIL_PICTURE"]["description"];
                 $arFields["DETAIL_PICTURE"] = $arNewPicture;
             } elseif ($arDef["IGNORE_ERRORS"] !== "Y") {
                 unset($arFields["DETAIL_PICTURE"]);
                 $strWarning .= GetMessage("IBLOCK_FIELD_DETAIL_PICTURE") . ": " . $arNewPicture . "<br>";
             }
         }
         if (array_key_exists("DETAIL_PICTURE", $arFields) && is_array($arFields["DETAIL_PICTURE"]) && $arDef["USE_WATERMARK_FILE"] === "Y") {
             if (strlen($arFields["DETAIL_PICTURE"]["tmp_name"]) > 0 && ($arFields["DETAIL_PICTURE"]["tmp_name"] === $arFields["PREVIEW_PICTURE"]["tmp_name"] || $arFields["DETAIL_PICTURE"]["COPY_FILE"] == "Y" && !$arFields["DETAIL_PICTURE"]["copy"])) {
                 $tmp_name = CTempFile::GetFileName(basename($arFields["DETAIL_PICTURE"]["tmp_name"]));
                 CheckDirPath($tmp_name);
                 copy($arFields["DETAIL_PICTURE"]["tmp_name"], $tmp_name);
                 $arFields["DETAIL_PICTURE"]["copy"] = true;
                 $arFields["DETAIL_PICTURE"]["tmp_name"] = $tmp_name;
             }
             CIBLock::FilterPicture($arFields["DETAIL_PICTURE"]["tmp_name"], array("name" => "watermark", "position" => $arDef["WATERMARK_FILE_POSITION"], "type" => "file", "size" => "real", "alpha_level" => 100 - min(max($arDef["WATERMARK_FILE_ALPHA"], 0), 100), "file" => $_SERVER["DOCUMENT_ROOT"] . Rel2Abs("/", $arDef["WATERMARK_FILE"])));
         }
         if (array_key_exists("DETAIL_PICTURE", $arFields) && is_array($arFields["DETAIL_PICTURE"]) && $arDef["USE_WATERMARK_TEXT"] === "Y") {
             if (strlen($arFields["DETAIL_PICTURE"]["tmp_name"]) > 0 && ($arFields["DETAIL_PICTURE"]["tmp_name"] === $arFields["PREVIEW_PICTURE"]["tmp_name"] || $arFields["DETAIL_PICTURE"]["COPY_FILE"] == "Y" && !$arFields["DETAIL_PICTURE"]["copy"])) {
                 $tmp_name = CTempFile::GetFileName(basename($arFields["DETAIL_PICTURE"]["tmp_name"]));
                 CheckDirPath($tmp_name);
                 copy($arFields["DETAIL_PICTURE"]["tmp_name"], $tmp_name);
                 $arFields["DETAIL_PICTURE"]["copy"] = true;
                 $arFields["DETAIL_PICTURE"]["tmp_name"] = $tmp_name;
             }
             CIBLock::FilterPicture($arFields["DETAIL_PICTURE"]["tmp_name"], array("name" => "watermark", "position" => $arDef["WATERMARK_TEXT_POSITION"], "type" => "text", "coefficient" => $arDef["WATERMARK_TEXT_SIZE"], "text" => $arDef["WATERMARK_TEXT"], "font" => $_SERVER["DOCUMENT_ROOT"] . Rel2Abs("/", $arDef["WATERMARK_TEXT_FONT"]), "color" => $arDef["WATERMARK_TEXT_COLOR"]));
         }
     }
     $ipropTemplates = new \Bitrix\Iblock\InheritedProperty\ElementTemplates($ar_element["IBLOCK_ID"], $ar_element["ID"]);
     if (array_key_exists("PREVIEW_PICTURE", $arFields) && is_array($arFields["PREVIEW_PICTURE"])) {
         if (strlen($arFields["PREVIEW_PICTURE"]["name"]) <= 0 && strlen($arFields["PREVIEW_PICTURE"]["del"]) <= 0 && !is_set($arFields["PREVIEW_PICTURE"], "description")) {
             unset($arFields["PREVIEW_PICTURE"]);
         } else {
             $arFields["PREVIEW_PICTURE"]["MODULE_ID"] = "iblock";
             $arFields["PREVIEW_PICTURE"]["old_file"] = $ar_wf_element["PREVIEW_PICTURE"];
             $arFields["PREVIEW_PICTURE"]["name"] = \Bitrix\Iblock\Template\Helper::makeFileName($ipropTemplates, "ELEMENT_PREVIEW_PICTURE_FILE_NAME", array_merge($ar_element, $arFields), $arFields["PREVIEW_PICTURE"]);
         }
     }
     if (array_key_exists("DETAIL_PICTURE", $arFields) && is_array($arFields["DETAIL_PICTURE"])) {
         if (strlen($arFields["DETAIL_PICTURE"]["name"]) <= 0 && strlen($arFields["DETAIL_PICTURE"]["del"]) <= 0 && !is_set($arFields["DETAIL_PICTURE"], "description")) {
             unset($arFields["DETAIL_PICTURE"]);
         } else {
             $arFields["DETAIL_PICTURE"]["MODULE_ID"] = "iblock";
             $arFields["DETAIL_PICTURE"]["old_file"] = $ar_wf_element["DETAIL_PICTURE"];
             $arFields["DETAIL_PICTURE"]["name"] = \Bitrix\Iblock\Template\Helper::makeFileName($ipropTemplates, "ELEMENT_DETAIL_PICTURE_FILE_NAME", array_merge($ar_element, $arFields), $arFields["DETAIL_PICTURE"]);
         }
     }
     if (is_set($arFields, "DATE_ACTIVE_FROM")) {
         $arFields["ACTIVE_FROM"] = $arFields["DATE_ACTIVE_FROM"];
     }
     if (is_set($arFields, "DATE_ACTIVE_TO")) {
         $arFields["ACTIVE_TO"] = $arFields["DATE_ACTIVE_TO"];
     }
     if (is_set($arFields, "EXTERNAL_ID")) {
         $arFields["XML_ID"] = $arFields["EXTERNAL_ID"];
     }
     $PREVIEW_tmp = is_set($arFields, "PREVIEW_TEXT") ? $arFields["PREVIEW_TEXT"] : $ar_wf_element["PREVIEW_TEXT"];
     $PREVIEW_TYPE_tmp = is_set($arFields, "PREVIEW_TEXT_TYPE") ? $arFields["PREVIEW_TEXT_TYPE"] : $ar_wf_element["PREVIEW_TEXT_TYPE"];
     $DETAIL_tmp = is_set($arFields, "DETAIL_TEXT") ? $arFields["DETAIL_TEXT"] : $ar_wf_element["DETAIL_TEXT"];
     $DETAIL_TYPE_tmp = is_set($arFields, "DETAIL_TEXT_TYPE") ? $arFields["DETAIL_TEXT_TYPE"] : $ar_wf_element["DETAIL_TEXT_TYPE"];
     $arFields["SEARCHABLE_CONTENT"] = ToUpper((is_set($arFields, "NAME") ? $arFields["NAME"] : $ar_wf_element["NAME"]) . "\r\n" . ($PREVIEW_TYPE_tmp == "html" ? HTMLToTxt($PREVIEW_tmp) : $PREVIEW_tmp) . "\r\n" . ($DETAIL_TYPE_tmp == "html" ? HTMLToTxt($DETAIL_tmp) : $DETAIL_tmp));
     if (is_set($arFields["IBLOCK_SECTION_ID"]) && !is_set($arFields, "IBLOCK_SECTION")) {
         $arFields["IBLOCK_SECTION"] = array($arFields["IBLOCK_SECTION_ID"]);
     }
     $arFields["IBLOCK_ID"] = $ar_element["IBLOCK_ID"];
     if (!$this->CheckFields($arFields, $ID, $bCheckDiskQuota) || strlen($strWarning)) {
         $this->LAST_ERROR .= $strWarning;
         $Result = false;
         $arFields["RESULT_MESSAGE"] =& $this->LAST_ERROR;
     } else {
         unset($arFields["ID"]);
         if (array_key_exists("PREVIEW_PICTURE", $arFields)) {
             $SAVED_PREVIEW_PICTURE = $arFields["PREVIEW_PICTURE"];
         } else {
             $SAVED_PREVIEW_PICTURE = false;
         }
         if (array_key_exists("DETAIL_PICTURE", $arFields)) {
             $SAVED_DETAIL_PICTURE = $arFields["DETAIL_PICTURE"];
         } else {
             $SAVED_DETAIL_PICTURE = false;
         }
         // edit was done in workflow mode
         if ($bWorkFlow) {
             $arFields["WF_PARENT_ELEMENT_ID"] = $ID;
             if (array_key_exists("PREVIEW_PICTURE", $arFields)) {
                 if (is_array($arFields["PREVIEW_PICTURE"])) {
                     if (strlen($arFields["PREVIEW_PICTURE"]["name"]) <= 0 && strlen($arFields["PREVIEW_PICTURE"]["del"]) <= 0) {
                         if (array_key_exists("description", $arFields["PREVIEW_PICTURE"])) {
                             $arFile = CFile::GetFileArray($ar_wf_element["PREVIEW_PICTURE"]);
                             if ($arFields["PREVIEW_PICTURE"]["description"] != $arFile["DESCRIPTION"]) {
                                 //Description updated, so it's new file
                                 $arNewFile = CFile::MakeFileArray($ar_wf_element["PREVIEW_PICTURE"]);
                                 $arNewFile["description"] = $arFields["PREVIEW_PICTURE"]["description"];
                                 $arNewFile["MODULE_ID"] = "iblock";
                                 $arFields["PREVIEW_PICTURE"] = $arNewFile;
                             } else {
                                 $arFields["PREVIEW_PICTURE"] = $ar_wf_element["PREVIEW_PICTURE"];
                             }
                         } else {
                             //File was not changed at all
                             $arFields["PREVIEW_PICTURE"] = $ar_wf_element["PREVIEW_PICTURE"];
                         }
                     } else {
                         unset($arFields["PREVIEW_PICTURE"]["old_file"]);
                     }
                 }
             } else {
                 $arFields["PREVIEW_PICTURE"] = $ar_wf_element["PREVIEW_PICTURE"];
             }
             if (array_key_exists("DETAIL_PICTURE", $arFields)) {
                 if (is_array($arFields["DETAIL_PICTURE"])) {
                     if (strlen($arFields["DETAIL_PICTURE"]["name"]) <= 0 && strlen($arFields["DETAIL_PICTURE"]["del"]) <= 0) {
                         if (array_key_exists("description", $arFields["DETAIL_PICTURE"])) {
                             $arFile = CFile::GetFileArray($ar_wf_element["DETAIL_PICTURE"]);
                             if ($arFields["DETAIL_PICTURE"]["description"] != $arFile["DESCRIPTION"]) {
                                 //Description updated, so it's new file
                                 $arNewFile = CFile::MakeFileArray($ar_wf_element["DETAIL_PICTURE"]);
                                 $arNewFile["description"] = $arFields["DETAIL_PICTURE"]["description"];
                                 $arNewFile["MODULE_ID"] = "iblock";
                                 $arFields["DETAIL_PICTURE"] = $arNewFile;
                             } else {
                                 $arFields["DETAIL_PICTURE"] = $ar_wf_element["DETAIL_PICTURE"];
                             }
                         } else {
                             //File was not changed at all
                             $arFields["DETAIL_PICTURE"] = $ar_wf_element["DETAIL_PICTURE"];
                         }
                     } else {
                         unset($arFields["DETAIL_PICTURE"]["old_file"]);
                     }
                 }
             } else {
                 $arFields["DETAIL_PICTURE"] = $ar_wf_element["DETAIL_PICTURE"];
             }
             $NID = $this->Add($arFields);
             if ($NID > 0) {
                 if ($arFields["WF_STATUS_ID"] == 1) {
                     $DB->Query("UPDATE b_iblock_element SET TIMESTAMP_X=TIMESTAMP_X, WF_NEW=null WHERE ID=" . $ID);
                     $DB->Query("UPDATE b_iblock_element SET TIMESTAMP_X=TIMESTAMP_X, WF_NEW=null WHERE WF_PARENT_ELEMENT_ID=" . $ID);
                     $ar_wf_element["WF_NEW"] = false;
                 }
                 if ($this->bWF_SetMove) {
                     CIBlockElement::WF_SetMove($NID, $LAST_ID);
                 }
                 if ($ar_element["WF_STATUS_ID"] != 1 && $ar_wf_element["WF_STATUS_ID"] != $arFields["WF_STATUS_ID"] && $arFields["WF_STATUS_ID"] != 1) {
                     $DB->Query("UPDATE b_iblock_element SET TIMESTAMP_X=TIMESTAMP_X, WF_STATUS_ID=" . $arFields["WF_STATUS_ID"] . " WHERE ID=" . $ID);
                 }
             }
             //element was not published, so keep original
             if (is_set($arFields, "WF_STATUS_ID") && $arFields["WF_STATUS_ID"] != 1 && $ar_element["WF_STATUS_ID"] == 1 || !is_set($arFields, "WF_STATUS_ID") && $ar_wf_element["WF_STATUS_ID"] != 1) {
                 CIBlockElement::WF_CleanUpHistoryCopies($ID);
                 return true;
             }
             $arFields['WF_PARENT_ELEMENT_ID'] = false;
             $rs = $DB->Query("SELECT PREVIEW_PICTURE, DETAIL_PICTURE from b_iblock_element WHERE ID = " . $NID);
             $ar_new_element = $rs->Fetch();
         } else {
             $ar_new_element = false;
         }
         if ($ar_new_element) {
             if (!intval($ar_new_element["PREVIEW_PICTURE"])) {
                 $arFields["PREVIEW_PICTURE"] = false;
             } else {
                 $arFields["PREVIEW_PICTURE"] = $ar_new_element["PREVIEW_PICTURE"];
             }
             if (!intval($ar_new_element["DETAIL_PICTURE"])) {
                 $arFields["DETAIL_PICTURE"] = false;
             } else {
                 $arFields["DETAIL_PICTURE"] = $ar_new_element["DETAIL_PICTURE"];
             }
             if (is_array($arFields["PROPERTY_VALUES"]) && count($arFields["PROPERTY_VALUES"]) > 0) {
                 $i = 0;
                 $db_prop = CIBlockProperty::GetList(array(), array("IBLOCK_ID" => $arFields["IBLOCK_ID"], "CHECK_PERMISSIONS" => "N", "PROPERTY_TYPE" => "F"));
                 while ($arProp = $db_prop->Fetch()) {
                     $i++;
                     unset($arFields["PROPERTY_VALUES"][$arProp["CODE"]]);
                     unset($arFields["PROPERTY_VALUES"][$arProp["ID"]]);
                     $arFields["PROPERTY_VALUES"][$arProp["ID"]] = array();
                 }
                 if ($i > 0) {
                     //Delete previous files
                     $props = CIBlockElement::GetProperty($arFields["IBLOCK_ID"], $ID, "sort", "asc", array("PROPERTY_TYPE" => "F", "EMPTY" => "N"));
                     while ($arProp = $props->Fetch()) {
                         $arFields["PROPERTY_VALUES"][$arProp["ID"]][$arProp['PROPERTY_VALUE_ID']] = array("VALUE" => array("del" => "Y"), "DESCRIPTION" => false);
                     }
                     //Add copy from history
                     $arDup = array();
                     //This is cure for files duplication bug (just save element one more time)
                     $props = CIBlockElement::GetProperty($arFields["IBLOCK_ID"], $NID, "sort", "asc", array("PROPERTY_TYPE" => "F", "EMPTY" => "N"));
                     while ($arProp = $props->Fetch()) {
                         if (!array_key_exists($arProp["VALUE"], $arDup)) {
                             $arFields["PROPERTY_VALUES"][$arProp["ID"]][$arProp['PROPERTY_VALUE_ID']] = array("VALUE" => $arProp["VALUE"], "DESCRIPTION" => $arProp["DESCRIPTION"]);
                             $arDup[$arProp["VALUE"]] = true;
                             //This is cure for files duplication bug
                         }
                     }
                 }
             }
         } else {
             if (array_key_exists("PREVIEW_PICTURE", $arFields)) {
                 CFile::SaveForDB($arFields, "PREVIEW_PICTURE", "iblock");
             }
             if (array_key_exists("DETAIL_PICTURE", $arFields)) {
                 CFile::SaveForDB($arFields, "DETAIL_PICTURE", "iblock");
             }
         }
         unset($arFields["IBLOCK_ID"]);
         unset($arFields["WF_NEW"]);
         unset($arFields["IBLOCK_SECTION_ID"]);
         $bTimeStampNA = false;
         if (is_set($arFields, "TIMESTAMP_X") && ($arFields["TIMESTAMP_X"] === NULL || $arFields["TIMESTAMP_X"] === false)) {
             $bTimeStampNA = true;
             unset($arFields["TIMESTAMP_X"]);
         }
         $strUpdate = $DB->PrepareUpdate("b_iblock_element", $arFields, "iblock");
         if (strlen($strUpdate) > 0) {
             $strUpdate .= ", ";
         }
         $strSql = "UPDATE b_iblock_element SET " . $strUpdate . ($bTimeStampNA ? "TIMESTAMP_X=TIMESTAMP_X" : "TIMESTAMP_X=now()") . " WHERE ID=" . $ID;
         $DB->Query($strSql, false, "FILE: " . __FILE__ . "<br> LINE: " . __LINE__);
         if (array_key_exists("PROPERTY_VALUES", $arFields) && is_array($arFields["PROPERTY_VALUES"]) && count($arFields["PROPERTY_VALUES"]) > 0) {
             CIBlockElement::SetPropertyValues($ID, $ar_element["IBLOCK_ID"], $arFields["PROPERTY_VALUES"]);
         }
         if (is_set($arFields, "IBLOCK_SECTION")) {
             CIBlockElement::SetElementSection($ID, $arFields["IBLOCK_SECTION"], false, $arIBlock["RIGHTS_MODE"] === "E" ? $arIBlock["ID"] : 0);
         }
         if ($arIBlock["RIGHTS_MODE"] === "E") {
             $obElementRights = new CIBlockElementRights($arIBlock["ID"], $ID);
             if (array_key_exists("RIGHTS", $arFields) && is_array($arFields["RIGHTS"])) {
                 $obElementRights->SetRights($arFields["RIGHTS"]);
             }
         }
         if (array_key_exists("IPROPERTY_TEMPLATES", $arFields)) {
             $ipropTemplates = new \Bitrix\Iblock\InheritedProperty\ElementTemplates($arIBlock["ID"], $ID);
             $ipropTemplates->set($arFields["IPROPERTY_TEMPLATES"]);
         }
         if ($bUpdateSearch) {
             CIBlockElement::UpdateSearch($ID, true);
         }
         if ($bWorkFlow) {
             CIBlockElement::WF_CleanUpHistoryCopies($ID);
         }
         //Restore saved values
         if ($SAVED_PREVIEW_PICTURE !== false) {
             $arFields["PREVIEW_PICTURE_ID"] = $arFields["PREVIEW_PICTURE"];
             $arFields["PREVIEW_PICTURE"] = $SAVED_PREVIEW_PICTURE;
         } else {
             unset($arFields["PREVIEW_PICTURE"]);
         }
         if ($SAVED_DETAIL_PICTURE !== false) {
             $arFields["DETAIL_PICTURE_ID"] = $arFields["DETAIL_PICTURE"];
             $arFields["DETAIL_PICTURE"] = $SAVED_DETAIL_PICTURE;
         } else {
             unset($arFields["DETAIL_PICTURE"]);
         }
         if ($arIBlock["FIELDS"]["LOG_ELEMENT_EDIT"]["IS_REQUIRED"] == "Y") {
             $USER_ID = is_object($USER) ? intval($USER->GetID()) : 0;
             $arEvents = GetModuleEvents("main", "OnBeforeEventLog", true);
             if (empty($arEvents) || ExecuteModuleEventEx($arEvents[0], array($USER_ID)) === false) {
                 $rsElement = CIBlockElement::GetList(array(), array("=ID" => $ID, "CHECK_PERMISSIONS" => "N", "SHOW_NEW" => "Y"), false, false, array("ID", "NAME", "LIST_PAGE_URL", "CODE"));
                 $arElement = $rsElement->GetNext();
                 $res = array("ID" => $ID, "CODE" => $arElement["CODE"], "NAME" => $arElement["NAME"], "ELEMENT_NAME" => $arIBlock["ELEMENT_NAME"], "USER_ID" => $USER_ID, "IBLOCK_PAGE_URL" => $arElement["LIST_PAGE_URL"]);
                 CEventLog::Log("IBLOCK", "IBLOCK_ELEMENT_EDIT", "iblock", $arIBlock["ID"], serialize($res));
             }
         }
         $Result = true;
         /************* QUOTA *************/
         $_SESSION["SESS_RECOUNT_DB"] = "Y";
         /************* QUOTA *************/
     }
     $arFields["ID"] = $ID;
     $arFields["IBLOCK_ID"] = $ar_element["IBLOCK_ID"];
     $arFields["RESULT"] =& $Result;
     if (isset($arFields["PREVIEW_PICTURE"]) && $arFields["PREVIEW_PICTURE"]["COPY_FILE"] == "Y" && $arFields["PREVIEW_PICTURE"]["copy"]) {
         @unlink($arFields["PREVIEW_PICTURE"]["tmp_name"]);
         @rmdir(dirname($arFields["PREVIEW_PICTURE"]["tmp_name"]));
     }
     if (isset($arFields["DETAIL_PICTURE"]) && $arFields["DETAIL_PICTURE"]["COPY_FILE"] == "Y" && $arFields["DETAIL_PICTURE"]["copy"]) {
         @unlink($arFields["DETAIL_PICTURE"]["tmp_name"]);
         @rmdir(dirname($arFields["DETAIL_PICTURE"]["tmp_name"]));
     }
     foreach (GetModuleEvents("iblock", "OnAfterIBlockElementUpdate", true) as $arEvent) {
         ExecuteModuleEventEx($arEvent, array(&$arFields));
     }
     if (defined("BX_COMP_MANAGED_CACHE")) {
         $GLOBALS["CACHE_MANAGER"]->ClearByTag("iblock_id_" . $arIBlock["ID"]);
     }
     return $Result;
 }
コード例 #2
0
ファイル: iblockelement.php プロジェクト: DarneoStudio/bitrix
 public static function GetProperty($IBLOCK_ID, $ELEMENT_ID, $by = "sort", $order = "asc", $arFilter = array())
 {
     global $DB;
     if (is_array($by)) {
         if ($order != "asc") {
             $arFilter = $order;
         }
         $arOrder = $by;
     } else {
         $arOrder = false;
     }
     $IBLOCK_ID = intval($IBLOCK_ID);
     $ELEMENT_ID = intval($ELEMENT_ID);
     $VERSION = CIBlockElement::GetIBVersion($IBLOCK_ID);
     $strSqlSearch = "";
     foreach ($arFilter as $key => $val) {
         switch (strtoupper($key)) {
             case "ACTIVE":
                 if ($val == "Y" || $val == "N") {
                     $strSqlSearch .= "AND BP.ACTIVE='" . $val . "'\n";
                 }
                 break;
             case "SEARCHABLE":
                 if ($val == "Y" || $val == "N") {
                     $strSqlSearch .= "AND BP.SEARCHABLE='" . $val . "'\n";
                 }
                 break;
             case "NAME":
                 if (strlen($val) > 0) {
                     $strSqlSearch .= "AND " . CIBLock::_Upper("BP.NAME") . " LIKE " . CIBlock::_Upper("'" . $DB->ForSql($val) . "'") . "\n";
                 }
                 break;
             case "ID":
                 if (is_array($val)) {
                     if (!empty($val)) {
                         $strSqlSearch .= "AND BP.ID in (" . implode(", ", array_map("intval", $val)) . ")\n";
                     }
                 } elseif (strlen($val) > 0) {
                     $strSqlSearch .= "AND BP.ID=" . IntVal($val) . "\n";
                 }
                 break;
             case "PROPERTY_TYPE":
                 if (strlen($val) > 0) {
                     $strSqlSearch .= "AND BP.PROPERTY_TYPE='" . $DB->ForSql($val) . "'\n";
                 }
                 break;
             case "CODE":
                 if (strlen($val) > 0) {
                     $strSqlSearch .= "AND " . CIBLock::_Upper("BP.CODE") . " LIKE " . CIBLock::_Upper("'" . $DB->ForSql($val) . "'") . "\n";
                 }
                 break;
             case "EMPTY":
                 if (strlen($val) > 0) {
                     if ($val == "Y") {
                         $strSqlSearch .= "AND BEP.ID IS NULL\n";
                     } elseif ($VERSION != 2) {
                         $strSqlSearch .= "AND BEP.ID IS NOT NULL\n";
                     }
                 }
                 break;
         }
     }
     $arSqlOrder = array();
     if ($arOrder) {
         foreach ($arOrder as $by => $order) {
             $order = strtolower($order);
             if ($order != "desc") {
                 $order = "asc";
             }
             $by = strtolower($by);
             if ($by == "sort") {
                 $arSqlOrder["BP.SORT"] = $order;
             } elseif ($by == "id") {
                 $arSqlOrder["BP.ID"] = $order;
             } elseif ($by == "name") {
                 $arSqlOrder["BP.NAME"] = $order;
             } elseif ($by == "active") {
                 $arSqlOrder["BP.ACTIVE"] = $order;
             } elseif ($by == "value_id") {
                 $arSqlOrder["BEP.ID"] = $order;
             } elseif ($by == "enum_sort") {
                 $arSqlOrder["BEPE.SORT"] = $order;
             } else {
                 $arSqlOrder["BP.SORT"] = $order;
             }
         }
     } else {
         if ($by == "id") {
             $arSqlOrder["BP.ID"] = "asc";
         } elseif ($by == "name") {
             $arSqlOrder["BP.NAME"] = "asc";
         } elseif ($by == "active") {
             $arSqlOrder["BP.ACTIVE"] = "asc";
         } elseif ($by == "value_id") {
             $arSqlOrder["BEP.ID"] = $order;
         } elseif ($by == "enum_sort") {
             $arSqlOrder["BEPE.SORT"] = $order;
         } else {
             $arSqlOrder["BP.SORT"] = "asc";
             $by = "sort";
         }
         if ($order != "desc") {
             $arSqlOrder["BP.SORT"] = "asc";
             $arSqlOrder["BP.ID"] = "asc";
             $arSqlOrder["BEPE.SORT"] = "asc";
             $arSqlOrder["BEP.ID"] = "asc";
             $order = "asc";
         } else {
             $arSqlOrder["BP.SORT"] = "desc";
             $arSqlOrder["BP.ID"] = "desc";
             $arSqlOrder["BEPE.SORT"] = "desc";
             $arSqlOrder["BEP.ID"] = "desc";
         }
     }
     $strSqlOrder = "";
     foreach ($arSqlOrder as $key => $val) {
         $strSqlOrder .= ", " . $key . " " . $val;
     }
     if ($strSqlOrder != "") {
         $strSqlOrder = ' ORDER BY ' . substr($strSqlOrder, 1);
     }
     if ($VERSION == 2) {
         $strTable = "b_iblock_element_prop_m" . $IBLOCK_ID;
     } else {
         $strTable = "b_iblock_element_property";
     }
     $strSql = "\n\t\t\tSELECT BP.*, BEP.ID as PROPERTY_VALUE_ID, BEP.VALUE, BEP.DESCRIPTION, BEPE.VALUE VALUE_ENUM, BEPE.XML_ID VALUE_XML_ID, BEPE.SORT VALUE_SORT\n\t\t\tFROM b_iblock B\n\t\t\t\tINNER JOIN b_iblock_property BP ON B.ID=BP.IBLOCK_ID\n\t\t\t\tLEFT JOIN " . $strTable . " BEP ON (BP.ID = BEP.IBLOCK_PROPERTY_ID AND BEP.IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . ")\n\t\t\t\tLEFT JOIN b_iblock_property_enum BEPE ON (BP.PROPERTY_TYPE = 'L' AND BEPE.ID=BEP.VALUE_ENUM AND BEPE.PROPERTY_ID=BP.ID)\n\t\t\tWHERE B.ID = " . $IBLOCK_ID . "\n\t\t\t\t" . $strSqlSearch . "\n\t\t\t" . $strSqlOrder;
     if ($VERSION == 2) {
         $result = array();
         $arElements = array();
         $rs = $DB->Query($strSql);
         while ($ar = $rs->Fetch()) {
             if ($ar["VERSION"] == 2 && $ar["MULTIPLE"] == "N") {
                 if (!array_key_exists($ELEMENT_ID, $arElements)) {
                     $strSql = "\n\t\t\t\t\t\t\tSELECT *\n\t\t\t\t\t\t\tFROM b_iblock_element_prop_s" . $ar["IBLOCK_ID"] . "\n\t\t\t\t\t\t\tWHERE IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . "\n\t\t\t\t\t\t";
                     $rs2 = $DB->Query($strSql);
                     $arElements[$ELEMENT_ID] = $rs2->Fetch();
                 }
                 if (!isset($arFilter["EMPTY"]) || $arFilter["EMPTY"] == "Y" || strlen($arElements[$ELEMENT_ID]["PROPERTY_" . $ar["ID"]]) > 0) {
                     $val = $arElements[$ELEMENT_ID]["PROPERTY_" . $ar["ID"]];
                     $ar["PROPERTY_VALUE_ID"] = $ELEMENT_ID . ":" . $ar["ID"];
                     if ($ar["PROPERTY_TYPE"] == "L" && intval($val) > 0) {
                         $arEnum = CIBlockPropertyEnum::GetByID($val);
                         if ($arEnum !== false) {
                             $ar["VALUE_ENUM"] = $arEnum["VALUE"];
                             $ar["VALUE_XML_ID"] = $arEnum["XML_ID"];
                             $ar["VALUE_SORT"] = $arEnum["SORT"];
                         }
                     } else {
                         $ar["VALUE_ENUM"] = "";
                     }
                     if ($ar["PROPERTY_TYPE"] == "N" && strlen($val) > 0) {
                         $val = CIBlock::NumberFormat($val);
                     }
                     $ar["DESCRIPTION"] = $arElements[$ELEMENT_ID]["DESCRIPTION_" . $ar["ID"]];
                     $ar["VALUE"] = $val;
                 } else {
                     continue;
                 }
             }
             if ($arFilter["EMPTY"] == "N" && $ar["PROPERTY_VALUE_ID"] == "") {
                 continue;
             }
             $result[] = $ar;
         }
         $rs = new CIBlockPropertyResult();
         $rs->InitFromArray($result);
     } else {
         $rs = new CIBlockPropertyResult($DB->Query($strSql));
     }
     return $rs;
 }
コード例 #3
0
ファイル: iblockelement.php プロジェクト: DarneoStudio/bitrix
 function SetPropertyValues($ELEMENT_ID, $IBLOCK_ID, $PROPERTY_VALUES, $PROPERTY_CODE = false)
 {
     global $DB;
     global $BX_IBLOCK_PROP_CACHE;
     $ELEMENT_ID = (int) $ELEMENT_ID;
     $IBLOCK_ID = (int) $IBLOCK_ID;
     if (!is_array($PROPERTY_VALUES)) {
         $PROPERTY_VALUES = array($PROPERTY_VALUES);
     }
     $uniq_flt = $IBLOCK_ID;
     $arFilter = array("IBLOCK_ID" => $IBLOCK_ID, "CHECK_PERMISSIONS" => "N");
     if ($PROPERTY_CODE === false) {
         $arFilter["ACTIVE"] = "Y";
         $uniq_flt .= "|ACTIVE:" . $arFilter["ACTIVE"];
     } elseif ((int) $PROPERTY_CODE > 0) {
         $arFilter["ID"] = (int) $PROPERTY_CODE;
         $uniq_flt .= "|ID:" . $arFilter["ID"];
     } else {
         $arFilter["CODE"] = $PROPERTY_CODE;
         $uniq_flt .= "|CODE:" . $arFilter["CODE"];
     }
     if (!isset($BX_IBLOCK_PROP_CACHE[$IBLOCK_ID])) {
         $BX_IBLOCK_PROP_CACHE[$IBLOCK_ID] = array();
     }
     if (!isset($BX_IBLOCK_PROP_CACHE[$IBLOCK_ID][$uniq_flt])) {
         $BX_IBLOCK_PROP_CACHE[$IBLOCK_ID][$uniq_flt] = array();
         $db_prop = CIBlockProperty::GetList(array(), $arFilter);
         while ($prop = $db_prop->Fetch()) {
             $BX_IBLOCK_PROP_CACHE[$IBLOCK_ID][$uniq_flt][$prop["ID"]] = $prop;
         }
         unset($prop);
         unset($db_prop);
     }
     $ar_prop =& $BX_IBLOCK_PROP_CACHE[$IBLOCK_ID][$uniq_flt];
     reset($ar_prop);
     $bRecalcSections = false;
     //Read current property values from database
     $arDBProps = array();
     if (CIBLock::GetArrayByID($IBLOCK_ID, "VERSION") == 2) {
         $rs = $DB->Query("\n\t\t\t\tselect *\n\t\t\t\tfrom b_iblock_element_prop_m" . $IBLOCK_ID . "\n\t\t\t\twhere IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . "\n\t\t\t\torder by ID asc\n\t\t\t");
         while ($ar = $rs->Fetch()) {
             $property_id = $ar["IBLOCK_PROPERTY_ID"];
             if (!isset($arDBProps[$property_id])) {
                 $arDBProps[$property_id] = array();
             }
             $arDBProps[$property_id][$ar["ID"]] = $ar;
         }
         unset($ar);
         unset($rs);
         $rs = $DB->Query("\n\t\t\t\tselect *\n\t\t\t\tfrom b_iblock_element_prop_s" . $IBLOCK_ID . "\n\t\t\t\twhere IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . "\n\t\t\t");
         if ($ar = $rs->Fetch()) {
             foreach ($ar_prop as $property) {
                 $property_id = $property["ID"];
                 if ($property["MULTIPLE"] == "N" && isset($ar["PROPERTY_" . $property_id]) && strlen($ar["PROPERTY_" . $property_id])) {
                     if (!isset($arDBProps[$property_id])) {
                         $arDBProps[$property_id] = array();
                     }
                     $arDBProps[$property_id][$ELEMENT_ID . ":" . $property_id] = array("ID" => $ELEMENT_ID . ":" . $property_id, "IBLOCK_PROPERTY_ID" => $property_id, "VALUE" => $ar["PROPERTY_" . $property_id], "DESCRIPTION" => $ar["DESCRIPTION_" . $property_id]);
                 }
             }
             if (isset($property)) {
                 unset($property);
             }
         }
         unset($ar);
         unset($rs);
     } else {
         $rs = $DB->Query("\n\t\t\t\tselect *\n\t\t\t\tfrom b_iblock_element_property\n\t\t\t\twhere IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . "\n\t\t\t\torder by ID asc\n\t\t\t");
         while ($ar = $rs->Fetch()) {
             $property_id = $ar["IBLOCK_PROPERTY_ID"];
             if (!isset($arDBProps[$property_id])) {
                 $arDBProps[$property_id] = array();
             }
             $arDBProps[$property_id][$ar["ID"]] = $ar;
         }
         unset($ar);
         unset($rs);
     }
     foreach (GetModuleEvents("iblock", "OnIBlockElementSetPropertyValues", true) as $arEvent) {
         ExecuteModuleEventEx($arEvent, array($ELEMENT_ID, $IBLOCK_ID, $PROPERTY_VALUES, $PROPERTY_CODE, $ar_prop, $arDBProps));
     }
     if (isset($arEvent)) {
         unset($arEvent);
     }
     $arFilesToDelete = array();
     $arV2ClearCache = array();
     foreach ($ar_prop as $prop) {
         if ($PROPERTY_CODE) {
             $PROP = $PROPERTY_VALUES;
         } else {
             if (strlen($prop["CODE"]) > 0 && array_key_exists($prop["CODE"], $PROPERTY_VALUES)) {
                 $PROP = $PROPERTY_VALUES[$prop["CODE"]];
             } else {
                 $PROP = $PROPERTY_VALUES[$prop["ID"]];
             }
         }
         if (!is_array($PROP) || $prop["PROPERTY_TYPE"] == "F" && (array_key_exists("tmp_name", $PROP) || array_key_exists("del", $PROP)) || count($PROP) == 2 && array_key_exists("VALUE", $PROP) && array_key_exists("DESCRIPTION", $PROP)) {
             $PROP = array($PROP);
         }
         if ($prop["USER_TYPE"] != "") {
             $arUserType = CIBlockProperty::GetUserType($prop["USER_TYPE"]);
             if (array_key_exists("ConvertToDB", $arUserType)) {
                 foreach ($PROP as $key => $value) {
                     if (!is_array($value) || !array_key_exists("VALUE", $value)) {
                         $value = array("VALUE" => $value);
                     }
                     $prop["ELEMENT_ID"] = $ELEMENT_ID;
                     $PROP[$key] = call_user_func_array($arUserType["ConvertToDB"], array($prop, $value));
                 }
             }
         }
         if ($prop["VERSION"] == 2) {
             if ($prop["MULTIPLE"] == "Y") {
                 $strTable = "b_iblock_element_prop_m" . $prop["IBLOCK_ID"];
             } else {
                 $strTable = "b_iblock_element_prop_s" . $prop["IBLOCK_ID"];
             }
         } else {
             $strTable = "b_iblock_element_property";
         }
         if ($prop["PROPERTY_TYPE"] == "L") {
             $DB->Query(CIBLockElement::DeletePropertySQL($prop, $ELEMENT_ID));
             if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") {
                 $arV2ClearCache[$prop["ID"]] = "PROPERTY_" . $prop["ID"] . " = NULL" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"]);
             }
             $ids = "0";
             foreach ($PROP as $key => $value) {
                 if (is_array($value)) {
                     $value = intval($value["VALUE"]);
                 } else {
                     $value = intval($value);
                 }
                 if ($value <= 0) {
                     continue;
                 }
                 $ids .= "," . $value;
                 if ($prop["MULTIPLE"] != "Y") {
                     break;
                 }
             }
             if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") {
                 $DB->Query("\n\t\t\t\t\t\tUPDATE\n\t\t\t\t\t\t\tb_iblock_element_prop_s" . $prop["IBLOCK_ID"] . " E\n\t\t\t\t\t\t\t,b_iblock_property P\n\t\t\t\t\t\t\t,b_iblock_property_enum PEN\n\t\t\t\t\t\tSET\n\t\t\t\t\t\t\tE.PROPERTY_" . $prop["ID"] . " = PEN.ID\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tE.IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . "\n\t\t\t\t\t\t\tAND P.ID = " . $prop["ID"] . "\n\t\t\t\t\t\t\tAND P.ID = PEN.PROPERTY_ID\n\t\t\t\t\t\t\tAND PEN.ID IN (" . $ids . ")\n\t\t\t\t\t");
             } else {
                 $DB->Query("\n\t\t\t\t\t\tINSERT INTO " . $strTable . "\n\t\t\t\t\t\t(IBLOCK_ELEMENT_ID, IBLOCK_PROPERTY_ID, VALUE, VALUE_ENUM)\n\t\t\t\t\t\tSELECT " . $ELEMENT_ID . ", P.ID, PEN.ID, PEN.ID\n\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\tb_iblock_property P\n\t\t\t\t\t\t\t,b_iblock_property_enum PEN\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tP.ID = " . $prop["ID"] . "\n\t\t\t\t\t\t\tAND P.ID = PEN.PROPERTY_ID\n\t\t\t\t\t\t\tAND PEN.ID IN (" . $ids . ")\n\t\t\t\t\t");
             }
         } elseif ($prop["PROPERTY_TYPE"] == "G") {
             $bRecalcSections = true;
             $DB->Query(CIBLockElement::DeletePropertySQL($prop, $ELEMENT_ID));
             if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") {
                 $arV2ClearCache[$prop["ID"]] = "PROPERTY_" . $prop["ID"] . " = NULL" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"]);
             }
             $DB->Query("\n\t\t\t\t\tDELETE FROM b_iblock_section_element\n\t\t\t\t\tWHERE ADDITIONAL_PROPERTY_ID = " . $prop["ID"] . "\n\t\t\t\t\tAND IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . "\n\t\t\t\t");
             $ids = "0";
             foreach ($PROP as $key => $value) {
                 if (is_array($value)) {
                     $value = intval($value["VALUE"]);
                 } else {
                     $value = intval($value);
                 }
                 if ($value <= 0) {
                     continue;
                 }
                 $ids .= "," . $value;
                 if ($prop["MULTIPLE"] != "Y") {
                     break;
                 }
             }
             if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") {
                 $DB->Query("\n\t\t\t\t\t\tUPDATE\n\t\t\t\t\t\t\tb_iblock_element_prop_s" . $prop["IBLOCK_ID"] . " E\n\t\t\t\t\t\t\t,b_iblock_property P\n\t\t\t\t\t\t\t,b_iblock_section S\n\t\t\t\t\t\tSET\n\t\t\t\t\t\t\tE.PROPERTY_" . $prop["ID"] . " = S.ID\n\t\t\t\t\t\t\t" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"]) . "\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tE.IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . "\n\t\t\t\t\t\t\tAND P.ID = " . $prop["ID"] . "\n\t\t\t\t\t\t\tAND (\n\t\t\t\t\t\t\t\tP.LINK_IBLOCK_ID IS NULL\n\t\t\t\t\t\t\t\tOR P.LINK_IBLOCK_ID = 0\n\t\t\t\t\t\t\t\tOR S.IBLOCK_ID = P.LINK_IBLOCK_ID\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\tAND S.ID IN (" . $ids . ")\n\t\t\t\t\t");
             } else {
                 $DB->Query("\n\t\t\t\t\t\tINSERT INTO " . $strTable . "\n\t\t\t\t\t\t(IBLOCK_ELEMENT_ID, IBLOCK_PROPERTY_ID, VALUE, VALUE_NUM)\n\t\t\t\t\t\tSELECT " . $ELEMENT_ID . ", P.ID, S.ID, S.ID\n\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\tb_iblock_property P\n\t\t\t\t\t\t\t,b_iblock_section S\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tP.ID=" . $prop["ID"] . "\n\t\t\t\t\t\t\tAND (\n\t\t\t\t\t\t\t\tP.LINK_IBLOCK_ID IS NULL\n\t\t\t\t\t\t\t\tOR P.LINK_IBLOCK_ID = 0\n\t\t\t\t\t\t\t\tOR S.IBLOCK_ID = P.LINK_IBLOCK_ID\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\tAND S.ID IN (" . $ids . ")\n\t\t\t\t\t");
             }
             $DB->Query("\n\t\t\t\t\tINSERT INTO b_iblock_section_element\n\t\t\t\t\t(IBLOCK_ELEMENT_ID, IBLOCK_SECTION_ID, ADDITIONAL_PROPERTY_ID)\n\t\t\t\t\tSELECT " . $ELEMENT_ID . ", S.ID, P.ID\n\t\t\t\t\tFROM\n\t\t\t\t\t\tb_iblock_property P\n\t\t\t\t\t\t,b_iblock_section S\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tP.ID = " . $prop["ID"] . "\n\t\t\t\t\t\tAND (\n\t\t\t\t\t\t\tP.LINK_IBLOCK_ID IS NULL\n\t\t\t\t\t\t\tOR P.LINK_IBLOCK_ID = 0\n\t\t\t\t\t\t\tOR S.IBLOCK_ID = P.LINK_IBLOCK_ID\n\t\t\t\t\t\t)\n\t\t\t\t\t\tAND S.ID IN (" . $ids . ")\n\t\t\t\t");
         } elseif ($prop["PROPERTY_TYPE"] == "E") {
             $arWas = array();
             if ($arDBProps[$prop["ID"]]) {
                 foreach ($arDBProps[$prop["ID"]] as $res) {
                     $val = $PROP[$res["ID"]];
                     if (is_array($val)) {
                         $val_desc = $val["DESCRIPTION"];
                         $val = $val["VALUE"];
                     } else {
                         $val_desc = false;
                     }
                     if (isset($arWas[$val])) {
                         $val = "";
                     } else {
                         $arWas[$val] = true;
                     }
                     if (strlen($val) <= 0) {
                         if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") {
                             $DB->Query($s = "\n\t\t\t\t\t\t\t\t\tUPDATE b_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\t\t\tSET PROPERTY_" . $prop["ID"] . " = null\n\t\t\t\t\t\t\t\t\t" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"]) . "\n\t\t\t\t\t\t\t\t\tWHERE IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\t");
                         } else {
                             $DB->Query($s = "\n\t\t\t\t\t\t\t\t\tDELETE FROM " . $strTable . "\n\t\t\t\t\t\t\t\t\tWHERE ID=" . $res["ID"] . "\n\t\t\t\t\t\t\t\t");
                         }
                         if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") {
                             $arV2ClearCache[$prop["ID"]] = "PROPERTY_" . $prop["ID"] . " = NULL" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"]);
                         }
                     } elseif ($res["VALUE"] !== $val || $res["DESCRIPTION"] . '' !== $val_desc . '') {
                         if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") {
                             $DB->Query("\n\t\t\t\t\t\t\t\t\tUPDATE b_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\t\t\tSET PROPERTY_" . $prop["ID"] . " = '" . $DB->ForSql($val) . "'\n\t\t\t\t\t\t\t\t\t" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"], $val_desc) . "\n\t\t\t\t\t\t\t\t\tWHERE IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\t");
                         } else {
                             $DB->Query("\n\t\t\t\t\t\t\t\t\tUPDATE " . $strTable . "\n\t\t\t\t\t\t\t\t\tSET VALUE = '" . $DB->ForSql($val) . "'\n\t\t\t\t\t\t\t\t\t\t,VALUE_NUM = " . CIBlock::roundDB($val) . "\n\t\t\t\t\t\t\t\t\t\t" . ($val_desc !== false ? ",DESCRIPTION = '" . $DB->ForSql($val_desc, 255) . "'" : "") . "\n\t\t\t\t\t\t\t\t\tWHERE ID=" . $res["ID"] . "\n\t\t\t\t\t\t\t\t");
                         }
                         if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") {
                             $arV2ClearCache[$prop["ID"]] = "PROPERTY_" . $prop["ID"] . " = NULL" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"]);
                         }
                     }
                     unset($PROP[$res["ID"]]);
                 }
                 //foreach($arDBProps[$prop["ID"]] as $res)
             }
             foreach ($PROP as $val) {
                 if (is_array($val)) {
                     $val_desc = $val["DESCRIPTION"];
                     $val = $val["VALUE"];
                 } else {
                     $val_desc = false;
                 }
                 if (isset($arWas[$val])) {
                     $val = "";
                 } else {
                     $arWas[$val] = true;
                 }
                 if (strlen($val) <= 0) {
                     continue;
                 }
                 if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") {
                     $DB->Query("\n\t\t\t\t\t\t\tUPDATE b_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\tSET\n\t\t\t\t\t\t\t\tPROPERTY_" . $prop["ID"] . " = '" . $DB->ForSql($val) . "'\n\t\t\t\t\t\t\t\t" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"], $val_desc) . "\n\t\t\t\t\t\t\tWHERE IBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\t\t");
                 } else {
                     $DB->Query("\n\t\t\t\t\t\t\tINSERT INTO " . $strTable . "\n\t\t\t\t\t\t\t(IBLOCK_ELEMENT_ID, IBLOCK_PROPERTY_ID, VALUE, VALUE_NUM" . ($val_desc !== false ? ", DESCRIPTION" : "") . ")\n\t\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\t\t" . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\t,P.ID\n\t\t\t\t\t\t\t\t,'" . $DB->ForSql($val) . "'\n\t\t\t\t\t\t\t\t," . CIBlock::roundDB($val) . "\n\t\t\t\t\t\t\t\t" . ($val_desc !== false ? ", '" . $DB->ForSQL($val_desc, 255) . "'" : "") . "\n\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\tb_iblock_property P\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tID = " . IntVal($prop["ID"]) . "\n\t\t\t\t\t\t");
                 }
                 if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") {
                     $arV2ClearCache[$prop["ID"]] = "PROPERTY_" . $prop["ID"] . " = NULL" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"]);
                 }
                 if ($prop["MULTIPLE"] != "Y") {
                     break;
                 }
             }
             //foreach($PROP as $value)
         } elseif ($prop["PROPERTY_TYPE"] == "F") {
             //We'll be adding values from the database into the head
             //for multiple values and into tje tail for single
             //these values were not passed into API call.
             if ($prop["MULTIPLE"] == "Y") {
                 $orderedPROP = array_reverse($PROP, true);
             } else {
                 $orderedPROP = $PROP;
             }
             if ($arDBProps[$prop["ID"]]) {
                 //Go from high ID to low
                 foreach (array_reverse($arDBProps[$prop["ID"]], true) as $res) {
                     //Preserve description from database
                     if (strlen($res["DESCRIPTION"])) {
                         $description = $res["DESCRIPTION"];
                     } else {
                         $description = false;
                     }
                     if (!array_key_exists($res["ID"], $orderedPROP)) {
                         $orderedPROP[$res["ID"]] = array("VALUE" => $res["VALUE"], "DESCRIPTION" => $description);
                     } else {
                         $val = $orderedPROP[$res["ID"]];
                         if (is_array($val) && !array_key_exists("tmp_name", $val) && !array_key_exists("del", $val)) {
                             $val = $val["VALUE"];
                         }
                         //Check if no new file and no delete command
                         if (!strlen($val["tmp_name"]) && !strlen($val["del"])) {
                             //But save description from incoming value
                             if (array_key_exists("description", $val)) {
                                 $description = trim($val["description"]);
                             }
                             $orderedPROP[$res["ID"]] = array("VALUE" => $res["VALUE"], "DESCRIPTION" => $description);
                         }
                     }
                 }
             }
             //Restore original order
             if ($prop["MULTIPLE"] == "Y") {
                 $orderedPROP = array_reverse($orderedPROP, true);
             }
             $preserveID = array();
             //Now delete from database all marked for deletion  records
             if ($arDBProps[$prop["ID"]]) {
                 foreach ($arDBProps[$prop["ID"]] as $res) {
                     $val = $orderedPROP[$res["ID"]];
                     if (is_array($val) && !array_key_exists("tmp_name", $val) && !array_key_exists("del", $val)) {
                         $val = $val["VALUE"];
                     }
                     if (is_array($val) && strlen($val["del"])) {
                         unset($orderedPROP[$res["ID"]]);
                         $arFilesToDelete[$res["VALUE"]] = array("FILE_ID" => $res["VALUE"], "ELEMENT_ID" => $ELEMENT_ID, "IBLOCK_ID" => $prop["IBLOCK_ID"]);
                     } elseif ($prop["MULTIPLE"] != "Y") {
                         //Delete all stored in database for replacement.
                         $arFilesToDelete[$res["VALUE"]] = array("FILE_ID" => $res["VALUE"], "ELEMENT_ID" => $ELEMENT_ID, "IBLOCK_ID" => $prop["IBLOCK_ID"]);
                     }
                     if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") {
                         $DB->Query("\n\t\t\t\t\t\t\t\tUPDATE b_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\t\tSET PROPERTY_" . $prop["ID"] . " = null\n\t\t\t\t\t\t\t\t" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"]) . "\n\t\t\t\t\t\t\t\tWHERE IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . "\n\t\t\t\t\t\t\t");
                     } else {
                         $DB->Query("DELETE FROM " . $strTable . " WHERE ID = " . $res["ID"]);
                         $preserveID[$res["ID"]] = $res["ID"];
                     }
                     if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") {
                         $arV2ClearCache[$prop["ID"]] = "PROPERTY_" . $prop["ID"] . " = NULL" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"]);
                     }
                 }
                 //foreach($arDBProps[$prop["ID"]] as $res)
             }
             //Check if we have to save property values id's
             if ($preserveID) {
                 //Find tail mark where all added files started
                 $tailStart = null;
                 foreach (array_reverse($orderedPROP, true) as $propertyValueId => $val) {
                     if (intval($propertyValueId) > 0) {
                         break;
                     }
                     $tailStart = $propertyValueId;
                 }
                 $prevId = 0;
                 foreach ($orderedPROP as $propertyValueId => $val) {
                     if ($propertyValueId === $tailStart) {
                         break;
                     }
                     if (intval($propertyValueId) < $prevId) {
                         $preserveID = array();
                         break;
                     }
                     $prevId = $propertyValueId;
                 }
             }
             //Write new values into database in specified order
             foreach ($orderedPROP as $propertyValueId => $val) {
                 if (is_array($val) && !array_key_exists("tmp_name", $val)) {
                     $val_desc = $val["DESCRIPTION"];
                     $val = $val["VALUE"];
                 } else {
                     $val_desc = false;
                 }
                 if (is_array($val)) {
                     $val["MODULE_ID"] = "iblock";
                     if ($val_desc !== false) {
                         $val["description"] = $val_desc;
                     }
                     $val = CFile::SaveFile($val, "iblock");
                 } elseif ($val > 0 && $val_desc !== false) {
                     CFile::UpdateDesc($val, $val_desc);
                 }
                 if (intval($val) <= 0) {
                     continue;
                 }
                 if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") {
                     $DB->Query($s = "\n\t\t\t\t\t\t\tUPDATE b_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\tSET\n\t\t\t\t\t\t\t\tPROPERTY_" . $prop["ID"] . " = '" . $DB->ForSql($val) . "'\n\t\t\t\t\t\t\t\t" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"], $val_desc) . "\n\t\t\t\t\t\t\tWHERE IBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\t\t");
                 } elseif (array_key_exists($propertyValueId, $preserveID)) {
                     $DB->Query("\n\t\t\t\t\t\t\tINSERT INTO " . $strTable . "\n\t\t\t\t\t\t\t(ID, IBLOCK_ELEMENT_ID, IBLOCK_PROPERTY_ID, VALUE, VALUE_NUM" . ($val_desc !== false ? ", DESCRIPTION" : "") . ")\n\t\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\t\t" . $preserveID[$propertyValueId] . "\n\t\t\t\t\t\t\t\t," . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\t,P.ID\n\t\t\t\t\t\t\t\t,'" . $DB->ForSql($val) . "'\n\t\t\t\t\t\t\t\t," . CIBlock::roundDB($val) . "\n\t\t\t\t\t\t\t\t" . ($val_desc !== false ? ", '" . $DB->ForSQL($val_desc, 255) . "'" : "") . "\n\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\tb_iblock_property P\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tID = " . IntVal($prop["ID"]) . "\n\t\t\t\t\t\t");
                 } else {
                     $DB->Query("\n\t\t\t\t\t\t\tINSERT INTO " . $strTable . "\n\t\t\t\t\t\t\t(IBLOCK_ELEMENT_ID, IBLOCK_PROPERTY_ID, VALUE, VALUE_NUM" . ($val_desc !== false ? ", DESCRIPTION" : "") . ")\n\t\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\t\t" . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\t,P.ID\n\t\t\t\t\t\t\t\t,'" . $DB->ForSql($val) . "'\n\t\t\t\t\t\t\t\t," . CIBlock::roundDB($val) . "\n\t\t\t\t\t\t\t\t" . ($val_desc !== false ? ", '" . $DB->ForSQL($val_desc, 255) . "'" : "") . "\n\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\tb_iblock_property P\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tID = " . IntVal($prop["ID"]) . "\n\t\t\t\t\t\t");
                 }
                 if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") {
                     $arV2ClearCache[$prop["ID"]] = "PROPERTY_" . $prop["ID"] . " = NULL" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"]);
                 }
                 if ($prop["MULTIPLE"] != "Y") {
                     break;
                 }
             }
             //foreach($PROP as $value)
         } else {
             if ($arDBProps[$prop["ID"]]) {
                 foreach ($arDBProps[$prop["ID"]] as $res) {
                     $val = $PROP[$res["ID"]];
                     if (is_array($val)) {
                         $val_desc = $val["DESCRIPTION"];
                         $val = $val["VALUE"];
                     } else {
                         $val_desc = false;
                     }
                     if (strlen($val) <= 0) {
                         if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") {
                             $DB->Query("\n\t\t\t\t\t\t\t\t\tUPDATE b_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\t\t\tSET\n\t\t\t\t\t\t\t\t\t\tPROPERTY_" . $prop["ID"] . " = null\n\t\t\t\t\t\t\t\t\t\t" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"]) . "\n\t\t\t\t\t\t\t\t\tWHERE IBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\t");
                         } else {
                             $DB->Query("DELETE FROM " . $strTable . " WHERE ID=" . $res["ID"]);
                         }
                         if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") {
                             $arV2ClearCache[$prop["ID"]] = "PROPERTY_" . $prop["ID"] . " = NULL" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"]);
                         }
                     } else {
                         if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") {
                             if ($prop["PROPERTY_TYPE"] == "N") {
                                 $val = CIBlock::roundDB($val);
                             }
                             $DB->Query("\n\t\t\t\t\t\t\t\t\tUPDATE b_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\t\t\tSET PROPERTY_" . $prop["ID"] . "='" . $DB->ForSql($val) . "'\n\t\t\t\t\t\t\t\t\t" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"], $val_desc) . "\n\t\t\t\t\t\t\t\t\tWHERE IBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\t");
                         } else {
                             $DB->Query("\n\t\t\t\t\t\t\t\t\tUPDATE " . $strTable . "\n\t\t\t\t\t\t\t\t\tSET \tVALUE='" . $DB->ForSql($val) . "'\n\t\t\t\t\t\t\t\t\t\t,VALUE_NUM=" . CIBlock::roundDB($val) . "\n\t\t\t\t\t\t\t\t\t\t" . ($val_desc !== false ? ",DESCRIPTION='" . $DB->ForSql($val_desc, 255) . "'" : "") . "\n\t\t\t\t\t\t\t\t\tWHERE ID=" . $res["ID"] . "\n\t\t\t\t\t\t\t\t");
                         }
                         if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") {
                             $arV2ClearCache[$prop["ID"]] = "PROPERTY_" . $prop["ID"] . " = NULL" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"]);
                         }
                     }
                     unset($PROP[$res["ID"]]);
                 }
                 //foreach ($arDBProps[$prop["ID"]] as $res)
             }
             foreach ($PROP as $val) {
                 if (is_array($val) && !is_set($val, "tmp_name")) {
                     $val_desc = $val["DESCRIPTION"];
                     $val = $val["VALUE"];
                 } else {
                     $val_desc = false;
                 }
                 if (strlen($val) <= 0) {
                     continue;
                 }
                 if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") {
                     if ($prop["PROPERTY_TYPE"] == "N") {
                         $val = CIBlock::roundDB($val);
                     }
                     $DB->Query("\n\t\t\t\t\t\t\tUPDATE b_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\tSET\n\t\t\t\t\t\t\t\tPROPERTY_" . $prop["ID"] . " = '" . $DB->ForSql($val) . "'\n\t\t\t\t\t\t\t\t" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"], $val_desc) . "\n\t\t\t\t\t\t\tWHERE IBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\t\t");
                 } else {
                     $DB->Query("\n\t\t\t\t\t\t\tINSERT INTO " . $strTable . "\n\t\t\t\t\t\t\t(IBLOCK_ELEMENT_ID, IBLOCK_PROPERTY_ID, VALUE, VALUE_NUM" . ($val_desc !== false ? ", DESCRIPTION" : "") . ")\n\t\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\t\t" . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\t,P.ID\n\t\t\t\t\t\t\t\t,'" . $DB->ForSql($val) . "'\n\t\t\t\t\t\t\t\t," . CIBlock::roundDB($val) . "\n\t\t\t\t\t\t\t\t" . ($val_desc !== false ? ", '" . $DB->ForSQL($val_desc, 255) . "'" : "") . "\n\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\tb_iblock_property P\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tID = " . IntVal($prop["ID"]) . "\n\t\t\t\t\t\t");
                 }
                 if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") {
                     $arV2ClearCache[$prop["ID"]] = "PROPERTY_" . $prop["ID"] . " = NULL" . self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"]);
                 }
                 if ($prop["MULTIPLE"] != "Y") {
                     break;
                 }
             }
             //foreach($PROP as $value)
         }
         //if($prop["PROPERTY_TYPE"]=="F")
     }
     if ($arV2ClearCache) {
         $DB->Query("\n\t\t\t\tUPDATE b_iblock_element_prop_s" . $IBLOCK_ID . "\n\t\t\t\tSET " . implode(",", $arV2ClearCache) . "\n\t\t\t\tWHERE IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . "\n\t\t\t");
     }
     foreach ($arFilesToDelete as $deleteTask) {
         CIBLockElement::DeleteFile($deleteTask["FILE_ID"], false, "PROPERTY", $deleteTask["ELEMENT_ID"], $deleteTask["IBLOCK_ID"]);
     }
     if ($bRecalcSections) {
         CIBlockElement::RecalcSections($ELEMENT_ID);
     }
     /****************************** QUOTA ******************************/
     $_SESSION["SESS_RECOUNT_DB"] = "Y";
     /****************************** QUOTA ******************************/
     foreach (GetModuleEvents("iblock", "OnAfterIBlockElementSetPropertyValues", true) as $arEvent) {
         ExecuteModuleEventEx($arEvent, array($ELEMENT_ID, $IBLOCK_ID, $PROPERTY_VALUES, $PROPERTY_CODE));
     }
 }
コード例 #4
0
 function Update($ID, $arFields, $bResort = true, $bUpdateSearch = true, $bResizePictures = false)
 {
     global $USER, $DB, $APPLICATION;
     $ID = intval($ID);
     $db_record = CIBlockSection::GetList(array(), array("ID" => $ID, "CHECK_PERMISSIONS" => "N"));
     if (!($db_record = $db_record->Fetch())) {
         return false;
     }
     if (is_set($arFields, "EXTERNAL_ID")) {
         $arFields["XML_ID"] = $arFields["EXTERNAL_ID"];
     }
     unset($arFields["GLOBAL_ACTIVE"]);
     unset($arFields["DEPTH_LEVEL"]);
     unset($arFields["LEFT_MARGIN"]);
     unset($arFields["RIGHT_MARGIN"]);
     unset($arFields["IBLOCK_ID"]);
     unset($arFields["DATE_CREATE"]);
     unset($arFields["CREATED_BY"]);
     $arIBlock = CIBlock::GetArrayByID($db_record["IBLOCK_ID"]);
     if ($bResizePictures) {
         $arDef = $arIBlock["FIELDS"]["SECTION_PICTURE"]["DEFAULT_VALUE"];
         if ($arDef["DELETE_WITH_DETAIL"] === "Y" && $arFields["DETAIL_PICTURE"]["del"] === "Y") {
             $arFields["PICTURE"]["del"] = "Y";
         }
         if ($arDef["FROM_DETAIL"] === "Y" && ($arFields["PICTURE"]["size"] <= 0 || $arDef["UPDATE_WITH_DETAIL"] === "Y") && is_array($arFields["DETAIL_PICTURE"]) && $arFields["DETAIL_PICTURE"]["size"] > 0) {
             if ($arFields["PICTURE"]["del"] !== "Y" && $arDef["UPDATE_WITH_DETAIL"] !== "Y") {
                 $arOldSection = $db_record;
             } else {
                 $arOldSection = false;
             }
             if (!$arOldSection || !$arOldSection["PICTURE"]) {
                 $arNewPreview = $arFields["DETAIL_PICTURE"];
                 $arNewPreview["COPY_FILE"] = "Y";
                 $arNewPreview["description"] = $arFields["PICTURE"]["description"];
                 $arFields["PICTURE"] = $arNewPreview;
             }
         }
         if (array_key_exists("PICTURE", $arFields) && is_array($arFields["PICTURE"]) && $arFields["PICTURE"]["size"] > 0 && $arDef["SCALE"] === "Y") {
             $arNewPicture = CIBlock::ResizePicture($arFields["PICTURE"], $arDef);
             if (is_array($arNewPicture)) {
                 $arNewPicture["description"] = $arFields["PICTURE"]["description"];
                 $arFields["PICTURE"] = $arNewPicture;
             } elseif ($arDef["IGNORE_ERRORS"] !== "Y") {
                 unset($arFields["PICTURE"]);
                 $strWarning .= GetMessage("IBLOCK_FIELD_PREVIEW_PICTURE") . ": " . $arNewPicture . "<br>";
             }
         }
         if (array_key_exists("PICTURE", $arFields) && is_array($arFields["PICTURE"]) && $arDef["USE_WATERMARK_FILE"] === "Y") {
             if (strlen($arFields["PICTURE"]["tmp_name"]) > 0 && $arFields["PICTURE"]["tmp_name"] === $arFields["DETAIL_PICTURE"]["tmp_name"]) {
                 $arFields["PICTURE"]["tmp_name"] = CTempFile::GetFileName(basename($arFields["PICTURE"]["tmp_name"]));
                 CheckDirPath($arFields["PICTURE"]["tmp_name"]);
                 copy($arFields["DETAIL_PICTURE"]["tmp_name"], $arFields["PICTURE"]["tmp_name"]);
             }
             CIBLock::FilterPicture($arFields["PICTURE"]["tmp_name"], array("name" => "watermark", "position" => $arDef["WATERMARK_FILE_POSITION"], "type" => "file", "size" => "real", "alpha_level" => 100 - min(max($arDef["WATERMARK_FILE_ALPHA"], 0), 100), "file" => $_SERVER["DOCUMENT_ROOT"] . Rel2Abs("/", $arDef["WATERMARK_FILE"])));
         }
         if (array_key_exists("PICTURE", $arFields) && is_array($arFields["PICTURE"]) && $arDef["USE_WATERMARK_TEXT"] === "Y") {
             if (strlen($arFields["PICTURE"]["tmp_name"]) > 0 && $arFields["PICTURE"]["tmp_name"] === $arFields["DETAIL_PICTURE"]["tmp_name"]) {
                 $arFields["PICTURE"]["tmp_name"] = CTempFile::GetFileName(basename($arFields["PICTURE"]["tmp_name"]));
                 CheckDirPath($arFields["PICTURE"]["tmp_name"]);
                 copy($arFields["DETAIL_PICTURE"]["tmp_name"], $arFields["PICTURE"]["tmp_name"]);
             }
             CIBLock::FilterPicture($arFields["PICTURE"]["tmp_name"], array("name" => "watermark", "position" => $arDef["WATERMARK_TEXT_POSITION"], "type" => "text", "coefficient" => $arDef["WATERMARK_TEXT_SIZE"], "text" => $arDef["WATERMARK_TEXT"], "font" => $_SERVER["DOCUMENT_ROOT"] . Rel2Abs("/", $arDef["WATERMARK_TEXT_FONT"]), "color" => $arDef["WATERMARK_TEXT_COLOR"]));
         }
         $arDef = $arIBlock["FIELDS"]["SECTION_DETAIL_PICTURE"]["DEFAULT_VALUE"];
         if (array_key_exists("DETAIL_PICTURE", $arFields) && is_array($arFields["DETAIL_PICTURE"]) && $arDef["SCALE"] === "Y") {
             $arNewPicture = CIBlock::ResizePicture($arFields["DETAIL_PICTURE"], $arDef);
             if (is_array($arNewPicture)) {
                 $arNewPicture["description"] = $arFields["DETAIL_PICTURE"]["description"];
                 $arFields["DETAIL_PICTURE"] = $arNewPicture;
             } elseif ($arDef["IGNORE_ERRORS"] !== "Y") {
                 unset($arFields["DETAIL_PICTURE"]);
                 $strWarning .= GetMessage("IBLOCK_FIELD_DETAIL_PICTURE") . ": " . $arNewPicture . "<br>";
             }
         }
         if (array_key_exists("DETAIL_PICTURE", $arFields) && is_array($arFields["DETAIL_PICTURE"]) && $arDef["USE_WATERMARK_FILE"] === "Y") {
             if (strlen($arFields["DETAIL_PICTURE"]["tmp_name"]) > 0 && $arFields["DETAIL_PICTURE"]["tmp_name"] === $arFields["PICTURE"]["tmp_name"]) {
                 $arFields["DETAIL_PICTURE"]["tmp_name"] = CTempFile::GetFileName(basename($arFields["DETAIL_PICTURE"]["tmp_name"]));
                 CheckDirPath($arFields["DETAIL_PICTURE"]["tmp_name"]);
                 copy($arFields["PICTURE"]["tmp_name"], $arFields["DETAIL_PICTURE"]["tmp_name"]);
             }
             CIBLock::FilterPicture($arFields["DETAIL_PICTURE"]["tmp_name"], array("name" => "watermark", "position" => $arDef["WATERMARK_FILE_POSITION"], "type" => "file", "size" => "real", "alpha_level" => 100 - min(max($arDef["WATERMARK_FILE_ALPHA"], 0), 100), "file" => $_SERVER["DOCUMENT_ROOT"] . Rel2Abs("/", $arDef["WATERMARK_FILE"]), "fill" => "resize"));
         }
         if (array_key_exists("DETAIL_PICTURE", $arFields) && is_array($arFields["DETAIL_PICTURE"]) && $arDef["USE_WATERMARK_TEXT"] === "Y") {
             if (strlen($arFields["DETAIL_PICTURE"]["tmp_name"]) > 0 && $arFields["DETAIL_PICTURE"]["tmp_name"] === $arFields["PICTURE"]["tmp_name"]) {
                 $arFields["DETAIL_PICTURE"]["tmp_name"] = CTempFile::GetFileName(basename($arFields["DETAIL_PICTURE"]["tmp_name"]));
                 CheckDirPath($arFields["DETAIL_PICTURE"]["tmp_name"]);
                 copy($arFields["PICTURE"]["tmp_name"], $arFields["DETAIL_PICTURE"]["tmp_name"]);
             }
             CIBLock::FilterPicture($arFields["DETAIL_PICTURE"]["tmp_name"], array("name" => "watermark", "position" => $arDef["WATERMARK_TEXT_POSITION"], "type" => "text", "coefficient" => $arDef["WATERMARK_TEXT_SIZE"], "text" => $arDef["WATERMARK_TEXT"], "font" => $_SERVER["DOCUMENT_ROOT"] . Rel2Abs("/", $arDef["WATERMARK_TEXT_FONT"]), "color" => $arDef["WATERMARK_TEXT_COLOR"]));
         }
     }
     if (is_set($arFields, "PICTURE")) {
         if (strlen($arFields["PICTURE"]["name"]) <= 0 && strlen($arFields["PICTURE"]["del"]) <= 0) {
             unset($arFields["PICTURE"]);
         } else {
             $arFields["PICTURE"]["old_file"] = $db_record["PICTURE"];
             $arFields["PICTURE"]["MODULE_ID"] = "iblock";
         }
     }
     if (is_set($arFields, "DETAIL_PICTURE")) {
         if (strlen($arFields["DETAIL_PICTURE"]["name"]) <= 0 && strlen($arFields["DETAIL_PICTURE"]["del"]) <= 0) {
             unset($arFields["DETAIL_PICTURE"]);
         } else {
             $arFields["DETAIL_PICTURE"]["old_file"] = $db_record["DETAIL_PICTURE"];
             $arFields["DETAIL_PICTURE"]["MODULE_ID"] = "iblock";
         }
     }
     if (is_set($arFields, "ACTIVE") && $arFields["ACTIVE"] != "Y") {
         $arFields["ACTIVE"] = "N";
     }
     if (is_set($arFields, "DESCRIPTION_TYPE") && $arFields["DESCRIPTION_TYPE"] != "html") {
         $arFields["DESCRIPTION_TYPE"] = "text";
     }
     if (isset($arFields["IBLOCK_SECTION_ID"])) {
         $arFields["IBLOCK_SECTION_ID"] = intval($arFields["IBLOCK_SECTION_ID"]);
         if ($arFields["IBLOCK_SECTION_ID"] <= 0) {
             $arFields["IBLOCK_SECTION_ID"] = false;
         }
     }
     $DESC_tmp = is_set($arFields, "DESCRIPTION") ? $arFields["DESCRIPTION"] : $db_record["DESCRIPTION"];
     $DESC_TYPE_tmp = is_set($arFields, "DESCRIPTION_TYPE") ? $arFields["DESCRIPTION_TYPE"] : $db_record["DESCRIPTION_TYPE"];
     $arFields["SEARCHABLE_CONTENT"] = ToUpper((is_set($arFields, "NAME") ? $arFields["NAME"] : $db_record["NAME"]) . "\r\n" . ($DESC_TYPE_tmp == "html" ? HTMLToTxt($DESC_tmp) : $DESC_tmp));
     if (is_object($USER)) {
         if (!isset($arFields["MODIFIED_BY"]) || intval($arFields["MODIFIED_BY"]) <= 0) {
             $arFields["MODIFIED_BY"] = intval($USER->GetID());
         }
     }
     if (!$this->CheckFields($arFields, $ID)) {
         $Result = false;
         $arFields["RESULT_MESSAGE"] =& $this->LAST_ERROR;
     } elseif (!$GLOBALS["USER_FIELD_MANAGER"]->CheckFields("IBLOCK_" . $db_record["IBLOCK_ID"] . "_SECTION", $ID, $arFields)) {
         $Result = false;
         $err = $APPLICATION->GetException();
         if (is_object($err)) {
             $this->LAST_ERROR .= str_replace("<br><br>", "<br>", $err->GetString() . "<br>");
         }
         $arFields["RESULT_MESSAGE"] =& $this->LAST_ERROR;
     } else {
         if (array_key_exists("PICTURE", $arFields)) {
             $SAVED_PICTURE = $arFields["PICTURE"];
             CFile::SaveForDB($arFields, "PICTURE", "iblock");
         }
         if (array_key_exists("DETAIL_PICTURE", $arFields)) {
             $SAVED_DETAIL_PICTURE = $arFields["DETAIL_PICTURE"];
             CFile::SaveForDB($arFields, "DETAIL_PICTURE", "iblock");
         }
         unset($arFields["ID"]);
         $strUpdate = $DB->PrepareUpdate("b_iblock_section", $arFields, "iblock");
         if (array_key_exists("PICTURE", $arFields)) {
             $arFields["PICTURE"] = $SAVED_PICTURE;
         }
         if (array_key_exists("DETAIL_PICTURE", $arFields)) {
             $arFields["DETAIL_PICTURE"] = $SAVED_DETAIL_PICTURE;
         }
         CIBlock::_transaction_lock($db_record["IBLOCK_ID"]);
         if (strlen($strUpdate) > 0) {
             $strSql = "UPDATE b_iblock_section SET " . $strUpdate . " WHERE ID = " . $ID;
             $arBinds = array();
             if (array_key_exists("DESCRIPTION", $arFields)) {
                 $arBinds["DESCRIPTION"] = $arFields["DESCRIPTION"];
             }
             if (array_key_exists("SEARCHABLE_CONTENT", $arFields)) {
                 $arBinds["SEARCHABLE_CONTENT"] = $arFields["SEARCHABLE_CONTENT"];
             }
             $DB->QueryBind($strSql, $arBinds);
         }
         if ($bResort) {
             //Move inside the tree
             if (isset($arFields["SORT"]) && $arFields["SORT"] != $db_record["SORT"] || isset($arFields["NAME"]) && $arFields["NAME"] != $db_record["NAME"] || isset($arFields["IBLOCK_SECTION_ID"]) && $arFields["IBLOCK_SECTION_ID"] != $db_record["IBLOCK_SECTION_ID"]) {
                 //First "delete" from the tree
                 $distance = intval($db_record["RIGHT_MARGIN"]) - intval($db_record["LEFT_MARGIN"]) + 1;
                 $DB->Query("\n\t\t\t\t\t\tUPDATE b_iblock_section SET\n\t\t\t\t\t\t\tTIMESTAMP_X=" . ($DB->type == "ORACLE" ? "NULL" : "TIMESTAMP_X") . "\n\t\t\t\t\t\t\t,LEFT_MARGIN = -LEFT_MARGIN\n\t\t\t\t\t\t\t,RIGHT_MARGIN = -RIGHT_MARGIN\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tIBLOCK_ID = " . $db_record["IBLOCK_ID"] . "\n\t\t\t\t\t\t\tAND LEFT_MARGIN >= " . intval($db_record["LEFT_MARGIN"]) . "\n\t\t\t\t\t\t\tAND LEFT_MARGIN <= " . intval($db_record["RIGHT_MARGIN"]) . "\n\t\t\t\t\t");
                 $DB->Query("\n\t\t\t\t\t\tUPDATE b_iblock_section SET\n\t\t\t\t\t\t\tTIMESTAMP_X=" . ($DB->type == "ORACLE" ? "NULL" : "TIMESTAMP_X") . "\n\t\t\t\t\t\t\t,RIGHT_MARGIN = RIGHT_MARGIN - " . $distance . "\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tIBLOCK_ID = " . $db_record["IBLOCK_ID"] . "\n\t\t\t\t\t\t\tAND RIGHT_MARGIN > " . $db_record["RIGHT_MARGIN"] . "\n\t\t\t\t\t");
                 $DB->Query("\n\t\t\t\t\t\tUPDATE b_iblock_section SET\n\t\t\t\t\t\t\tTIMESTAMP_X=" . ($DB->type == "ORACLE" ? "NULL" : "TIMESTAMP_X") . "\n\t\t\t\t\t\t\t,LEFT_MARGIN = LEFT_MARGIN - " . $distance . "\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tIBLOCK_ID = " . $db_record["IBLOCK_ID"] . "\n\t\t\t\t\t\t\tAND LEFT_MARGIN > " . $db_record["LEFT_MARGIN"] . "\n\t\t\t\t\t");
                 //Next insert into the the tree almost as we do when inserting the new one
                 $PARENT_ID = isset($arFields["IBLOCK_SECTION_ID"]) ? intval($arFields["IBLOCK_SECTION_ID"]) : intval($db_record["IBLOCK_SECTION_ID"]);
                 $NAME = isset($arFields["NAME"]) ? $arFields["NAME"] : $db_record["NAME"];
                 $SORT = isset($arFields["SORT"]) ? intval($arFields["SORT"]) : intval($db_record["SORT"]);
                 $arParents = array();
                 $strSql = "\n\t\t\t\t\t\tSELECT BS.ID, BS.ACTIVE, BS.GLOBAL_ACTIVE, BS.DEPTH_LEVEL, BS.LEFT_MARGIN, BS.RIGHT_MARGIN\n\t\t\t\t\t\tFROM b_iblock_section BS\n\t\t\t\t\t\tWHERE BS.IBLOCK_ID = " . $db_record["IBLOCK_ID"] . "\n\t\t\t\t\t\tAND BS.ID in (" . intval($db_record["IBLOCK_SECTION_ID"]) . ", " . $PARENT_ID . ")\n\t\t\t\t\t";
                 $rsParents = $DB->Query($strSql);
                 while ($arParent = $rsParents->Fetch()) {
                     $arParents[$arParent["ID"]] = $arParent;
                 }
                 //Find rightmost child of the parent
                 $strSql = "\n\t\t\t\t\t\tSELECT BS.ID, BS.RIGHT_MARGIN, BS.DEPTH_LEVEL\n\t\t\t\t\t\tFROM b_iblock_section BS\n\t\t\t\t\t\tWHERE BS.IBLOCK_ID = " . $db_record["IBLOCK_ID"] . "\n\t\t\t\t\t\tAND " . ($PARENT_ID > 0 ? "BS.IBLOCK_SECTION_ID=" . $PARENT_ID : "BS.IBLOCK_SECTION_ID IS NULL") . "\n\t\t\t\t\t\tAND (\n\t\t\t\t\t\t\t(BS.SORT < " . $SORT . ")\n\t\t\t\t\t\t\tOR (BS.SORT = " . $SORT . " AND BS.NAME < '" . $DB->ForSQL($NAME) . "')\n\t\t\t\t\t\t)\n\t\t\t\t\t\tAND BS.ID <> " . $ID . "\n\t\t\t\t\t\tORDER BY BS.SORT DESC, BS.NAME DESC\n\t\t\t\t\t";
                 $rsChild = $DB->Query($strSql);
                 if ($arChild = $rsChild->Fetch()) {
                     //We found the left neighbour
                     $arUpdate = array("LEFT_MARGIN" => intval($arChild["RIGHT_MARGIN"]) + 1, "DEPTH_LEVEL" => intval($arChild["DEPTH_LEVEL"]));
                 } else {
                     //If we have parent, when take its left_margin
                     if (isset($arParents[$PARENT_ID]) && $arParents[$PARENT_ID]) {
                         $arUpdate = array("LEFT_MARGIN" => intval($arParents[$PARENT_ID]["LEFT_MARGIN"]) + 1, "DEPTH_LEVEL" => intval($arParents[$PARENT_ID]["DEPTH_LEVEL"]) + 1);
                     } else {
                         //We are only one/leftmost section in the iblock.
                         $arUpdate = array("LEFT_MARGIN" => 1, "DEPTH_LEVEL" => 1);
                     }
                 }
                 $move_distance = intval($db_record["LEFT_MARGIN"]) - $arUpdate["LEFT_MARGIN"];
                 $DB->Query("\n\t\t\t\t\t\tUPDATE b_iblock_section SET\n\t\t\t\t\t\t\tTIMESTAMP_X=" . ($DB->type == "ORACLE" ? "NULL" : "TIMESTAMP_X") . "\n\t\t\t\t\t\t\t,LEFT_MARGIN = LEFT_MARGIN + " . $distance . "\n\t\t\t\t\t\t\t,RIGHT_MARGIN = RIGHT_MARGIN + " . $distance . "\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tIBLOCK_ID = " . $db_record["IBLOCK_ID"] . "\n\t\t\t\t\t\t\tAND LEFT_MARGIN >= " . $arUpdate["LEFT_MARGIN"] . "\n\t\t\t\t\t");
                 $DB->Query("\n\t\t\t\t\t\tUPDATE b_iblock_section SET\n\t\t\t\t\t\t\tTIMESTAMP_X=" . ($DB->type == "ORACLE" ? "NULL" : "TIMESTAMP_X") . "\n\t\t\t\t\t\t\t,LEFT_MARGIN = -LEFT_MARGIN - " . $move_distance . "\n\t\t\t\t\t\t\t,RIGHT_MARGIN = -RIGHT_MARGIN - " . $move_distance . "\n\t\t\t\t\t\t\t" . ($arUpdate["DEPTH_LEVEL"] != intval($db_record["DEPTH_LEVEL"]) ? ",DEPTH_LEVEL = DEPTH_LEVEL - " . ($db_record["DEPTH_LEVEL"] - $arUpdate["DEPTH_LEVEL"]) : "") . "\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tIBLOCK_ID = " . $db_record["IBLOCK_ID"] . "\n\t\t\t\t\t\t\tAND LEFT_MARGIN <= " . -intval($db_record["LEFT_MARGIN"]) . "\n\t\t\t\t\t\t\tAND LEFT_MARGIN >= " . -intval($db_record["RIGHT_MARGIN"]) . "\n\t\t\t\t\t");
                 if (isset($arParents[$PARENT_ID])) {
                     $DB->Query("\n\t\t\t\t\t\t\tUPDATE b_iblock_section SET\n\t\t\t\t\t\t\t\tTIMESTAMP_X=" . ($DB->type == "ORACLE" ? "NULL" : "TIMESTAMP_X") . "\n\t\t\t\t\t\t\t\t,RIGHT_MARGIN = RIGHT_MARGIN + " . $distance . "\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tIBLOCK_ID = " . $db_record["IBLOCK_ID"] . "\n\t\t\t\t\t\t\t\tAND LEFT_MARGIN <= " . $arParents[$PARENT_ID]["LEFT_MARGIN"] . "\n\t\t\t\t\t\t\t\tAND RIGHT_MARGIN >= " . $arParents[$PARENT_ID]["RIGHT_MARGIN"] . "\n\t\t\t\t\t\t");
                 }
             }
             //Check if parent was changed
             if (isset($arFields["IBLOCK_SECTION_ID"]) && $arFields["IBLOCK_SECTION_ID"] != $db_record["IBLOCK_SECTION_ID"]) {
                 $rsSection = CIBlockSection::GetByID($ID);
                 $arSection = $rsSection->Fetch();
                 $strSql = "\n\t\t\t\t\t\tSELECT ID, GLOBAL_ACTIVE\n\t\t\t\t\t\tFROM b_iblock_section\n\t\t\t\t\t\tWHERE IBLOCK_ID = " . $arSection["IBLOCK_ID"] . "\n\t\t\t\t\t\tAND ID = " . intval($arFields["IBLOCK_SECTION_ID"]) . "\n\t\t\t\t\t";
                 $rsParent = $DB->Query($strSql);
                 $arParent = $rsParent->Fetch();
                 //If new parent is not globally active
                 //or we are not active either
                 //we must be not globally active too
                 if ($arParent && $arParent["GLOBAL_ACTIVE"] == "N" || $arFields["ACTIVE"] == "N") {
                     $DB->Query("\n\t\t\t\t\t\t\tUPDATE b_iblock_section SET\n\t\t\t\t\t\t\t\tTIMESTAMP_X=" . ($DB->type == "ORACLE" ? "NULL" : "TIMESTAMP_X") . "\n\t\t\t\t\t\t\t\t,GLOBAL_ACTIVE = 'N'\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tIBLOCK_ID = " . $arSection["IBLOCK_ID"] . "\n\t\t\t\t\t\t\t\tAND LEFT_MARGIN >= " . intval($arSection["LEFT_MARGIN"]) . "\n\t\t\t\t\t\t\t\tAND RIGHT_MARGIN <= " . intval($arSection["RIGHT_MARGIN"]) . "\n\t\t\t\t\t\t");
                 } elseif ($arSection["ACTIVE"] == "N" && $arFields["ACTIVE"] == "Y") {
                     $this->RecalcGlobalActiveFlag($arSection);
                 } elseif ((!$arParent || $arParent["GLOBAL_ACTIVE"] == "Y") && $arSection["GLOBAL_ACTIVE"] == "N" && ($arSection["ACTIVE"] == "Y" || $arFields["ACTIVE"] == "Y")) {
                     $this->RecalcGlobalActiveFlag($arSection);
                 }
                 //Otherwise we may not to change anything
             } elseif (isset($arFields["ACTIVE"]) && $arFields["ACTIVE"] != $db_record["ACTIVE"]) {
                 //Make all children globally inactive
                 if ($arFields["ACTIVE"] == "N") {
                     $DB->Query("\n\t\t\t\t\t\t\tUPDATE b_iblock_section SET\n\t\t\t\t\t\t\t\tTIMESTAMP_X=" . ($DB->type == "ORACLE" ? "NULL" : "TIMESTAMP_X") . "\n\t\t\t\t\t\t\t\t,GLOBAL_ACTIVE = 'N'\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tIBLOCK_ID = " . $db_record["IBLOCK_ID"] . "\n\t\t\t\t\t\t\t\tAND LEFT_MARGIN >= " . intval($db_record["LEFT_MARGIN"]) . "\n\t\t\t\t\t\t\t\tAND RIGHT_MARGIN <= " . intval($db_record["RIGHT_MARGIN"]) . "\n\t\t\t\t\t\t");
                 } else {
                     //Check for parent activity
                     $strSql = "\n\t\t\t\t\t\t\tSELECT ID, GLOBAL_ACTIVE\n\t\t\t\t\t\t\tFROM b_iblock_section\n\t\t\t\t\t\t\tWHERE IBLOCK_ID = " . $db_record["IBLOCK_ID"] . "\n\t\t\t\t\t\t\tAND ID = " . intval($db_record["IBLOCK_SECTION_ID"]) . "\n\t\t\t\t\t\t";
                     $rsParent = $DB->Query($strSql);
                     $arParent = $rsParent->Fetch();
                     //Parent is active
                     //and we changed
                     //so need to recalc
                     if (!$arParent || $arParent["GLOBAL_ACTIVE"] == "Y") {
                         $this->RecalcGlobalActiveFlag($db_record);
                     }
                 }
             }
         }
         if ($arIBlock["RIGHTS_MODE"] === "E") {
             $obSectionRights = new CIBlockSectionRights($arIBlock["ID"], $ID);
             //Check if parent changed with extended rights mode
             if (isset($arFields["IBLOCK_SECTION_ID"]) && $arFields["IBLOCK_SECTION_ID"] != $db_record["IBLOCK_SECTION_ID"]) {
                 $obSectionRights->ChangeParents(array($db_record["IBLOCK_SECTION_ID"]), array($arFields["IBLOCK_SECTION_ID"]));
             }
             if (array_key_exists("RIGHTS", $arFields) && is_array($arFields["RIGHTS"])) {
                 $obSectionRights->SetRights($arFields["RIGHTS"]);
             }
         }
         $uf_updated = $GLOBALS["USER_FIELD_MANAGER"]->Update("IBLOCK_" . $db_record["IBLOCK_ID"] . "_SECTION", $ID, $arFields);
         if ($uf_updated) {
             $DB->Query("UPDATE b_iblock_section SET TIMESTAMP_X = " . $DB->CurrentTimeFunction() . " WHERE ID = " . $ID);
         }
         if (CIBlock::GetArrayByID($db_record["IBLOCK_ID"], "SECTION_PROPERTY") === "Y" && array_key_exists("SECTION_PROPERTY", $arFields) && is_array($arFields["SECTION_PROPERTY"])) {
             CIBlockSectionPropertyLink::DeleteBySection($ID);
             foreach ($arFields["SECTION_PROPERTY"] as $PROPERTY_ID => $arLink) {
                 CIBlockSectionPropertyLink::Add($ID, $PROPERTY_ID, $arLink);
             }
         }
         if ($bUpdateSearch) {
             CIBlockSection::UpdateSearch($ID);
         }
         if ($arIBlock["FIELDS"]["LOG_SECTION_EDIT"]["IS_REQUIRED"] == "Y") {
             $USER_ID = is_object($USER) ? intval($USER->GetID()) : 0;
             $db_events = GetModuleEvents("main", "OnBeforeEventLog");
             $arEvent = $db_events->Fetch();
             if (!$arEvent || ExecuteModuleEventEx($arEvent, array($USER_ID)) === false) {
                 $rsSection = CIBlockSection::GetList(array(), array("=ID" => $ID), false, array("LIST_PAGE_URL", "NAME", "CODE"));
                 $arSection = $rsSection->GetNext();
                 $res = array("ID" => $ID, "CODE" => $arSection["CODE"], "NAME" => $arSection["NAME"], "SECTION_NAME" => $arIBlock["SECTION_NAME"], "USER_ID" => $USER_ID, "IBLOCK_PAGE_URL" => $arSection["LIST_PAGE_URL"]);
                 CEventLog::Log("IBLOCK", "IBLOCK_SECTION_EDIT", "iblock", $arIBlock["ID"], serialize($res));
             }
         }
         $Result = true;
         /*********** QUOTA ***************/
         $_SESSION["SESS_RECOUNT_DB"] = "Y";
         /*********** QUOTA ***************/
     }
     $arFields["ID"] = $ID;
     $arFields["IBLOCK_ID"] = $db_record["IBLOCK_ID"];
     $arFields["RESULT"] =& $Result;
     $events = GetModuleEvents("iblock", "OnAfterIBlockSectionUpdate");
     while ($arEvent = $events->Fetch()) {
         ExecuteModuleEventEx($arEvent, array(&$arFields));
     }
     if (defined("BX_COMP_MANAGED_CACHE")) {
         $GLOBALS["CACHE_MANAGER"]->ClearByTag("iblock_id_" . $arIBlock["ID"]);
     }
     return $Result;
 }
コード例 #5
0
ファイル: iblockelement.php プロジェクト: nycmic/bittest
	function SetPropertyValues($ELEMENT_ID, $IBLOCK_ID, $PROPERTY_VALUES, $PROPERTY_CODE = false)
	{
		global $DB;
		global $BX_IBLOCK_PROP_CACHE;

		$ELEMENT_ID = intVal($ELEMENT_ID);
		$IBLOCK_ID = intval($IBLOCK_ID);

		if (!is_array($PROPERTY_VALUES))
			$PROPERTY_VALUES = array($PROPERTY_VALUES);

		$uniq_flt = $IBLOCK_ID;
		$arFilter = array(
			"IBLOCK_ID" => $IBLOCK_ID,
			"CHECK_PERMISSIONS" => "N",
		);

		if ($PROPERTY_CODE === false)
		{
			$arFilter["ACTIVE"] = "Y";
			$uniq_flt .= "|ACTIVE:".$arFilter["ACTIVE"];
		}
		elseif(intval($PROPERTY_CODE) > 0)
		{
			$arFilter["ID"] = intval($PROPERTY_CODE);
			$uniq_flt .= "|ID:".$arFilter["ID"];
		}
		else
		{
			$arFilter["CODE"] = $PROPERTY_CODE;
			$uniq_flt .= "|CODE:".$arFilter["CODE"];
		}

		if (!isset($BX_IBLOCK_PROP_CACHE[$IBLOCK_ID]))
			$BX_IBLOCK_PROP_CACHE[$IBLOCK_ID] = array();

		if (!isset($BX_IBLOCK_PROP_CACHE[$IBLOCK_ID][$uniq_flt]))
		{
			$BX_IBLOCK_PROP_CACHE[$IBLOCK_ID][$uniq_flt] = array();

			$db_prop = CIBlockProperty::GetList(array(), $arFilter);
			while($prop = $db_prop->Fetch())
			{
				$BX_IBLOCK_PROP_CACHE[$IBLOCK_ID][$uniq_flt][] = $prop;
			}
		}

		$ar_prop = &$BX_IBLOCK_PROP_CACHE[$IBLOCK_ID][$uniq_flt];
		reset($ar_prop);

		$bRecalcSections = false;

		//Read current property values from database
		$arDBProps = array();
		if (CIBLock::GetArrayByID($IBLOCK_ID, "VERSION") == 2)
		{
			$rs = $DB->Query("
				select *
				from b_iblock_element_prop_m".$IBLOCK_ID."
				where IBLOCK_ELEMENT_ID = ".$ELEMENT_ID."
				order by ID asc
			");
			while ($ar = $rs->Fetch())
			{
				$property_id = $ar["IBLOCK_PROPERTY_ID"];
				if (!isset($arDBProps[$property_id]))
					$arDBProps[$property_id] = array();

				$arDBProps[$property_id][$ar["ID"]] = $ar;
			}

			$rs = $DB->Query("
				select *
				from b_iblock_element_prop_s".$IBLOCK_ID."
				where IBLOCK_ELEMENT_ID = ".$ELEMENT_ID."
			");
			if ($ar = $rs->Fetch())
			{
				foreach($ar_prop as $property)
				{
					$property_id = $property["ID"];
					if(
						$property["MULTIPLE"] == "N"
						&& isset($ar["PROPERTY_".$property_id])
						&& strlen($ar["PROPERTY_".$property_id])
					)
					{
						if (!isset($arDBProps[$property_id]))
							$arDBProps[$property_id] = array();

						$arDBProps[$property_id][$pr["ID"]] = array(
							"ID" => $ELEMENT_ID.":".$property_id,
							"IBLOCK_PROPERTY_ID" => $property_id,
							"VALUE" => $ar["PROPERTY_".$property_id],
							"DESCRIPTION" => $ar["DESCRIPTION_".$property_id],
						);
					}
				}
			}
		}
		else
		{
			$rs = $DB->Query("
				select *
				from b_iblock_element_property
				where IBLOCK_ELEMENT_ID = ".$ELEMENT_ID."
				order by ID asc
			");
			while ($ar = $rs->Fetch())
			{
				$property_id = $ar["IBLOCK_PROPERTY_ID"];
				if (!isset($arDBProps[$property_id]))
					$arDBProps[$property_id] = array();

				$arDBProps[$property_id][$ar["ID"]] = $ar;
			}
		}

		$arFilesToDelete = array();
		$arV2ClearCache = array();
		foreach ($ar_prop as $prop)
		{
			if ($PROPERTY_CODE)
			{
				$PROP = $PROPERTY_VALUES;
			}
			else
			{
				if (strlen($prop["CODE"]) > 0 && array_key_exists($prop["CODE"], $PROPERTY_VALUES))
					$PROP = $PROPERTY_VALUES[$prop["CODE"]];
				else
					$PROP = $PROPERTY_VALUES[$prop["ID"]];
			}

			if (
				!is_array($PROP)
				|| (
					$prop["PROPERTY_TYPE"] == "F"
					&& (
						array_key_exists("tmp_name", $PROP)
						|| array_key_exists("del", $PROP)
					)
				)
				|| (
					count($PROP) == 2
					&& array_key_exists("VALUE", $PROP)
					&& array_key_exists("DESCRIPTION", $PROP)
				)
			)
			{
				$PROP = array($PROP);
			}

			if ($prop["USER_TYPE"] != "")
			{
				$arUserType = CIBlockProperty::GetUserType($prop["USER_TYPE"]);
				if (array_key_exists("ConvertToDB", $arUserType))
				{
					foreach ($PROP as $key => $value)
					{
						if(
							!is_array($value)
							|| !array_key_exists("VALUE", $value)
						)
						{
							$value = array("VALUE"=>$value);
						}

						$PROP[$key] = call_user_func_array($arUserType["ConvertToDB"], array($prop, $value));
					}
				}
			}

			if ($prop["VERSION"] == 2)
			{
				if ($prop["MULTIPLE"] == "Y")
					$strTable = "b_iblock_element_prop_m".$prop["IBLOCK_ID"];
				else
					$strTable = "b_iblock_element_prop_s".$prop["IBLOCK_ID"];
			}
			else
			{
				$strTable = "b_iblock_element_property";
			}

			if ($prop["PROPERTY_TYPE"] == "L")
			{
				$DB->Query(CIBLockElement::DeletePropertySQL($prop, $ELEMENT_ID));
				if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y")
				{
					$arV2ClearCache[$prop["ID"]] =
						"PROPERTY_".$prop["ID"]." = NULL"
						.self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"])
					;
				}

				$ids = "0";
				foreach ($PROP as $key => $value)
				{
					if (is_array($value))
						$value = intval($value["VALUE"]);
					else
						$value = intval($value);

					if ($value <= 0)
						continue;

					$ids .= ",".$value;

					if ($prop["MULTIPLE"] != "Y")
						break;
				}

				if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N")
				{
					$DB->Query("
						UPDATE
							b_iblock_element_prop_s".$prop["IBLOCK_ID"]." E
							,b_iblock_property P
							,b_iblock_property_enum PEN
						SET
							E.PROPERTY_".$prop["ID"]." = PEN.ID
						WHERE
							E.IBLOCK_ELEMENT_ID = ".$ELEMENT_ID."
							AND P.ID = ".$prop["ID"]."
							AND P.ID = PEN.PROPERTY_ID
							AND PEN.ID IN (".$ids.")
					");
				}
				else
				{
					$DB->Query("
						INSERT INTO ".$strTable."
						(IBLOCK_ELEMENT_ID, IBLOCK_PROPERTY_ID, VALUE, VALUE_ENUM)
						SELECT ".$ELEMENT_ID.", P.ID, PEN.ID, PEN.ID
						FROM
							b_iblock_property P
							,b_iblock_property_enum PEN
						WHERE
							P.ID = ".$prop["ID"]."
							AND P.ID = PEN.PROPERTY_ID
							AND PEN.ID IN (".$ids.")
					");
				}
			}
			elseif ($prop["PROPERTY_TYPE"] == "G")
			{
				$bRecalcSections = true;
				$DB->Query(CIBLockElement::DeletePropertySQL($prop, $ELEMENT_ID));
				if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y")
				{
					$arV2ClearCache[$prop["ID"]] =
						"PROPERTY_".$prop["ID"]." = NULL"
						.self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"])
					;
				}
				$DB->Query("
					DELETE FROM b_iblock_section_element
					WHERE ADDITIONAL_PROPERTY_ID = ".$prop["ID"]."
					AND IBLOCK_ELEMENT_ID = ".$ELEMENT_ID."
				");

				$ids = "0";
				foreach ($PROP as $key => $value)
				{
					if (is_array($value))
						$value = intval($value["VALUE"]);
					else
						$value = intval($value);

					if ($value <= 0)
						continue;

					$ids .= ",".$value;

					if ($prop["MULTIPLE"] != "Y")
						break;
				}

				if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N")
				{
					$DB->Query("
						UPDATE
							b_iblock_element_prop_s".$prop["IBLOCK_ID"]." E
							,b_iblock_property P
							,b_iblock_section S
						SET
							E.PROPERTY_".$prop["ID"]." = S.ID
							".self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"])."
						WHERE
							E.IBLOCK_ELEMENT_ID = ".$ELEMENT_ID."
							AND P.ID = ".$prop["ID"]."
							AND (
								P.LINK_IBLOCK_ID IS NULL
								OR P.LINK_IBLOCK_ID = 0
								OR S.IBLOCK_ID = P.LINK_IBLOCK_ID
							)
							AND S.ID IN (".$ids.")
					");
				}
				else
				{
					$DB->Query("
						INSERT INTO ".$strTable."
						(IBLOCK_ELEMENT_ID, IBLOCK_PROPERTY_ID, VALUE, VALUE_NUM)
						SELECT ".$ELEMENT_ID.", P.ID, S.ID, S.ID
						FROM
							b_iblock_property P
							,b_iblock_section S
						WHERE
							P.ID=".$prop["ID"]."
							AND (
								P.LINK_IBLOCK_ID IS NULL
								OR P.LINK_IBLOCK_ID = 0
								OR S.IBLOCK_ID = P.LINK_IBLOCK_ID
							)
							AND S.ID IN (".$ids.")
					");
				}
				$DB->Query("
					INSERT INTO b_iblock_section_element
					(IBLOCK_ELEMENT_ID, IBLOCK_SECTION_ID, ADDITIONAL_PROPERTY_ID)
					SELECT ".$ELEMENT_ID.", S.ID, P.ID
					FROM
						b_iblock_property P
						,b_iblock_section S
					WHERE
						P.ID = ".$prop["ID"]."
						AND (
							P.LINK_IBLOCK_ID IS NULL
							OR P.LINK_IBLOCK_ID = 0
							OR S.IBLOCK_ID = P.LINK_IBLOCK_ID
						)
						AND S.ID IN (".$ids.")
				");
			}
			elseif ($prop["PROPERTY_TYPE"] == "E")
			{
				$arWas = array();
				if ($arDBProps[$prop["ID"]])
				{
					foreach($arDBProps[$prop["ID"]] as $res)
					{
						$val = $PROP[$res["ID"]];
						if (is_array($val))
						{
							$val_desc = $val["DESCRIPTION"];
							$val = $val["VALUE"];
						}
						else
						{
							$val_desc = false;
						}

						if (isset($arWas[$val]))
							$val = "";
						else
							$arWas[$val] = true;

						if (strlen($val) <= 0) //Delete property value
						{
							$DB->Query(CIBLockElement::DeletePropertySQL($prop, $ELEMENT_ID));
							if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y")
							{
								$arV2ClearCache[$prop["ID"]] =
									"PROPERTY_".$prop["ID"]." = NULL"
									.self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"])
								;
							}
						}
						elseif ($res["VALUE"] !== $val) //Update property value
						{
							if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N")
							{
								$DB->Query("
									UPDATE b_iblock_element_prop_s".$prop["IBLOCK_ID"]."
									SET PROPERTY_".$prop["ID"]." = '".$DB->ForSql($val)."'
									".self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"], $val_desc)."
									WHERE IBLOCK_ELEMENT_ID = ".$ELEMENT_ID."
								");
							}
							else
							{
								$DB->Query("
									UPDATE ".$strTable."
									SET VALUE = '".$DB->ForSql($val)."'
										,VALUE_NUM = ".CIBlock::roundDB($val)."
										".($val_desc!==false ? ",DESCRIPTION = '".$DB->ForSql($val_desc, 255)."'" : "")."
									WHERE ID=".$res["ID"]."
								");
							}

							if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y")
							{
								$arV2ClearCache[$prop["ID"]] =
									"PROPERTY_".$prop["ID"]." = NULL"
									.self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"])
								;
							}
						}

						unset($PROP[$res["ID"]]);
					} //foreach($arDBProps[$prop["ID"]] as $res)
				}

				foreach ($PROP as $val)
				{
					if (is_array($val))
					{
						$val_desc = $val["DESCRIPTION"];
						$val = $val["VALUE"];
					}
					else
					{
						$val_desc = false;
					}

					if (isset($arWas[$val]))
						$val = "";
					else
						$arWas[$val] = true;

					if (strlen($val) <= 0)
						continue;

					if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N")
					{
						$DB->Query("
							UPDATE b_iblock_element_prop_s".$prop["IBLOCK_ID"]."
							SET
								PROPERTY_".$prop["ID"]." = '".$DB->ForSql($val)."'
								".self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"], $val_desc)."
							WHERE IBLOCK_ELEMENT_ID=".$ELEMENT_ID."
						");
					}
					else
					{
						$DB->Query("
							INSERT INTO ".$strTable."
							(IBLOCK_ELEMENT_ID, IBLOCK_PROPERTY_ID, VALUE, VALUE_NUM".($val_desc!==false?", DESCRIPTION":"").")
							SELECT
								".$ELEMENT_ID."
								,P.ID
								,'".$DB->ForSql($val)."'
								,".CIBlock::roundDB($val)."
								".($val_desc!==false?", '".$DB->ForSQL($val_desc, 255)."'":"")."
							FROM
								b_iblock_property P
							WHERE
								ID = ".IntVal($prop["ID"])."
						");
					}

					if($prop["VERSION"]==2 && $prop["MULTIPLE"]=="Y")
					{
						$arV2ClearCache[$prop["ID"]] =
							"PROPERTY_".$prop["ID"]." = NULL"
							.self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"])
						;
					}

					if ($prop["MULTIPLE"] != "Y")
						break;
				} //foreach($PROP as $value)
			}
			elseif ($prop["PROPERTY_TYPE"] == "F")
			{
				//We'll be adding values from the database into the tail
				//this values was not passed into API call.
				$orderedPROP = array_reverse($PROP, true);
				if ($arDBProps[$prop["ID"]])
				{
					//Go from high ID to low
					foreach (array_reverse($arDBProps[$prop["ID"]], true) as $res)
					{
						//Preserve description from database
						if (strlen($res["DESCRIPTION"]))
							$description = $res["DESCRIPTION"];
						else
							$description = false;

						if (!array_key_exists($res["ID"], $orderedPROP))
						{
							$orderedPROP[$res["ID"]] = array(
								"VALUE" => $res["VALUE"],
								"DESCRIPTION" => $description,
							);
						}
						else
						{
							$val = $orderedPROP[$res["ID"]];
							if (
								is_array($val)
								&& !array_key_exists("tmp_name", $val)
								&& !array_key_exists("del", $val)
							)
								$val = $val["VALUE"];

							//Check if no new file and no delete command
							if (
								!strlen($val["tmp_name"])
								&& !strlen($val["del"])
							) //Overwrite with database value
							{
								//But save description from incoming value
								if (array_key_exists("description", $val))
									$description = trim($val["description"]);

								$orderedPROP[$res["ID"]] = array(
									"VALUE" => $res["VALUE"],
									"DESCRIPTION" => $description,
								);
							}
						}
					}
				}
				//Reverse the array and now lower ID is in the beginning
				$orderedPROP = array_reverse($orderedPROP, true);

				//Now delete from database all marked for deletion  records
				if ($arDBProps[$prop["ID"]])
				{
					foreach ($arDBProps[$prop["ID"]] as $res)
					{
						$val = $orderedPROP[$res["ID"]];
						if (
							is_array($val)
							&& !array_key_exists("tmp_name", $val)
							&& !array_key_exists("del", $val)
						)
						{
							$val = $val["VALUE"];
						}

						if (is_array($val) && strlen($val["del"]))
						{
							unset($orderedPROP[$res["ID"]]);
							$arFilesToDelete[] = array(
								"FILE_ID" => $res["VALUE"],
								"ELEMENT_ID" => $ELEMENT_ID,
								"IBLOCK_ID" => $prop["IBLOCK_ID"],
							);
						}
						elseif ($prop["MULTIPLE"] != "Y")
						{
							//Delete all stored in database for replacement.
							$arFilesToDelete[] = array(
								"FILE_ID" => $res["VALUE"],
								"ELEMENT_ID" => $ELEMENT_ID,
								"IBLOCK_ID" => $prop["IBLOCK_ID"],
							);
						}

						if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N")
						{
							$DB->Query("
								UPDATE b_iblock_element_prop_s".$prop["IBLOCK_ID"]."
								SET PROPERTY_".$prop["ID"]." = null
								".self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"])."
								WHERE IBLOCK_ELEMENT_ID = ".$ELEMENT_ID."
							");
						}
						else
						{
							$DB->Query("DELETE FROM ".$strTable." WHERE ID = ".$res["ID"]);
						}

						if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y")
						{
							$arV2ClearCache[$prop["ID"]] =
								"PROPERTY_".$prop["ID"]." = NULL"
								.self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"])
							;
						}
					} //foreach($arDBProps[$prop["ID"]] as $res)
				}

				//Write new values into database in specified order
				foreach ($orderedPROP as $val)
				{
					if(
						is_array($val)
						&& !array_key_exists("tmp_name", $val)
					)
					{
						$val_desc = $val["DESCRIPTION"];
						$val = $val["VALUE"];
					}
					else
					{
						$val_desc = false;
					}

					if (is_array($val))
					{
						$val["MODULE_ID"] = "iblock";
						if ($val_desc !== false)
							$val["description"] = $val_desc;

						$val = CFile::SaveFile($val, "iblock");
					}
					elseif (
						$val > 0
						&& $val_desc !== false
					)
					{
						CFile::UpdateDesc($val, $val_desc);
					}

					if (intval($val) <= 0)
						continue;

					if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N")
					{
						$DB->Query($s="
							UPDATE b_iblock_element_prop_s".$prop["IBLOCK_ID"]."
							SET
								PROPERTY_".$prop["ID"]." = '".$DB->ForSql($val)."'
								".self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"], $val_desc)."
							WHERE IBLOCK_ELEMENT_ID=".$ELEMENT_ID."
						");
					}
					else
					{
						$DB->Query("
							INSERT INTO ".$strTable."
							(IBLOCK_ELEMENT_ID, IBLOCK_PROPERTY_ID, VALUE, VALUE_NUM".($val_desc!==false?", DESCRIPTION":"").")
							SELECT
								".$ELEMENT_ID."
								,P.ID
								,'".$DB->ForSql($val)."'
								,".CIBlock::roundDB($val)."
								".($val_desc!==false?", '".$DB->ForSQL($val_desc, 255)."'":"")."
							FROM
								b_iblock_property P
							WHERE
								ID = ".IntVal($prop["ID"])."
						");
					}

					if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y")
					{
						$arV2ClearCache[$prop["ID"]] =
							"PROPERTY_".$prop["ID"]." = NULL"
							.self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"])
						;
					}

					if ($prop["MULTIPLE"] != "Y")
						break;

				} //foreach($PROP as $value)
			}
			else //if($prop["PROPERTY_TYPE"] == "S" || $prop["PROPERTY_TYPE"] == "N")
			{
				if ($arDBProps[$prop["ID"]])
				{
					foreach ($arDBProps[$prop["ID"]] as $res)
					{
						$val = $PROP[$res["ID"]];
						if (is_array($val))
						{
							$val_desc = $val["DESCRIPTION"];
							$val = $val["VALUE"];
						}
						else
						{
							$val_desc = false;
						}

						if (strlen($val) <= 0)
						{
							if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N")
							{
								$DB->Query("
									UPDATE b_iblock_element_prop_s".$prop["IBLOCK_ID"]."
									SET
										PROPERTY_".$prop["ID"]." = null
										".self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"])."
									WHERE IBLOCK_ELEMENT_ID=".$ELEMENT_ID."
								");
							}
							else
							{
								$DB->Query("DELETE FROM ".$strTable." WHERE ID=".$res["ID"]);
							}

							if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y")
							{
								$arV2ClearCache[$prop["ID"]] =
									"PROPERTY_".$prop["ID"]." = NULL"
									.self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"])
								;
							}
						}
						else
						{
							if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N")
							{
								if($prop["PROPERTY_TYPE"]=="N")
									$val = CIBlock::roundDB($val);

								$DB->Query("
									UPDATE b_iblock_element_prop_s".$prop["IBLOCK_ID"]."
									SET PROPERTY_".$prop["ID"]."='".$DB->ForSql($val)."'
									".self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"], $val_desc)."
									WHERE IBLOCK_ELEMENT_ID=".$ELEMENT_ID."
								");
							}
							else
							{
								$DB->Query("
									UPDATE ".$strTable."
									SET 	VALUE='".$DB->ForSql($val)."'
										,VALUE_NUM=".CIBlock::roundDB($val)."
										".($val_desc!==false ? ",DESCRIPTION='".$DB->ForSql($val_desc, 255)."'" : "")."
									WHERE ID=".$res["ID"]."
								");
							}

							if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y")
							{
								$arV2ClearCache[$prop["ID"]] =
									"PROPERTY_".$prop["ID"]." = NULL"
									.self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"])
								;
							}
						}
						unset($PROP[$res["ID"]]);
					} //foreach ($arDBProps[$prop["ID"]] as $res)
				}

				foreach($PROP as $val)
				{
					if(is_array($val) && !is_set($val, "tmp_name"))
					{
						$val_desc = $val["DESCRIPTION"];
						$val = $val["VALUE"];
					}
					else
					{
						$val_desc = false;
					}

					if (strlen($val) <= 0)
						continue;

					if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N")
					{
						$DB->Query("
							UPDATE b_iblock_element_prop_s".$prop["IBLOCK_ID"]."
							SET
								PROPERTY_".$prop["ID"]." = '".$DB->ForSql($val)."'
								".self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"], $val_desc)."
							WHERE IBLOCK_ELEMENT_ID=".$ELEMENT_ID."
						");
					}
					else
					{
						$DB->Query("
							INSERT INTO ".$strTable."
							(IBLOCK_ELEMENT_ID, IBLOCK_PROPERTY_ID, VALUE, VALUE_NUM".($val_desc!==false?", DESCRIPTION":"").")
							SELECT
								".$ELEMENT_ID."
								,P.ID
								,'".$DB->ForSql($val)."'
								,".CIBlock::roundDB($val)."
								".($val_desc!==false?", '".$DB->ForSQL($val_desc, 255)."'":"")."
							FROM
								b_iblock_property P
							WHERE
								ID = ".IntVal($prop["ID"])."
						");
					}

					if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y")
					{
						$arV2ClearCache[$prop["ID"]] =
							"PROPERTY_".$prop["ID"]." = NULL"
							.self::__GetDescriptionUpdateSql($prop["IBLOCK_ID"], $prop["ID"])
						;
					}

					if ($prop["MULTIPLE"] != "Y")
						break;
				} //foreach($PROP as $value)
			} //if($prop["PROPERTY_TYPE"]=="F")
		}

		if ($arV2ClearCache)
		{
			$DB->Query("
				UPDATE b_iblock_element_prop_s".$IBLOCK_ID."
				SET ".implode(",", $arV2ClearCache)."
				WHERE IBLOCK_ELEMENT_ID = ".$ELEMENT_ID."
			");
		}

		foreach ($arFilesToDelete as $deleteTask)
		{
			CIBLockElement::DeleteFile(
				$deleteTask["FILE_ID"],
				false,
				"PROPERTY", $deleteTask["ELEMENT_ID"],
				$deleteTask["IBLOCK_ID"]
			);
		}

		if($bRecalcSections)
			CIBlockElement::RecalcSections($ELEMENT_ID);

		/****************************** QUOTA ******************************/
			$_SESSION["SESS_RECOUNT_DB"] = "Y";
		/****************************** QUOTA ******************************/

		foreach (GetModuleEvents("iblock", "OnAfterIBlockElementSetPropertyValues", true) as $arEvent)
			ExecuteModuleEventEx($arEvent, array($ELEMENT_ID, $IBLOCK_ID, $PROPERTY_VALUES, $PROPERTY_CODE));
	}