BxDolStudioBuilderPageQuery.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaStudio UNA Studio
  7. * @{
  8. */
  9. class BxDolStudioBuilderPageQuery extends BxDolStudioPageQuery
  10. {
  11. function __construct()
  12. {
  13. parent::__construct();
  14. }
  15. public function insertPage ($sObj, $sModule, $sUri, $sUrl, $sTitleLangKey, $iType = 1, $iLayoutId = 5, $iVisibleForLevels = 2147483647, $sClass = '', $sClassFile = '')
  16. {
  17. $b = $this->query('INSERT INTO `sys_objects_page` SET
  18. `author` = :author,
  19. `added` = :added,
  20. `object` = :obj,
  21. `uri` = :uri,
  22. `title` = :title,
  23. `module` = :module,
  24. `cover` = :cover,
  25. `type_id` = :type,
  26. `layout_id` = :layout,
  27. `visible_for_levels` = :levels,
  28. `visible_for_levels_editable` = 1,
  29. `url` = :url,
  30. `cache_lifetime` = 0,
  31. `cache_editable` = 1,
  32. `deletable` = 1,
  33. `override_class_name` = :class,
  34. `override_class_file` = :file
  35. ', array(
  36. 'author' => bx_get_logged_profile_id(),
  37. 'added' => time(),
  38. 'obj' => $sObj,
  39. 'uri' => $sUri,
  40. 'title' => $sTitleLangKey,
  41. 'module' => $sModule,
  42. 'cover' => 0,
  43. 'type' => $iType,
  44. 'layout' => $iLayoutId,
  45. 'levels' => $iVisibleForLevels,
  46. 'url' => $sUrl,
  47. 'class' => $sClass,
  48. 'file' => $sClassFile,
  49. ));
  50. if (!$b)
  51. return false;
  52. return $this->lastId();
  53. }
  54. function getPages($aParams, &$aItems, $bReturnCount = true)
  55. {
  56. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  57. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  58. if(!isset($aParams['order']) || empty($aParams['order']))
  59. $sOrderClause = "ORDER BY `tp`.`object` ASC";
  60. switch($aParams['type']) {
  61. case 'by_id':
  62. $aMethod['name'] = 'getRow';
  63. $aMethod['params'][1] = array(
  64. 'id' => $aParams['value']
  65. );
  66. $sWhereClause = " AND `tp`.`id`=:id ";
  67. break;
  68. case 'by_object':
  69. $aMethod['name'] = 'getRow';
  70. $aMethod['params'][1] = array(
  71. 'object' => $aParams['value']
  72. );
  73. $sWhereClause = " AND `tp`.`object`=:object ";
  74. break;
  75. case 'by_uri':
  76. $aMethod['name'] = 'getRow';
  77. $aMethod['params'][1] = array(
  78. 'uri' => $aParams['value']
  79. );
  80. $sWhereClause = " AND `tp`.`uri`=:uri ";
  81. break;
  82. case 'by_object_full':
  83. $aMethod['name'] = 'getRow';
  84. $aMethod['params'][1] = array(
  85. 'object' => $aParams['value']
  86. );
  87. $sSelectClause = ", `tpl`.`name` AS `layout_name`, `tpl`.`icon` AS `layout_icon`, `tpl`.`title` AS `layout_title`, `tpl`.`template` AS `layout_template`, `tpl`.`cells_number` AS `layout_cells_number`";
  88. $sJoinClause = "LEFT JOIN `sys_pages_layouts` AS `tpl` ON `tp`.`layout_id`=`tpl`.`id`";
  89. $sWhereClause = " AND `tp`.`object`=:object ";
  90. break;
  91. case 'by_module':
  92. $aMethod['params'][1] = array(
  93. 'module' => $aParams['value']
  94. );
  95. $sWhereClause = " AND `tp`.`module`=:module ";
  96. break;
  97. case 'all':
  98. break;
  99. }
  100. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  101. `tp`.`id` AS `id`,
  102. `tp`.`object` AS `object`,
  103. `tp`.`uri` AS `uri`,
  104. `tp`.`title_system` AS `title_system`,
  105. `tp`.`title` AS `title`,
  106. `tp`.`module` AS `module`,
  107. `tp`.`cover` AS `cover`,
  108. `tp`.`cover_image` AS `cover_image`,
  109. `tp`.`type_id` AS `type_id`,
  110. `tp`.`layout_id` AS `layout_id`,
  111. `tp`.`submenu` AS `submenu`,
  112. `tp`.`visible_for_levels` AS `visible_for_levels`,
  113. `tp`.`visible_for_levels_editable` AS `visible_for_levels_editable`,
  114. `tp`.`url` AS `url`,
  115. `tp`.`meta_description` AS `meta_description`,
  116. `tp`.`meta_keywords` AS `meta_keywords`,
  117. `tp`.`meta_robots` AS `meta_robots`,
  118. `tp`.`cache_lifetime` AS `cache_lifetime`,
  119. `tp`.`cache_editable` AS `cache_editable`,
  120. `tp`.`deletable` AS `deletable`,
  121. `tp`.`override_class_name` AS `override_class_name`,
  122. `tp`.`override_class_file` AS `override_class_file`" . $sSelectClause . "
  123. FROM `sys_objects_page` AS `tp` " . $sJoinClause . "
  124. WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  125. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  126. if(!$bReturnCount)
  127. return !empty($aItems);
  128. return (int)$this->getOne("SELECT FOUND_ROWS()");
  129. }
  130. function updatePage($iId, $aFields)
  131. {
  132. $sSql = "UPDATE `sys_objects_page` SET `" . implode("`=?, `", array_keys($aFields)) . "`=? WHERE `id`=?";
  133. $sSql = call_user_func_array(array($this, 'prepare'), array_merge(array($sSql), array_values($aFields), array($iId)));
  134. return $this->res($sSql);
  135. }
  136. function deletePages($aParams)
  137. {
  138. $aBindings = array();
  139. $sWhereClause = "";
  140. switch($aParams['type']) {
  141. case 'by_id':
  142. $aBindings = array(
  143. 'id' => $aParams['value']
  144. );
  145. $sWhereClause = "AND `tp`.`id`=:id ";
  146. break;
  147. case 'by_object':
  148. $aBindings = array(
  149. 'object' => $aParams['value']
  150. );
  151. $sWhereClause = "AND `tp`.`object`=:object ";
  152. break;
  153. case 'all':
  154. break;
  155. }
  156. $sSql = "DELETE FROM `tp` USING `sys_objects_page` AS `tp` WHERE 1 " . $sWhereClause;
  157. return (int)$this->query($sSql, $aBindings) > 0;
  158. }
  159. function isUniqUri($sUri)
  160. {
  161. $sSql = $this->prepare("SELECT `id` FROM `sys_objects_page` WHERE `uri`=? LIMIT 1", $sUri);
  162. return (int)$this->getOne($sSql) <= 0;
  163. }
  164. function getTypes($aParams, &$aItems, $bReturnCount = true)
  165. {
  166. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  167. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  168. if(!isset($aParams['order']) || empty($aParams['order']))
  169. $sOrderClause = "ORDER BY `tpt`.`id` ASC";
  170. switch($aParams['type']) {
  171. case 'by_id':
  172. $aMethod['name'] = 'getRow';
  173. $aMethod['params'][1] = array(
  174. 'id' => $aParams['value']
  175. );
  176. $sWhereClause = "AND `tpt`.`id`=:id ";
  177. break;
  178. case 'all':
  179. break;
  180. }
  181. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  182. `tpt`.`id` AS `id`,
  183. `tpt`.`title` AS `title`,
  184. `tpt`.`template` AS `template`,
  185. `tpt`.`order` AS `order`" . $sSelectClause . "
  186. FROM `sys_pages_types` AS `tpt` " . $sJoinClause . "
  187. WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  188. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  189. if(!$bReturnCount)
  190. return !empty($aItems);
  191. return (int)$this->getOne("SELECT FOUND_ROWS()");
  192. }
  193. function getSubmenus()
  194. {
  195. return BxDolDb::getInstance()->getPairs('SELECT `object`, `title` FROM `sys_objects_menu` WHERE `template_id` = 8', 'object', 'title');
  196. }
  197. function getLayouts($aParams, &$aItems, $bReturnCount = true)
  198. {
  199. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  200. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  201. if(!isset($aParams['order']) || empty($aParams['order']))
  202. $sOrderClause = "ORDER BY `tpl`.`id` ASC";
  203. switch($aParams['type']) {
  204. case 'by_id':
  205. $aMethod['name'] = 'getRow';
  206. $aMethod['params'][1] = array(
  207. 'id' => $aParams['value']
  208. );
  209. $sWhereClause = "AND `tpl`.`id`=:id ";
  210. break;
  211. case 'all':
  212. break;
  213. }
  214. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  215. `tpl`.`id` AS `id`,
  216. `tpl`.`name` AS `name`,
  217. `tpl`.`icon` AS `icon`,
  218. `tpl`.`title` AS `title`,
  219. `tpl`.`template` AS `template`,
  220. `tpl`.`cells_number` AS `cells_number`" . $sSelectClause . "
  221. FROM `sys_pages_layouts` AS `tpl` " . $sJoinClause . "
  222. WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  223. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  224. if(!$bReturnCount)
  225. return !empty($aItems);
  226. return (int)$this->getOne("SELECT FOUND_ROWS()");
  227. }
  228. function getDesignBoxes($aParams, &$aItems, $bReturnCount = true)
  229. {
  230. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  231. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  232. if(!isset($aParams['order']) || empty($aParams['order']))
  233. $sOrderClause = "ORDER BY `tpd`.`order` ASC";
  234. switch($aParams['type']) {
  235. case 'by_id':
  236. $aMethod['name'] = 'getRow';
  237. $aMethod['params'][1] = array(
  238. 'id' => $aParams['value']
  239. );
  240. $sWhereClause = "AND `tpd`.`id`=:id ";
  241. break;
  242. case 'ordered':
  243. $sWhereClause = "AND `tpd`.`order`<>0 ";
  244. break;
  245. case 'all':
  246. break;
  247. }
  248. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  249. `tpd`.`id` AS `id`,
  250. `tpd`.`title` AS `title`,
  251. `tpd`.`template` AS `template`" . $sSelectClause . "
  252. FROM `sys_pages_design_boxes` AS `tpd` " . $sJoinClause . "
  253. WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  254. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  255. if(!$bReturnCount)
  256. return !empty($aItems);
  257. return (int)$this->getOne("SELECT FOUND_ROWS()");
  258. }
  259. function getModulesWithCopyableBlocks()
  260. {
  261. $sSql = $this->prepare("SELECT
  262. `tm`.`name` AS `module`
  263. FROM `sys_modules` AS `tm`
  264. LEFT JOIN `sys_pages_blocks` AS `tpb` ON `tm`.`name`=`tpb`.`module`
  265. WHERE `tm`.`type`=? AND `tpb`.`copyable`=?
  266. GROUP BY `tm`.`name`", BX_DOL_MODULE_TYPE_MODULE, 1);
  267. return $this->getColumn($sSql);
  268. }
  269. function getBlocks($aParams, &$aItems, $bReturnCount = true)
  270. {
  271. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  272. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  273. if(!isset($aParams['order']) || empty($aParams['order']))
  274. $sOrderClause = "ORDER BY `tpb`.`order` ASC";
  275. switch($aParams['type']) {
  276. case 'by_id':
  277. $aMethod['name'] = 'getRow';
  278. $aMethod['params'][1] = array(
  279. 'id' => $aParams['value']
  280. );
  281. $sWhereClause = "AND `tpb`.`id`=:id ";
  282. break;
  283. case 'skeleton_by_type':
  284. $aMethod['name'] = 'getRow';
  285. $aMethod['params'][1] = array(
  286. 'module' => BX_DOL_STUDIO_BP_SKELETONS,
  287. 'type' => $aParams['value']
  288. );
  289. $sWhereClause = "AND `tpb`.`object`='' AND `tpb`.`module`=:module AND `tpb`.`type`=:type ";
  290. break;
  291. case 'by_ids':
  292. $sWhereClause = "AND `tpb`.`id` IN (" . $this->implode_escape($aParams['value']) . ")";
  293. break;
  294. case 'by_object':
  295. $aMethod['params'][1] = array(
  296. 'object' => $aParams['value']
  297. );
  298. $sWhereClause = "AND `tpb`.`object`=:object";
  299. break;
  300. case 'by_object_cell':
  301. $aMethod['params'][1] = array(
  302. 'object' => $aParams['object'],
  303. 'cell_id' => $aParams['cell']
  304. );
  305. $sWhereClause = "AND `tpb`.`object`=:object AND `tpb`.`cell_id`=:cell_id";
  306. break;
  307. case 'by_module_to_copy':
  308. $aMethod['params'][1] = array(
  309. 'module' => $aParams['value']
  310. );
  311. $sWhereClause = "AND `tpb`.`module`=:module AND `tpb`.`copyable`=1";
  312. if($aParams['value'] == BX_DOL_STUDIO_BP_SKELETONS)
  313. $sWhereClause .= " AND `tpb`.`object`=''";
  314. break;
  315. case 'counter_by_pages':
  316. $aMethod['name'] = 'getPairs';
  317. $aMethod['params'][1] = 'object';
  318. $aMethod['params'][2] = 'counter';
  319. $sSelectClause = ", COUNT(*) AS `counter`";
  320. $sGroupClause = "GROUP BY `tpb`.`object`";
  321. break;
  322. case 'all':
  323. break;
  324. }
  325. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  326. `tpb`.`id` AS `id`,
  327. `tpb`.`object` AS `object`,
  328. `tpb`.`cell_id` AS `cell_id`,
  329. `tpb`.`module` AS `module`,
  330. `tpb`.`title_system` AS `title_system`,
  331. `tpb`.`title` AS `title`,
  332. `tpb`.`designbox_id` AS `designbox_id`,
  333. `tpb`.`hidden_on` AS `hidden_on`,
  334. `tpb`.`visible_for_levels` AS `visible_for_levels`,
  335. `tpb`.`type` AS `type`,
  336. `tpb`.`content` AS `content`,
  337. `tpb`.`deletable` AS `deletable`,
  338. `tpb`.`copyable` AS `copyable`,
  339. `tpb`.`active` AS `active`,
  340. `tpb`.`order` AS `order`" . $sSelectClause . "
  341. FROM `sys_pages_blocks` AS `tpb` " . $sJoinClause . "
  342. WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  343. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  344. if(!$bReturnCount)
  345. return !empty($aItems);
  346. return (int)$this->getOne("SELECT FOUND_ROWS()");
  347. }
  348. function insertBlock($aFields)
  349. {
  350. $aFields['order'] = $this->getBlockOrderMax($aFields['object']) + 1;
  351. $sSql = "INSERT INTO `sys_pages_blocks` SET " . $this->arrayToSQL($aFields);
  352. if (!$this->query($sSql))
  353. return false;
  354. return $this->lastId();
  355. }
  356. function updateBlock($iId, $aFields)
  357. {
  358. $sSql = "UPDATE `sys_pages_blocks` SET " . $this->arrayToSQL($aFields) . " WHERE `id`=:id";
  359. return $this->res($sSql, array('id' => $iId));
  360. }
  361. function deleteBlocks($aParams)
  362. {
  363. $aBindings = array();
  364. $sWhereClause = "";
  365. $aBlockIds = array();
  366. switch($aParams['type']) {
  367. case 'by_id':
  368. $aBindings = array(
  369. 'id' => $aParams['value']
  370. );
  371. $sWhereClause = "AND `tpb`.`id`=:id ";
  372. $aBlockIds[] = $aParams['value'];
  373. break;
  374. case 'by_object':
  375. $aBindings = array(
  376. 'object' => $aParams['value']
  377. );
  378. $sWhereClause = "AND `tpb`.`object`=:object ";
  379. $aBlockIds = $this->getColumn("SELECT `id` FROM `sys_pages_blocks` AS `tpb` WHERE 1 " . $sWhereClause, $aBindings);
  380. break;
  381. default:
  382. return false;
  383. }
  384. $sSql = "DELETE FROM `tpb` USING `sys_pages_blocks` AS `tpb` WHERE 1 " . $sWhereClause;
  385. $b = ((int)$this->query($sSql, $aBindings) > 0);
  386. if ($b && $aBlockIds)
  387. BxDolWikiQuery::deleteAllRevisions($aBlockIds);
  388. return $b;
  389. }
  390. function resetBlocksByPage($sObject, $iCellId)
  391. {
  392. $sSql = $this->prepare("UPDATE `sys_pages_blocks` SET `cell_id`=? WHERE `object`=? AND `cell_id`>?", $iCellId, $sObject, $iCellId);
  393. return $this->query($sSql);
  394. }
  395. function getBlockOrderMax($sObject, $iCellId = 1)
  396. {
  397. $sSql = $this->prepare("SELECT MAX(`order`) FROM `sys_pages_blocks` WHERE `object`=? AND `cell_id`=? LIMIT 1", $sObject, $iCellId);
  398. return (int)$this->getOne($sSql);
  399. }
  400. function deleteImage($aParams)
  401. {
  402. $sWhereClause = "";
  403. switch($aParams['type']) {
  404. case 'by_id':
  405. $aMethod['params'][1] = array(
  406. 'id' => $aParams['value']
  407. );
  408. $sWhereClause = "AND `tpb`.`id`=:id ";
  409. break;
  410. case 'all':
  411. break;
  412. }
  413. $sSql = "DELETE FROM `tpb` USING `sys_pages_blocks` AS `tpb` WHERE 1 " . $sWhereClause;
  414. return (int)$this->query($sSql) > 0;
  415. }
  416. function getMenus($bFull = false)
  417. {
  418. $sSql = "SELECT * FROM `sys_objects_menu` WHERE 1 ORDER BY `module`, `object`";
  419. if($bFull)
  420. return $this->getAllWithKey($sSql, 'object');
  421. else
  422. return $this->getPairs($sSql, 'object', 'title');
  423. }
  424. }
  425. /** @} */