BxDolStudioPermissionsQuery.php 4.5 KB

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