api.smshistory.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /**
  3. I ain't happy, I'm feeling glad
  4. I got sunshine in a bag
  5. I'm useless, but not for long
  6. The future is coming on
  7. */
  8. class SMSHistory {
  9. const URL_ME = '?module=smshistory';
  10. protected $smsAdvancedEnabled = false;
  11. public function __construct() {
  12. global $ubillingConfig;
  13. $this->smsAdvancedEnabled = $ubillingConfig->getAlterParam('SMS_SERVICES_ADVANCED_ENABLED');
  14. }
  15. /**
  16. * Gets sms history data from DB
  17. *
  18. * @param string $WHEREString
  19. *
  20. * @return array
  21. */
  22. public function getSMSHistoryData($WHEREString = '') {
  23. if (empty($WHEREString)) {
  24. $WHEREString = "WHERE DATE(`date_send`) = CURDATE()";
  25. }
  26. $tQuery = "SELECT * FROM `sms_history` " . $WHEREString . " ;";
  27. $Result = simple_queryall($tQuery);
  28. return $Result;
  29. }
  30. /**
  31. * Renders JSON for JQDT
  32. *
  33. * @param $QueryData
  34. */
  35. public function renderJSON($QueryData) {
  36. $json = new wf_JqDtHelper();
  37. if ($this->smsAdvancedEnabled) {
  38. $smsDirections = new SMSDirections();
  39. }
  40. if (!empty($QueryData)) {
  41. $allAddresses = zb_AddressGetFulladdresslistCached();
  42. $data = array();
  43. foreach ($QueryData as $EachRec) {
  44. foreach ($EachRec as $FieldName => $FieldVal) {
  45. switch ($FieldName) {
  46. case 'smssrvid':
  47. if ($this->smsAdvancedEnabled) {
  48. $SMSSrvName = $smsDirections->getDirectionNameById($FieldVal);
  49. if (!empty($SMSSrvName)) {
  50. $data[] = (empty($FieldVal)) ? $SMSSrvName . ' (' . __('by default') . ')' : $SMSSrvName;
  51. } else {
  52. $data[] = __('ID not found');
  53. }
  54. }
  55. break;
  56. case 'login':
  57. if (empty($FieldVal)) {
  58. $data[] = '';
  59. } else {
  60. $usrAddress = (empty($allAddresses[$FieldVal])) ? '' : $allAddresses[$FieldVal];
  61. $data[] = wf_Link('?module=userprofile&username=' . $FieldVal, web_profile_icon() . ' ' . $FieldVal, false, '', 'style="color:#341e19"')
  62. . wf_delimiter(0) . $usrAddress;
  63. }
  64. break;
  65. case 'delivered':
  66. case 'no_statuschk':
  67. $data[] = ($FieldVal == 1) ? __('Yes') : __('No');
  68. break;
  69. default:
  70. $data[] = $FieldVal;
  71. }
  72. }
  73. $json->addRow($data);
  74. unset($data);
  75. }
  76. }
  77. $json->getJson();
  78. }
  79. /**
  80. * Renders JQDT and returns it
  81. *
  82. * @return string
  83. */
  84. public function renderJQDT($UserLogin = '') {
  85. $AjaxURLStr = ( empty($UserLogin) ) ? '' . self::URL_ME . '&ajax=true' . '' : '' . self::URL_ME . '&ajax=true&usrlogin=' . $UserLogin . '';
  86. $columns = array();
  87. if ($this->smsAdvancedEnabled) {
  88. $columnTargets = (empty($UserLogin)) ? '[0, 4, 5, 9]' : '[0, 2, 4, 5, 9]';
  89. $CheckCol1 = '8';
  90. $CheckCol2 = '9';
  91. } else {
  92. $columnTargets = (empty($UserLogin)) ? '[0, 3, 4, 8]' : '[0, 1, 3, 4, 8]';
  93. $CheckCol1 = '7';
  94. $CheckCol2 = '8';
  95. }
  96. $opts = '"order": [[ 0, "desc" ]],
  97. "columnDefs": [ {"targets": ' . $columnTargets . ', "visible": false},
  98. {"targets": [1], "width": "90px"},
  99. {"targets": [3], "width": "85px"},
  100. {"targets": [6, 7], "width": "100px"},
  101. {"targets": [' . $CheckCol1 . ', ' . $CheckCol2 . '], "className": "dt-center"}
  102. ],
  103. "rowCallback": function(row, data, index) {
  104. if ( data[' . $CheckCol1 . '] == "' . __('No') . '" && data[' . $CheckCol2 . '] == "' . __('Yes') . '" ) {
  105. $(\'td\', row).css(\'background-color\', \'red\');
  106. $(\'td\', row).css(\'color\', \'#FFFF44\');
  107. $(\'td\', row).css(\'opacity\', \'0.8\');
  108. }
  109. if ( data[' . $CheckCol1 . '] == "' . __('Yes') . '" && data[' . $CheckCol2 . '] == "' . __('Yes') . '") {
  110. $(\'td\', row).css(\'background-color\', \'#228B22\');
  111. $(\'td\', row).css(\'color\', \'white\');
  112. $(\'td\', row).css(\'opacity\', \'0.8\');
  113. }
  114. if ( data[' . $CheckCol1 . '] == "' . __('No') . '" && data[' . $CheckCol2 . '] == "' . __('No') . '") {
  115. $(\'td\', row).css(\'background-color\', \'#fffc5e\');
  116. $(\'td\', row).css(\'color\', \'#4800FF\');
  117. $(\'td\', row).css(\'opacity\', \'0.8\');
  118. }
  119. }
  120. ';
  121. $columns[] = ('ID');
  122. if ($this->smsAdvancedEnabled) {
  123. $columns[] = __('SMS service');
  124. }
  125. $columns[] = ('Login');
  126. $columns[] = __('Phone');
  127. $columns[] = __('Service message ID');
  128. $columns[] = __('Service packet ID');
  129. $columns[] = __('Send date');
  130. $columns[] = __('Status check date');
  131. $columns[] = __('Delivered');
  132. $columns[] = __('No status check');
  133. $columns[] = __('Send status');
  134. $columns[] = __('Message text');
  135. return ( wf_JqDtLoader($columns, $AjaxURLStr, false, __('results'), 100, $opts) );
  136. }
  137. /**
  138. * Renders and returns controls for sms history web form
  139. *
  140. * @return string
  141. */
  142. public function renderControls() {
  143. $AjaxURLStr = '' . self::URL_ME . '&ajax=true' . '';
  144. $JQDTID = 'jqdt_' . md5($AjaxURLStr);
  145. $QickSelID = wf_InputId();
  146. $DateFromID = wf_InputId();
  147. $DateToID = wf_InputId();
  148. $ButtonID = wf_InputId();
  149. $StatusSelID = wf_InputId();
  150. $Today = curdate();
  151. $Yesterday = $this->getDateDiff(curdate(), 'P1D');
  152. $WeekAgo = $this->getDateDiff(curdate(), 'P1W');
  153. $MonthAgo = $this->getDateDiff(curdate(), 'P1M');
  154. /* $DateFromPreset = ( wf_CheckGet(array('smshistdatefrom')) ) ? $_GET['smshistdatefrom'] : curdate();
  155. $DateToPreset = ( wf_CheckGet(array('smshistdateto')) ) ? $_GET['smshistdateto'] : curdate(); */
  156. $DateFromPreset = $Today;
  157. $DateToPreset = $Today;
  158. $QuickFilterPreset = array($Today => __('Today'),
  159. $Yesterday => __('Yesterday'),
  160. $WeekAgo => __('Week ago'),
  161. $MonthAgo => __('Month ago')
  162. );
  163. $StatusFilterPreset = array('all' => __('All'),
  164. 'delivered' => __('Delivered'),
  165. 'undelivered' => __('Not delivered'),
  166. 'unknown' => __('Undefined')
  167. );
  168. $inputs = wf_tag('h3', false);
  169. $inputs .= __('Show columns:');
  170. $inputs .= wf_tag('h3', true);
  171. $cells = wf_TableCell($inputs);
  172. $inputs = wf_tag('h3', false);
  173. $inputs .= __('Filter by:');
  174. $inputs .= wf_tag('h3', true);
  175. $cells .= wf_TableCell($inputs, '', '', 'colspan="2"');
  176. $rows = wf_TableRow($cells);
  177. $inputs = wf_CheckInput('showdbidclmn', __('Inner DB ID'), true, false, '__showdbidclmn');
  178. $cells = wf_TableCell($inputs);
  179. $inputs = wf_tag('font', false, '', '');
  180. $inputs .= __('Quick filter for:');
  181. $inputs .= wf_tag('font', true);
  182. $cells .= wf_TableCell($inputs);
  183. $inputs = wf_Selector('quickfilter', $QuickFilterPreset, '', $Today, true, false, $QickSelID);
  184. $cells .= wf_TableCell($inputs);
  185. $rows .= wf_TableRow($cells);
  186. $inputs = wf_CheckInput('showselfidclmn', __('Service message ID'), true, false, '__showselfidclmn');
  187. $cells = wf_TableCell($inputs);
  188. $inputs = wf_tag('font', false, '', '');
  189. $inputs .= __('Send date from:');
  190. $inputs .= wf_tag('font', true);
  191. $cells .= wf_TableCell($inputs);
  192. $inputs = wf_DatePickerPreset('smshistdatefrom', $DateFromPreset, false, $DateFromID);
  193. $cells .= wf_TableCell($inputs);
  194. $rows .= wf_TableRow($cells);
  195. $inputs = wf_CheckInput('showpackidclmn', __('Service packet ID'), true, false, '__showpackidclmn');
  196. $cells = wf_TableCell($inputs);
  197. $inputs = wf_tag('font', false, '', '');
  198. $inputs .= __('Send date to:');
  199. $inputs .= wf_tag('font', true);
  200. $cells .= wf_TableCell($inputs);
  201. $inputs = wf_DatePickerPreset('smshistdateto', $DateToPreset, false, $DateToID);
  202. $cells .= wf_TableCell($inputs);
  203. $rows .= wf_TableRow($cells);
  204. $inputs = wf_CheckInput('shownostatuschkclmn', __('No status check'), true, false, '__shownostatuschkclmn');
  205. $cells = wf_TableCell($inputs);
  206. $inputs = wf_tag('font', false, '', '');
  207. $inputs .= __('Message status');
  208. $inputs .= wf_tag('font', true);
  209. $cells .= wf_TableCell($inputs);
  210. $inputs = wf_Selector('statusfilter', $StatusFilterPreset, '', 'all', true, false, $StatusSelID);
  211. $cells .= wf_TableCell($inputs);
  212. $rows .= wf_TableRow($cells);
  213. $cells = wf_TableCell('');
  214. $inputs = wf_tag('a', false, 'ubButton', 'style="width:100%; cursor:pointer;" id="' . $ButtonID . '"');
  215. $inputs .= __('Show');
  216. $inputs .= wf_tag('a', true);
  217. $cells .= wf_TableCell($inputs, '', '', 'colspan="2" align="center"');
  218. $rows .= wf_TableRow($cells);
  219. $table = wf_TableBody($rows, '60%', '0', '', '');
  220. $inputs = wf_Plate($table, '98%', '170px', 'glamour');
  221. $inputs .= wf_CleanDiv() . wf_delimiter();
  222. $inputs .= wf_tag('script', false, '', 'type="text/javascript"');
  223. $inputs .= wf_JQDTColumnHideShow('__showdbidclmn', 'change', $JQDTID, 0);
  224. $inputs .= wf_JQDTColumnHideShow('__showselfidclmn', 'change', $JQDTID, ($this->smsAdvancedEnabled) ? 4 : 3);
  225. $inputs .= wf_JQDTColumnHideShow('__showpackidclmn', 'change', $JQDTID, ($this->smsAdvancedEnabled) ? 5 : 4);
  226. $inputs .= wf_JQDTColumnHideShow('__shownostatuschkclmn', 'change', $JQDTID, ($this->smsAdvancedEnabled) ? 9 : 8);
  227. $inputs .= '$(\'#' . $QickSelID . '\').on("change", function() {
  228. $(\'#' . $DateFromID . '\').datepicker("setDate", $(\'#' . $QickSelID . '\').val());
  229. if ( $(\'#' . $QickSelID . ' option:selected\').text() == \'' . __('Yesterday') . '\' ) {
  230. $(\'#' . $DateToID . '\').datepicker("setDate", $(\'#' . $QickSelID . '\').val());
  231. } else {
  232. $(\'#' . $DateToID . '\').datepicker("setDate", "' . $Today . '");
  233. }
  234. });
  235. $(\'#' . $ButtonID . '\').on("click", function(evt) {
  236. evt.preventDefault();
  237. var FromDate = $(\'#' . $DateFromID . '\').val();
  238. var ToDate = $(\'#' . $DateToID . '\').val();
  239. var SelStatus = $(\'#' . $StatusSelID . '\').val();
  240. $(\'#' . $JQDTID . '\').DataTable().ajax.url(\'' . $AjaxURLStr . '&smshistdatefrom="\'+FromDate+\'"&smshistdateto="\'+ToDate+\'"&msgstatus=\'+SelStatus).load();
  241. });
  242. ';
  243. $inputs .= wf_tag('script', true);
  244. return $inputs;
  245. }
  246. /**
  247. * Returns difference between 2 dates
  248. *
  249. * Maybe should be placed in api.astral or api.compat or somewhere elsewhere?
  250. *
  251. * @param $DateFrom - date to count from in compatible with DateTime object format
  252. * @param $SubtractVal - subtract value in compatible with DateInterval object format
  253. *
  254. * @return string
  255. */
  256. protected function getDateDiff($DateFrom, $SubtractVal) {
  257. $DateObj = new DateTime($DateFrom);
  258. $DateObj->sub(new DateInterval($SubtractVal));
  259. return $DateObj->format('Y-m-d');
  260. }
  261. }
  262. ?>