BxAclDb.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 ACL ACL
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. class BxAclDb extends BxDolModuleDb
  12. {
  13. protected $_oConfig;
  14. /*
  15. * Constructor.
  16. */
  17. function __construct(&$oConfig)
  18. {
  19. parent::__construct($oConfig);
  20. $this->_oConfig = $oConfig;
  21. }
  22. public function getLevels($aParams, $bReturnCount = false)
  23. {
  24. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  25. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  26. $sSelectClause = "
  27. `tal`.`ID` AS `id`,
  28. `tal`.`Name` AS `name`,
  29. `tal`.`Icon` AS `icon`,
  30. `tal`.`Description` AS `description`,
  31. `tal`.`Active` AS `active`,
  32. `tal`.`Purchasable` AS `purchasable`,
  33. `tal`.`Removable` AS `removable`,
  34. `tal`.`QuotaSize` AS `quota_size`,
  35. `tal`.`QuotaNumber` AS `quota_number`,
  36. `tal`.`QuotaMaxFileSize` AS `quota_max_file_size`,
  37. `tal`.`Order` AS `order`";
  38. if(!isset($aParams['order']) || empty($aParams['order']))
  39. $sOrderClause = "ORDER BY `tal`.`Order` ASC";
  40. switch($aParams['type']) {
  41. case 'by_id':
  42. $aMethod['name'] = 'getRow';
  43. $aMethod['params'][1] = array(
  44. 'id' => $aParams['value']
  45. );
  46. $sWhereClause .= "AND `tal`.`id`=:id";
  47. break;
  48. case 'for_selector':
  49. $aMethod['name'] = 'getPairs';
  50. $aMethod['params'][1] = 'id';
  51. $aMethod['params'][2] = 'name';
  52. $sWhereClause .= "AND `tal`.`Active`='yes' AND (`tal`.`Purchasable`='yes' OR `tal`.`Removable`='yes')";
  53. break;
  54. }
  55. $sSql = "SELECT {select} FROM `sys_acl_levels` AS `tal` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  56. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
  57. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  58. if(!$bReturnCount)
  59. return $aItems;
  60. $aMethod['name'] = 'getOne';
  61. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array("COUNT(*)", "", ""), $sSql);
  62. return array(
  63. 'items' => $aItems,
  64. 'count' => (int)call_user_func_array(array($this, $aMethod['name']), $aMethod['params'])
  65. );
  66. }
  67. public function updateLevels($aSet, $aWhere)
  68. {
  69. $sSql = "UPDATE `sys_acl_levels` SET " . $this->arrayToSQL($aSet, " AND ") . " WHERE " . $this->arrayToSQL($aWhere, " AND ");
  70. return (int)$this->query($sSql) > 0;
  71. }
  72. public function getPrices($aParams, $bReturnCount = false)
  73. {
  74. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  75. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  76. $sSelectClause = "
  77. `tap`.`id` AS `id`,
  78. `tap`.`level_id` AS `level_id`,
  79. `tap`.`name` AS `name`,
  80. `tap`.`period` AS `period`,
  81. `tap`.`period_unit` AS `period_unit`,
  82. `tap`.`price` AS `price`,
  83. `tap`.`order` AS `order`";
  84. if(!isset($aParams['order']) || empty($aParams['order']))
  85. $sOrderClause = "ORDER BY `tap`.`Order` ASC";
  86. switch($aParams['type']) {
  87. case 'by_id':
  88. $aMethod['name'] = 'getRow';
  89. $aMethod['params'][1] = array(
  90. 'id' => $aParams['value']
  91. );
  92. $sWhereClause .= "AND `tap`.`id`=:id";
  93. break;
  94. case 'by_id_full':
  95. $aMethod['name'] = 'getRow';
  96. $aMethod['params'][1] = array(
  97. 'id' => $aParams['value']
  98. );
  99. $sSelectClause .= ", `tal`.`Name` AS `level_name`, `tal`.`Description` AS `level_description`";
  100. $sJoinClause .= "LEFT JOIN `sys_acl_levels` AS `tal` ON `tap`.`level_id`=`tal`.`ID`";
  101. $sWhereClause .= "AND `tap`.`id`=:id";
  102. break;
  103. case 'by_level_id':
  104. $aMethod['params'][1] = array(
  105. 'level_id' => $aParams['value']
  106. );
  107. $sWhereClause .= "AND `tap`.`level_id`=:level_id";
  108. break;
  109. case 'by_level_id_duration':
  110. $aMethod['name'] = 'getRow';
  111. $aMethod['params'][1] = array(
  112. 'level_id' => $aParams['level_id'],
  113. 'period' => $aParams['period'],
  114. 'period_unit' => $aParams['period_unit'],
  115. );
  116. $sWhereClause .= "AND `tap`.`level_id`=:level_id AND `tap`.`period`=:period AND `tap`.`period_unit`=:period_unit";
  117. break;
  118. case 'all_full':
  119. $sSelectClause .= ", `tal`.`Name` AS `level_name`, `tal`.`Description` AS `level_description`";
  120. $sJoinClause .= "LEFT JOIN `sys_acl_levels` AS `tal` ON `tap`.`level_id`=`tal`.`ID`";
  121. break;
  122. }
  123. $sSql = "SELECT {select} FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` AS `tap` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  124. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
  125. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  126. if(!$bReturnCount)
  127. return $aItems;
  128. $aMethod['name'] = 'getOne';
  129. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array("COUNT(*)", "", ""), $sSql);
  130. return array(
  131. 'items' => $aItems,
  132. 'count' => (int)call_user_func_array(array($this, $aMethod['name']), $aMethod['params'])
  133. );
  134. }
  135. public function getPriceOrderMax($iLevelId)
  136. {
  137. $sSql = $this->prepare("SELECT MAX(`order`) FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` WHERE `level_id`=?", $iLevelId);
  138. return (int)$this->getOne($sSql);
  139. }
  140. public function deletePrices($aWhere)
  141. {
  142. $sSql = "DELETE FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` WHERE " . $this->arrayToSQL($aWhere, " AND ");
  143. return (int)$this->query($sSql) > 0;
  144. }
  145. }
  146. /** @} */