function SaveFile($arFile, $strSavePath, $bForceMD5 = false, $bSkipExt = false) { $strFileName = GetFileName($arFile["name"]); /* filename.gif */ if (isset($arFile["del"]) && $arFile["del"] != '') { CFile::DoDelete($arFile["old_file"]); if ($strFileName == '') { return "NULL"; } } if ($arFile["name"] == '') { if (isset($arFile["description"]) && intval($arFile["old_file"]) > 0) { CFile::UpdateDesc($arFile["old_file"], $arFile["description"]); } return false; } if (array_key_exists("content", $arFile)) { if (!array_key_exists("size", $arFile)) { $arFile["size"] = CUtil::BinStrlen($arFile["content"]); } } else { $arFile["size"] = filesize($arFile["tmp_name"]); } $arFile["ORIGINAL_NAME"] = $strFileName; $io = CBXVirtualIo::GetInstance(); if (self::validateFile($strFileName, $arFile, $bForceMD5) !== "") { return false; } $upload_dir = COption::GetOptionString("main", "upload_dir", "upload"); if ($arFile["type"] == "image/pjpeg" || $arFile["type"] == "image/jpg") { $arFile["type"] = "image/jpeg"; } //.jpe is not image type on many systems if (strtolower(GetFileExtension($strFileName)) == "jpe") { $strFileName = substr($strFileName, 0, -4) . ".jpg"; } $bExternalStorage = false; foreach (GetModuleEvents("main", "OnFileSave", true) as $arEvent) { if (ExecuteModuleEventEx($arEvent, array(&$arFile, $strFileName, $strSavePath, $bForceMD5, $bSkipExt))) { $bExternalStorage = true; break; } } if (!$bExternalStorage) { $newName = ''; if ($bForceMD5 != true && COption::GetOptionString("main", "save_original_file_name", "N") == "Y") { $dir_add = ''; $i = 0; while (true) { $dir_add = substr(md5(uniqid(mt_rand(), true)), 0, 3); if (!$io->FileExists($_SERVER["DOCUMENT_ROOT"] . "/" . $upload_dir . "/" . $strSavePath . "/" . $dir_add . "/" . $strFileName)) { break; } if ($i >= 25) { $j = 0; while (true) { $dir_add = substr(md5(mt_rand()), 0, 3) . "/" . substr(md5(mt_rand()), 0, 3); if (!$io->FileExists($_SERVER["DOCUMENT_ROOT"] . "/" . $upload_dir . "/" . $strSavePath . "/" . $dir_add . "/" . $strFileName)) { break; } if ($j >= 25) { $dir_add = substr(md5(mt_rand()), 0, 3) . "/" . md5(mt_rand()); break; } $j++; } break; } $i++; } if (substr($strSavePath, -1, 1) != "/") { $strSavePath .= "/" . $dir_add; } else { $strSavePath .= $dir_add . "/"; } $newName = $strFileName; } else { $strFileExt = $bSkipExt == true ? '' : strrchr($strFileName, "."); while (true) { $newName = md5(uniqid(mt_rand(), true)) . $strFileExt; if (substr($strSavePath, -1, 1) != "/") { $strSavePath .= "/" . substr($newName, 0, 3); } else { $strSavePath .= substr($newName, 0, 3) . "/"; } if (!$io->FileExists($_SERVER["DOCUMENT_ROOT"] . "/" . $upload_dir . "/" . $strSavePath . "/" . $newName)) { break; } } } $arFile["SUBDIR"] = $strSavePath; $arFile["FILE_NAME"] = $newName; $strDirName = $_SERVER["DOCUMENT_ROOT"] . "/" . $upload_dir . "/" . $strSavePath . "/"; $strDbFileNameX = $strDirName . $newName; $strPhysicalFileNameX = $io->GetPhysicalName($strDbFileNameX); CheckDirPath($strDirName); if (is_set($arFile, "content")) { $f = fopen($strPhysicalFileNameX, "ab"); if (!$f) { return false; } if (!fwrite($f, $arFile["content"])) { return false; } fclose($f); } elseif (!copy($arFile["tmp_name"], $strPhysicalFileNameX) && !move_uploaded_file($arFile["tmp_name"], $strPhysicalFileNameX)) { CFile::DoDelete($arFile["old_file"]); return false; } if (isset($arFile["old_file"])) { CFile::DoDelete($arFile["old_file"]); } @chmod($strPhysicalFileNameX, BX_FILE_PERMISSIONS); $imgArray = CFile::GetImageSize($strDbFileNameX); if (is_array($imgArray)) { $arFile["WIDTH"] = $imgArray[0]; $arFile["HEIGHT"] = $imgArray[1]; } else { $arFile["WIDTH"] = 0; $arFile["HEIGHT"] = 0; } } /****************************** QUOTA ******************************/ if (COption::GetOptionInt("main", "disk_space") > 0) { CDiskQuota::updateDiskQuota("file", $arFile["size"], "insert"); } /****************************** QUOTA ******************************/ $NEW_IMAGE_ID = CFile::DoInsert(array("HEIGHT" => $arFile["HEIGHT"], "WIDTH" => $arFile["WIDTH"], "FILE_SIZE" => $arFile["size"], "CONTENT_TYPE" => $arFile["type"], "SUBDIR" => $arFile["SUBDIR"], "FILE_NAME" => $arFile["FILE_NAME"], "MODULE_ID" => $arFile["MODULE_ID"], "ORIGINAL_NAME" => $arFile["ORIGINAL_NAME"], "DESCRIPTION" => isset($arFile["description"]) ? $arFile["description"] : '', "HANDLER_ID" => isset($arFile["HANDLER_ID"]) ? $arFile["HANDLER_ID"] : '')); CFile::CleanCache($NEW_IMAGE_ID); return $NEW_IMAGE_ID; }
function SetPropertyValues($ELEMENT_ID, $IBLOCK_ID, $PROPERTY_VALUES, $PROPERTY_CODE = false) { global $DB; $ELEMENT_ID = intVal($ELEMENT_ID); if (!is_array($PROPERTY_VALUES)) { $PROPERTY_VALUES = array($PROPERTY_VALUES); } $arFilter = array("IBLOCK_ID" => $IBLOCK_ID, "CHECK_PERMISSIONS" => "N"); if ($PROPERTY_CODE !== false) { if (IntVal($PROPERTY_CODE) > 0) { $arFilter["ID"] = IntVal($PROPERTY_CODE); } else { $arFilter["CODE"] = $PROPERTY_CODE; } } else { $arFilter["ACTIVE"] = "Y"; } $uniq_flt = md5(serialize($arFilter)); global $BX_IBLOCK_PROP_CACHE; if (!is_set($BX_IBLOCK_PROP_CACHE, $IBLOCK_ID)) { $BX_IBLOCK_PROP_CACHE[$IBLOCK_ID] = array(); } if (is_set($BX_IBLOCK_PROP_CACHE[$IBLOCK_ID], $uniq_flt)) { $ar_prop =& $BX_IBLOCK_PROP_CACHE[$IBLOCK_ID][$uniq_flt]; } else { $db_prop = CIBlockProperty::GetList(array(), $arFilter); $ar_prop = array(); while ($prop = $db_prop->Fetch()) { $ar_prop[] = $prop; } $BX_IBLOCK_PROP_CACHE[$IBLOCK_ID][$uniq_flt] =& $ar_prop; } Reset($ar_prop); $bRecalcSections = false; $arPROP_ID = array_keys($PROPERTY_VALUES); $cacheValues = false; if (count($ar_prop) > 1) { $cacheValues = array(); $strSql = "SELECT ep.ID, ep.VALUE, ep.IBLOCK_PROPERTY_ID " . "FROM b_iblock_element_property ep, b_iblock_property p " . "WHERE ep.IBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\tAND ep.IBLOCK_PROPERTY_ID = p.ID " . "\tAND p.PROPERTY_TYPE <> 'L' " . "\tAND p.PROPERTY_TYPE <> 'G' "; $db_res = $DB->Query($strSql); while ($res = $db_res->Fetch()) { if (!isset($cacheValues[$res["IBLOCK_PROPERTY_ID"]])) { $cacheValues[$res["IBLOCK_PROPERTY_ID"]] = array(); } $cacheValues[$res["IBLOCK_PROPERTY_ID"]][] = $res; } } $ids = "0"; foreach ($ar_prop as $prop) { if ($PROPERTY_CODE) { $PROP = $PROPERTY_VALUES; } else { if (strlen($prop["CODE"]) > 0 && in_array($prop["CODE"], $arPROP_ID, TRUE)) { $PROP = $PROPERTY_VALUES[$prop["CODE"]]; } else { $PROP = $PROPERTY_VALUES[$prop["ID"]]; } } if ($prop["PROPERTY_TYPE"] == "F") { if (!is_array($PROP) || is_array($PROP) && (is_set($PROP, "tmp_name") || is_set($PROP, "del")) || count($PROP) == 2 && is_set($PROP, "VALUE") && is_set($PROP, "DESCRIPTION")) { $PROP = array($PROP); } } elseif (!is_array($PROP) || count($PROP) == 2 && is_set($PROP, "VALUE") && is_set($PROP, "DESCRIPTION")) { $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)) { $value = array("VALUE" => $value); } elseif (!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), false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") { $strSql = "\n\t\t\t\t\t\tUPDATE\tb_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\tSET\tPROPERTY_" . $prop["ID"] . "=NULL, DESCRIPTION_" . $prop["ID"] . "=NULL\n\t\t\t\t\t\tWHERE\tIBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\t"; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } $ids = "0"; foreach ($PROP as $key => $value) { if (is_array($value)) { $value = $value["VALUE"]; } if (IntVal($value) <= 0) { continue; } $ids .= "," . IntVal($value); if ($prop["MULTIPLE"] != "Y") { break; } } if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") { $strSql = "\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 { $strSql = "\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"; } $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } elseif ($prop["PROPERTY_TYPE"] == "G") { $bRecalcSections = true; $DB->Query(CIBLockElement::DeletePropertySQL($prop, $ELEMENT_ID), false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") { $strSql = "\n\t\t\t\t\t\tUPDATE\tb_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\tSET\tPROPERTY_" . $prop["ID"] . "=NULL, DESCRIPTION_" . $prop["ID"] . "=NULL\n\t\t\t\t\t\tWHERE\tIBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\t"; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } $DB->Query("DELETE FROM b_iblock_section_element WHERE ADDITIONAL_PROPERTY_ID=" . $prop["ID"] . " AND IBLOCK_ELEMENT_ID=" . $ELEMENT_ID, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); $ids = "0"; foreach ($PROP as $key => $value) { if (is_array($value)) { $value = $value["VALUE"]; } if (IntVal($value) <= 0) { continue; } $ids .= "," . IntVal($value); if ($prop["MULTIPLE"] != "Y") { break; } } if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") { $strSql = "\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,DESCRIPTION_" . $prop["ID"] . "=null\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 S.IBLOCK_ID = P.LINK_IBLOCK_ID\n\t\t\t\t\t\t\tAND S.ID IN (" . $ids . ")\n\t\t\t\t\t"; } else { $strSql = "\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 S.IBLOCK_ID = P.LINK_IBLOCK_ID\n\t\t\t\t\t\t\tAND S.ID IN (" . $ids . ")\n\t\t\t\t\t"; } $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); $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"] . " " . "\tAND S.IBLOCK_ID = P.LINK_IBLOCK_ID " . "\tAND S.ID IN (" . $ids . ") ", false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } else { $ids = "0"; $arV = array(); if ($cacheValues === false || $prop["VERSION"] == 2) { if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") { $strSql = "\n\t\t\t\t\t\t\tSELECT\tconcat(IBLOCK_ELEMENT_ID,':','" . $prop["ID"] . "') ID, PROPERTY_" . $prop["ID"] . " VALUE\n\t\t\t\t\t\t\tFROM\tb_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\tWHERE\tIBLOCK_ELEMENT_ID=" . $ELEMENT_ID; } else { $strSql = "\n\t\t\t\t\t\t\tSELECT\tID, VALUE\n\t\t\t\t\t\t\tFROM\t" . $strTable . "\n\t\t\t\t\t\t\tWHERE\tIBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\tAND IBLOCK_PROPERTY_ID=" . $prop["ID"]; } $db_res = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); while ($res = $db_res->Fetch()) { $arV[] = $res; } } elseif (is_set($cacheValues, $prop["ID"])) { $arV = $cacheValues[$prop["ID"]]; } $arWas = array(); foreach ($arV as $res) { $val = $PROP[$res["ID"]]; if (is_array($val) && !is_set($val, "tmp_name") && !is_set($val, "del")) { $val_desc = $val["DESCRIPTION"]; $val = $val["VALUE"]; } else { $val_desc = false; } if ($prop["PROPERTY_TYPE"] == "E") { if (in_array($val, $arWas)) { $val = ""; } else { $arWas[] = $val; } } if ($prop["PROPERTY_TYPE"] == "S" || $prop["PROPERTY_TYPE"] == "N" || $prop["PROPERTY_TYPE"] == "E") { if (strlen($val) <= 0) { if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") { $strSql = "\n\t\t\t\t\t\t\t\t\tUPDATE\tb_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,DESCRIPTION_" . $prop["ID"] . "=null\n\t\t\t\t\t\t\t\t\tWHERE\tIBLOCK_ELEMENT_ID=" . $ELEMENT_ID; } else { $strSql = "DELETE FROM " . $strTable . " WHERE ID=" . $res["ID"]; } $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") { $strSql = "\n\t\t\t\t\t\t\t\t\tUPDATE\tb_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\t\t\tSET\tPROPERTY_" . $prop["ID"] . "=NULL, DESCRIPTION_" . $prop["ID"] . "=NULL\n\t\t\t\t\t\t\t\t\tWHERE\tIBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\t"; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } } else { if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") { if ($prop["PROPERTY_TYPE"] == "N") { $val = CIBlock::roundDB($val); } $strSql = "\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,DESCRIPTION_" . $prop["ID"] . "=" . ($val_desc !== false ? "'" . $DB->ForSQL($val_desc, 255) . "'" : "null") . "\n\t\t\t\t\t\t\t\t\tWHERE IBLOCK_ELEMENT_ID=" . $ELEMENT_ID; } else { $strSql = "\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"]; } $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") { $strSql = "\n\t\t\t\t\t\t\t\t\tUPDATE\tb_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\t\t\tSET\tPROPERTY_" . $prop["ID"] . "=NULL, DESCRIPTION_" . $prop["ID"] . "=NULL\n\t\t\t\t\t\t\t\t\tWHERE\tIBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\t"; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } } } elseif ($prop["PROPERTY_TYPE"] == "F") { if (is_array($val)) { if (strlen($val["del"]) > 0) { $val = "NULL"; } else { $val["MODULE_ID"] = "iblock"; if ($val_desc !== false) { $val["description"] = $val_desc; } if ($val_desc !== false && strlen($val["name"]) <= 0) { //update description only if ($res["VALUE"] > 0) { CFile::UpdateDesc($res["VALUE"], $val_desc); } $val = false; } else { //register new file $val = CFile::SaveFile($val, "iblock"); } } } if ($val == "NULL") { CIBLockElement::DeleteFile($res["VALUE"], $ELEMENT_ID, "PROPERTY", -1, $prop["IBLOCK_ID"]); if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") { $strSql = "\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,DESCRIPTION_" . $prop["ID"] . "=null\n\t\t\t\t\t\t\t\t\tWHERE IBLOCK_ELEMENT_ID=" . $ELEMENT_ID; } else { $strSql = "DELETE FROM " . $strTable . " WHERE ID=" . $res["ID"]; } $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") { $strSql = "\n\t\t\t\t\t\t\t\t\tUPDATE\tb_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\t\t\tSET\tPROPERTY_" . $prop["ID"] . "=NULL, DESCRIPTION_" . $prop["ID"] . "=NULL\n\t\t\t\t\t\t\t\t\tWHERE\tIBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\t"; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } } elseif (IntVal($val) > 0) { if (intval($val) != $res["VALUE"]) { CIBLockElement::DeleteFile($res["VALUE"], $ELEMENT_ID, "PROPERTY", -1, $prop["IBLOCK_ID"]); } if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") { $strSql = "\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"] . "='" . intval($val) . "'\n\t\t\t\t\t\t\t\t\t" . ($val_desc !== false ? ",DESCRIPTION_" . $prop["ID"] . "='" . $DB->ForSql($val_desc, 255) . "'" : "") . "\n\t\t\t\t\t\t\t\t\tWHERE IBLOCK_ELEMENT_ID=" . $ELEMENT_ID; } else { $strSql = "\n\t\t\t\t\t\t\t\t\tUPDATE " . $strTable . "\n\t\t\t\t\t\t\t\t\tSET \tVALUE='" . intval($val) . "'\n\t\t\t\t\t\t\t\t\t\t,VALUE_NUM='" . intval($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"]; } $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") { $strSql = "\n\t\t\t\t\t\t\t\t\tUPDATE\tb_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\t\t\tSET\tPROPERTY_" . $prop["ID"] . "=NULL, DESCRIPTION_" . $prop["ID"] . "=NULL\n\t\t\t\t\t\t\t\t\tWHERE\tIBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\t"; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } } elseif ($val_desc !== false) { if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") { $strSql = "\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 DESCRIPTION_" . $prop["ID"] . "='" . $DB->ForSql($val_desc, 255) . "'\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 { $strSql = "\n\t\t\t\t\t\t\t\t\tUPDATE " . $strTable . "\n\t\t\t\t\t\t\t\t\tSET 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"; } $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") { $strSql = "\n\t\t\t\t\t\t\t\t\tUPDATE\tb_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\t\t\tSET\tPROPERTY_" . $prop["ID"] . "=NULL, DESCRIPTION_" . $prop["ID"] . "=NULL\n\t\t\t\t\t\t\t\t\tWHERE\tIBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\t"; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } } } $ids .= "," . $res["ID"]; unset($PROP[$res["ID"]]); } //foreach($arV as $res) foreach ($PROP as $key => $val) { if (is_array($val) && !is_set($val, "tmp_name")) { $val_desc = $val["DESCRIPTION"]; $val = $val["VALUE"]; } else { $val_desc = false; } if ($prop["PROPERTY_TYPE"] == "F") { if (is_array($val)) { $val["MODULE_ID"] = "iblock"; if ($val_desc !== false) { $val["description"] = $val_desc; } $val = CFile::SaveFile($val, "iblock"); } if (intval($val) <= 0) { $val = false; } elseif ($prop["MULTIPLE"] != "Y" && strlen($val) > 0) { $strSql = "\n\t\t\t\t\t\t\t\tSELECT VALUE\n\t\t\t\t\t\t\t\tFROM b_iblock_element_property\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\tIBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\t\tAND IBLOCK_PROPERTY_ID=" . IntVal($prop["ID"]) . "\n\t\t\t\t\t\t\t"; if ($prop["VERSION"] == 2) { if ($prop["MULTIPLE"] == "Y") { $strSql = "\n\t\t\t\t\t\t\t\t\t\tSELECT PROPERTY_" . $prop["ID"] . " VALUE\n\t\t\t\t\t\t\t\t\t\tFROM b_iblock_element_prop_m" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\tIBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\t\t\tAND IBLOCK_PROPERTY_ID=" . IntVal($prop["ID"]) . "\n\t\t\t\t\t\t\t\t\t"; } else { $strSql = "\n\t\t\t\t\t\t\t\t\t\tSELECT PROPERTY_" . $prop["ID"] . " VALUE\n\t\t\t\t\t\t\t\t\t\tFROM b_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\t\t\t\tWHERE IBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\t\t"; } } $pfres = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); while ($pfar = $pfres->Fetch()) { CIBLockElement::DeleteFile($pfar["VALUE"], $ELEMENT_ID, "PROPERTY", -1, $prop["IBLOCK_ID"]); } $DB->Query(CIBLockElement::DeletePropertySQL($prop, $ELEMENT_ID), false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") { $strSql = "\n\t\t\t\t\t\t\t\t\tUPDATE\tb_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\t\t\tSET\tPROPERTY_" . $prop["ID"] . "=NULL, DESCRIPTION_" . $prop["ID"] . "=NULL\n\t\t\t\t\t\t\t\t\tWHERE\tIBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\t"; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } } } elseif ($prop["PROPERTY_TYPE"] == "E") { if (in_array($val, $arWas)) { $val = ""; } else { $arWas[] = $val; } } if (strlen($val) > 0) { if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "N") { $strSql = "\n\t\t\t\t\t\t\t\tUPDATE\tb_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\t\tSET\n\t\t\t\t\t\t\t\t\tPROPERTY_" . $prop["ID"] . " = '" . $DB->ForSql($val) . "'\n\t\t\t\t\t\t\t\t\t,DESCRIPTION_" . $prop["ID"] . "=" . ($val_desc !== false ? "'" . $DB->ForSQL($val_desc, 255) . "'" : "null") . "\n\t\t\t\t\t\t\t\tWHERE\tIBLOCK_ELEMENT_ID=" . $ELEMENT_ID; } else { $strSql = "\n\t\t\t\t\t\t\t\tINSERT INTO " . $strTable . "\n\t\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\t\tSELECT\n\t\t\t\t\t\t\t\t\t" . $ELEMENT_ID . "\n\t\t\t\t\t\t\t\t\t,P.ID\n\t\t\t\t\t\t\t\t\t,'" . $DB->ForSql($val) . "'\n\t\t\t\t\t\t\t\t\t," . CIBlock::roundDB($val) . "\n\t\t\t\t\t\t\t\t\t" . ($val_desc !== false ? ", '" . $DB->ForSQL($val_desc, 255) . "'" : "") . "\n\t\t\t\t\t\t\t\tFROM\tb_iblock_property P\n\t\t\t\t\t\t\t\tWHERE\tID=" . IntVal($prop["ID"]); } $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($prop["VERSION"] == 2 && $prop["MULTIPLE"] == "Y") { $strSql = "\n\t\t\t\t\t\t\t\tUPDATE\tb_iblock_element_prop_s" . $prop["IBLOCK_ID"] . "\n\t\t\t\t\t\t\t\tSET\tPROPERTY_" . $prop["ID"] . "=NULL, DESCRIPTION_" . $prop["ID"] . "=NULL\n\t\t\t\t\t\t\t\tWHERE\tIBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\t\t\t"; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } if ($prop["MULTIPLE"] != "Y") { break; } } $ids .= "," . $prop["ID"]; } //foreach($PROP as $key=>$value) } //if($prop["PROPERTY_TYPE"]=="L") } if ($bRecalcSections) { CIBlockElement::RecalcSections($ELEMENT_ID); } /****************************** QUOTA ******************************/ $_SESSION["SESS_RECOUNT_DB"] = "Y"; /****************************** QUOTA ******************************/ }
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)); } }
/** * {@inheritdoc} */ public function processEditAction() { parent::processEditAction(); if ($this->getSettings('MULTIPLE')) { if ($this->getSettings('READONLY') === true) { //удаляем все добавленные файлы в режиме только для чтения foreach ($this->data[$this->code] as $key => $value) { if (!is_array($value)) { unset($this->data[$this->code][$key]); } } return false; } if (class_exists('\\Bitrix\\Main\\UI\\FileInput', true) && $this->getSettings('IMAGE') === true) { foreach ($this->data[$this->code] as $key => $value) { if (is_array($value) && ($value['name'] || $value['tmp_name'])) { $_FILES[$this->code]['name'][$key] = $value['name']; $_FILES[$this->code]['type'][$key] = $value['type']; $_FILES[$this->code]['tmp_name'][$key] = $value['tmp_name']; $_FILES[$this->code]['error'][$key] = $value['error']; $_FILES[$this->code]['size'][$key] = $value['size']; unset($this->data[$this->code][$key]); } else { $_FILES[$this->code]['name'][$key] = ''; } } if (!count($this->data[$this->code])) { unset($this->data[$this->code]); } } if (!empty($_FILES[$this->getCode()])) { foreach ($_FILES[$this->getCode()]['name'] as $key => $fileName) { if (empty($fileName) || empty($_FILES[$this->getCode()]['tmp_name'][$key]) || !empty($_FILES[$this->getCode()]['error'][$key])) { if (isset($_REQUEST[$this->getCode() . '_del'][$key])) { if (is_array($this->data[$this->getCode()][$key]) && !empty($this->data[$this->getCode()][$key]['VALUE'])) { \CFile::Delete(intval($this->data[$this->getCode()][$key]['VALUE'])); } else { \CFile::Delete(intval($this->data[$this->getCode()][$key])); } unset($this->data[$this->getCode()][$key]); } elseif ($this->data[$this->getCode()][$key]['VALUE']) { \CFile::UpdateDesc($this->data[$this->getCode()][$key]['VALUE'], $_REQUEST[$this->getCode() . '_descr'][$key]); } continue; } elseif (is_int($key)) { //Удаляем старый файл при замене if (is_array($this->data[$this->getCode()][$key]) && !empty($this->data[$this->getCode()][$key]['VALUE'])) { \CFile::Delete(intval($this->data[$this->getCode()][$key]['VALUE'])); } else { \CFile::Delete(intval($this->data[$this->getCode()][$key])); } } $description = null; if (isset($_REQUEST[$this->getCode() . '_descr'][$key])) { $description = $_REQUEST[$this->getCode() . '_descr'][$key]; } if (empty($this->data[$this->getCode()][$key])) { unset($this->data[$this->getCode()][$key]); } $fileId = $this->saveFile($fileName, $_FILES[$this->getCode()]['tmp_name'][$key], false, $description); if ($fileId) { $this->data[$this->getCode()][$key] = array('VALUE' => $fileId); } else { $this->addError('DIGITALWAND_AH_FAIL_ADD_FILE', array('FILE_NAME' => $_FILES[$this->getCode()]['name'][$key])); } } } } else { if (class_exists('\\Bitrix\\Main\\UI\\FileInput', true) && $this->getSettings('IMAGE') === true) { if (is_array($this->data[$this->code . '_FILE']) && ($this->data[$this->code . '_FILE']['name'] || $this->data[$this->code . '_FILE']['tmp_name'])) { $_FILES['FIELDS']['name'][$this->code . '_FILE'] = $this->data[$this->code . '_FILE']['name']; $_FILES['FIELDS']['type'][$this->code . '_FILE'] = $this->data[$this->code . '_FILE']['type']; $_FILES['FIELDS']['tmp_name'][$this->code . '_FILE'] = $this->data[$this->code . '_FILE']['tmp_name']; $_FILES['FIELDS']['error'][$this->code . '_FILE'] = $this->data[$this->code . '_FILE']['error']; $_FILES['FIELDS']['size'][$this->code . '_FILE'] = $this->data[$this->code . '_FILE']['size']; } } unset($this->data[$this->code . '_FILE']); if ($this->getSettings('READONLY') === true) { return false; } if (empty($_FILES['FIELDS']['name'][$this->code . '_FILE']) || empty($_FILES['FIELDS']['tmp_name'][$this->code . '_FILE']) || !empty($_FILES['FIELDS']['error'][$this->code . '_FILE'])) { if (isset($_REQUEST['FIELDS_del'][$this->code . '_FILE']) and $_REQUEST['FIELDS_del'][$this->code . '_FILE'] == 'Y') { \CFile::Delete(intval($this->data[$this->code])); $this->data[$this->code] = 0; } elseif ($this->data[$this->code] && isset($_REQUEST['FIELDS_descr'][$this->code . '_FILE'])) { \CFile::UpdateDesc($this->data[$this->code], $_REQUEST['FIELDS_descr'][$this->code . '_FILE']); } return false; } $description = null; if (isset($_REQUEST['FIELDS_descr'][$this->code . '_FILE'])) { $description = $_REQUEST['FIELDS_descr'][$this->code . '_FILE']; } $name = $_FILES['FIELDS']['name'][$this->code . '_FILE']; $path = $_FILES['FIELDS']['tmp_name'][$this->code . '_FILE']; $type = $_FILES['FIELDS']['type'][$this->code . '_FILE']; $this->saveFile($name, $path, $type, $description); } }
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)); }
function SaveFile($arFile, $strSavePath, $bForceMD5=false, $bSkipExt=false) { $strFileName = GetFileName($arFile["name"]); /* filename.gif */ if(isset($arFile["del"]) && $arFile["del"] <> '') { CFile::DoDelete($arFile["old_file"]); if($strFileName == '') return "NULL"; } if($arFile["name"] == '') { if(is_set($arFile, "description") && intval($arFile["old_file"])>0) CFile::UpdateDesc($arFile["old_file"], $arFile["description"]); return false; } if(is_set($arFile, "content") && !is_set($arFile, "size")) $arFile["size"] = CUtil::BinStrlen($arFile["content"]); else $arFile["size"] = filesize($arFile["tmp_name"]); /****************************** QUOTA ******************************/ if (COption::GetOptionInt("main", "disk_space") > 0) { $quota = new CDiskQuota(); if (!$quota->checkDiskQuota($arFile)) return false; } /****************************** QUOTA ******************************/ $arFile["ORIGINAL_NAME"] = $strFileName; $io = CBXVirtualIo::GetInstance(); if($bForceMD5 != true && COption::GetOptionString("main", "save_original_file_name", "N") == "Y") { if(COption::GetOptionString("main", "translit_original_file_name", "N") == "Y") $strFileName = CUtil::translit($strFileName, LANGUAGE_ID, array("max_len"=>1024, "safe_chars"=>".")); if(COption::GetOptionString("main", "convert_original_file_name", "Y") == "Y") $strFileName = $io->RandomizeInvalidFilename($strFileName); } if(!$io->ValidateFilenameString($strFileName)) return false; //check for double extension vulnerability $strFileName = RemoveScriptExtension($strFileName); if($strFileName == '') return false; if(strlen($strFileName) > 255) return false; //check .htaccess etc. if(IsFileUnsafe($strFileName)) return false; //nginx returns octet-stream for .jpg if(GetFileNameWithoutExtension($strFileName) == '') return false; $upload_dir = COption::GetOptionString("main", "upload_dir", "upload"); if($arFile["type"]=="image/pjpeg" || $arFile["type"]=="image/jpg") $arFile["type"]="image/jpeg"; //.jpe is not image type on many systems if(strtolower(GetFileExtension($strFileName)) == "jpe") $strFileName = substr($strFileName, 0, -4).".jpg"; $bExternalStorage = false; foreach(GetModuleEvents("main", "OnFileSave", true) as $arEvent) { if(ExecuteModuleEventEx($arEvent, array(&$arFile, $strFileName, $strSavePath, $bForceMD5, $bSkipExt))) { $bExternalStorage = true; break; } } if(!$bExternalStorage) { $newName = ''; if($bForceMD5 != true && COption::GetOptionString("main", "save_original_file_name", "N")=="Y") { $dir_add = ''; $i=0; while(true) { $dir_add = substr(md5(uniqid(mt_rand(), true)), 0, 3); if(!$io->FileExists($_SERVER["DOCUMENT_ROOT"]."/".$upload_dir."/".$strSavePath."/".$dir_add."/".$strFileName)) break; if($i>=25) { $j=0; while(true) { $dir_add = substr(md5(mt_rand()), 0, 3)."/".substr(md5(mt_rand()), 0, 3); if(!$io->FileExists($_SERVER["DOCUMENT_ROOT"]."/".$upload_dir."/".$strSavePath."/".$dir_add."/".$strFileName)) break; if($j>=25) { $dir_add = substr(md5(mt_rand()), 0, 3)."/".md5(mt_rand()); break; } $j++; } break; } $i++; } if(substr($strSavePath, -1, 1) <> "/") $strSavePath .= "/".$dir_add; else $strSavePath .= $dir_add."/"; $newName = $strFileName; } else { $strFileExt = ($bSkipExt == true? '' : strrchr($strFileName, ".")); while(true) { $newName = md5(uniqid(mt_rand(), true)).$strFileExt; if(substr($strSavePath, -1, 1) <> "/") $strSavePath .= "/".substr($newName, 0, 3); else $strSavePath .= substr($newName, 0, 3)."/"; if(!$io->FileExists($_SERVER["DOCUMENT_ROOT"]."/".$upload_dir."/".$strSavePath."/".$newName)) break; } } $arFile["SUBDIR"] = $strSavePath; $arFile["FILE_NAME"] = $newName; $strDirName = $_SERVER["DOCUMENT_ROOT"]."/".$upload_dir."/".$strSavePath."/"; $strDbFileNameX = $strDirName.$newName; $strPhysicalFileNameX = $io->GetPhysicalName($strDbFileNameX); CheckDirPath($strDirName); if(is_set($arFile, "content")) { $f = fopen($strPhysicalFileNameX, "ab"); if(!$f) return false; if(!fwrite($f, $arFile["content"])) return false; fclose($f); } elseif( !copy($arFile["tmp_name"], $strPhysicalFileNameX) && !move_uploaded_file($arFile["tmp_name"], $strPhysicalFileNameX) ) { CFile::DoDelete($arFile["old_file"]); return false; } if(isset($arFile["old_file"])) CFile::DoDelete($arFile["old_file"]); @chmod($strPhysicalFileNameX, BX_FILE_PERMISSIONS); $imgArray = CFile::GetImageSize($strDbFileNameX); if(is_array($imgArray)) { $arFile["WIDTH"] = $imgArray[0]; $arFile["HEIGHT"] = $imgArray[1]; } else { $arFile["WIDTH"] = 0; $arFile["HEIGHT"] = 0; } } //if(!$bExternalStorage) /****************************** QUOTA ******************************/ if (COption::GetOptionInt("main", "disk_space") > 0) { CDiskQuota::updateDiskQuota("file", $arFile["size"], "insert"); } /****************************** QUOTA ******************************/ $NEW_IMAGE_ID = CFile::DoInsert(array( "HEIGHT" => $arFile["HEIGHT"], "WIDTH" => $arFile["WIDTH"], "FILE_SIZE" => $arFile["size"], "CONTENT_TYPE" => $arFile["type"], "SUBDIR" => $arFile["SUBDIR"], "FILE_NAME" => $arFile["FILE_NAME"], "MODULE_ID" => $arFile["MODULE_ID"], "ORIGINAL_NAME" => $arFile["ORIGINAL_NAME"], "DESCRIPTION" => isset($arFile["description"])? $arFile["description"]: '', "HANDLER_ID" => isset($arFile["HANDLER_ID"])? $arFile["HANDLER_ID"]: '', )); CFile::CleanCache($NEW_IMAGE_ID); return $NEW_IMAGE_ID; }
function SaveFile($arFile, $strSavePath, $bForceMD5 = false, $bSkipExt = false) { $strFileName = GetFileName($arFile["name"]); /* filename.gif */ if (isset($arFile["del"]) && $arFile["del"] != '') { CFile::DoDelete($arFile["old_file"]); if ($strFileName == '') { return "NULL"; } } if ($arFile["name"] == '') { if (isset($arFile["description"]) && intval($arFile["old_file"]) > 0) { CFile::UpdateDesc($arFile["old_file"], $arFile["description"]); } return false; } if (isset($arFile["content"])) { if (!isset($arFile["size"])) { $arFile["size"] = CUtil::BinStrlen($arFile["content"]); } } else { try { $file = new IO\File($arFile["tmp_name"]); $arFile["size"] = $file->getSize(); } catch (IO\IoException $e) { $arFile["size"] = 0; } } $arFile["ORIGINAL_NAME"] = $strFileName; //translit, replace unsafe chars, etc. $strFileName = self::transformName($strFileName, $bForceMD5, $bSkipExt); //transformed name must be valid, check disk quota, etc. if (self::validateFile($strFileName, $arFile) !== "") { return false; } if ($arFile["type"] == "image/pjpeg" || $arFile["type"] == "image/jpg") { $arFile["type"] = "image/jpeg"; } $bExternalStorage = false; foreach (GetModuleEvents("main", "OnFileSave", true) as $arEvent) { if (ExecuteModuleEventEx($arEvent, array(&$arFile, $strFileName, $strSavePath, $bForceMD5, $bSkipExt))) { $bExternalStorage = true; break; } } if (!$bExternalStorage) { $upload_dir = COption::GetOptionString("main", "upload_dir", "upload"); $io = CBXVirtualIo::GetInstance(); if ($bForceMD5 != true && COption::GetOptionString("main", "save_original_file_name", "N") == "Y") { $dir_add = ''; $i = 0; while (true) { $dir_add = substr(md5(uniqid("", true)), 0, 3); if (!$io->FileExists($_SERVER["DOCUMENT_ROOT"] . "/" . $upload_dir . "/" . $strSavePath . "/" . $dir_add . "/" . $strFileName)) { break; } if ($i >= 25) { $j = 0; while (true) { $dir_add = substr(md5(mt_rand()), 0, 3) . "/" . substr(md5(mt_rand()), 0, 3); if (!$io->FileExists($_SERVER["DOCUMENT_ROOT"] . "/" . $upload_dir . "/" . $strSavePath . "/" . $dir_add . "/" . $strFileName)) { break; } if ($j >= 25) { $dir_add = substr(md5(mt_rand()), 0, 3) . "/" . md5(mt_rand()); break; } $j++; } break; } $i++; } if (substr($strSavePath, -1, 1) != "/") { $strSavePath .= "/" . $dir_add; } else { $strSavePath .= $dir_add . "/"; } } else { $strFileExt = $bSkipExt == true || ($ext = GetFileExtension($strFileName)) == '' ? '' : "." . $ext; while (true) { if (substr($strSavePath, -1, 1) != "/") { $strSavePath .= "/" . substr($strFileName, 0, 3); } else { $strSavePath .= substr($strFileName, 0, 3) . "/"; } if (!$io->FileExists($_SERVER["DOCUMENT_ROOT"] . "/" . $upload_dir . "/" . $strSavePath . "/" . $strFileName)) { break; } //try the new name $strFileName = md5(uniqid("", true)) . $strFileExt; } } $arFile["SUBDIR"] = $strSavePath; $arFile["FILE_NAME"] = $strFileName; $strDirName = $_SERVER["DOCUMENT_ROOT"] . "/" . $upload_dir . "/" . $strSavePath . "/"; $strDbFileNameX = $strDirName . $strFileName; $strPhysicalFileNameX = $io->GetPhysicalName($strDbFileNameX); CheckDirPath($strDirName); if (is_set($arFile, "content")) { $f = fopen($strPhysicalFileNameX, "ab"); if (!$f) { return false; } if (fwrite($f, $arFile["content"]) === false) { return false; } fclose($f); } elseif (!copy($arFile["tmp_name"], $strPhysicalFileNameX) && !move_uploaded_file($arFile["tmp_name"], $strPhysicalFileNameX)) { CFile::DoDelete($arFile["old_file"]); return false; } if (isset($arFile["old_file"])) { CFile::DoDelete($arFile["old_file"]); } @chmod($strPhysicalFileNameX, BX_FILE_PERMISSIONS); //flash is not an image $flashEnabled = !CFile::IsImage($arFile["ORIGINAL_NAME"], $arFile["type"]); $imgArray = CFile::GetImageSize($strDbFileNameX, false, $flashEnabled); if (is_array($imgArray)) { $arFile["WIDTH"] = $imgArray[0]; $arFile["HEIGHT"] = $imgArray[1]; if ($imgArray[2] == IMAGETYPE_JPEG) { $exifData = CFile::ExtractImageExif($io->GetPhysicalName($strDbFileNameX)); if ($exifData && isset($exifData['Orientation'])) { //swap width and height if ($exifData['Orientation'] >= 5 && $exifData['Orientation'] <= 8) { $arFile["WIDTH"] = $imgArray[1]; $arFile["HEIGHT"] = $imgArray[0]; } $properlyOriented = CFile::ImageHandleOrientation($exifData['Orientation'], $io->GetPhysicalName($strDbFileNameX)); if ($properlyOriented) { $jpgQuality = intval(COption::GetOptionString('main', 'image_resize_quality', '95')); if ($jpgQuality <= 0 || $jpgQuality > 100) { $jpgQuality = 95; } imagejpeg($properlyOriented, $io->GetPhysicalName($strDbFileNameX), $jpgQuality); } } } } else { $arFile["WIDTH"] = 0; $arFile["HEIGHT"] = 0; } } if ($arFile["WIDTH"] == 0 || $arFile["HEIGHT"] == 0) { //mock image because we got false from CFile::GetImageSize() if (strpos($arFile["type"], "image/") === 0) { $arFile["type"] = "application/octet-stream"; } } if ($arFile["type"] == '' || !is_string($arFile["type"])) { $arFile["type"] = "application/octet-stream"; } /****************************** QUOTA ******************************/ if (COption::GetOptionInt("main", "disk_space") > 0) { CDiskQuota::updateDiskQuota("file", $arFile["size"], "insert"); } /****************************** QUOTA ******************************/ $NEW_IMAGE_ID = CFile::DoInsert(array("HEIGHT" => $arFile["HEIGHT"], "WIDTH" => $arFile["WIDTH"], "FILE_SIZE" => $arFile["size"], "CONTENT_TYPE" => $arFile["type"], "SUBDIR" => $arFile["SUBDIR"], "FILE_NAME" => $arFile["FILE_NAME"], "MODULE_ID" => $arFile["MODULE_ID"], "ORIGINAL_NAME" => $arFile["ORIGINAL_NAME"], "DESCRIPTION" => isset($arFile["description"]) ? $arFile["description"] : '', "HANDLER_ID" => isset($arFile["HANDLER_ID"]) ? $arFile["HANDLER_ID"] : '', "EXTERNAL_ID" => isset($arFile["external_id"]) ? $arFile["external_id"] : md5(mt_rand()))); CFile::CleanCache($NEW_IMAGE_ID); return $NEW_IMAGE_ID; }