示例#1
0
 function PrepareSql(&$arFields, $arOrder, $arFilter, $arGroupBy, $arSelectFields, $arUF = array())
 {
     global $DB;
     $obUserFieldsSql = false;
     if (is_array($arUF) && array_key_exists("ENTITY_ID", $arUF)) {
         $obUserFieldsSql = new CUserTypeSQL();
         $obUserFieldsSql->SetEntity($arUF["ENTITY_ID"], $arFields["ID"]["FIELD"]);
         $obUserFieldsSql->SetSelect($arSelectFields);
         $obUserFieldsSql->SetFilter($arFilter);
         $obUserFieldsSql->SetOrder($arOrder);
     }
     $strSqlSelect = "";
     $strSqlFrom = "";
     $strSqlWhere = "";
     $strSqlGroupBy = "";
     $strSqlOrderBy = "";
     $arGroupByFunct = array("COUNT", "AVG", "MIN", "MAX", "SUM");
     $arAlreadyJoined = array();
     // GROUP BY -->
     if (is_array($arGroupBy) && count($arGroupBy) > 0) {
         $arSelectFields = $arGroupBy;
         foreach ($arGroupBy as $key => $val) {
             $val = strtoupper($val);
             $key = strtoupper($key);
             if (array_key_exists($val, $arFields) && !in_array($key, $arGroupByFunct)) {
                 if (strlen($strSqlGroupBy) > 0) {
                     $strSqlGroupBy .= ", ";
                 }
                 $strSqlGroupBy .= $arFields[$val]["FIELD"];
                 if (isset($arFields[$val]["FROM"]) && strlen($arFields[$val]["FROM"]) > 0 && !in_array($arFields[$val]["FROM"], $arAlreadyJoined)) {
                     if (strlen($strSqlFrom) > 0) {
                         $strSqlFrom .= " ";
                     }
                     $strSqlFrom .= $arFields[$val]["FROM"];
                     $arAlreadyJoined[] = $arFields[$val]["FROM"];
                 }
             }
         }
     }
     // <-- GROUP BY
     // WHERE -->
     $arAlreadyJoinedOld = $arAlreadyJoined;
     $strSqlWhere .= CSqlUtil::PrepareWhere($arFields, $arFilter, $arAlreadyJoined);
     $arAlreadyJoinedDiff = array_diff($arAlreadyJoined, $arAlreadyJoinedOld);
     foreach ($arAlreadyJoinedDiff as $from_tmp) {
         if (strlen($strSqlFrom) > 0) {
             $strSqlFrom .= " ";
         }
         $strSqlFrom .= $from_tmp;
     }
     if ($obUserFieldsSql) {
         $r = $obUserFieldsSql->GetFilter();
         if (strlen($r) > 0) {
             $strSqlWhere .= (strlen($strSqlWhere) > 0 ? " AND" : "") . " (" . $r . ") ";
         }
     }
     // <-- WHERE
     // ORDER BY -->
     $arSqlOrder = array();
     foreach ($arOrder as $by => $order) {
         $by = strtoupper($by);
         $order = strtoupper($order);
         if ($order != "ASC") {
             $order = "DESC";
         } else {
             $order = "ASC";
         }
         if (array_key_exists($by, $arFields)) {
             if ($arFields[$by]["TYPE"] == "datetime" || $arFields[$by]["TYPE"] == "date") {
                 $arSqlOrder[] = " " . $by . "_X1 " . $order . " ";
                 if (!is_array($arSelectFields) || !in_array($by, $arSelectFields)) {
                     $arSelectFields[] = $by;
                 }
             } else {
                 $arSqlOrder[] = " " . $arFields[$by]["FIELD"] . " " . $order . " ";
             }
             if (isset($arFields[$by]["FROM"]) && strlen($arFields[$by]["FROM"]) > 0 && !in_array($arFields[$by]["FROM"], $arAlreadyJoined)) {
                 if (strlen($strSqlFrom) > 0) {
                     $strSqlFrom .= " ";
                 }
                 $strSqlFrom .= $arFields[$by]["FROM"];
                 $arAlreadyJoined[] = $arFields[$by]["FROM"];
             }
         } elseif ($obUserFieldsSql && ($s = $obUserFieldsSql->GetOrder($by))) {
             $arSqlOrder[$by] = " " . $s . " " . $order . " ";
         }
     }
     $strSqlOrderBy = "";
     DelDuplicateSort($arSqlOrder);
     $tmp_count = count($arSqlOrder);
     for ($i = 0; $i < $tmp_count; $i++) {
         if (strlen($strSqlOrderBy) > 0) {
             $strSqlOrderBy .= ", ";
         }
         if (strtoupper($DB->type) == "ORACLE") {
             if (substr($arSqlOrder[$i], -3) == "ASC") {
                 $strSqlOrderBy .= $arSqlOrder[$i] . " NULLS FIRST";
             } else {
                 $strSqlOrderBy .= $arSqlOrder[$i] . " NULLS LAST";
             }
         } else {
             $strSqlOrderBy .= $arSqlOrder[$i];
         }
     }
     // <-- ORDER BY
     // SELECT -->
     $arFieldsKeys = array_keys($arFields);
     if (is_array($arGroupBy) && count($arGroupBy) == 0) {
         $strSqlSelect = "COUNT(%%_DISTINCT_%% " . $arFields[$arFieldsKeys[0]]["FIELD"] . ") as CNT ";
     } else {
         if (isset($arSelectFields) && !is_array($arSelectFields) && is_string($arSelectFields) && strlen($arSelectFields) > 0 && array_key_exists($arSelectFields, $arFields)) {
             $arSelectFields = array($arSelectFields);
         }
         if (!isset($arSelectFields) || !is_array($arSelectFields) || count($arSelectFields) <= 0 || in_array("*", $arSelectFields)) {
             $tmp_count = count($arFieldsKeys);
             for ($i = 0; $i < $tmp_count; $i++) {
                 if (isset($arFields[$arFieldsKeys[$i]]["WHERE_ONLY"]) && $arFields[$arFieldsKeys[$i]]["WHERE_ONLY"] == "Y") {
                     continue;
                 }
                 if (strlen($strSqlSelect) > 0) {
                     $strSqlSelect .= ", ";
                 }
                 if ($arFields[$arFieldsKeys[$i]]["TYPE"] == "datetime") {
                     if (array_key_exists($arFieldsKeys[$i], $arOrder)) {
                         $strSqlSelect .= $arFields[$arFieldsKeys[$i]]["FIELD"] . " as " . $arFieldsKeys[$i] . "_X1, ";
                     }
                     $strSqlSelect .= $DB->DateToCharFunction($arFields[$arFieldsKeys[$i]]["FIELD"], "FULL") . " as " . $arFieldsKeys[$i];
                 } elseif ($arFields[$arFieldsKeys[$i]]["TYPE"] == "date") {
                     if (array_key_exists($arFieldsKeys[$i], $arOrder)) {
                         $strSqlSelect .= $arFields[$arFieldsKeys[$i]]["FIELD"] . " as " . $arFieldsKeys[$i] . "_X1, ";
                     }
                     $strSqlSelect .= $DB->DateToCharFunction($arFields[$arFieldsKeys[$i]]["FIELD"], "SHORT") . " as " . $arFieldsKeys[$i];
                 } else {
                     $strSqlSelect .= $arFields[$arFieldsKeys[$i]]["FIELD"] . " as " . $arFieldsKeys[$i];
                 }
                 if (isset($arFields[$arFieldsKeys[$i]]["FROM"]) && strlen($arFields[$arFieldsKeys[$i]]["FROM"]) > 0 && !in_array($arFields[$arFieldsKeys[$i]]["FROM"], $arAlreadyJoined)) {
                     if (strlen($strSqlFrom) > 0) {
                         $strSqlFrom .= " ";
                     }
                     $strSqlFrom .= $arFields[$arFieldsKeys[$i]]["FROM"];
                     $arAlreadyJoined[] = $arFields[$arFieldsKeys[$i]]["FROM"];
                 }
             }
         } else {
             foreach ($arSelectFields as $key => $val) {
                 $val = strtoupper($val);
                 $key = strtoupper($key);
                 if (array_key_exists($val, $arFields)) {
                     if (strlen($strSqlSelect) > 0) {
                         $strSqlSelect .= ", ";
                     }
                     if (in_array($key, $arGroupByFunct)) {
                         $strSqlSelect .= $key . "(" . $arFields[$val]["FIELD"] . ") as " . $val;
                     } else {
                         if ($arFields[$val]["TYPE"] == "datetime") {
                             if (array_key_exists($val, $arOrder)) {
                                 $strSqlSelect .= $arFields[$val]["FIELD"] . " as " . $val . "_X1, ";
                             }
                             $strSqlSelect .= $DB->DateToCharFunction($arFields[$val]["FIELD"], "FULL") . " as " . $val;
                         } elseif ($arFields[$val]["TYPE"] == "date") {
                             if (array_key_exists($val, $arOrder)) {
                                 $strSqlSelect .= $arFields[$val]["FIELD"] . " as " . $val . "_X1, ";
                             }
                             $strSqlSelect .= $DB->DateToCharFunction($arFields[$val]["FIELD"], "SHORT") . " as " . $val;
                         } else {
                             $strSqlSelect .= $arFields[$val]["FIELD"] . " as " . $val;
                         }
                     }
                     if (isset($arFields[$val]["FROM"]) && strlen($arFields[$val]["FROM"]) > 0 && !in_array($arFields[$val]["FROM"], $arAlreadyJoined)) {
                         if (strlen($strSqlFrom) > 0) {
                             $strSqlFrom .= " ";
                         }
                         $strSqlFrom .= $arFields[$val]["FROM"];
                         $arAlreadyJoined[] = $arFields[$val]["FROM"];
                     }
                 }
             }
         }
         if ($obUserFieldsSql) {
             $strSqlSelect .= (strlen($strSqlSelect) <= 0 ? $arFields["ID"]["FIELD"] : "") . $obUserFieldsSql->GetSelect();
         }
         if (strlen($strSqlGroupBy) > 0) {
             if (strlen($strSqlSelect) > 0) {
                 $strSqlSelect .= ", ";
             }
             $strSqlSelect .= "COUNT(%%_DISTINCT_%% " . $arFields[$arFieldsKeys[0]]["FIELD"] . ") as CNT";
         } else {
             $strSqlSelect = "%%_DISTINCT_%% " . $strSqlSelect;
         }
     }
     // <-- SELECT
     if ($obUserFieldsSql) {
         $strSqlFrom .= " " . $obUserFieldsSql->GetJoin($arFields["ID"]["FIELD"]);
     }
     return array("SELECT" => $strSqlSelect, "FROM" => $strSqlFrom, "WHERE" => $strSqlWhere, "GROUPBY" => $strSqlGroupBy, "ORDERBY" => $strSqlOrderBy);
 }
