PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/sale/mysql/auxiliary.php

https://gitlab.com/alexprowars/bitrix
PHP | 265 lines | 204 code | 53 blank | 8 comment | 47 complexity | 1515495e21ce249aa3f34f8568dd5502 MD5 | raw file
  1. <?php
  2. require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/sale/general/auxiliary.php");
  3. class CSaleAuxiliary extends CAllSaleAuxiliary
  4. {
  5. //********** SELECT **************//
  6. public static function GetByID($ID)
  7. {
  8. global $DB;
  9. $ID = intval($ID);
  10. if ($ID <= 0)
  11. return false;
  12. $strSql =
  13. "SELECT A.ID, A.USER_ID, A.ITEM, A.ITEM_MD5, ".
  14. " ".$DB->DateToCharFunction("A.TIMESTAMP_X", "FULL")." as TIMESTAMP_X, ".
  15. " ".$DB->DateToCharFunction("A.DATE_INSERT", "FULL")." as DATE_INSERT ".
  16. "FROM b_sale_auxiliary A ".
  17. "WHERE A.ID = ".$ID." ";
  18. $dbAuxiliary = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  19. if ($arAuxiliary = $dbAuxiliary->Fetch())
  20. return $arAuxiliary;
  21. return false;
  22. }
  23. public static function GetByParams($userID, $itemMD5)
  24. {
  25. global $DB;
  26. $userID = intval($userID);
  27. if ($userID <= 0)
  28. return false;
  29. $itemMD5 = Trim($itemMD5);
  30. if ($itemMD5 == '')
  31. return false;
  32. $itemMD5 = md5($itemMD5);
  33. $strSql =
  34. "SELECT A.ID, A.USER_ID, A.ITEM, A.ITEM_MD5, ".
  35. " ".$DB->DateToCharFunction("A.TIMESTAMP_X", "FULL")." as TIMESTAMP_X, ".
  36. " ".$DB->DateToCharFunction("A.DATE_INSERT", "FULL")." as DATE_INSERT ".
  37. "FROM b_sale_auxiliary A ".
  38. "WHERE A.USER_ID = ".$userID." ".
  39. " AND A.ITEM_MD5 = '".$DB->ForSql($itemMD5)."' ";
  40. $dbAuxiliary = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  41. if ($arAuxiliary = $dbAuxiliary->Fetch())
  42. return $arAuxiliary;
  43. return false;
  44. }
  45. public static function GetList($arOrder = array(), $arFilter = array(), $arGroupBy = false, $arNavStartParams = false, $arSelectFields = array())
  46. {
  47. global $DB;
  48. if (count($arSelectFields) <= 0)
  49. $arSelectFields = array("ID", "USER_ID", "TIMESTAMP_X", "ITEM", "ITEM_MD5", "DATE_INSERT");
  50. // FIELDS -->
  51. $arFields = array(
  52. "ID" => array("FIELD" => "A.ID", "TYPE" => "int"),
  53. "USER_ID" => array("FIELD" => "A.USER_ID", "TYPE" => "int"),
  54. "TIMESTAMP_X" => array("FIELD" => "A.TIMESTAMP_X", "TYPE" => "datetime"),
  55. "ITEM" => array("FIELD" => "A.ITEM", "TYPE" => "string"),
  56. "ITEM_MD5" => array("FIELD" => "A.ITEM_MD5", "TYPE" => "string", "WHERE" => array("CSaleAuxiliary", "PrepareItemMD54Where")),
  57. "DATE_INSERT" => array("FIELD" => "A.DATE_INSERT", "TYPE" => "datetime")
  58. );
  59. // <-- FIELDS
  60. $arSqls = CSaleOrder::PrepareSql($arFields, $arOrder, $arFilter, $arGroupBy, $arSelectFields);
  61. $arSqls["SELECT"] = str_replace("%%_DISTINCT_%%", "", $arSqls["SELECT"]);
  62. if (is_array($arGroupBy) && count($arGroupBy)==0)
  63. {
  64. $strSql =
  65. "SELECT ".$arSqls["SELECT"]." ".
  66. "FROM b_sale_auxiliary A ".
  67. " ".$arSqls["FROM"]." ";
  68. if ($arSqls["WHERE"] <> '')
  69. $strSql .= "WHERE ".$arSqls["WHERE"]." ";
  70. if ($arSqls["GROUPBY"] <> '')
  71. $strSql .= "GROUP BY ".$arSqls["GROUPBY"]." ";
  72. //echo "!1!=".htmlspecialcharsbx($strSql)."<br>";
  73. $dbRes = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  74. if ($arRes = $dbRes->Fetch())
  75. return $arRes["CNT"];
  76. else
  77. return False;
  78. }
  79. $strSql =
  80. "SELECT ".$arSqls["SELECT"]." ".
  81. "FROM b_sale_auxiliary A ".
  82. " ".$arSqls["FROM"]." ";
  83. if ($arSqls["WHERE"] <> '')
  84. $strSql .= "WHERE ".$arSqls["WHERE"]." ";
  85. if ($arSqls["GROUPBY"] <> '')
  86. $strSql .= "GROUP BY ".$arSqls["GROUPBY"]." ";
  87. if ($arSqls["ORDERBY"] <> '')
  88. $strSql .= "ORDER BY ".$arSqls["ORDERBY"]." ";
  89. if (is_array($arNavStartParams) && intval($arNavStartParams["nTopCount"])<=0)
  90. {
  91. $strSql_tmp =
  92. "SELECT COUNT('x') as CNT ".
  93. "FROM b_sale_auxiliary A ".
  94. " ".$arSqls["FROM"]." ";
  95. if ($arSqls["WHERE"] <> '')
  96. $strSql_tmp .= "WHERE ".$arSqls["WHERE"]." ";
  97. if ($arSqls["GROUPBY"] <> '')
  98. $strSql_tmp .= "GROUP BY ".$arSqls["GROUPBY"]." ";
  99. //echo "!2.1!=".htmlspecialcharsbx($strSql_tmp)."<br>";
  100. $dbRes = $DB->Query($strSql_tmp, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  101. $cnt = 0;
  102. if ($arSqls["GROUPBY"] == '')
  103. {
  104. if ($arRes = $dbRes->Fetch())
  105. $cnt = $arRes["CNT"];
  106. }
  107. else
  108. {
  109. // FOR MYSQL!!! ANOTHER CODE FOR ORACLE
  110. $cnt = $dbRes->SelectedRowsCount();
  111. }
  112. $dbRes = new CDBResult();
  113. //echo "!2.2!=".htmlspecialcharsbx($strSql)."<br>";
  114. $dbRes->NavQuery($strSql, $cnt, $arNavStartParams);
  115. }
  116. else
  117. {
  118. if (is_array($arNavStartParams) && intval($arNavStartParams["nTopCount"])>0)
  119. $strSql .= "LIMIT ".intval($arNavStartParams["nTopCount"]);
  120. //echo "!3!=".htmlspecialcharsbx($strSql)."<br><br>";
  121. $dbRes = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  122. }
  123. return $dbRes;
  124. }
  125. public static function DeleteByTime($periodLength, $periodType)
  126. {
  127. global $DB;
  128. $periodLength = intval($periodLength);
  129. if ($periodLength <= 0)
  130. return False;
  131. $periodType = Trim($periodType);
  132. $periodType = ToUpper($periodType);
  133. if ($periodType == '')
  134. return False;
  135. $deleteVal = 0;
  136. if ($periodType == "I")
  137. $deleteVal = mktime(date("H"), date("i") - $periodLength, date("s"), date("m"), date("d"), date("Y"));
  138. elseif ($periodType == "H")
  139. $deleteVal = mktime(date("H") - $periodLength, date("i"), date("s"), date("m"), date("d"), date("Y"));
  140. elseif ($periodType == "D")
  141. $deleteVal = mktime(date("H"), date("i"), date("s"), date("m"), date("d") - $periodLength, date("Y"));
  142. elseif ($periodType == "W")
  143. $deleteVal = mktime(date("H"), date("i"), date("s"), date("m"), date("d") - 7 * $periodLength, date("Y"));
  144. elseif ($periodType == "M")
  145. $deleteVal = mktime(date("H"), date("i"), date("s"), date("m") - $periodLength, date("d"), date("Y"));
  146. elseif ($periodType == "Q")
  147. $deleteVal = mktime(date("H"), date("i"), date("s"), date("m") - 3 * $periodLength, date("d"), date("Y"));
  148. elseif ($periodType == "S")
  149. $deleteVal = mktime(date("H"), date("i"), date("s"), date("m") - 6 * $periodLength, date("d"), date("Y"));
  150. elseif ($periodType == "Y")
  151. $deleteVal = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y") - $periodLength);
  152. if ($deleteVal <= 0)
  153. return False;
  154. return $DB->Query("DELETE FROM b_sale_auxiliary WHERE DATE_INSERT < '".Date("Y-m-d H:i:s", $deleteVal)."' ", true);
  155. }
  156. public static function Add($arFields)
  157. {
  158. global $DB;
  159. $arFields1 = array();
  160. foreach ($arFields as $key => $value)
  161. {
  162. if (mb_substr($key, 0, 1) == "=")
  163. {
  164. $arFields1[mb_substr($key, 1)] = $value;
  165. unset($arFields[$key]);
  166. }
  167. }
  168. if (!CSaleAuxiliary::CheckFields("ADD", $arFields, 0))
  169. return false;
  170. $arInsert = $DB->PrepareInsert("b_sale_auxiliary", $arFields);
  171. foreach ($arFields1 as $key => $value)
  172. {
  173. if ($arInsert[0] <> '') $arInsert[0] .= ", ";
  174. $arInsert[0] .= $key;
  175. if ($arInsert[1] <> '') $arInsert[1] .= ", ";
  176. $arInsert[1] .= $value;
  177. }
  178. $strSql =
  179. "INSERT INTO b_sale_auxiliary(".$arInsert[0].") ".
  180. "VALUES(".$arInsert[1].")";
  181. $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  182. $ID = intval($DB->LastID());
  183. return $ID;
  184. }
  185. public static function Update($ID, $arFields)
  186. {
  187. global $DB;
  188. $ID = intval($ID);
  189. if ($ID <= 0)
  190. return False;
  191. $arFields1 = array();
  192. foreach ($arFields as $key => $value)
  193. {
  194. if (mb_substr($key, 0, 1) == "=")
  195. {
  196. $arFields1[mb_substr($key, 1)] = $value;
  197. unset($arFields[$key]);
  198. }
  199. }
  200. if (!CSaleAuxiliary::CheckFields("UPDATE", $arFields, $ID))
  201. return false;
  202. $strUpdate = $DB->PrepareUpdate("b_sale_auxiliary", $arFields);
  203. foreach ($arFields1 as $key => $value)
  204. {
  205. if ($strUpdate <> '') $strUpdate .= ", ";
  206. $strUpdate .= $key."=".$value." ";
  207. }
  208. $strSql = "UPDATE b_sale_auxiliary SET ".$strUpdate." WHERE ID = ".$ID." ";
  209. $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  210. return $ID;
  211. }
  212. }