BxAclDb.php 7.0 KB

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