api.callshistory.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * Performs view/search/display of incoming calls data received with PBXNum
  4. */
  5. class CallsHistory {
  6. /**
  7. * Contains system alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Calls log data source table
  14. *
  15. * @var string
  16. */
  17. protected $dataSource = '';
  18. /**
  19. * Contains previously loaded calls as id=>callData
  20. *
  21. * @var array
  22. */
  23. protected $allCalls = array();
  24. /**
  25. * May contains login filter for calls
  26. *
  27. * @var string
  28. */
  29. protected $loginSearch = '';
  30. /**
  31. * Contains user assigned tags as login=>usertags
  32. *
  33. * @var array
  34. */
  35. protected $userTags = array();
  36. /**
  37. * Incoming calls database abstraction layer
  38. *
  39. * @var object
  40. */
  41. protected $callsDb = '';
  42. /**
  43. * URL of user profile route
  44. */
  45. const URL_PROFILE = '?module=userprofile&username=';
  46. /**
  47. * Default module URL
  48. */
  49. const URL_ME = '?module=callshist';
  50. /**
  51. * Creates new CallsHistory instance
  52. *
  53. * @return void
  54. */
  55. public function __construct() {
  56. $this->loadConfig();
  57. $this->initDb();
  58. }
  59. /**
  60. * Sets user login to filter
  61. *
  62. * @param string $login
  63. *
  64. * @return void
  65. */
  66. public function setLogin($login = '') {
  67. $this->loginSearch = ubRouting::filters($login, 'mres');
  68. }
  69. /**
  70. * Loads required configs and sets some options
  71. *
  72. * @global object $ubillingConfig
  73. *
  74. * @return void
  75. */
  76. protected function loadConfig() {
  77. global $ubillingConfig;
  78. $this->altCfg = $ubillingConfig->getAlter();
  79. $this->dataSource = PBXNum::LOG_TABLE;
  80. }
  81. /**
  82. * Inits incoming calls database abstraction layer
  83. *
  84. * @return void
  85. */
  86. protected function initDb() {
  87. $this->callsDb = new NyanORM($this->dataSource);
  88. }
  89. /**
  90. * Loads some calls list into protected property
  91. *
  92. * @return void
  93. */
  94. protected function loadCalls() {
  95. if (!empty($this->loginSearch)) {
  96. //login search with full date range
  97. $this->callsDb->where('login', '=', $this->loginSearch);
  98. } else {
  99. //or just for current year
  100. $this->callsDb->where('date', 'LIKE', curyear() . '-%');
  101. }
  102. $this->allCalls = $this->callsDb->getAll('id');
  103. }
  104. /**
  105. * Loads existing tagtypes and usertags into protected props for further usage
  106. *
  107. * @return void
  108. */
  109. protected function loadUserTags() {
  110. $this->userTags = zb_UserGetAllTags();
  111. }
  112. /**
  113. * Renders user tags if available
  114. *
  115. * @param string $userLogin
  116. *
  117. * @return string
  118. */
  119. protected function renderUserTags($userLogin) {
  120. $result = '';
  121. if (!empty($userLogin)) {
  122. if (isset($this->userTags[$userLogin])) {
  123. if (!empty($this->userTags[$userLogin])) {
  124. $result .= implode(', ', $this->userTags[$userLogin]);
  125. }
  126. }
  127. }
  128. return ($result);
  129. }
  130. /**
  131. * Renders calls log container
  132. *
  133. * @return string
  134. */
  135. public function renderCalls() {
  136. $result = '';
  137. $columns = array('Date', 'Number', 'User', 'Tariff', 'Tags');
  138. $opts = '"order": [[ 0, "desc" ]]';
  139. $loginFilter = (!empty($this->loginSearch)) ? '&username=' . $this->loginSearch : '';
  140. $result .= wf_JqDtLoader($columns, self::URL_ME . '&ajaxcalls=true' . $loginFilter, false, __('Calls'), 100, $opts);
  141. return ($result);
  142. }
  143. /**
  144. * Renders ajax data source with loaded calls history
  145. *
  146. * @return void
  147. */
  148. public function renderCallsAjaxList() {
  149. //loading some data
  150. $this->loadCalls();
  151. $this->loadUserTags();
  152. $allUserData = zb_UserGetAllDataCache();
  153. $json = new wf_JqDtHelper();
  154. $directionIcon = wf_img('skins/calls/incoming.png'); //thinking about future
  155. if (!empty($this->allCalls)) {
  156. foreach ($this->allCalls as $io => $each) {
  157. if (!empty($each['login'])) {
  158. $userRealName = @$allUserData[$each['login']]['realname'];
  159. $userTariff = @$allUserData[$each['login']]['Tariff'];
  160. $userLink = wf_Link(self::URL_PROFILE . $each['login'], web_profile_icon() . ' ' . @$allUserData[$each['login']]['fulladress']) . ' ' . $userRealName;
  161. $userTags = $this->renderUserTags($each['login']);
  162. } else {
  163. $userLink = '';
  164. $userRealName = '';
  165. $userTariff = '';
  166. $userTags = '';
  167. }
  168. $data[] = $directionIcon . ' ' . $each['date'];
  169. $data[] = $each['number'];
  170. $data[] = $userLink;
  171. $data[] = $userTariff;
  172. $data[] = $userTags;
  173. $json->addRow($data);
  174. unset($data);
  175. }
  176. }
  177. $json->getJson();
  178. }
  179. /**
  180. * Updates data for calls without previously guessed user login
  181. *
  182. * @param bool $rawResult
  183. *
  184. * @return string|array
  185. */
  186. public function updateUnknownLogins($rawResult = false) {
  187. set_time_limit(0);
  188. $messages = new UbillingMessageHelper();
  189. $this->loadCalls();
  190. $telepathy = new Telepathy(false, true, false, true);
  191. $telepathy->usePhones();
  192. $result = '';
  193. $countGuessed = 0;
  194. $countMissed = 0;
  195. if (!empty($this->allCalls)) {
  196. foreach ($this->allCalls as $io => $each) {
  197. //user unknown
  198. if (empty($each['login'])) {
  199. $detectedLogin = $telepathy->getByPhone($each['number'], true, true);
  200. if (!empty($detectedLogin)) {
  201. $this->callsDb->data('login', $detectedLogin);
  202. $this->callsDb->where('id', '=', $each['id']);
  203. $this->callsDb->save();
  204. $notification = $each['date'] . ' ' . $each['number'] . ' ' . __('Assigned') . ' ' . $detectedLogin;
  205. $result .= $messages->getStyledMessage($notification, 'success');
  206. $countGuessed++;
  207. } else {
  208. $countMissed++;
  209. }
  210. }
  211. }
  212. }
  213. if ($rawResult) {
  214. $result = array(
  215. 'GUESSED' => $countGuessed,
  216. 'MISSED' => $countMissed,
  217. );
  218. } else {
  219. $result .= $messages->getStyledMessage(__('telepathically guessed') . ': ' . $countGuessed, 'info');
  220. $result .= $messages->getStyledMessage(__('skipped') . ': ' . $countMissed, 'warning');
  221. }
  222. //some logging, why not?
  223. log_register('CALLSHIST USERS UPDATE GUESSED `' . $countGuessed . '` MISSED`' . $countMissed . '`');
  224. return ($result);
  225. }
  226. }