api.callmeback.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. /**
  3. * callback service implementation
  4. */
  5. class CallMeBack {
  6. /**
  7. * Some calls data model placeholder
  8. *
  9. * @var object
  10. */
  11. protected $calls = '';
  12. /**
  13. * System telepathy object placeholder
  14. *
  15. * @var object
  16. */
  17. protected $telepathy = '';
  18. /**
  19. * Contains available user address data as login=>address
  20. *
  21. * @var array
  22. */
  23. protected $allAddress = array();
  24. /**
  25. * Basic control module URL
  26. */
  27. const URL_ME = '?module=callmeback';
  28. /**
  29. * Contains user navigation URL
  30. */
  31. const URL_USERPROFILE = '?module=userprofile&username=';
  32. /**
  33. * Creates new callmeback instance
  34. */
  35. public function __construct() {
  36. /**
  37. * Through the darkness of future past
  38. * The magician longs to see.
  39. * One chanse out between two worlds
  40. * Fire walk with me
  41. */
  42. $this->initCalls();
  43. }
  44. /**
  45. * Inits calls model for further usage
  46. */
  47. protected function initCalls() {
  48. $this->calls = new NyanORM('callmeback');
  49. }
  50. /**
  51. * Inits system telepaty class
  52. */
  53. protected function initTelepathy() {
  54. $this->loadAddressData();
  55. $this->telepathy = new Telepathy(false, true, false, true);
  56. $this->telepathy->usePhones();
  57. }
  58. /**
  59. * Loads address data required for user telepathy into protected property
  60. *
  61. * @return void
  62. */
  63. protected function loadAddressData() {
  64. $this->allAddress = zb_AddressGetFulladdresslistCached();
  65. }
  66. /**
  67. * Returns array of all calls which required some reaction as id=>calldata
  68. *
  69. * @return array
  70. */
  71. protected function getUndoneCalls() {
  72. $this->calls->where('state', '=', 'undone');
  73. return ($this->calls->getAll('id'));
  74. }
  75. /**
  76. * Returns all processed calls array as id=>calldata
  77. *
  78. * @return array
  79. */
  80. protected function getDoneCalls() {
  81. $this->calls->where('state', '!=', 'undone');
  82. return ($this->calls->getAll('id'));
  83. }
  84. /**
  85. * Create some callback record in database for further employee reactions.
  86. *
  87. * @param int $number
  88. *
  89. * @return void
  90. */
  91. public function createCall($number) {
  92. $number = ubRouting::filters($number, 'int');
  93. if ($number) {
  94. $this->calls->data('date', curdatetime());
  95. $this->calls->data('number', $number);
  96. $this->calls->data('state', 'undone');
  97. $this->calls->create();
  98. }
  99. }
  100. /**
  101. * Sets call state in database
  102. *
  103. * @param int $callId
  104. * @param string $state
  105. *
  106. * @return void
  107. */
  108. public function setCallState($callId, $state) {
  109. $callId = ubRouting::filters($callId, 'int');
  110. $stateF = ubRouting::filters($state, 'mres');
  111. $this->calls->where('id', '=', $callId);
  112. $this->calls->data('state', $stateF);
  113. $this->calls->data('statedate', curdatetime());
  114. $this->calls->data('admin', whoami());
  115. $this->calls->save();
  116. log_register('CALLMEBACK SET [' . $callId . '] STATE `' . $state . '`');
  117. }
  118. /**
  119. * Sets call record user login in database
  120. *
  121. * @param int $callId
  122. * @param string $userLogin
  123. *
  124. * @return void
  125. */
  126. public function setCallUserLogin($callId, $userLogin) {
  127. $callId = ubRouting::filters($callId, 'int');
  128. $loginF = ubRouting::filters($userLogin, 'mres');
  129. if ($loginF) {
  130. $this->calls->where('id', '=', $callId);
  131. $this->calls->data('userlogin', $loginF);
  132. $this->calls->save();
  133. }
  134. }
  135. /**
  136. * Returns unprocessed calls count
  137. *
  138. * @return int
  139. */
  140. public function getUndoneCount() {
  141. $this->calls->where('state', '=', 'undone');
  142. $result = $this->calls->getFieldsCount();
  143. return ($result);
  144. }
  145. /**
  146. * Returns all processed calls count
  147. *
  148. * @return array
  149. */
  150. protected function getDoneCallsCount() {
  151. $this->calls->where('state', '!=', 'undone');
  152. return ($this->calls->getFieldsCount());
  153. }
  154. /**
  155. * Try to detect user by its mobile and returns its login
  156. *
  157. * @param string $number
  158. *
  159. * @return string
  160. */
  161. protected function getUserLoginByPhone($number) {
  162. $result = $this->telepathy->getByPhoneFast($number, true, true);
  163. return ($result);
  164. }
  165. /**
  166. * Just returns user profile link by its login
  167. *
  168. * @param string $login
  169. *
  170. * @return string
  171. */
  172. protected function getUserLinkByLogin($login = '') {
  173. $result = '';
  174. if (!empty($login)) {
  175. $result .= wf_Link(self::URL_USERPROFILE . $login, web_profile_icon() . ' ' . @$this->allAddress[$login]);
  176. } else {
  177. $result .= '-';
  178. }
  179. return ($result);
  180. }
  181. /**
  182. * Renders undone calls with some controls
  183. *
  184. * @return string
  185. */
  186. public function renderUndoneCalls() {
  187. $result = '';
  188. $this->initTelepathy();
  189. $undoneCalls = $this->getUndoneCalls();
  190. if (!empty($undoneCalls)) {
  191. $cells = wf_TableCell(__('Date'));
  192. $cells .= wf_TableCell(__('Number'));
  193. $cells .= wf_TableCell(__('User'));
  194. $cells .= wf_TableCell(__('Actions'), '50%');
  195. $rows = wf_TableRow($cells, 'row1');
  196. foreach ($undoneCalls as $io => $each) {
  197. $userLink = '';
  198. if (empty($each['userlogin'])) {
  199. $userLogin = $this->getUserLoginByPhone($each['number']);
  200. if (!empty($userLogin)) {
  201. //guessed!
  202. $userLink = $this->getUserLinkByLogin($userLogin);
  203. //updating database call record
  204. $this->setCallUserLogin($each['id'],$userLogin);
  205. }
  206. } else {
  207. $userLink = $this->getUserLinkByLogin($each['userlogin']);
  208. }
  209. $cells = wf_TableCell($each['date']);
  210. $cells .= wf_TableCell($each['number']);
  211. $cells .= wf_TableCell($userLink);
  212. $callControls = wf_Link(self::URL_ME . '&setcall=' . $each['id'] . '&state=done', wf_img('skins/calls/phone_green.png') . ' ' . __('Recalled'), false, 'ubButton') . ' ';
  213. $callControls .= wf_Link(self::URL_ME . '&setcall=' . $each['id'] . '&state=noanswer', wf_img('skins/calls/phone_red.png') . ' ' . __('No answer'), false, 'ubButton') . ' ';
  214. $callControls .= wf_Link(self::URL_ME . '&setcall=' . $each['id'] . '&state=wrongnum', wf_img('skins/calls/phone_fail.png') . ' ' . __('Wrong number'), false, 'ubButton') . ' ';
  215. $cells .= wf_TableCell($callControls);
  216. $rows .= wf_TableRow($cells, 'row5');
  217. }
  218. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  219. } else {
  220. $messages = new UbillingMessageHelper();
  221. $result .= $messages->getStyledMessage(__('Nothing to show'), 'success');
  222. }
  223. return ($result);
  224. }
  225. /**
  226. * Returns main module control panel
  227. *
  228. * @return string
  229. */
  230. public function renderPanel() {
  231. $result = '';
  232. $result .= wf_Link(self::URL_ME, wf_img('skins/undone_icon.png') . ' ' . __('Undone calls'), false, 'ubButton') . ' ';
  233. $result .= wf_Link(self::URL_ME . '&showdone=true', wf_img('skins/done_icon.png') . ' ' . __('Processed calls'), false, 'ubButton') . ' ';
  234. return ($result);
  235. }
  236. /**
  237. * Renders processed calls container
  238. *
  239. * @return string
  240. */
  241. public function renderProcessedCalls() {
  242. $result = '';
  243. $doneCalls = $this->getDoneCallsCount();
  244. if ($doneCalls > 0) {
  245. $columns = array('ID', 'Date', 'Number', 'User', 'End date', 'Admin', 'Status');
  246. $opts = '"order": [[ 0, "desc" ]]';
  247. $result .= wf_JqDtLoader($columns, self::URL_ME . '&ajaxdonecalls=true', false, 'Calls', 100, $opts);
  248. } else {
  249. $messages = new UbillingMessageHelper();
  250. $result .= $messages->getStyledMessage(__('Nothing to show'), 'warning');
  251. }
  252. return ($result);
  253. }
  254. /**
  255. * Performs formatting/localizing call state
  256. *
  257. * @param strings $state
  258. *
  259. * @return string
  260. */
  261. protected function getStateLabel($state) {
  262. $result = '';
  263. switch ($state) {
  264. case 'done':
  265. $result = wf_img('skins/calls/phone_green.png') . ' ' . __('Done');
  266. break;
  267. case 'noanswer':
  268. $result = wf_img('skins/calls/phone_red.png') . ' ' . __('No answer');
  269. break;
  270. case 'wrongnum':
  271. $result = wf_img('skins/calls/phone_fail.png') . ' ' . __('Wrong number');
  272. break;
  273. default:
  274. $result = $state;
  275. break;
  276. }
  277. return ($result);
  278. }
  279. /**
  280. * Renders processed calls JSON data
  281. *
  282. * @return void
  283. */
  284. public function getAjProcessedList() {
  285. $allCalls = $this->getDoneCalls();
  286. if (!empty($allCalls)) {
  287. $allEmployeeLogins = ts_GetAllEmployeeLoginsAssocCached();
  288. $this->initTelepathy();
  289. $json = new wf_JqDtHelper();
  290. foreach ($allCalls as $io => $each) {
  291. //TODO: take decision about following part that impacts performance
  292. // if (empty($each['userlogin'])) {
  293. // $guessedLogin=$this->getUserLoginByPhone($each['number']);
  294. // if ($guessedLogin) {
  295. // $this->setCallUserLogin($each['id'],$guessedLogin);
  296. // }
  297. // }
  298. $data[] = $each['id'];
  299. $data[] = $each['date'];
  300. $data[] = $each['number'];
  301. $data[] = $this->getUserLinkByLogin($each['userlogin']);
  302. $data[] = $each['statedate'];
  303. $employeeLabel = '';
  304. if (!empty($each['admin'])) {
  305. if (isset($allEmployeeLogins[$each['admin']])) {
  306. $employeeLabel = $allEmployeeLogins[$each['admin']];
  307. } else {
  308. $employeeLabel = $each['admin'];
  309. }
  310. }
  311. $data[] = $employeeLabel;
  312. $data[] = $this->getStateLabel($each['state']);
  313. $json->addRow($data);
  314. unset($data);
  315. }
  316. $json->getJson();
  317. }
  318. }
  319. }