BxDolStudioPermissionsQuery.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinStudio Dolphin Studio
  7. * @{
  8. */
  9. defined('BX_DOL') or die('hack attempt');
  10. bx_import('BxDolAclQuery');
  11. class BxDolStudioPermissionsQuery extends BxDolAclQuery {
  12. function __construct() {
  13. parent::__construct();
  14. }
  15. function isLevelUsed($iId) {
  16. $sSql = $this->prepare("SELECT UNIX_TIMESTAMP(MAX(`DateExpires`)) as `MaxDateExpires` FROM `sys_acl_levels_members` WHERE `IDLevel`=?", $iId);
  17. return (int)$this->getOne($sSql) > time();
  18. }
  19. function getLevelOrderMax() {
  20. return (int)$this->getOne("SELECT MAX(`Order`) FROM `sys_acl_levels` WHERE 1");
  21. }
  22. function updateLevels($iId, $aFields) {
  23. $sSql = "UPDATE `sys_acl_levels` SET `" . implode("`=?, `", array_keys($aFields)) . "`=? WHERE `ID`=?";
  24. $sSql = call_user_func_array(array($this, 'prepare'), array_merge(array($sSql), array_values($aFields), array($iId)));
  25. return (int)$this->query($sSql) > 0;
  26. }
  27. function deleteLevel($aParams) {
  28. $sWhereClause = $sLimitClause = "";
  29. switch($aParams['type']) {
  30. case 'by_id':
  31. $sWhereClause .= $this->prepare("AND `tal`.`ID`=?", $aParams['value']);
  32. break;
  33. }
  34. $sSql = "DELETE FROM `tal`, `tap`, `tam`
  35. USING `sys_acl_levels` AS `tal`
  36. LEFT JOIN `sys_acl_level_prices` AS `tap` ON `tal`.`ID`=`tap`.`IDLevel`
  37. LEFT JOIN `sys_acl_matrix` AS `tam` ON `tal`.`ID`=`tam`.`IDLevel`
  38. WHERE 1 " . $sWhereClause . " " . $sLimitClause;
  39. return (int)$this->query($sSql) > 0;
  40. }
  41. function switchAction($iLevelId, $iActionId, $bEnable) {
  42. if($bEnable)
  43. $sSql = $this->prepare("INSERT INTO `sys_acl_matrix`(`IDLevel`, `IDAction`) VALUES(?, ?)", $iLevelId, $iActionId);
  44. else
  45. $sSql = $this->prepare("DELETE FROM `sys_acl_matrix` WHERE `IDLevel`=? AND `IDAction`=?", $iLevelId, $iActionId);
  46. return (int)$this->query($sSql) > 0;
  47. }
  48. function getOptions($aParams, &$aItems, $bReturnCount = true) {
  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 `tam`.`IDAction` ASC";
  53. switch($aParams['type']) {
  54. case 'by_level_action_ids':
  55. $aMethod['name'] = 'getRow';
  56. $sSelectClause = ", `taa`.`Title` AS `action_title`, `taa`.`Countable` AS `action_countable`";
  57. $sJoinClause = "LEFT JOIN `sys_acl_actions` AS `taa` ON `tam`.`IDAction`=`taa`.`ID` ";
  58. $sWhereClause = $this->prepare(" AND `tam`.`IDLevel`=? AND `tam`.`IDAction`=? ", $aParams['level_id'], $aParams['action_id']);
  59. break;
  60. }
  61. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  62. `tam`.`IDLevel` AS `level_id`,
  63. `tam`.`IDAction` AS `action_id`,
  64. `tam`.`AllowedCount` AS `allowed_count`,
  65. `tam`.`AllowedPeriodLen` AS `allowed_period_len`,
  66. `tam`.`AllowedPeriodStart` AS `allowed_period_start`,
  67. `tam`.`AllowedPeriodEnd` AS `allowed_period_end`,
  68. `tam`.`AdditionalParamValue` AS `additional_param_value`" . $sSelectClause . "
  69. FROM `sys_acl_matrix` AS `tam` " . $sJoinClause . "
  70. WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  71. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  72. if(!$bReturnCount)
  73. return !empty($aItems);
  74. return (int)$this->getOne("SELECT FOUND_ROWS()");
  75. }
  76. function updateOptions($iLevelId, $iActionId, $aFields) {
  77. $sSql = "UPDATE `sys_acl_matrix` SET `" . implode("`=?, `", array_keys($aFields)) . "`=? WHERE `IDLevel`=? AND `IDAction`=?";
  78. $sSql = call_user_func_array(array($this, 'prepare'), array_merge(array($sSql), array_values($aFields), array($iLevelId, $iActionId)));
  79. return (int)$this->query($sSql) > 0;
  80. }
  81. function getPriceOrderMax($iLevelId) {
  82. $sSql = $this->prepare("SELECT MAX(`Order`) FROM `sys_acl_level_prices` WHERE `IDLevel`=?", $iLevelId);
  83. return (int)$this->getOne($sSql);
  84. }
  85. function deletePrices($aParams) {
  86. $sWhereClause = "";
  87. switch($aParams['type']) {
  88. case 'by_level_id':
  89. $sWhereClause .= $this->prepare("AND `IDLevel`=?", $aParams['value']);
  90. break;
  91. }
  92. return (int)$this->query("DELETE FROM `sys_acl_level_prices` WHERE 1 " . $sWhereClause) > 0;
  93. }
  94. function deleteActions($aParams) {
  95. $sWhereClause = "";
  96. switch($aParams['type']) {
  97. case 'by_level_id':
  98. $sWhereClause .= $this->prepare("AND `IDLevel`=?", $aParams['value']);
  99. break;
  100. }
  101. return (int)$this->query("DELETE FROM `sys_acl_matrix` WHERE 1 " . $sWhereClause) > 0;
  102. }
  103. }
  104. /** @} */