BxDolStudioBuilderPageQuery.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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`.`inj_head` AS `inj_head`,
  121. `tp`.`inj_footer` AS `inj_footer`,
  122. `tp`.`sticky_columns` AS `sticky_columns`,
  123. `tp`.`deletable` AS `deletable`,
  124. `tp`.`override_class_name` AS `override_class_name`,
  125. `tp`.`override_class_file` AS `override_class_file`" . $sSelectClause . "
  126. FROM `sys_objects_page` AS `tp` " . $sJoinClause . "
  127. WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  128. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  129. if(!$bReturnCount)
  130. return !empty($aItems);
  131. return (int)$this->getOne("SELECT FOUND_ROWS()");
  132. }
  133. function updatePage($iId, $aFields)
  134. {
  135. $sSql = "UPDATE `sys_objects_page` SET `" . implode("`=?, `", array_keys($aFields)) . "`=? WHERE `id`=?";
  136. $oStml = call_user_func_array(array($this, 'prepare'), array_merge(array($sSql), array_values($aFields), array($iId)));
  137. return $this->res($oStml);
  138. }
  139. function deletePages($aParams)
  140. {
  141. $aBindings = array();
  142. $sWhereClause = "";
  143. switch($aParams['type']) {
  144. case 'by_id':
  145. $aBindings = array(
  146. 'id' => $aParams['value']
  147. );
  148. $sWhereClause = "AND `tp`.`id`=:id ";
  149. break;
  150. case 'by_object':
  151. $aBindings = array(
  152. 'object' => $aParams['value']
  153. );
  154. $sWhereClause = "AND `tp`.`object`=:object ";
  155. break;
  156. case 'all':
  157. break;
  158. }
  159. $sSql = "DELETE FROM `tp` USING `sys_objects_page` AS `tp` WHERE 1 " . $sWhereClause;
  160. return (int)$this->query($sSql, $aBindings) > 0;
  161. }
  162. function isUniqUri($sUri)
  163. {
  164. $sSql = $this->prepare("SELECT `id` FROM `sys_objects_page` WHERE `uri`=? LIMIT 1", $sUri);
  165. return (int)$this->getOne($sSql) <= 0;
  166. }
  167. function getTypes($aParams, &$aItems, $bReturnCount = true)
  168. {
  169. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  170. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  171. if(!isset($aParams['order']) || empty($aParams['order']))
  172. $sOrderClause = "ORDER BY `tpt`.`id` ASC";
  173. switch($aParams['type']) {
  174. case 'by_id':
  175. $aMethod['name'] = 'getRow';
  176. $aMethod['params'][1] = array(
  177. 'id' => $aParams['value']
  178. );
  179. $sWhereClause = "AND `tpt`.`id`=:id ";
  180. break;
  181. case 'all':
  182. break;
  183. }
  184. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  185. `tpt`.`id` AS `id`,
  186. `tpt`.`title` AS `title`,
  187. `tpt`.`template` AS `template`,
  188. `tpt`.`order` AS `order`" . $sSelectClause . "
  189. FROM `sys_pages_types` AS `tpt` " . $sJoinClause . "
  190. WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  191. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  192. if(!$bReturnCount)
  193. return !empty($aItems);
  194. return (int)$this->getOne("SELECT FOUND_ROWS()");
  195. }
  196. function getSubmenus()
  197. {
  198. return BxDolDb::getInstance()->getPairs('SELECT `object`, `title` FROM `sys_objects_menu` WHERE `template_id` = 8', 'object', 'title');
  199. }
  200. function getLayouts($aParams, &$aItems, $bReturnCount = true)
  201. {
  202. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  203. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  204. if(!isset($aParams['order']) || empty($aParams['order']))
  205. $sOrderClause = "ORDER BY `tpl`.`id` ASC";
  206. switch($aParams['type']) {
  207. case 'by_id':
  208. $aMethod['name'] = 'getRow';
  209. $aMethod['params'][1] = array(
  210. 'id' => $aParams['value']
  211. );
  212. $sWhereClause = "AND `tpl`.`id`=:id ";
  213. break;
  214. case 'all':
  215. break;
  216. }
  217. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  218. `tpl`.`id` AS `id`,
  219. `tpl`.`name` AS `name`,
  220. `tpl`.`icon` AS `icon`,
  221. `tpl`.`title` AS `title`,
  222. `tpl`.`template` AS `template`,
  223. `tpl`.`cells_number` AS `cells_number`" . $sSelectClause . "
  224. FROM `sys_pages_layouts` AS `tpl` " . $sJoinClause . "
  225. WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  226. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  227. if(!$bReturnCount)
  228. return !empty($aItems);
  229. return (int)$this->getOne("SELECT FOUND_ROWS()");
  230. }
  231. function getDesignBoxes($aParams, &$aItems, $bReturnCount = true)
  232. {
  233. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  234. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  235. if(!isset($aParams['order']) || empty($aParams['order']))
  236. $sOrderClause = "ORDER BY `tpd`.`order` ASC";
  237. switch($aParams['type']) {
  238. case 'by_id':
  239. $aMethod['name'] = 'getRow';
  240. $aMethod['params'][1] = array(
  241. 'id' => $aParams['value']
  242. );
  243. $sWhereClause = "AND `tpd`.`id`=:id ";
  244. break;
  245. case 'ordered':
  246. $sWhereClause = "AND `tpd`.`order`<>0 ";
  247. break;
  248. case 'all':
  249. break;
  250. }
  251. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  252. `tpd`.`id` AS `id`,
  253. `tpd`.`title` AS `title`,
  254. `tpd`.`template` AS `template`" . $sSelectClause . "
  255. FROM `sys_pages_design_boxes` AS `tpd` " . $sJoinClause . "
  256. WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  257. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  258. if(!$bReturnCount)
  259. return !empty($aItems);
  260. return (int)$this->getOne("SELECT FOUND_ROWS()");
  261. }
  262. function getModulesWithCopyableBlocks()
  263. {
  264. $sSql = $this->prepare("SELECT
  265. `tm`.`name` AS `module`
  266. FROM `sys_modules` AS `tm`
  267. LEFT JOIN `sys_pages_blocks` AS `tpb` ON `tm`.`name`=`tpb`.`module`
  268. WHERE `tm`.`type`=? AND `tpb`.`copyable`=?
  269. GROUP BY `tm`.`name`", BX_DOL_MODULE_TYPE_MODULE, 1);
  270. return $this->getColumn($sSql);
  271. }
  272. function getBlocks($aParams, &$aItems, $bReturnCount = true)
  273. {
  274. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  275. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  276. if(!isset($aParams['order']) || empty($aParams['order']))
  277. $sOrderClause = "ORDER BY `tpb`.`order` ASC";
  278. switch($aParams['type']) {
  279. case 'by_id':
  280. $aMethod['name'] = 'getRow';
  281. $aMethod['params'][1] = array(
  282. 'id' => $aParams['value']
  283. );
  284. $sWhereClause = "AND `tpb`.`id`=:id ";
  285. break;
  286. case 'skeleton_by_type':
  287. $aMethod['name'] = 'getRow';
  288. $aMethod['params'][1] = array(
  289. 'module' => BX_DOL_STUDIO_BP_SKELETONS,
  290. 'type' => $aParams['value']
  291. );
  292. $sWhereClause = "AND `tpb`.`object`='' AND `tpb`.`module`=:module AND `tpb`.`type`=:type ";
  293. break;
  294. case 'by_ids':
  295. $sWhereClause = "AND `tpb`.`id` IN (" . $this->implode_escape($aParams['value']) . ")";
  296. break;
  297. case 'by_object':
  298. $aMethod['params'][1] = array(
  299. 'object' => $aParams['value']
  300. );
  301. $sWhereClause = "AND `tpb`.`object`=:object";
  302. break;
  303. case 'by_object_cell':
  304. $aMethod['params'][1] = array(
  305. 'object' => $aParams['object'],
  306. 'cell_id' => $aParams['cell']
  307. );
  308. $sWhereClause = "AND `tpb`.`object`=:object AND `tpb`.`cell_id`=:cell_id";
  309. break;
  310. case 'by_module_to_copy':
  311. $aMethod['params'][1] = array(
  312. 'module' => $aParams['value']
  313. );
  314. $sWhereClause = "AND `tpb`.`module`=:module AND `tpb`.`copyable`=1";
  315. if($aParams['value'] == BX_DOL_STUDIO_BP_SKELETONS)
  316. $sWhereClause .= " AND `tpb`.`object`=''";
  317. break;
  318. case 'counter_by_pages':
  319. $aMethod['name'] = 'getPairs';
  320. $aMethod['params'][1] = 'object';
  321. $aMethod['params'][2] = 'counter';
  322. $sSelectClause = ", COUNT(*) AS `counter`";
  323. $sGroupClause = "GROUP BY `tpb`.`object`";
  324. break;
  325. case 'all':
  326. break;
  327. }
  328. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  329. `tpb`.`id` AS `id`,
  330. `tpb`.`object` AS `object`,
  331. `tpb`.`cell_id` AS `cell_id`,
  332. `tpb`.`module` AS `module`,
  333. `tpb`.`title_system` AS `title_system`,
  334. `tpb`.`title` AS `title`,
  335. `tpb`.`designbox_id` AS `designbox_id`,
  336. `tpb`.`hidden_on` AS `hidden_on`,
  337. `tpb`.`visible_for_levels` AS `visible_for_levels`,
  338. `tpb`.`type` AS `type`,
  339. `tpb`.`content` AS `content`,
  340. `tpb`.`deletable` AS `deletable`,
  341. `tpb`.`copyable` AS `copyable`,
  342. `tpb`.`active` AS `active`,
  343. `tpb`.`order` AS `order`" . $sSelectClause . "
  344. FROM `sys_pages_blocks` AS `tpb` " . $sJoinClause . "
  345. WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  346. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  347. if(!$bReturnCount)
  348. return !empty($aItems);
  349. return (int)$this->getOne("SELECT FOUND_ROWS()");
  350. }
  351. function insertBlock($aFields)
  352. {
  353. $aFields['order'] = $this->getBlockOrderMax($aFields['object']) + 1;
  354. $sSql = "INSERT INTO `sys_pages_blocks` SET " . $this->arrayToSQL($aFields);
  355. if (!$this->query($sSql))
  356. return false;
  357. return $this->lastId();
  358. }
  359. function updateBlock($iId, $aFields)
  360. {
  361. $oStmt = $this->prepare("UPDATE `sys_pages_blocks` SET " . $this->arrayToSQL($aFields) . " WHERE `id`=:id");
  362. return $this->res($oStmt, array('id' => $iId)) ? true : false;
  363. }
  364. function deleteBlocks($aParams)
  365. {
  366. $aBindings = array();
  367. $sWhereClause = "";
  368. $aBlockIds = array();
  369. switch($aParams['type']) {
  370. case 'by_id':
  371. $aBindings = array(
  372. 'id' => $aParams['value']
  373. );
  374. $sWhereClause = "AND `tpb`.`id`=:id ";
  375. $aBlockIds[] = $aParams['value'];
  376. break;
  377. case 'by_object':
  378. $aBindings = array(
  379. 'object' => $aParams['value']
  380. );
  381. $sWhereClause = "AND `tpb`.`object`=:object ";
  382. $aBlockIds = $this->getColumn("SELECT `id` FROM `sys_pages_blocks` AS `tpb` WHERE 1 " . $sWhereClause, $aBindings);
  383. break;
  384. default:
  385. return false;
  386. }
  387. $sSql = "DELETE FROM `tpb` USING `sys_pages_blocks` AS `tpb` WHERE 1 " . $sWhereClause;
  388. $b = ((int)$this->query($sSql, $aBindings) > 0);
  389. if ($b && $aBlockIds)
  390. BxDolWikiQuery::deleteAllRevisions($aBlockIds);
  391. return $b;
  392. }
  393. function resetBlocksByPage($sObject, $iCellId, $bDeactivate = false)
  394. {
  395. $aSetClause = array('cell_id' => $iCellId);
  396. if($bDeactivate)
  397. $aSetClause['active'] = 0;
  398. return $this->query("UPDATE `sys_pages_blocks` SET " . $this->arrayToSQL($aSetClause) . " WHERE `object`=:object AND `cell_id`>:cell_id", array(
  399. 'object' => $sObject,
  400. 'cell_id' => $iCellId
  401. ));
  402. }
  403. function getBlockOrderMax($sObject, $iCellId = 1)
  404. {
  405. $sSql = $this->prepare("SELECT MAX(`order`) FROM `sys_pages_blocks` WHERE `object`=? AND `cell_id`=? LIMIT 1", $sObject, $iCellId);
  406. return (int)$this->getOne($sSql);
  407. }
  408. function deleteImage($aParams)
  409. {
  410. $sWhereClause = "";
  411. switch($aParams['type']) {
  412. case 'by_id':
  413. $aMethod['params'][1] = array(
  414. 'id' => $aParams['value']
  415. );
  416. $sWhereClause = "AND `tpb`.`id`=:id ";
  417. break;
  418. case 'all':
  419. break;
  420. }
  421. $sSql = "DELETE FROM `tpb` USING `sys_pages_blocks` AS `tpb` WHERE 1 " . $sWhereClause;
  422. return (int)$this->query($sSql) > 0;
  423. }
  424. function getMenus($bFull = false)
  425. {
  426. $sSql = "SELECT * FROM `sys_objects_menu` WHERE 1 ORDER BY `module`, `object`";
  427. if($bFull)
  428. return $this->getAllWithKey($sSql, 'object');
  429. else
  430. return $this->getPairs($sSql, 'object', 'title');
  431. }
  432. }
  433. /** @} */