api.mobilesext.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. /**
  3. * Additional users mobile numbers basic class
  4. */
  5. class MobilesExt {
  6. /**
  7. * Contains system alter.ini config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains all additiona mobile numbers as id=>data
  14. *
  15. * @var array
  16. */
  17. protected $allMobiles = array();
  18. /**
  19. * Additional mobiles database abstraction layer here.
  20. *
  21. * @var object
  22. */
  23. protected $mobilesDb = '';
  24. /**
  25. * System message helper object placeholder
  26. *
  27. * @var obejct
  28. */
  29. protected $messages = '';
  30. /**
  31. * Some predefined stuff such as routes, URLs, etc..
  32. */
  33. const URL_ME = '?module=mobileedit';
  34. const TABLE_MOBILES = 'mobileext';
  35. const ROUTE_LOGIN = 'username';
  36. const ROUTE_DELETE_ID = 'deletemobileextid';
  37. const PROUTE_NEW_LOGIN = 'newmobileextlogin';
  38. const PROUTE_NEW_NUMBER = 'newmobileextnumber';
  39. const PROUTE_NEW_NOTES = 'newmobileextnotes';
  40. const PROUTE_ED_ID = 'editmobileextid';
  41. const PROUTE_ED_NUMBER = 'editmobileextnumber';
  42. const PROUTE_ED_NOTES = 'editmobileextnotes';
  43. /**
  44. * Creates new MobilesExt instance
  45. *
  46. * @return void
  47. */
  48. public function __construct() {
  49. /**
  50. * Шива-Шиво, чому так паршиво?
  51. * Чому, повинні все це бачити на живо?
  52. */
  53. $this->initMessages();
  54. $this->loadAlter();
  55. $this->initDb();
  56. $this->loadAllMobiles();
  57. }
  58. /**
  59. * Inits system messages helper object for further usage
  60. *
  61. * @return void
  62. */
  63. protected function initMessages() {
  64. $this->messages = new UbillingMessageHelper();
  65. }
  66. /**
  67. * Loads system alter config
  68. *
  69. * @global object $ubillingConfig
  70. *
  71. * @return void
  72. */
  73. protected function loadAlter() {
  74. global $ubillingConfig;
  75. $this->altCfg = $ubillingConfig->getAlter();
  76. }
  77. /**
  78. * Inits database abstraction layer for further usage.
  79. *
  80. * @return void
  81. */
  82. protected function initDb() {
  83. $this->mobilesDb = new NyanORM(self::TABLE_MOBILES);
  84. }
  85. /**
  86. * Loads all additional mobiles data from database
  87. *
  88. * @return void
  89. */
  90. protected function loadAllMobiles() {
  91. $this->allMobiles = $this->mobilesDb->getAll('id');
  92. }
  93. /**
  94. * Returns filtered array for some user phones as id => data or as login => array_of_mobiles
  95. *
  96. * @param string $login
  97. * @param bool $loginAsKey
  98. *
  99. * @return array
  100. */
  101. public function getUserMobiles($login, $loginAsKey = false) {
  102. $result = array();
  103. if (!empty($login)) {
  104. if (!empty($this->allMobiles)) {
  105. foreach ($this->allMobiles as $io => $each) {
  106. if ($each['login'] == $login) {
  107. if ($loginAsKey) {
  108. $result[$login][] = $each['mobile'];
  109. } else {
  110. $result[$each['id']] = $each;
  111. }
  112. }
  113. }
  114. }
  115. }
  116. return ($result);
  117. }
  118. /**
  119. * Creates new additional mobile for some user
  120. *
  121. * @param string $login
  122. * @param string $mobile
  123. * @param string $notes
  124. *
  125. * @return int
  126. */
  127. public function createUserMobile($login, $mobile, $notes = '') {
  128. $result = '';
  129. if ((!empty($login)) AND ( !empty($mobile))) {
  130. $this->mobilesDb->data('login', ubRouting::filters($login, 'mres'));
  131. $this->mobilesDb->data('mobile', ubRouting::filters($mobile, 'mres'));
  132. $this->mobilesDb->data('notes', ubRouting::filters($notes, 'mres'));
  133. $this->mobilesDb->create();
  134. $result = $this->mobilesDb->getLastId();
  135. log_register('MOBILEEXT CREATE (' . $login . ') MOBILE `' . $mobile . '` [' . $result . ']');
  136. }
  137. return ($result);
  138. }
  139. /**
  140. * Deletes some additional mobile record from database by its ID
  141. *
  142. * @param int $mobileId
  143. *
  144. * @return void
  145. */
  146. public function deleteUserMobile($mobileId) {
  147. $mobileId = ubRouting::filters($mobileId, 'int');
  148. if (isset($this->allMobiles[$mobileId])) {
  149. $mobileData = $this->allMobiles[$mobileId];
  150. $this->mobilesDb->where('id', '=', $mobileId);
  151. $this->mobilesDb->delete();
  152. log_register('MOBILEEXT DELETE (' . $mobileData['login'] . ') MOBILE `' . $mobileData['mobile'] . '` [' . $mobileId . ']');
  153. }
  154. }
  155. /**
  156. * Changes additional mobile database records if required
  157. *
  158. * @param int $mobileId
  159. * @param string $mobile
  160. * @param string $notes
  161. *
  162. * @return void
  163. */
  164. public function updateUserMobile($mobileId, $mobile, $notes = '') {
  165. $mobileId = ubRouting::filters($mobileId, 'int');
  166. if (isset($this->allMobiles[$mobileId])) {
  167. $mobileData = $this->allMobiles[$mobileId];
  168. $somethingChanged = false;
  169. if ((!empty($mobile)) AND ( $mobileData['mobile'] != $mobile)) {
  170. $somethingChanged = true;
  171. $this->mobilesDb->data('mobile', ubRouting::filters($mobile, 'mres'));
  172. log_register('MOBILEEXT CHANGE (' . $mobileData['login'] . ') MOBILE ON `' . $mobile . '` [' . $mobileId . ']');
  173. }
  174. if ($mobileData['notes'] != $notes) {
  175. $somethingChanged = true;
  176. $this->mobilesDb->data('notes', ubRouting::filters($notes, 'mres'));
  177. log_register('MOBILEEXT CHANGE (' . $mobileData['login'] . ') NOTES');
  178. }
  179. //push changes to DB
  180. if ($somethingChanged) {
  181. $this->mobilesDb->where('id', '=', $mobileId);
  182. $this->mobilesDb->save();
  183. }
  184. }
  185. }
  186. /**
  187. * Renders create form for some user
  188. *
  189. * @return string
  190. */
  191. public function renderCreateForm($login) {
  192. $result = '';
  193. if (!empty($login)) {
  194. $formFilter = (@$this->altCfg['MOBILE_FILTERS_DISABLED']) ? '' : 'mobile';
  195. $inputs = wf_HiddenInput(self::PROUTE_NEW_LOGIN, $login);
  196. $inputs .= wf_TextInput(self::PROUTE_NEW_NUMBER, __('New mobile'), '', false, '20', $formFilter);
  197. $inputs .= wf_TextInput(self::PROUTE_NEW_NOTES, __('New notes'), '', false, '40');
  198. $inputs .= wf_Submit(__('Create'));
  199. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  200. $result .= wf_CleanDiv();
  201. }
  202. return ($result);
  203. }
  204. /**
  205. * Renders additional mobile edit form
  206. *
  207. * @param int $mobileId
  208. *
  209. * @return string
  210. */
  211. protected function renderEditForm($mobileId) {
  212. $result = '';
  213. $mobileId = vf($mobileId, 3);
  214. if (isset($this->allMobiles[$mobileId])) {
  215. $formFilter = (@$this->altCfg['MOBILE_FILTERS_DISABLED']) ? '' : 'mobile';
  216. $mobileData = $this->allMobiles[$mobileId];
  217. $inputs = wf_HiddenInput(self::PROUTE_ED_ID, $mobileId);
  218. $inputs .= wf_TextInput(self::PROUTE_ED_NUMBER, __('Mobile'), $mobileData['mobile'], true, '20', $formFilter);
  219. $inputs .= wf_TextInput(self::PROUTE_ED_NOTES, __('Notes'), $mobileData['notes'], true, '40');
  220. $inputs .= wf_Submit(__('Save'));
  221. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  222. $result .= wf_CleanDiv();
  223. }
  224. return ($result);
  225. }
  226. /**
  227. * Returns list of all user additional mobiles with required controls
  228. *
  229. * @param string $login
  230. *
  231. * @return string
  232. */
  233. public function renderUserMobilesList($login) {
  234. $result = '';
  235. $userMobiles = $this->getUserMobiles($login);
  236. if (!empty($userMobiles)) {
  237. $cells = wf_TableCell(__('Mobile'));
  238. $cells .= wf_TableCell(__('Notes'));
  239. $cells .= wf_TableCell(__('Actions'));
  240. $rows = wf_TableRow($cells, 'row1');
  241. foreach ($userMobiles as $io => $each) {
  242. $cells = wf_TableCell($each['mobile']);
  243. $cells .= wf_TableCell($each['notes']);
  244. $deleteUrl = self::URL_ME . '&' . self::ROUTE_LOGIN . '=' . $login . '&' . self::ROUTE_DELETE_ID . '=' . $each['id'];
  245. $cancelUrl = self::URL_ME . '&' . self::ROUTE_LOGIN . '=' . $login;
  246. $dialogTitle = __('Delete') . ' ' . __('Additional mobile') . '?';
  247. $alertLabel = __('Delete') . ' ' . __('Additional mobile') . ' ' . $each['mobile'] . '? ' . $this->messages->getDeleteAlert();
  248. $actLinks = wf_ConfirmDialog($deleteUrl, web_delete_icon(), $alertLabel, '', $cancelUrl, $dialogTitle);
  249. $actLinks .= wf_modalAuto(web_edit_icon(), __('Edit') . ' ' . $each['mobile'], $this->renderEditForm($each['id']));
  250. $cells .= wf_TableCell($actLinks);
  251. $rows .= wf_TableRow($cells, 'row3');
  252. }
  253. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  254. }
  255. return ($result);
  256. }
  257. /**
  258. * Returns all available additional mobiles data as id=>data
  259. *
  260. * @return array
  261. */
  262. public function getAllMobiles() {
  263. return ($this->allMobiles);
  264. }
  265. /**
  266. * Returns array of all users additional mobiles as login=>mobiles array
  267. *
  268. * @return array
  269. */
  270. public function getAllUsersMobileNumbers() {
  271. $result = array();
  272. if (!empty($this->allMobiles)) {
  273. foreach ($this->allMobiles as $io => $each) {
  274. $result[$each['login']][] = $each['mobile'];
  275. }
  276. }
  277. return($result);
  278. }
  279. /**
  280. * Returns all additional mobiles data as mobile=>login
  281. *
  282. * @return array
  283. */
  284. public function getAllMobilesUsers() {
  285. $result = array();
  286. if (!empty($this->allMobiles)) {
  287. foreach ($this->allMobiles as $io => $each) {
  288. $result[$each['mobile']] = $each['login'];
  289. }
  290. }
  291. return ($result);
  292. }
  293. /**
  294. * Renders fast ext mobile add form
  295. *
  296. * @param string $login
  297. *
  298. * @return void
  299. */
  300. public function fastNumAttachForm($login) {
  301. $result = '';
  302. $pbxNum = new PBXNum();
  303. $inCallsLog = $pbxNum->parseLog();
  304. $telepathy = new Telepathy(false, true, false, false);
  305. $telepathy->usePhones();
  306. if (!empty($inCallsLog)) {
  307. $numsTmp = array();
  308. $curdate = curdate();
  309. $curhour = date("H:");
  310. foreach ($inCallsLog as $io => $each) {
  311. //only today calls
  312. if ($each['date'] == $curdate) {
  313. if ((empty($each['login'])) AND ( $each['reply'] == 0)) {
  314. //just for last hour
  315. if (substr($each['time'], 0, 3) == $curhour) {
  316. if (!empty($each['number'])) {
  317. //is this really unknown number?
  318. $detectedLogin = $telepathy->getByPhone($each['number'], true, true);
  319. if (empty($detectedLogin)) {
  320. $numsTmp[$each['number']] = $each['time'] . ' - ' . $each['number'];
  321. }
  322. }
  323. }
  324. }
  325. }
  326. }
  327. //new extmobile form rendering
  328. if (!empty($numsTmp)) {
  329. if (!empty($login)) {
  330. $inputs = wf_HiddenInput(self::PROUTE_NEW_LOGIN, $login);
  331. $inputs .= wf_Selector(self::PROUTE_NEW_NUMBER, $numsTmp, __('New mobile'), '', false);
  332. $inputs .= wf_TextInput(self::PROUTE_NEW_NOTES, __('New notes'), '', false, '40');
  333. $inputs .= wf_Submit(__('Create'));
  334. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  335. $result .= wf_CleanDiv();
  336. }
  337. show_window(__('Some of numbers which calls us today'), $result);
  338. }
  339. }
  340. }
  341. }