示例#2
0
 public static function PrepareExternalFilter(&$filter, $params = array())
 {
     if (!isset($filter['FM']) || empty($filter['FM']) || !is_array($params) || empty($params)) {
         return;
     }
     $entityID = isset($params['ENTITY_ID']) ? $params['ENTITY_ID'] : '';
     $masterAlias = isset($params['MASTER_ALIAS']) ? $params['MASTER_ALIAS'] : '';
     if ($masterAlias === '') {
         $masterAlias = 'L';
     }
     $masterIdentity = isset($params['MASTER_IDENTITY']) ? $params['MASTER_IDENTITY'] : '';
     if ($masterIdentity === '') {
         $masterIdentity = 'ID';
     }
     $fields = self::GetFields();
     $joins = array();
     $c = 0;
     foreach ($filter['FM'] as $filterPart) {
         if ($entityID !== '') {
             $filterPart['ENTITY_ID'] = $entityID;
         }
         $c++;
         $alias = "CFM{$c}";
         $where = CSqlUtil::PrepareWhere($fields, $filterPart, $joins);
         $joins[] = array('TYPE' => 'INNER', 'SQL' => "INNER JOIN (SELECT DISTINCT CFM.ELEMENT_ID FROM b_crm_field_multi CFM WHERE {$where}) {$alias} ON {$masterAlias}.{$masterIdentity} = {$alias}.ELEMENT_ID");
     }
     if (!empty($joins)) {
         if (!isset($filter['__JOINS'])) {
             $filter['__JOINS'] = $joins;
         } else {
             $filter['__JOINS'] = array_merge($filter['__JOINS'], $joins);
         }
     }
 }