PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/sale/mysql/person_type.php

https://gitlab.com/alexprowars/bitrix
PHP | 225 lines | 178 code | 40 blank | 7 comment | 40 complexity | a61a3cfe678bac75011356ffdff0b3bb MD5 | raw file
  1. <?php
  2. require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/sale/general/person_type.php");
  3. class CSalePersonType extends CAllSalePersonType
  4. {
  5. public static function GetList($arOrder = array(), $arFilter = array(), $arGroupBy = false, $arNavStartParams = false, $arSelectFields = array())
  6. {
  7. global $DB;
  8. if (!is_array($arOrder) && !is_array($arFilter))
  9. {
  10. $arOrder = strval($arOrder);
  11. $arFilter = strval($arFilter);
  12. if ($arOrder <> '' && $arFilter <> '')
  13. $arOrder = array($arOrder => $arFilter);
  14. else
  15. $arOrder = array();
  16. if (is_array($arGroupBy))
  17. $arFilter = $arGroupBy;
  18. else
  19. $arFilter = array();
  20. $arGroupBy = false;
  21. }
  22. if(empty($arSelectFields))
  23. $arSelectFields = Array("ID", "LID", "NAME", "SORT", "ACTIVE", "CODE");
  24. if(is_set($arFilter, "LID") && !empty($arFilter["LID"]))
  25. {
  26. $arFilter["LIDS"] = $arFilter["LID"];
  27. unset($arFilter["LID"]);
  28. }
  29. $arFilter['ENTITY_REGISTRY_TYPE'] = \Bitrix\Sale\Registry::REGISTRY_TYPE_ORDER;
  30. // FIELDS -->
  31. $arFields = array(
  32. "ID" => array("FIELD" => "PT.ID", "TYPE" => "int"),
  33. "LID" => array("FIELD" => "PT.LID", "TYPE" => "string"),
  34. "LIDS" => array("FIELD" => "PTS.SITE_ID", "TYPE" => "string", "FROM" => "LEFT JOIN b_sale_person_type_site PTS ON (PT.ID = PTS.PERSON_TYPE_ID)"),
  35. "NAME" => array("FIELD" => "PT.NAME", "TYPE" => "string"),
  36. "CODE" => array("FIELD" => "PT.CODE", "TYPE" => "string"),
  37. "SORT" => array("FIELD" => "PT.SORT", "TYPE" => "int"),
  38. "ACTIVE" => array("FIELD" => "PT.ACTIVE", "TYPE" => "char"),
  39. "ENTITY_REGISTRY_TYPE" => array("FIELD" => "ENTITY_REGISTRY_TYPE", "TYPE" => "string"),
  40. );
  41. // <-- FIELDS
  42. $arSqls = CSaleOrder::PrepareSql($arFields, $arOrder, $arFilter, $arGroupBy, $arSelectFields);
  43. $arSqls["SELECT"] = str_replace("%%_DISTINCT_%%", "DISTINCT", $arSqls["SELECT"]);
  44. if (is_array($arGroupBy) && count($arGroupBy)==0)
  45. {
  46. $strSql =
  47. "SELECT ".$arSqls["SELECT"]." ".
  48. "FROM b_sale_person_type PT ".
  49. " ".$arSqls["FROM"]." ";
  50. if ($arSqls["WHERE"] <> '')
  51. $strSql .= "WHERE ".$arSqls["WHERE"]." ";
  52. if ($arSqls["GROUPBY"] <> '')
  53. $strSql .= "GROUP BY ".$arSqls["GROUPBY"]." ";
  54. //echo "!1!=".htmlspecialcharsbx($strSql)."<br>";
  55. $dbRes = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  56. if ($arRes = $dbRes->Fetch())
  57. return $arRes["CNT"];
  58. else
  59. return False;
  60. }
  61. $strSql =
  62. "SELECT ".$arSqls["SELECT"]." ".
  63. "FROM b_sale_person_type PT ".
  64. " ".$arSqls["FROM"]." ";
  65. if ($arSqls["WHERE"] <> '')
  66. $strSql .= "WHERE ".$arSqls["WHERE"]." ";
  67. if ($arSqls["GROUPBY"] <> '')
  68. $strSql .= "GROUP BY ".$arSqls["GROUPBY"]." ";
  69. if ($arSqls["ORDERBY"] <> '')
  70. $strSql .= "ORDER BY ".$arSqls["ORDERBY"]." ";
  71. if (is_array($arNavStartParams) && intval($arNavStartParams["nTopCount"])<=0)
  72. {
  73. $strSql_tmp =
  74. "SELECT COUNT('x') as CNT ".
  75. "FROM b_sale_person_type PT ".
  76. " ".$arSqls["FROM"]." ";
  77. if ($arSqls["WHERE"] <> '')
  78. $strSql_tmp .= "WHERE ".$arSqls["WHERE"]." ";
  79. if ($arSqls["GROUPBY"] <> '')
  80. $strSql_tmp .= "GROUP BY ".$arSqls["GROUPBY"]." ";
  81. //echo "!2.1!=".htmlspecialcharsbx($strSql_tmp)."<br>";
  82. $dbRes = $DB->Query($strSql_tmp, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  83. $cnt = 0;
  84. if ($arSqls["GROUPBY"] == '')
  85. {
  86. if ($arRes = $dbRes->Fetch())
  87. $cnt = $arRes["CNT"];
  88. }
  89. else
  90. {
  91. // FOR MYSQL!!! ANOTHER CODE FOR ORACLE
  92. $cnt = $dbRes->SelectedRowsCount();
  93. }
  94. $dbRes = new CDBResult();
  95. //echo "!2.2!=".htmlspecialcharsbx($strSql)."<br>";
  96. $dbRes->NavQuery($strSql, $cnt, $arNavStartParams);
  97. }
  98. else
  99. {
  100. if (is_array($arNavStartParams) && intval($arNavStartParams["nTopCount"])>0)
  101. $strSql .= "LIMIT ".intval($arNavStartParams["nTopCount"]);
  102. //echo "!3!=".htmlspecialcharsbx($strSql)."<br>";
  103. $dbRes = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  104. }
  105. $arPT = array();
  106. $arResTmp = array();
  107. while ($arRes = $dbRes->Fetch())
  108. {
  109. if(intval($arRes["ID"]) > 0)
  110. {
  111. if(!in_array($arRes["ID"], $arPT))
  112. $arPT[] = $arRes["ID"];
  113. $arResTmp[] = $arRes;
  114. }
  115. }
  116. if(!empty($arPT) && is_array($arPT))
  117. {
  118. $strSql = "SELECT * from b_sale_person_type_site WHERE PERSON_TYPE_ID IN (".implode(",", $arPT).")";
  119. $dbRes1 = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  120. while ($arRes1 = $dbRes1->Fetch())
  121. {
  122. $arRes2[$arRes1["PERSON_TYPE_ID"]][] = $arRes1["SITE_ID"];
  123. }
  124. }
  125. foreach($arResTmp as $k => $v)
  126. $arResTmp[$k]["LIDS"] = $arRes2[$v["ID"]];
  127. $dbRes = new CDBResult();
  128. $dbRes->InitFromArray($arResTmp);
  129. return $dbRes;
  130. }
  131. public static function Add($arFields)
  132. {
  133. global $DB;
  134. if (!CSalePersonType::CheckFields("ADD", $arFields))
  135. return false;
  136. $db_events = GetModuleEvents("sale", "OnBeforePersonTypeAdd");
  137. while ($arEvent = $db_events->Fetch())
  138. if (ExecuteModuleEventEx($arEvent, Array(&$arFields))===false)
  139. return false;
  140. $arLID = Array();
  141. if(is_set($arFields, "LID"))
  142. {
  143. if(is_array($arFields["LID"]))
  144. $arLID = $arFields["LID"];
  145. else
  146. $arLID[] = $arFields["LID"];
  147. $str_LID = "''";
  148. $arFields["LID"] = false;
  149. foreach($arLID as $k => $v)
  150. {
  151. if($v <> '')
  152. {
  153. $str_LID .= ", '".$DB->ForSql($v)."'";
  154. if(empty($arFields["LID"]))
  155. $arFields["LID"] = $v;
  156. }
  157. else
  158. unset($arLID[$k]);
  159. }
  160. }
  161. $arFields['ENTITY_REGISTRY_TYPE'] = \Bitrix\Sale\Registry::REGISTRY_TYPE_ORDER;
  162. $arInsert = $DB->PrepareInsert("b_sale_person_type", $arFields);
  163. $strSql =
  164. "INSERT INTO b_sale_person_type(".$arInsert[0].") ".
  165. "VALUES(".$arInsert[1].")";
  166. $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  167. $ID = intval($DB->LastID());
  168. if(count($arLID)>0)
  169. {
  170. $strSql = "DELETE FROM b_sale_person_type_site WHERE PERSON_TYPE_ID=".$ID;
  171. $DB->Query($strSql, false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);
  172. $strSql =
  173. "INSERT INTO b_sale_person_type_site(PERSON_TYPE_ID, SITE_ID) ".
  174. "SELECT ".$ID.", LID ".
  175. "FROM b_lang ".
  176. "WHERE LID IN (".$str_LID.") ";
  177. $DB->Query($strSql, false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);
  178. }
  179. unset($GLOBALS["SALE_PERSON_TYPE_LIST_CACHE"]);
  180. $events = GetModuleEvents("sale", "OnPersonTypeAdd");
  181. while ($arEvent = $events->Fetch())
  182. ExecuteModuleEventEx($arEvent, Array($ID, $arFields));
  183. return $ID;
  184. }
  185. }