BxDolStudioPermissionsQuery.php 4.5 KB

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