Esempio n. 1
0
 public static function GetProperty($IBLOCK_ID, $ELEMENT_ID, $by = "sort", $order = "asc", $arFilter = array())
 {
     global $DB;
     if (is_array($by)) {
         if ($order != "asc") {
             $arFilter = $order;
         }
         $arOrder = $by;
     } else {
         $arOrder = false;
     }
     $IBLOCK_ID = intval($IBLOCK_ID);
     $ELEMENT_ID = intval($ELEMENT_ID);
     $VERSION = CIBlockElement::GetIBVersion($IBLOCK_ID);
     $strSqlSearch = "";
     foreach ($arFilter as $key => $val) {
         switch (strtoupper($key)) {
             case "ACTIVE":
                 if ($val == "Y" || $val == "N") {
                     $strSqlSearch .= "AND BP.ACTIVE='" . $val . "'\n";
                 }
                 break;
             case "SEARCHABLE":
                 if ($val == "Y" || $val == "N") {
                     $strSqlSearch .= "AND BP.SEARCHABLE='" . $val . "'\n";
                 }
                 break;
             case "NAME":
                 if (strlen($val) > 0) {
                     $strSqlSearch .= "AND " . CIBLock::_Upper("BP.NAME") . " LIKE " . CIBlock::_Upper("'" . $DB->ForSql($val) . "'") . "\n";
                 }
                 break;
             case "ID":
                 if (is_array($val)) {
                     if (!empty($val)) {
                         $strSqlSearch .= "AND BP.ID in (" . implode(", ", array_map("intval", $val)) . ")\n";
                     }
                 } elseif (strlen($val) > 0) {
                     $strSqlSearch .= "AND BP.ID=" . IntVal($val) . "\n";
                 }
                 break;
             case "PROPERTY_TYPE":
                 if (strlen($val) > 0) {
                     $strSqlSearch .= "AND BP.PROPERTY_TYPE='" . $DB->ForSql($val) . "'\n";
                 }
                 break;
             case "CODE":
                 if (strlen($val) > 0) {
                     $strSqlSearch .= "AND " . CIBLock::_Upper("BP.CODE") . " LIKE " . CIBLock::_Upper("'" . $DB->ForSql($val) . "'") . "\n";
                 }
                 break;
             case "EMPTY":
                 if (strlen($val) > 0) {
                     if ($val == "Y") {
                         $strSqlSearch .= "AND BEP.ID IS NULL\n";
                     } elseif ($VERSION != 2) {
                         $strSqlSearch .= "AND BEP.ID IS NOT NULL\n";
                     }
                 }
                 break;
         }
     }
     $arSqlOrder = array();
     if ($arOrder) {
         foreach ($arOrder as $by => $order) {
             $order = strtolower($order);
             if ($order != "desc") {
                 $order = "asc";
             }
             $by = strtolower($by);
             if ($by == "sort") {
                 $arSqlOrder["BP.SORT"] = $order;
             } elseif ($by == "id") {
                 $arSqlOrder["BP.ID"] = $order;
             } elseif ($by == "name") {
                 $arSqlOrder["BP.NAME"] = $order;
             } elseif ($by == "active") {
                 $arSqlOrder["BP.ACTIVE"] = $order;
             } elseif ($by == "value_id") {
                 $arSqlOrder["BEP.ID"] = $order;
             } elseif ($by == "enum_sort") {
                 $arSqlOrder["BEPE.SORT"] = $order;
             } else {
                 $arSqlOrder["BP.SORT"] = $order;
             }
         }
     } else {
         if ($by == "id") {
             $arSqlOrder["BP.ID"] = "asc";
         } elseif ($by == "name") {
             $arSqlOrder["BP.NAME"] = "asc";
         } elseif ($by == "active") {
             $arSqlOrder["BP.ACTIVE"] = "asc";
         } elseif ($by == "value_id") {
             $arSqlOrder["BEP.ID"] = $order;
         } elseif ($by == "enum_sort") {
             $arSqlOrder["BEPE.SORT"] = $order;
         } else {
             $arSqlOrder["BP.SORT"] = "asc";
             $by = "sort";
         }
         if ($order != "desc") {
             $arSqlOrder["BP.SORT"] = "asc";
             $arSqlOrder["BP.ID"] = "asc";
             $arSqlOrder["BEPE.SORT"] = "asc";
             $arSqlOrder["BEP.ID"] = "asc";
             $order = "asc";
         } else {
             $arSqlOrder["BP.SORT"] = "desc";
             $arSqlOrder["BP.ID"] = "desc";
             $arSqlOrder["BEPE.SORT"] = "desc";
             $arSqlOrder["BEP.ID"] = "desc";
         }
     }
     $strSqlOrder = "";
     foreach ($arSqlOrder as $key => $val) {
         $strSqlOrder .= ", " . $key . " " . $val;
     }
     if ($strSqlOrder != "") {
         $strSqlOrder = ' ORDER BY ' . substr($strSqlOrder, 1);
     }
     if ($VERSION == 2) {
         $strTable = "b_iblock_element_prop_m" . $IBLOCK_ID;
     } else {
         $strTable = "b_iblock_element_property";
     }
     $strSql = "\n\t\t\tSELECT BP.*, BEP.ID as PROPERTY_VALUE_ID, BEP.VALUE, BEP.DESCRIPTION, BEPE.VALUE VALUE_ENUM, BEPE.XML_ID VALUE_XML_ID, BEPE.SORT VALUE_SORT\n\t\t\tFROM b_iblock B\n\t\t\t\tINNER JOIN b_iblock_property BP ON B.ID=BP.IBLOCK_ID\n\t\t\t\tLEFT JOIN " . $strTable . " BEP ON (BP.ID = BEP.IBLOCK_PROPERTY_ID AND BEP.IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . ")\n\t\t\t\tLEFT JOIN b_iblock_property_enum BEPE ON (BP.PROPERTY_TYPE = 'L' AND BEPE.ID=BEP.VALUE_ENUM AND BEPE.PROPERTY_ID=BP.ID)\n\t\t\tWHERE B.ID = " . $IBLOCK_ID . "\n\t\t\t\t" . $strSqlSearch . "\n\t\t\t" . $strSqlOrder;
     if ($VERSION == 2) {
         $result = array();
         $arElements = array();
         $rs = $DB->Query($strSql);
         while ($ar = $rs->Fetch()) {
             if ($ar["VERSION"] == 2 && $ar["MULTIPLE"] == "N") {
                 if (!array_key_exists($ELEMENT_ID, $arElements)) {
                     $strSql = "\n\t\t\t\t\t\t\tSELECT *\n\t\t\t\t\t\t\tFROM b_iblock_element_prop_s" . $ar["IBLOCK_ID"] . "\n\t\t\t\t\t\t\tWHERE IBLOCK_ELEMENT_ID = " . $ELEMENT_ID . "\n\t\t\t\t\t\t";
                     $rs2 = $DB->Query($strSql);
                     $arElements[$ELEMENT_ID] = $rs2->Fetch();
                 }
                 if (!isset($arFilter["EMPTY"]) || $arFilter["EMPTY"] == "Y" || strlen($arElements[$ELEMENT_ID]["PROPERTY_" . $ar["ID"]]) > 0) {
                     $val = $arElements[$ELEMENT_ID]["PROPERTY_" . $ar["ID"]];
                     $ar["PROPERTY_VALUE_ID"] = $ELEMENT_ID . ":" . $ar["ID"];
                     if ($ar["PROPERTY_TYPE"] == "L" && intval($val) > 0) {
                         $arEnum = CIBlockPropertyEnum::GetByID($val);
                         if ($arEnum !== false) {
                             $ar["VALUE_ENUM"] = $arEnum["VALUE"];
                             $ar["VALUE_XML_ID"] = $arEnum["XML_ID"];
                             $ar["VALUE_SORT"] = $arEnum["SORT"];
                         }
                     } else {
                         $ar["VALUE_ENUM"] = "";
                     }
                     if ($ar["PROPERTY_TYPE"] == "N" && strlen($val) > 0) {
                         $val = CIBlock::NumberFormat($val);
                     }
                     $ar["DESCRIPTION"] = $arElements[$ELEMENT_ID]["DESCRIPTION_" . $ar["ID"]];
                     $ar["VALUE"] = $val;
                 } else {
                     continue;
                 }
             }
             if ($arFilter["EMPTY"] == "N" && $ar["PROPERTY_VALUE_ID"] == "") {
                 continue;
             }
             $result[] = $ar;
         }
         $rs = new CIBlockPropertyResult();
         $rs->InitFromArray($result);
     } else {
         $rs = new CIBlockPropertyResult($DB->Query($strSql));
     }
     return $rs;
 }