function NextStep21($arIBlock) { global $DB, $_SESSION, $arErrors, $arMessages, $INTERVAL; $strSql = "\n\t\tSELECT *\n\t\tFROM b_iblock_element_prop_s" . $arIBlock["ID"] . "\n\t"; $rsElement = $DB->Query($strSql); $t = getmicrotime(); $i = 0; while ($arElement = $rsElement->Fetch()) { $ELEMENT_ID = $arElement["IBLOCK_ELEMENT_ID"]; foreach ($arElement as $key => $value) { if (substr($key, 0, 9) == "PROPERTY_" && strlen($value) > 0) { $ID = intval(substr($key, 9)); //TODO make conversion and check forsql and bind!!!! if ($ID > 0 && array_key_exists($ID, $_SESSION["BX_IBLOCK_CONV"]["arSingle"])) { $arFields = array("IBLOCK_ELEMENT_ID" => $ELEMENT_ID, "IBLOCK_PROPERTY_ID" => $ID, "VALUE" => $value, "~VALUE_NUM" => CIBlock::roundDB($value), "~VALUE_ENUM" => intval($value), "DESCRIPTION" => $arElement["DESCRIPTION_" . $ID]); $arInsert = $DB->PrepareInsert("b_iblock_element_property", $arFields); $rs = $DB->QueryBind("INSERT INTO b_iblock_element_property (" . $arInsert[0] . ")VALUES(" . $arInsert[1] . ")", array("DESCRIPTION" => $arElement["DESCRIPTION_" . $ID])); } } } $strSql = "\n\t\t\tINSERT INTO b_iblock_element_property\n\t\t\t(IBLOCK_ELEMENT_ID,IBLOCK_PROPERTY_ID,VALUE,VALUE_ENUM,VALUE_NUM,DESCRIPTION)\n\t\t\tSELECT IBLOCK_ELEMENT_ID,IBLOCK_PROPERTY_ID,VALUE,VALUE_ENUM,VALUE_NUM,DESCRIPTION\n\t\t\tFROM b_iblock_element_prop_m" . $arIBlock["ID"] . "\n\t\t\tWHERE IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . "\n\t\t"; $rs = $DB->Query($strSql); $strSql = "\n\t\t\tDELETE\n\t\t\tFROM b_iblock_element_prop_m" . $arIBlock["ID"] . "\n\t\t\tWHERE IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . "\n\t\t"; $rs = $DB->Query($strSql); $strSql = "\n\t\t\tDELETE\n\t\t\tFROM b_iblock_element_prop_s" . $arIBlock["ID"] . "\n\t\t\tWHERE IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . "\n\t\t"; $rs = $DB->Query($strSql); $DB->Query("UPDATE b_iblock SET LAST_CONV_ELEMENT = " . $ELEMENT_ID . " WHERE ID = " . $arIBlock["ID"]); $i++; if (getmicrotime() - $t > $INTERVAL) { break; } } $_SESSION["BX_IBLOCK_CONV"]["DONE"] += $i; if ($_SESSION["BX_IBLOCK_CONV"]["TODO"] < $_SESSION["BX_IBLOCK_CONV"]["DONE"]) { $_SESSION["BX_IBLOCK_CONV"]["TODO"] = $_SESSION["BX_IBLOCK_CONV"]["DONE"]; } //$_SESSION["BX_IBLOCK_CONV"]["STAT"][]=$i;$arMessages[]=implode(', ',$_SESSION["BX_IBLOCK_CONV"]["STAT"]); if ($arElement) { return 3; } else { return 4; } }
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)); } }
public static function SetPropertyValuesEx($ELEMENT_ID, $IBLOCK_ID, $PROPERTY_VALUES, $FLAGS = array()) { //Check input parameters if (!is_array($PROPERTY_VALUES)) { return; } if (!is_array($FLAGS)) { $FLAGS = array(); } //FLAGS - modify function behavior //NewElement - if present no db values select will be issued //DoNotValidateLists - if present list values do not validates against metadata tables global $DB; $ELEMENT_ID = intval($ELEMENT_ID); if ($ELEMENT_ID <= 0) { return; } $IBLOCK_ID = intval($IBLOCK_ID); if ($IBLOCK_ID <= 0) { $rs = $DB->Query("select IBLOCK_ID from b_iblock_element where ID=" . $ELEMENT_ID); if ($ar = $rs->Fetch()) { $IBLOCK_ID = $ar["IBLOCK_ID"]; } else { return; } } //Get property metadata static $PROPS_CACHE = array(); if (!array_key_exists($IBLOCK_ID, $PROPS_CACHE)) { $PROPS_CACHE[$IBLOCK_ID] = array(0 => array()); $rs = CIBlockProperty::GetList(array(), array("IBLOCK_ID" => $IBLOCK_ID, "CHECK_PERMISSIONS" => "N", "ACTIVE" => "Y")); while ($ar = $rs->Fetch()) { $ar["ConvertToDB"] = false; if ($ar["USER_TYPE"] != "") { $arUserType = CIBlockProperty::GetUserType($ar["USER_TYPE"]); if (array_key_exists("ConvertToDB", $arUserType)) { $ar["ConvertToDB"] = $arUserType["ConvertToDB"]; } } $PROPS_CACHE[$IBLOCK_ID][$ar["ID"]] = $ar; //For CODE2ID conversion $PROPS_CACHE[$IBLOCK_ID][0][$ar["CODE"]] = $ar["ID"]; //VERSION $PROPS_CACHE[$IBLOCK_ID]["VERSION"] = $ar["VERSION"]; } } //Unify properties values arProps[$property_id]=>array($id=>array("VALUE", "DESCRIPTION"),....) $arProps = array(); foreach ($PROPERTY_VALUES as $key => $value) { //Code2ID if (array_key_exists($key, $PROPS_CACHE[$IBLOCK_ID][0])) { $key = $PROPS_CACHE[$IBLOCK_ID][0][$key]; } else { $key = intval($key); if ($key <= 0 || !array_key_exists($key, $PROPS_CACHE[$IBLOCK_ID])) { continue; } } if ($PROPS_CACHE[$IBLOCK_ID][$key]["PROPERTY_TYPE"] == "F") { if (is_array($value)) { $ar = array_keys($value); if (array_key_exists("tmp_name", $value) || array_key_exists("del", $value)) { $uni_value = array(array("ID" => 0, "VALUE" => $value, "DESCRIPTION" => "")); } elseif ($ar[0] === "VALUE" && $ar[1] === "DESCRIPTION") { $uni_value = array(array("ID" => 0, "VALUE" => $value["VALUE"], "DESCRIPTION" => $value["DESCRIPTION"])); } elseif (count($ar) === 1 && $ar[0] === "VALUE") { $uni_value = array(array("ID" => 0, "VALUE" => $value["VALUE"], "DESCRIPTION" => "")); } else { $uni_value = array(); foreach ($value as $id => $val) { if (is_array($val)) { if (array_key_exists("tmp_name", $val) || array_key_exists("del", $val)) { $uni_value[] = array("ID" => $id, "VALUE" => $val, "DESCRIPTION" => ""); } else { $ar = array_keys($val); if ($ar[0] === "VALUE" && $ar[1] === "DESCRIPTION") { $uni_value[] = array("ID" => $id, "VALUE" => $val["VALUE"], "DESCRIPTION" => $val["DESCRIPTION"]); } elseif (count($ar) === 1 && $ar[0] === "VALUE") { $uni_value[] = array("ID" => $id, "VALUE" => $val["VALUE"], "DESCRIPTION" => ""); } } } } } } else { //There was no valid file array found so we'll skip this property $uni_value = array(); } } elseif (!is_array($value)) { $uni_value = array(array("VALUE" => $value, "DESCRIPTION" => "")); } else { $ar = array_keys($value); if (count($ar) === 2 && $ar[0] === "VALUE" && $ar[1] === "DESCRIPTION") { $uni_value = array(array("VALUE" => $value["VALUE"], "DESCRIPTION" => $value["DESCRIPTION"])); } elseif (count($ar) === 1 && $ar[0] === "VALUE") { $uni_value = array(array("VALUE" => $value["VALUE"], "DESCRIPTION" => "")); } else { $uni_value = array(); foreach ($value as $id => $val) { if (!is_array($val)) { $uni_value[] = array("VALUE" => $val, "DESCRIPTION" => ""); } else { $ar = array_keys($val); if ($ar[0] === "VALUE" && $ar[1] === "DESCRIPTION") { $uni_value[] = array("VALUE" => $val["VALUE"], "DESCRIPTION" => $val["DESCRIPTION"]); } elseif (count($ar) === 1 && $ar[0] === "VALUE") { $uni_value[] = array("VALUE" => $val["VALUE"], "DESCRIPTION" => ""); } } } } } $arValueCounters = array(); foreach ($uni_value as $val) { if (!array_key_exists($key, $arProps)) { $arProps[$key] = array(); $arValueCounters[$key] = 0; } if ($PROPS_CACHE[$IBLOCK_ID][$key]["ConvertToDB"] !== false) { $arProperty = $PROPS_CACHE[$IBLOCK_ID][$key]; $arProperty["ELEMENT_ID"] = $ELEMENT_ID; $val = call_user_func_array($PROPS_CACHE[$IBLOCK_ID][$key]["ConvertToDB"], array($arProperty, $val)); } if (!is_array($val["VALUE"]) && strlen($val["VALUE"]) > 0 || is_array($val["VALUE"]) && count($val["VALUE"]) > 0) { if ($arValueCounters[$key] == 0 || $PROPS_CACHE[$IBLOCK_ID][$key]["MULTIPLE"] == "Y") { if (!is_array($val["VALUE"]) || !isset($val["VALUE"]["del"])) { $arValueCounters[$key]++; } $arProps[$key][] = $val; } } } } if (count($arProps) <= 0) { return; } //Read current property values from database $arDBProps = array(); if (!array_key_exists("NewElement", $FLAGS)) { if ($PROPS_CACHE[$IBLOCK_ID]["VERSION"] == 1) { $rs = $DB->Query("\n\t\t\t\t\tselect *\n\t\t\t\t\tfrom b_iblock_element_property\n\t\t\t\t\twhere IBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\tAND IBLOCK_PROPERTY_ID in (" . implode(", ", array_keys($arProps)) . ")\n\t\t\t\t"); while ($ar = $rs->Fetch()) { if (!array_key_exists($ar["IBLOCK_PROPERTY_ID"], $arDBProps)) { $arDBProps[$ar["IBLOCK_PROPERTY_ID"]] = array(); } $arDBProps[$ar["IBLOCK_PROPERTY_ID"]][$ar["ID"]] = $ar; } } else { $rs = $DB->Query("\n\t\t\t\t\tselect *\n\t\t\t\t\tfrom b_iblock_element_prop_m" . $IBLOCK_ID . "\n\t\t\t\t\twhere IBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t\tAND IBLOCK_PROPERTY_ID in (" . implode(", ", array_keys($arProps)) . ")\n\t\t\t\t"); while ($ar = $rs->Fetch()) { if (!array_key_exists($ar["IBLOCK_PROPERTY_ID"], $arDBProps)) { $arDBProps[$ar["IBLOCK_PROPERTY_ID"]] = array(); } $arDBProps[$ar["IBLOCK_PROPERTY_ID"]][$ar["ID"]] = $ar; } $rs = $DB->Query("\n\t\t\t\t\tselect *\n\t\t\t\t\tfrom b_iblock_element_prop_s" . $IBLOCK_ID . "\n\t\t\t\t\twhere IBLOCK_ELEMENT_ID=" . $ELEMENT_ID . "\n\t\t\t\t"); if ($ar = $rs->Fetch()) { foreach ($PROPS_CACHE[$IBLOCK_ID] as $property_id => $property) { if (array_key_exists($property_id, $arProps) && array_key_exists("PROPERTY_" . $property_id, $ar) && $property["MULTIPLE"] == "N" && strlen($ar["PROPERTY_" . $property_id]) > 0) { $pr = array("IBLOCK_PROPERTY_ID" => $property_id, "VALUE" => $ar["PROPERTY_" . $property_id], "DESCRIPTION" => $ar["DESCRIPTION_" . $property_id]); if (!array_key_exists($pr["IBLOCK_PROPERTY_ID"], $arDBProps)) { $arDBProps[$pr["IBLOCK_PROPERTY_ID"]] = array(); } $arDBProps[$pr["IBLOCK_PROPERTY_ID"]][$ELEMENT_ID . ":" . $property_id] = $pr; } } } } } $arFilesToDelete = array(); //Handle file properties foreach ($arProps as $property_id => $values) { if ($PROPS_CACHE[$IBLOCK_ID][$property_id]["PROPERTY_TYPE"] == "F") { foreach ($values as $i => $value) { $val = $value["VALUE"]; if (strlen($val["del"]) > 0) { $val = "NULL"; } else { $val["MODULE_ID"] = "iblock"; unset($val["old_file"]); if (strlen($value["DESCRIPTION"]) > 0) { $val["description"] = $value["DESCRIPTION"]; } $val = CFile::SaveFile($val, "iblock"); } if ($val == "NULL") { //Delete it! Actually it will not add an value unset($arProps[$property_id][$i]); } elseif (intval($val) > 0) { $arProps[$property_id][$i]["VALUE"] = intval($val); if (strlen($value["DESCRIPTION"]) <= 0) { $arProps[$property_id][$i]["DESCRIPTION"] = $arDBProps[$property_id][$value["ID"]]["DESCRIPTION"]; } } elseif (strlen($value["DESCRIPTION"]) > 0) { $arProps[$property_id][$i]["VALUE"] = $arDBProps[$property_id][$value["ID"]]["VALUE"]; //Only needs to update description so CFile::Delete will not called unset($arDBProps[$property_id][$value["ID"]]); } else { $arProps[$property_id][$i]["VALUE"] = $arDBProps[$property_id][$value["ID"]]["VALUE"]; //CFile::Delete will not called unset($arDBProps[$property_id][$value["ID"]]); } } if (array_key_exists($property_id, $arDBProps)) { foreach ($arDBProps[$property_id] as $id => $value) { $arFilesToDelete[] = array($value["VALUE"], $ELEMENT_ID, "PROPERTY", -1, $IBLOCK_ID); } } } } foreach ($arFilesToDelete as $ar) { call_user_func_array(array("CIBlockElement", "DeleteFile"), $ar); } //Now we'll try to find out properties which do not require any update if (!array_key_exists("NewElement", $FLAGS)) { foreach ($arProps as $property_id => $values) { if ($PROPS_CACHE[$IBLOCK_ID][$property_id]["PROPERTY_TYPE"] != "F") { if (array_key_exists($property_id, $arDBProps)) { $db_values = $arDBProps[$property_id]; if (count($values) == count($db_values)) { $bEqual = true; foreach ($values as $id => $value) { $bDBFound = false; foreach ($db_values as $db_id => $db_row) { if (strcmp($value["VALUE"], $db_row["VALUE"]) == 0 && strcmp($value["DESCRIPTION"], $db_row["DESCRIPTION"]) == 0) { unset($db_values[$db_id]); $bDBFound = true; break; } } if (!$bDBFound) { $bEqual = false; break; } } if ($bEqual) { unset($arProps[$property_id]); unset($arDBProps[$property_id]); } } } elseif (count($values) == 0) { //Values was not found in database neither no values input was given unset($arProps[$property_id]); } } } } //Init "commands" arrays $ar2Delete = array("b_iblock_element_property" => array(), "b_iblock_element_prop_m" . $IBLOCK_ID => array(), "b_iblock_section_element" => array()); $ar2Insert = array("values" => array("b_iblock_element_property" => array(), "b_iblock_element_prop_m" . $IBLOCK_ID => array()), "sqls" => array("b_iblock_element_property" => array(), "b_iblock_element_prop_m" . $IBLOCK_ID => array(), "b_iblock_section_element" => array())); $ar2Update = array(); foreach ($arDBProps as $property_id => $values) { if ($PROPS_CACHE[$IBLOCK_ID][$property_id]["VERSION"] == 1) { $ar2Delete["b_iblock_element_property"][$property_id] = true; } elseif ($PROPS_CACHE[$IBLOCK_ID][$property_id]["MULTIPLE"] == "Y") { $ar2Delete["b_iblock_element_prop_m" . $IBLOCK_ID][$property_id] = true; $ar2Update["b_iblock_element_prop_s" . $IBLOCK_ID][$property_id] = false; //null } else { $ar2Update["b_iblock_element_prop_s" . $IBLOCK_ID][$property_id] = false; //null } if ($PROPS_CACHE[$IBLOCK_ID][$property_id]["PROPERTY_TYPE"] == "G") { $ar2Delete["b_iblock_section_element"][$property_id] = true; } } foreach ($arProps as $property_id => $values) { $db_prop = $PROPS_CACHE[$IBLOCK_ID][$property_id]; if ($db_prop["PROPERTY_TYPE"] == "L" && !array_key_exists("DoNotValidateLists", $FLAGS)) { $arID = array(); foreach ($values as $value) { $value["VALUE"] = intval($value["VALUE"]); if ($value["VALUE"] > 0) { $arID[] = $value["VALUE"]; } } if (count($arID) > 0) { if ($db_prop["VERSION"] == 1) { $ar2Insert["sqls"]["b_iblock_element_property"][$property_id] = "\n\t\t\t\t\t\t\t\tINSERT INTO b_iblock_element_property\n\t\t\t\t\t\t\t\t(IBLOCK_ELEMENT_ID, IBLOCK_PROPERTY_ID, VALUE, VALUE_ENUM)\n\t\t\t\t\t\t\t\tSELECT " . $ELEMENT_ID . ", P.ID, PEN.ID, PEN.ID\n\t\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\t\tb_iblock_property P\n\t\t\t\t\t\t\t\t\t,b_iblock_property_enum PEN\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\tP.ID=" . $property_id . "\n\t\t\t\t\t\t\t\t\tAND P.ID=PEN.PROPERTY_ID\n\t\t\t\t\t\t\t\t\tAND PEN.ID IN (" . implode(", ", $arID) . ")\n\t\t\t\t\t\t"; } elseif ($db_prop["MULTIPLE"] == "Y") { $ar2Insert["sqls"]["b_iblock_element_prop_m" . $IBLOCK_ID][$property_id] = "\n\t\t\t\t\t\t\t\tINSERT INTO b_iblock_element_prop_m" . $IBLOCK_ID . "\n\t\t\t\t\t\t\t\t(IBLOCK_ELEMENT_ID, IBLOCK_PROPERTY_ID, VALUE, VALUE_ENUM)\n\t\t\t\t\t\t\t\tSELECT " . $ELEMENT_ID . ", P.ID, PEN.ID, PEN.ID\n\t\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\t\tb_iblock_property P\n\t\t\t\t\t\t\t\t\t,b_iblock_property_enum PEN\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\tP.ID=" . $property_id . "\n\t\t\t\t\t\t\t\t\tAND P.ID=PEN.PROPERTY_ID\n\t\t\t\t\t\t\t\t\tAND PEN.ID IN (" . implode(", ", $arID) . ")\n\t\t\t\t\t\t"; $ar2Update["b_iblock_element_prop_s" . $IBLOCK_ID][$property_id] = false; //null } else { $rs = $DB->Query("\n\t\t\t\t\t\t\t\tSELECT PEN.ID\n\t\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\t\tb_iblock_property P\n\t\t\t\t\t\t\t\t\t,b_iblock_property_enum PEN\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\tP.ID=" . $property_id . "\n\t\t\t\t\t\t\t\t\tAND P.ID=PEN.PROPERTY_ID\n\t\t\t\t\t\t\t\t\tAND PEN.ID IN (" . implode(", ", $arID) . ")\n\t\t\t\t\t\t"); if ($ar = $rs->Fetch()) { $ar2Update["b_iblock_element_prop_s" . $IBLOCK_ID][$property_id] = array("VALUE" => $ar["ID"], "DESCRIPTION" => ""); } } } continue; } if ($db_prop["PROPERTY_TYPE"] == "G") { $arID = array(); foreach ($values as $value) { $value["VALUE"] = intval($value["VALUE"]); if ($value["VALUE"] > 0) { $arID[] = $value["VALUE"]; } } if (count($arID) > 0) { if ($db_prop["VERSION"] == 1) { $ar2Insert["sqls"]["b_iblock_element_property"][$property_id] = "\n\t\t\t\t\t\t\t\tINSERT INTO b_iblock_element_property\n\t\t\t\t\t\t\t\t(IBLOCK_ELEMENT_ID, IBLOCK_PROPERTY_ID, VALUE, VALUE_NUM)\n\t\t\t\t\t\t\t\tSELECT " . $ELEMENT_ID . ", P.ID, S.ID, S.ID\n\t\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\t\tb_iblock_property P\n\t\t\t\t\t\t\t\t\t,b_iblock_section S\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\tP.ID=" . $property_id . "\n\t\t\t\t\t\t\t\t\tAND S.IBLOCK_ID = P.LINK_IBLOCK_ID\n\t\t\t\t\t\t\t\t\tAND S.ID IN (" . implode(", ", $arID) . ")\n\t\t\t\t\t\t"; } elseif ($db_prop["MULTIPLE"] == "Y") { $ar2Insert["sqls"]["b_iblock_element_prop_m" . $IBLOCK_ID][$property_id] = "\n\t\t\t\t\t\t\t\tINSERT INTO b_iblock_element_prop_m" . $IBLOCK_ID . "\n\t\t\t\t\t\t\t\t(IBLOCK_ELEMENT_ID, IBLOCK_PROPERTY_ID, VALUE, VALUE_NUM)\n\t\t\t\t\t\t\t\tSELECT " . $ELEMENT_ID . ", P.ID, S.ID, S.ID\n\t\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\t\tb_iblock_property P\n\t\t\t\t\t\t\t\t\t,b_iblock_section S\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\tP.ID=" . $property_id . "\n\t\t\t\t\t\t\t\t\tAND S.IBLOCK_ID = P.LINK_IBLOCK_ID\n\t\t\t\t\t\t\t\t\tAND S.ID IN (" . implode(", ", $arID) . ")\n\t\t\t\t\t\t"; $ar2Update["b_iblock_element_prop_s" . $IBLOCK_ID][$property_id] = false; //null } else { $rs = $DB->Query("\n\t\t\t\t\t\t\t\tSELECT S.ID\n\t\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\t\tb_iblock_property P\n\t\t\t\t\t\t\t\t\t,b_iblock_section S\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\tP.ID=" . $property_id . "\n\t\t\t\t\t\t\t\t\tAND S.IBLOCK_ID = P.LINK_IBLOCK_ID\n\t\t\t\t\t\t\t\t\tAND S.ID IN (" . implode(", ", $arID) . ")\n\t\t\t\t\t\t"); if ($ar = $rs->Fetch()) { $ar2Update["b_iblock_element_prop_s" . $IBLOCK_ID][$property_id] = array("VALUE" => $ar["ID"], "DESCRIPTION" => ""); } } $ar2Insert["sqls"]["b_iblock_section_element"][$property_id] = "\n\t\t\t\t\t\tINSERT INTO b_iblock_section_element\n\t\t\t\t\t\t(IBLOCK_ELEMENT_ID, IBLOCK_SECTION_ID, ADDITIONAL_PROPERTY_ID)\n\t\t\t\t\t\tSELECT " . $ELEMENT_ID . ", S.ID, P.ID\n\t\t\t\t\t\tFROM b_iblock_property P, b_iblock_section S\n\t\t\t\t\t\tWHERE P.ID=" . $property_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 (" . implode(", ", $arID) . ")\n\t\t\t\t\t"; } continue; } foreach ($values as $value) { if ($db_prop["VERSION"] == 1) { $ar2Insert["values"]["b_iblock_element_property"][$property_id][] = $value; } elseif ($db_prop["MULTIPLE"] == "Y") { $ar2Insert["values"]["b_iblock_element_prop_m" . $IBLOCK_ID][$property_id][] = $value; $ar2Update["b_iblock_element_prop_s" . $IBLOCK_ID][$property_id] = false; //null } else { $ar2Update["b_iblock_element_prop_s" . $IBLOCK_ID][$property_id] = $value; } } } foreach ($ar2Delete as $table => $arID) { if (count($arID) > 0) { if ($table == "b_iblock_section_element") { $DB->Query("\n\t\t\t\t\t\tdelete from " . $table . "\n\t\t\t\t\t\twhere IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . "\n\t\t\t\t\t\tand ADDITIONAL_PROPERTY_ID in (" . implode(", ", array_keys($arID)) . ")\n\t\t\t\t\t"); } else { $DB->Query("\n\t\t\t\t\t\tdelete from " . $table . "\n\t\t\t\t\t\twhere IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . "\n\t\t\t\t\t\tand IBLOCK_PROPERTY_ID in (" . implode(", ", array_keys($arID)) . ")\n\t\t\t\t\t"); } } } foreach ($ar2Insert["values"] as $table => $properties) { $strSqlPrefix = "\n\t\t\t\t\tinsert into " . $table . "\n\t\t\t\t\t(IBLOCK_PROPERTY_ID, IBLOCK_ELEMENT_ID, VALUE, VALUE_ENUM, VALUE_NUM, DESCRIPTION)\n\t\t\t\t\tvalues\n\t\t\t"; $maxValuesLen = $DB->type == "MYSQL" ? 1024 : 0; $strSqlValues = ""; foreach ($properties as $property_id => $values) { foreach ($values as $value) { if (strlen($value["VALUE"]) > 0) { $strSqlValues .= ",\n(" . $property_id . ", " . $ELEMENT_ID . ", " . "'" . $DB->ForSQL($value["VALUE"]) . "', " . intval($value["VALUE"]) . ", " . CIBlock::roundDB($value["VALUE"]) . ", " . (strlen($value["DESCRIPTION"]) ? "'" . $DB->ForSQL($value["DESCRIPTION"]) . "'" : "null") . " " . ")"; } if (strlen($strSqlValues) > $maxValuesLen) { $DB->Query($strSqlPrefix . substr($strSqlValues, 2)); $strSqlValues = ""; } } } if (strlen($strSqlValues) > 0) { $DB->Query($strSqlPrefix . substr($strSqlValues, 2)); $strSqlValues = ""; } } foreach ($ar2Insert["sqls"] as $table => $properties) { foreach ($properties as $property_id => $sql) { $DB->Query($sql); } } foreach ($ar2Update as $table => $properties) { $tableFields = $DB->GetTableFields($table); if (count($properties) > 0) { $arFields = array(); foreach ($properties as $property_id => $value) { if ($value === false || strlen($value["VALUE"]) <= 0) { $arFields[] = "PROPERTY_" . $property_id . " = null"; if (isset($tableFields["DESCRIPTION_" . $property_id])) { $arFields[] = "DESCRIPTION_" . $property_id . " = null"; } } else { $arFields[] = "PROPERTY_" . $property_id . " = '" . $DB->ForSQL($value["VALUE"]) . "'"; if (isset($tableFields["DESCRIPTION_" . $property_id])) { if (strlen($value["DESCRIPTION"])) { $arFields[] = "DESCRIPTION_" . $property_id . " = '" . $DB->ForSQL($value["DESCRIPTION"]) . "'"; } else { $arFields[] = "DESCRIPTION_" . $property_id . " = null"; } } } } $DB->Query("\n\t\t\t\t\tupdate " . $table . "\n\t\t\t\t\tset " . implode(",\n", $arFields) . "\n\t\t\t\t\twhere IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . "\n\t\t\t\t"); } } /****************************** QUOTA ******************************/ $_SESSION["SESS_RECOUNT_DB"] = "Y"; /****************************** QUOTA ******************************/ foreach (GetModuleEvents("iblock", "OnAfterIBlockElementSetPropertyValuesEx", true) as $arEvent) { ExecuteModuleEventEx($arEvent, array($ELEMENT_ID, $IBLOCK_ID, $PROPERTY_VALUES, $FLAGS)); } }
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)); }