BxAclDb.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. return $this->getColumn("SELECT `name` FROM `bx_acl_level_prices` WHERE 1 LIMIT :limit", ['limit' => $iLimit]);
  22. }
  23. public function getLevels($aParams, $bReturnCount = false)
  24. {
  25. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  26. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  27. $sSelectClause = "
  28. `tal`.`ID` AS `id`,
  29. `tal`.`Name` AS `name`,
  30. `tal`.`Icon` AS `icon`,
  31. `tal`.`Description` AS `description`,
  32. `tal`.`Active` AS `active`,
  33. `tal`.`Purchasable` AS `purchasable`,
  34. `tal`.`Removable` AS `removable`,
  35. `tal`.`QuotaSize` AS `quota_size`,
  36. `tal`.`QuotaNumber` AS `quota_number`,
  37. `tal`.`QuotaMaxFileSize` AS `quota_max_file_size`,
  38. `tal`.`Order` AS `order`";
  39. if(!isset($aParams['order']) || empty($aParams['order']))
  40. $sOrderClause = "ORDER BY `tal`.`Order` ASC";
  41. switch($aParams['type']) {
  42. case 'by_id':
  43. $aMethod['name'] = 'getRow';
  44. $aMethod['params'][1] = array(
  45. 'id' => $aParams['value']
  46. );
  47. $sWhereClause .= "AND `tal`.`id`=:id";
  48. break;
  49. case 'for_selector':
  50. $aMethod['name'] = 'getPairs';
  51. $aMethod['params'][1] = 'id';
  52. $aMethod['params'][2] = 'name';
  53. $sWhereClause .= "AND `tal`.`Active`='yes' AND (`tal`.`ID`='3' OR `tal`.`Purchasable`='yes' OR `tal`.`Removable`='yes')";
  54. break;
  55. }
  56. $sSql = "SELECT {select} FROM `sys_acl_levels` AS `tal` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  57. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
  58. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  59. if(!$bReturnCount)
  60. return $aItems;
  61. $aMethod['name'] = 'getOne';
  62. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array("COUNT(*)", "", ""), $sSql);
  63. return array(
  64. 'items' => $aItems,
  65. 'count' => (int)call_user_func_array(array($this, $aMethod['name']), $aMethod['params'])
  66. );
  67. }
  68. public function updateLevels($aSet, $aWhere)
  69. {
  70. $sSql = "UPDATE `sys_acl_levels` SET " . $this->arrayToSQL($aSet, " AND ") . " WHERE " . $this->arrayToSQL($aWhere, " AND ");
  71. return (int)$this->query($sSql) > 0;
  72. }
  73. public function getPrices($aParams, $bReturnCount = false)
  74. {
  75. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  76. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  77. $sSelectClause = "
  78. `tap`.`id` AS `id`,
  79. `tap`.`level_id` AS `level_id`,
  80. `tap`.`name` AS `name`,
  81. `tap`.`caption` AS `caption`,
  82. `tap`.`description` AS `description`,
  83. `tap`.`details` AS `details`,
  84. `tap`.`period` AS `period`,
  85. `tap`.`period_unit` AS `period_unit`,
  86. `tap`.`price` AS `price`,
  87. `tap`.`trial` AS `trial`,
  88. `tap`.`immediate` AS `immediate`,
  89. `tap`.`added` AS `added`,
  90. `tap`.`order` AS `order`";
  91. if(!isset($aParams['order']) || empty($aParams['order']))
  92. $sOrderClause = "ORDER BY `tap`.`Order` ASC";
  93. switch($aParams['type']) {
  94. case 'by_id':
  95. $aMethod['name'] = 'getRow';
  96. $aMethod['params'][1] = array(
  97. 'id' => $aParams['value']
  98. );
  99. $sWhereClause .= "AND `tap`.`id`=:id";
  100. break;
  101. case 'by_id_full':
  102. $aMethod['name'] = 'getRow';
  103. $aMethod['params'][1] = array(
  104. 'id' => $aParams['value']
  105. );
  106. $sSelectClause .= ", `tal`.`Name` AS `level_name`, `tal`.`Description` AS `level_description`";
  107. $sJoinClause .= "LEFT JOIN `sys_acl_levels` AS `tal` ON `tap`.`level_id`=`tal`.`ID`";
  108. $sWhereClause .= "AND `tap`.`id`=:id";
  109. break;
  110. case 'by_name_full':
  111. $aMethod['name'] = 'getRow';
  112. $aMethod['params'][1] = array(
  113. 'name' => $aParams['value']
  114. );
  115. $sSelectClause .= ", `tal`.`Name` AS `level_name`, `tal`.`Description` AS `level_description`";
  116. $sJoinClause .= "LEFT JOIN `sys_acl_levels` AS `tal` ON `tap`.`level_id`=`tal`.`ID`";
  117. $sWhereClause .= "AND `tap`.`name`=:name";
  118. break;
  119. case 'by_level_id':
  120. $aMethod['params'][1] = array(
  121. 'level_id' => $aParams['value']
  122. );
  123. $sWhereClause .= "AND `tap`.`level_id`=:level_id";
  124. break;
  125. case 'by_level_id_duration':
  126. $aMethod['name'] = 'getRow';
  127. $aMethod['params'][1] = array(
  128. 'level_id' => $aParams['level_id'],
  129. 'period' => $aParams['period'],
  130. 'period_unit' => $aParams['period_unit'],
  131. );
  132. $sWhereClause .= "AND `tap`.`level_id`=:level_id AND `tap`.`period`=:period AND `tap`.`period_unit`=:period_unit";
  133. break;
  134. case 'all_full':
  135. $sSelectClause .= ", `tal`.`Name` AS `level_name`, `tal`.`Description` AS `level_description`";
  136. $sJoinClause .= "LEFT JOIN `sys_acl_levels` AS `tal` ON `tap`.`level_id`=`tal`.`ID`";
  137. break;
  138. }
  139. $sSql = "SELECT {select} FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` AS `tap` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  140. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
  141. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  142. if(!$bReturnCount)
  143. return $aItems;
  144. $aMethod['name'] = 'getOne';
  145. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array("COUNT(*)", "", ""), $sSql);
  146. return array(
  147. 'items' => $aItems,
  148. 'count' => (int)call_user_func_array(array($this, $aMethod['name']), $aMethod['params'])
  149. );
  150. }
  151. public function getPriceOrderMax($iLevelId)
  152. {
  153. $sSql = $this->prepare("SELECT MAX(`order`) FROM `" . $this->_oConfig->CNF['TABLE_PRICES'] . "` WHERE `level_id`=?", $iLevelId);
  154. return (int)$this->getOne($sSql);
  155. }
  156. public function deletePrices($aWhere)
  157. {
  158. $CNF = &$this->_oConfig->CNF;
  159. $sWhereClause = $this->arrayToSQL($aWhere, " AND ");
  160. $aPrices = $this->getAll("SELECT * FROM `" . $CNF['TABLE_PRICES'] . "` WHERE " . $sWhereClause);
  161. if(!empty($aPrices) && is_array($aPrices)) {
  162. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  163. foreach($aPrices as $aPrice)
  164. $oLanguage->deleteLanguageString($aPrice['caption']);
  165. }
  166. return (int)$this->query("DELETE FROM `" . $CNF['TABLE_PRICES'] . "` WHERE " . $sWhereClause) > 0;
  167. }
  168. public function getLicenses($aParams, $bReturnCount = false)
  169. {
  170. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  171. $sSelectClause = "`tl`.*";
  172. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  173. switch($aParams['type']) {
  174. case 'by_order':
  175. $aMethod['name'] = 'getRow';
  176. $aMethod['params'][1] = [
  177. 'order' => $aParams['order']
  178. ];
  179. $sWhereClause .= "AND `tl`.`order`=:order";
  180. break;
  181. case 'by_license':
  182. $aMethod['name'] = 'getRow';
  183. $aMethod['params'][1] = [
  184. 'license' => $aParams['license']
  185. ];
  186. $sWhereClause .= "AND `tl`.`license`=:license";
  187. break;
  188. }
  189. $sSql = "SELECT {select} FROM `" . $this->_oConfig->CNF['TABLE_LICENSES'] . "` AS `tl` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  190. $aMethod['params'][0] = str_replace(['{select}', '{order}', '{limit}'], [$sSelectClause, $sOrderClause, $sLimitClause], $sSql);
  191. $aItems = call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  192. if(!$bReturnCount)
  193. return $aItems;
  194. $aMethod['name'] = 'getOne';
  195. $aMethod['params'][0] = str_replace(['{select}', '{order}', '{limit}'], ['COUNT(*)', '', ''], $sSql);
  196. return [
  197. 'items' => $aItems,
  198. 'count' => (int)call_user_func_array([$this, $aMethod['name']], $aMethod['params'])
  199. ];
  200. }
  201. public function insertLicense($aSet)
  202. {
  203. $sSetClause = $this->arrayToSQL($aSet);
  204. return (int)$this->query("INSERT INTO `" . $this->_oConfig->CNF['TABLE_LICENSES'] . "` SET " . $sSetClause) > 0;
  205. }
  206. public function updateLicenses($aSet, $aWhere)
  207. {
  208. $sSetClause = $this->arrayToSQL($aSet);
  209. $sWhereClause = $this->arrayToSQL($aWhere, " AND ");
  210. return (int)$this->query("UPDATE `" . $this->_oConfig->CNF['TABLE_LICENSES'] . "` SET " . $sSetClause . " WHERE " . $sWhereClause) > 0;
  211. }
  212. public function deleteLicenses($aWhere)
  213. {
  214. $sWhereClause = $this->arrayToSQL($aWhere, " AND ");
  215. return (int)$this->query("DELETE FROM `" . $this->_oConfig->CNF['TABLE_LICENSES'] . "` WHERE " . $sWhereClause) > 0;
  216. }
  217. }
  218. /** @} */