BxDolStudioWidgetsQuery.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 BxDolStudioWidgetsQuery extends BxDolStudioPageQuery implements iBxDolSingleton
  10. {
  11. public function __construct()
  12. {
  13. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  14. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  15. parent::__construct();
  16. }
  17. /**
  18. * Prevent cloning the instance
  19. */
  20. public function __clone()
  21. {
  22. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  23. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  24. }
  25. /**
  26. * Get singleton instance of the class
  27. */
  28. public static function getInstance()
  29. {
  30. $sClass = __CLASS__;
  31. if (!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  32. $GLOBALS['bxDolClasses'][__CLASS__] = new $sClass();
  33. return $GLOBALS['bxDolClasses'][__CLASS__];
  34. }
  35. function reorder($sPage, $aItems)
  36. {
  37. $iResult = 0;
  38. foreach($aItems as $iKey => $iWidgetId) {
  39. $sSql = $this->prepare("UPDATE `sys_std_pages_widgets` AS `tpw`
  40. LEFT JOIN `sys_std_pages` AS `tp` ON `tpw`.`page_id`=`tp`.`id`
  41. SET `tpw`.`order`=?
  42. WHERE `tpw`.`widget_id`=?", $iKey + 1, $iWidgetId);
  43. $iResult += (int)$this->query($sSql);
  44. }
  45. return $iResult;
  46. }
  47. function getWidgets($aParams)
  48. {
  49. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  50. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  51. if(!isset($aParams['order']) || empty($aParams['order']))
  52. $sOrderClause = "ORDER BY `tw2p`.`order` ASC";
  53. switch($aParams['type']) {
  54. case 'by_id':
  55. $aMethod['name'] = 'getRow';
  56. $aMethod['params'][1] = array(
  57. 'id' => $aParams['value']
  58. );
  59. $sWhereClause .= "AND `tw`.`id`=:id";
  60. break;
  61. case 'by_page_id':
  62. $aMethod['params'][1] = array(
  63. 'page_id' => $aParams['value']
  64. );
  65. $sWhereClause .= "AND `tw2p`.`page_id`=:page_id ";
  66. if(!empty($aParams['wtype'])) {
  67. $aMethod['params'][1]['type'] = $aParams['wtype'];
  68. $sWhereClause .= "AND `tw`.`type`=:type ";
  69. }
  70. break;
  71. case 'all_featured':
  72. $aMethod['params'][1] = array(
  73. 'featured' => $aParams['featured']
  74. );
  75. $sWhereClause .= "AND `tw`.`featured`=:featured";
  76. break;
  77. case 'all_bookmarks':
  78. $aMethod['params'][1] = array(
  79. 'profile_id' => $aParams['profile_id'],
  80. 'bookmark' => $aParams['bookmark']
  81. );
  82. $sSelectClause .= ", `twb`.`bookmark` AS `bookmark`";
  83. $sJoinClause .= "JOIN `sys_std_widgets_bookmarks` AS `twb` ON `tw`.`id`=`twb`.`widget_id` AND `twb`.`profile_id`=:profile_id ";
  84. $sWhereClause .= "AND `twb`.`bookmark`=:bookmark";
  85. break;
  86. case 'all_with_notices':
  87. $sWhereClause .= "AND `tw`.`cnt_notices`<>''";
  88. break;
  89. }
  90. $aMethod['params'][0] = "SELECT
  91. `tw`.`id` AS `id`,
  92. `tp`.`id` AS `page_id`,
  93. `tp`.`index` AS `page_index`,
  94. `tp`.`name` AS `page_name`,
  95. `tp`.`header` AS `page_header`,
  96. `tp`.`caption` AS `page_caption`,
  97. `tp`.`icon` AS `page_icon`,
  98. `tw`.`module` AS `module`,
  99. `tw`.`type` AS `type`,
  100. `tw`.`url` AS `url`,
  101. `tw`.`click` AS `click`,
  102. `tw`.`icon` AS `icon`,
  103. `tw`.`caption` AS `caption`,
  104. `tw`.`cnt_notices` AS `cnt_notices`,
  105. `tw`.`cnt_actions` AS `cnt_actions`,
  106. `tw`.`featured` AS `featured`,
  107. `tw2p`.`order` AS `order`" . $sSelectClause . "
  108. FROM `sys_std_widgets` AS `tw`
  109. LEFT JOIN `sys_std_pages` AS `tp` ON `tw`.`page_id`=`tp`.`id`
  110. LEFT JOIN `sys_std_pages_widgets` AS `tw2p` ON `tw`.`id`=`tw2p`.`widget_id` " . $sJoinClause . "
  111. WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  112. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  113. }
  114. public function updateWidget($aSet, $aWhere)
  115. {
  116. if(empty($aSet) || empty($aWhere))
  117. return false;
  118. return $this->query("UPDATE `sys_std_widgets` SET " . $this->arrayToSQL($aSet) . " WHERE " . $this->arrayToSQL($aWhere, ' AND '));
  119. }
  120. }
  121. /** @} */