api.stealthtariffs.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * Administrator restricted stealth tariffs implementeation
  4. */
  5. class StealthTariffs {
  6. /**
  7. * Contains array of all available stealth tariffs as tariffName=>stealthData
  8. *
  9. * @var array
  10. */
  11. protected $allStealthTariffs = array();
  12. /**
  13. * Contains array of all available system tariffs as tariffName=>tariffData
  14. *
  15. * @var array
  16. */
  17. protected $allTariffs = array();
  18. /**
  19. * Stealth tariffs database abstraction layer placeholder
  20. *
  21. * @var object
  22. */
  23. protected $stealthDb = '';
  24. /**
  25. * System messages helper instance placeholder
  26. *
  27. * @var object
  28. */
  29. protected $messages = '';
  30. //some other predefined stuff
  31. const TABLE_STEALTH = 'stealthtariffs';
  32. const RIGHT_STEALTH = 'STEALTHTARIFFS';
  33. const RIGHT_CONFIG = 'STEALTHTARIFFSCFG';
  34. const URL_ME = '?module=stealthtariffs';
  35. const ROUTE_DELETE = 'deletestealthtariff';
  36. const PROUTE_CREATE = 'createstealthtariff';
  37. public function __construct() {
  38. $this->initMessages();
  39. $this->initStealthDb();
  40. $this->loadStealtTariffs();
  41. }
  42. /**
  43. * Inits message helper instance
  44. *
  45. * @return void
  46. */
  47. protected function initMessages() {
  48. $this->messages = new UbillingMessageHelper();
  49. }
  50. /**
  51. * Inits database abstraction layer object for further usage
  52. *
  53. * @return void
  54. */
  55. protected function initStealthDb() {
  56. $this->stealthDb = new NyanORM(self::TABLE_STEALTH);
  57. }
  58. /**
  59. * Loads available stealth tariffs data from database
  60. *
  61. * @return void
  62. */
  63. protected function loadStealtTariffs() {
  64. $this->allStealthTariffs = $this->stealthDb->getAll('tariff');
  65. }
  66. /**
  67. * Loads existing tariffs. Required only for cfg iface.
  68. *
  69. * @return void
  70. */
  71. protected function loadAllTariffs() {
  72. $this->allTariffs = zb_TariffGetAllData();
  73. }
  74. /**
  75. * Checks is tariff marked as stealth?
  76. *
  77. * @param string $tariffName
  78. *
  79. * @return bool
  80. */
  81. protected function isStealth($tariffName) {
  82. $result = false;
  83. if (isset($this->allStealthTariffs[$tariffName])) {
  84. $result = true;
  85. }
  86. return($result);
  87. }
  88. /**
  89. * Checks is tariff not marked as stealth?
  90. *
  91. * @param string $tariffName
  92. *
  93. * @return bool
  94. */
  95. protected function isNotStealth($tariffName) {
  96. $result = true;
  97. if (isset($this->allStealthTariffs[$tariffName])) {
  98. $result = false;
  99. }
  100. return($result);
  101. }
  102. /**
  103. * Creates new stealth-tariff
  104. *
  105. * @param string $tariffName
  106. *
  107. * @return void/string on error
  108. */
  109. public function create($tariffName) {
  110. $result = '';
  111. //preloading existing tariffs
  112. $this->loadAllTariffs();
  113. $tariffNameF = ubRouting::filters($tariffName, 'mres');
  114. if (isset($this->allTariffs[$tariffName])) {
  115. if ($this->isNotStealth($tariffName)) {
  116. $this->stealthDb->data('tariff', $tariffNameF);
  117. $this->stealthDb->create();
  118. log_register('STEALTHTARIFFS CREATE `' . $tariffName . '`');
  119. } else {
  120. $result .= __('Tariff') . ' `' . $tariffName . '` ' . __('Already stealth');
  121. }
  122. } else {
  123. $result .= __('Strange exception') . ': ' . __('Tariff') . ' `' . $tariffName . '` ' . __('Not exists');
  124. }
  125. return($result);
  126. }
  127. /**
  128. * Deletes existing stealth tariff
  129. *
  130. * @param string $tariffName
  131. *
  132. * @return void/string on error
  133. */
  134. public function delete($tariffName) {
  135. $result = '';
  136. $tariffNameF = ubRouting::filters($tariffName, 'mres');
  137. if ($this->isStealth($tariffName)) {
  138. $this->stealthDb->where('tariff', '=', $tariffNameF);
  139. $this->stealthDb->delete();
  140. log_register('STEALTHTARIFFS DELETE `' . $tariffName . '`');
  141. } else {
  142. $result .= __('Stealth tariffs') . ' `' . $tariffName . '` ' . __('Not exists');
  143. }
  144. return($result);
  145. }
  146. /**
  147. * Flushes existing stealth tariff on system tariff deletion
  148. *
  149. * @param string $tariffName
  150. *
  151. * @return void
  152. */
  153. public function flush($tariffName) {
  154. $result = '';
  155. $tariffNameF = ubRouting::filters($tariffName, 'mres');
  156. $this->stealthDb->where('tariff', '=', $tariffNameF);
  157. $this->stealthDb->delete();
  158. log_register('STEALTHTARIFFS FLUSH `' . $tariffName . '`');
  159. }
  160. /**
  161. * Returns array copy without stealth tariffs
  162. *
  163. * @param array $tariffsArr
  164. *
  165. * @return array
  166. */
  167. public function truncateStealth($tariffsArr) {
  168. $result = array();
  169. if (!empty($tariffsArr)) {
  170. foreach ($tariffsArr as $eachTariff => $eachData) {
  171. if ($this->isNotStealth($eachTariff)) {
  172. $result[$eachTariff] = $eachData;
  173. }
  174. }
  175. }
  176. return($result);
  177. }
  178. /**
  179. * Renders new stealth-tariff creation form
  180. *
  181. * @return string
  182. */
  183. protected function renderCreateForm() {
  184. $result = '';
  185. if (!empty($this->allTariffs)) {
  186. $params = array();
  187. foreach ($this->allTariffs as $eachTariffName => $tariffData) {
  188. //excluding already stealth tariffs
  189. if (!$this->isStealth($eachTariffName)) {
  190. $params[$eachTariffName] = $eachTariffName;
  191. }
  192. }
  193. //
  194. // \ /
  195. // _\/\/_
  196. // _____/_//\\_\_____
  197. // 1981-2006
  198. //
  199. if (!empty($params)) {
  200. $inputs = wf_Selector(self::PROUTE_CREATE, $params, __('Tariff'), '', false) . ' ';
  201. $inputs .= wf_Submit(__('Mark this tariff as stealth'));
  202. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  203. } else {
  204. $result .= $this->messages->getStyledMessage(__('All tariffs marked as stealth'), 'info');
  205. }
  206. } else {
  207. $result .= $this->messages->getStyledMessage(__('Any tariffs available'), 'warning');
  208. }
  209. return($result);
  210. }
  211. /**
  212. * Renders existing stealth tariff deletion form
  213. *
  214. * @param string $tariffName
  215. *
  216. * @return string
  217. */
  218. protected function renderDeleteForm($tariffName) {
  219. $result = '';
  220. if ($this->isStealth($tariffName)) {
  221. $deleteUrl = self::URL_ME . '&' . self::ROUTE_DELETE . '=' . $tariffName;
  222. $cancelUrl = self::URL_ME;
  223. $control = web_delete_icon();
  224. $customTitle = __('Delete') . ' ' . $tariffName . '?';
  225. $label = $this->messages->getDeleteAlert();
  226. $result .= wf_ConfirmDialog($deleteUrl, $control, $label, '', $cancelUrl, $customTitle);
  227. }
  228. return($result);
  229. }
  230. /**
  231. * Renders list of available stealth tariffs
  232. *
  233. * @return string
  234. */
  235. public function renderList() {
  236. $result = '';
  237. //preloading system data required in future
  238. $this->loadAllTariffs();
  239. if (!empty($this->allStealthTariffs)) {
  240. $cells = wf_TableCell(__('Tariff'));
  241. $cells .= wf_TableCell(__('Fee'));
  242. $cells .= wf_TableCell(__('Actions'));
  243. $rows = wf_TableRow($cells, 'row1');
  244. foreach ($this->allStealthTariffs as $io => $each) {
  245. if (isset($this->allTariffs[$each['tariff']])) {
  246. $tariffFee = $this->allTariffs[$each['tariff']]['Fee'];
  247. $rowClass = 'row5';
  248. } else {
  249. $tariffFee = __('Deleted');
  250. $rowClass = 'sigdeleteduser';
  251. }
  252. $cells = wf_TableCell($each['tariff']);
  253. $cells .= wf_TableCell($tariffFee);
  254. $cells .= wf_TableCell($this->renderDeleteForm($each['tariff']));
  255. $rows .= wf_TableRow($cells, $rowClass);
  256. }
  257. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  258. } else {
  259. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'info');
  260. }
  261. $result .= wf_delimiter();
  262. $result .= $this->renderCreateForm();
  263. return($result);
  264. }
  265. }