api.lousytariffs.php 8.4 KB

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