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

/modules/iblock/classes/mysql/iblock.php

https://gitlab.com/alexprowars/bitrix
PHP | 319 lines | 293 code | 21 blank | 5 comment | 41 complexity | 5db7c8f83958921324ce7ef3385f8856 MD5 | raw file
  1. <?php
  2. class CIBlock extends CAllIBlock
  3. {
  4. ///////////////////////////////////////////////////////////////////
  5. // List of blocks
  6. ///////////////////////////////////////////////////////////////////
  7. public static function GetList($arOrder=Array("SORT"=>"ASC"), $arFilter=Array(), $bIncCnt = false)
  8. {
  9. global $DB, $USER;
  10. $strSqlSearch = "";
  11. $bAddSites = false;
  12. foreach($arFilter as $key => $val)
  13. {
  14. $res = CIBlock::MkOperationFilter($key);
  15. $key = mb_strtoupper($res["FIELD"]);
  16. $cOperationType = $res["OPERATION"];
  17. switch($key)
  18. {
  19. case "ACTIVE":
  20. $sql = CIBlock::FilterCreate("B.ACTIVE", $val, "string_equal", $cOperationType);
  21. break;
  22. case "LID":
  23. case "SITE_ID":
  24. $sql = CIBlock::FilterCreate("BS.SITE_ID", $val, "string_equal", $cOperationType);
  25. if($sql <> '')
  26. {
  27. $bAddSites = true;
  28. }
  29. break;
  30. case "NAME":
  31. case "CODE":
  32. case "XML_ID":
  33. case "PROPERTY_INDEX":
  34. $sql = CIBlock::FilterCreate("B.".$key, $val, "string", $cOperationType);
  35. break;
  36. case "EXTERNAL_ID":
  37. $sql = CIBlock::FilterCreate("B.XML_ID", $val, "string", $cOperationType);
  38. break;
  39. case "TYPE":
  40. $sql = CIBlock::FilterCreate("B.IBLOCK_TYPE_ID", $val, "string", $cOperationType);
  41. break;
  42. case "ID":
  43. case "VERSION":
  44. case "SOCNET_GROUP_ID":
  45. $sql = CIBlock::FilterCreate("B.".$key, $val, "number", $cOperationType);
  46. break;
  47. default:
  48. $sql = "";
  49. break;
  50. }
  51. if($sql <> '')
  52. {
  53. $strSqlSearch .= " AND (".$sql.") ";
  54. }
  55. }
  56. $bCheckPermissions =
  57. !array_key_exists("CHECK_PERMISSIONS", $arFilter)
  58. || $arFilter["CHECK_PERMISSIONS"] !== "N"
  59. || array_key_exists("OPERATION", $arFilter)
  60. ;
  61. $bIsAdmin = is_object($USER) && $USER->IsAdmin();
  62. $permissionsBy = null;
  63. if ($bCheckPermissions && isset($arFilter['PERMISSIONS_BY']))
  64. {
  65. $permissionsBy = (int)$arFilter['PERMISSIONS_BY'];
  66. if ($permissionsBy < 0)
  67. $permissionsBy = null;
  68. }
  69. if($bCheckPermissions && ($permissionsBy !== null || !$bIsAdmin))
  70. {
  71. $min_permission = (mb_strlen($arFilter["MIN_PERMISSION"]) == 1) ? $arFilter["MIN_PERMISSION"] : "R";
  72. if ($permissionsBy !== null)
  73. {
  74. $iUserID = $permissionsBy;
  75. $strGroups = implode(',', CUser::GetUserGroup($permissionsBy));
  76. $bAuthorized = false;
  77. }
  78. else
  79. {
  80. if (is_object($USER))
  81. {
  82. $iUserID = (int)$USER->GetID();
  83. $strGroups = $USER->GetGroups();
  84. $bAuthorized = $USER->IsAuthorized();
  85. }
  86. else
  87. {
  88. $iUserID = 0;
  89. $strGroups = "2";
  90. $bAuthorized = false;
  91. }
  92. }
  93. $stdPermissions = "
  94. SELECT IBLOCK_ID
  95. FROM b_iblock_group IBG
  96. WHERE IBG.GROUP_ID IN (".$strGroups.")
  97. AND IBG.PERMISSION >= '".$min_permission."'
  98. ";
  99. if(!defined("ADMIN_SECTION"))
  100. $stdPermissions .= "
  101. AND (IBG.PERMISSION='X' OR B.ACTIVE='Y')
  102. ";
  103. if ($arFilter["OPERATION"] <> '')
  104. $operation = "'".$DB->ForSql($arFilter["OPERATION"])."'";
  105. elseif($min_permission >= "X")
  106. $operation = "'iblock_edit'";
  107. elseif($min_permission >= "U")
  108. $operation = "'element_edit'";
  109. elseif($min_permission >= "S")
  110. $operation = "'iblock_admin_display'";
  111. else
  112. $operation = "'section_read', 'element_read', 'section_element_bind', 'section_section_bind'";
  113. if($operation)
  114. {
  115. $acc = new CAccess;
  116. $acc->UpdateCodes($permissionsBy !== null ? array('USER_ID' => $permissionsBy) : false);
  117. $extPermissions = "
  118. SELECT IBLOCK_ID
  119. FROM b_iblock_right IBR
  120. INNER JOIN b_task_operation T ON T.TASK_ID = IBR.TASK_ID
  121. INNER JOIN b_operation O ON O.ID = T.OPERATION_ID
  122. ".($iUserID > 0? "LEFT": "INNER")." JOIN b_user_access UA ON UA.ACCESS_CODE = IBR.GROUP_CODE AND UA.USER_ID = ".$iUserID."
  123. WHERE IBR.ENTITY_TYPE = 'iblock'
  124. AND O.NAME in (".$operation.")
  125. ".($bAuthorized || $iUserID > 0? "AND (UA.USER_ID IS NOT NULL OR IBR.GROUP_CODE = 'AU')": "")."
  126. ";
  127. $sqlPermissions = "AND (
  128. B.ID IN ($stdPermissions)
  129. OR (B.RIGHTS_MODE = 'E' AND B.ID IN ($extPermissions))
  130. )";
  131. }
  132. else
  133. {
  134. $sqlPermissions = "AND (
  135. B.ID IN ($stdPermissions)
  136. )";
  137. }
  138. }
  139. else
  140. {
  141. $sqlPermissions = "";
  142. }
  143. if ($bAddSites)
  144. $sqlJoinSites = "LEFT JOIN b_iblock_site BS ON B.ID=BS.IBLOCK_ID
  145. LEFT JOIN b_lang L ON L.LID=BS.SITE_ID";
  146. else
  147. $sqlJoinSites = "INNER JOIN b_lang L ON L.LID=B.LID";
  148. if(!$bIncCnt)
  149. {
  150. $strSql = "
  151. SELECT DISTINCT
  152. B.*
  153. ,B.XML_ID as EXTERNAL_ID
  154. ,".$DB->DateToCharFunction("B.TIMESTAMP_X")." as TIMESTAMP_X
  155. ,L.DIR as LANG_DIR
  156. ,L.SERVER_NAME
  157. FROM
  158. b_iblock B
  159. ".$sqlJoinSites."
  160. WHERE 1 = 1
  161. ".$sqlPermissions."
  162. ".$strSqlSearch."
  163. ";
  164. }
  165. else
  166. {
  167. $strSql = "
  168. SELECT
  169. B.*
  170. ,B.XML_ID as EXTERNAL_ID
  171. ,".$DB->DateToCharFunction("B.TIMESTAMP_X")." as TIMESTAMP_X
  172. ,L.DIR as LANG_DIR
  173. ,L.SERVER_NAME
  174. ,COUNT(DISTINCT BE.ID) as ELEMENT_CNT
  175. FROM
  176. b_iblock B
  177. ".$sqlJoinSites."
  178. LEFT JOIN b_iblock_element BE ON (BE.IBLOCK_ID=B.ID
  179. AND (
  180. (BE.WF_STATUS_ID=1 AND BE.WF_PARENT_ELEMENT_ID IS NULL )
  181. ".($arFilter["CNT_ALL"]=="Y"? " OR BE.WF_NEW='Y' ":"")."
  182. )
  183. ".($arFilter["CNT_ACTIVE"]=="Y"?
  184. "AND BE.ACTIVE='Y'
  185. AND (BE.ACTIVE_TO >= ".$DB->CurrentDateFunction()." OR BE.ACTIVE_TO IS NULL)
  186. AND (BE.ACTIVE_FROM <= ".$DB->CurrentDateFunction()." OR BE.ACTIVE_FROM IS NULL)
  187. ":
  188. "")."
  189. )
  190. WHERE 1 = 1
  191. ".$sqlPermissions."
  192. ".$strSqlSearch."
  193. GROUP BY B.ID
  194. ";
  195. }
  196. $arSqlOrder = Array();
  197. if(is_array($arOrder))
  198. {
  199. foreach($arOrder as $by=>$order)
  200. {
  201. $by = mb_strtolower($by);
  202. $order = mb_strtolower($order);
  203. if ($order!="asc")
  204. $order = "desc";
  205. if ($by == "id") $arSqlOrder[$by] = " B.ID ".$order." ";
  206. elseif ($by == "lid") $arSqlOrder[$by] = " B.LID ".$order." ";
  207. elseif ($by == "iblock_type") $arSqlOrder[$by] = " B.IBLOCK_TYPE_ID ".$order." ";
  208. elseif ($by == "name") $arSqlOrder[$by] = " B.NAME ".$order." ";
  209. elseif ($by == "active") $arSqlOrder[$by] = " B.ACTIVE ".$order." ";
  210. elseif ($by == "sort") $arSqlOrder[$by] = " B.SORT ".$order." ";
  211. elseif ($by == "code") $arSqlOrder[$by] = " B.CODE ".$order." ";
  212. elseif ($bIncCnt && $by == "element_cnt") $arSqlOrder[$by] = " ELEMENT_CNT ".$order." ";
  213. else
  214. {
  215. $by = "timestamp_x";
  216. $arSqlOrder[$by] = " B.TIMESTAMP_X ".$order." ";
  217. }
  218. }
  219. }
  220. if(count($arSqlOrder) > 0)
  221. $strSqlOrder = " ORDER BY ".implode(",", $arSqlOrder);
  222. else
  223. $strSqlOrder = "";
  224. $res = $DB->Query($strSql.$strSqlOrder, false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);
  225. return $res;
  226. }
  227. public static function _Upper($str)
  228. {
  229. return $str;
  230. }
  231. public function _Add($ID)
  232. {
  233. global $DB;
  234. $err_mess = "FILE: ".__FILE__."<br>LINE: ";
  235. $ID = intval($ID);
  236. if(defined("MYSQL_TABLE_TYPE") && MYSQL_TABLE_TYPE <> '')
  237. {
  238. $DB->Query("SET storage_engine = '".MYSQL_TABLE_TYPE."'", true);
  239. }
  240. $strSql = "
  241. CREATE TABLE IF NOT EXISTS b_iblock_element_prop_s".$ID." (
  242. IBLOCK_ELEMENT_ID int(11) not null REFERENCES b_iblock_element(ID),
  243. primary key (IBLOCK_ELEMENT_ID)
  244. )
  245. ";
  246. $rs = $DB->DDL($strSql, false, $err_mess.__LINE__);
  247. $strSql = "
  248. CREATE TABLE IF NOT EXISTS b_iblock_element_prop_m".$ID." (
  249. ID int(11) not null auto_increment,
  250. IBLOCK_ELEMENT_ID int(11) not null REFERENCES b_iblock_element(ID),
  251. IBLOCK_PROPERTY_ID int(11) not null REFERENCES b_iblock_property(ID),
  252. VALUE text not null,
  253. VALUE_ENUM int(11),
  254. VALUE_NUM numeric(18,4),
  255. DESCRIPTION VARCHAR(255) NULL,
  256. PRIMARY KEY (ID),
  257. INDEX ix_iblock_elem_prop_m".$ID."_1(IBLOCK_ELEMENT_ID,IBLOCK_PROPERTY_ID),
  258. INDEX ix_iblock_elem_prop_m".$ID."_2(IBLOCK_PROPERTY_ID),
  259. INDEX ix_iblock_elem_prop_m".$ID."_3(VALUE_ENUM,IBLOCK_PROPERTY_ID)
  260. )
  261. ";
  262. if($rs)
  263. $rs = $DB->DDL($strSql, false, $err_mess.__LINE__);
  264. return $rs;
  265. }
  266. public static function _Order($by, $order, $default_order, $nullable = true)
  267. {
  268. $o = parent::_Order($by, $order, $default_order, $nullable);
  269. //$o[0] - bNullsFirst
  270. //$o[1] - asc|desc
  271. if($o[0])
  272. {
  273. if($o[1] == "asc")
  274. {
  275. return $by." asc";
  276. }
  277. else
  278. {
  279. return "length(".$by.")>0 asc, ".$by." desc";
  280. }
  281. }
  282. else
  283. {
  284. if($o[1] == "asc")
  285. {
  286. return "length(".$by.")>0 desc, ".$by." asc";
  287. }
  288. else
  289. {
  290. return $by." desc";
  291. }
  292. }
  293. }
  294. public static function _NotEmpty($column)
  295. {
  296. return "if(".$column." is null, 0, 1)";
  297. }
  298. }