BxDolStudioBuilderPageQuery.php 18 KB

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