api.phonebook.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * System-wide phonebook
  4. */
  5. class PhoneBook {
  6. /**
  7. * Stores system alter config, preloaded by constructor
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains available contacts data from DB as id=>contactData
  14. *
  15. * @var array
  16. */
  17. protected $allContacts = array();
  18. /**
  19. * Contains available buildpassport contacts data from DB as id=>contactData
  20. *
  21. * @var array
  22. */
  23. protected $allBuildContacts = array();
  24. /**
  25. * Default module route
  26. */
  27. const URL_ME = '?module=phonebook';
  28. public function __construct() {
  29. $this->loadAlter();
  30. $this->loadContacts();
  31. $this->loadBuildPassports();
  32. }
  33. /**
  34. * Loads system alter.ini into protected data property
  35. *
  36. * @global object $ubillingConfig
  37. *
  38. * @return void
  39. */
  40. protected function loadAlter() {
  41. global $ubillingConfig;
  42. $this->altCfg = $ubillingConfig->getAlter();
  43. }
  44. /**
  45. * Loads available contacts from database
  46. *
  47. * @return void
  48. */
  49. protected function loadContacts() {
  50. $query = "SELECT * from `contacts`";
  51. $all = simple_queryall($query);
  52. if (!empty($all)) {
  53. foreach ($all as $io => $each) {
  54. $this->allContacts[$each['id']] = $each;
  55. }
  56. }
  57. }
  58. /**
  59. * Loads available builpassport contact data from database
  60. * and do some preprocessing magic
  61. *
  62. * @return void
  63. */
  64. protected function loadBuildPassports() {
  65. if ($this->altCfg['BUILD_EXTENDED']) {
  66. $query = "SELECT DISTINCT `ownerphone`,`ownername` FROM `buildpassport` WHERE `ownerphone` !=''";
  67. $all = simple_queryall($query);
  68. if (!empty($all)) {
  69. foreach ($all as $io => $each) {
  70. $this->allBuildContacts[] = array('phone' => $each['ownerphone'], 'name' => $each['ownername']);
  71. }
  72. }
  73. }
  74. }
  75. /**
  76. * Renders contact creation form
  77. *
  78. * @return string
  79. */
  80. public function createForm() {
  81. $inputs = wf_TextInput('newcontactphone', __('Phone'), '', false, '20');
  82. $inputs .= wf_TextInput('newcontactname', __('Name'), '', false, '20');
  83. $inputs .= wf_Submit(__('Create'));
  84. $result = wf_Form('', 'POST', $inputs, 'glamour');
  85. return ($result);
  86. }
  87. /**
  88. * Renders contact editing form
  89. *
  90. * @param int $contactId
  91. *
  92. * @return string
  93. */
  94. protected function editForm($contactId) {
  95. $contactId = vf($contactId, 3);
  96. if (isset($this->allContacts[$contactId])) {
  97. $inputs = wf_TextInput('editcontactphone', __('Phone'), $this->allContacts[$contactId]['phone'], true, '20');
  98. $inputs .= wf_TextInput('editcontactname', __('Name'), $this->allContacts[$contactId]['name'], true, '20');
  99. $inputs .= wf_HiddenInput('editcontactid', $contactId);
  100. $inputs .= wf_Submit(__('Save'));
  101. $result = wf_Form('', 'POST', $inputs, 'glamour');
  102. }
  103. return ($result);
  104. }
  105. /**
  106. * Creates new DB contact record
  107. *
  108. * @param string $phone
  109. * @param string $name
  110. *
  111. * @return void
  112. */
  113. public function createContact($phone, $name) {
  114. $phoneF = mysql_real_escape_string($phone);
  115. $nameF = mysql_real_escape_string($name);
  116. $query = "INSERT INTO `contacts` (`id`,`phone`,`name`) VALUES (NULL, '" . $phoneF . "','" . $nameF . "');";
  117. nr_query($query);
  118. $newId = simple_get_lastid('contacts');
  119. log_register('PHONEBOOK CREATE [' . $newId . '] NAME `' . $name . '` PHONE `' . $phone . '`');
  120. }
  121. /**
  122. * Deletes contact record from database
  123. *
  124. * @param int $contactId
  125. *
  126. * @return void
  127. */
  128. public function deleteContact($contactId) {
  129. $contactId = vf($contactId, 3);
  130. if (isset($this->allContacts[$contactId])) {
  131. $query = "DELETE from `contacts` WHERE `id`='" . $contactId . "';";
  132. nr_query($query);
  133. log_register('PHONEBOOK DELETE [' . $contactId . ']');
  134. }
  135. }
  136. /**
  137. * Tequila in his heartbeat, His veins burned gasoline.
  138. * It kept his motor running but it never kept him clean.
  139. */
  140. /**
  141. * Saves changes into DB if its needed
  142. *
  143. * @return void
  144. */
  145. public function saveContact() {
  146. if (wf_CheckPost(array('editcontactphone', 'editcontactname', 'editcontactid'))) {
  147. $contactId = vf($_POST['editcontactid'], 3);
  148. if (isset($this->allContacts[$contactId])) {
  149. $newPhone = mysql_real_escape_string($_POST['editcontactphone']);
  150. $newName = mysql_real_escape_string($_POST['editcontactname']);
  151. $where = " WHERE `id`='" . $contactId . "';";
  152. if ($this->allContacts[$contactId]['phone'] != $newPhone) {
  153. simple_update_field('contacts', 'phone', $newPhone, $where);
  154. log_register('PHONEBOOK UPDATE [' . $contactId . '] PHONE `' . $_POST['editcontactphone'] . '`');
  155. }
  156. if ($this->allContacts[$contactId]['name'] != $newName) {
  157. simple_update_field('contacts', 'name', $newName, $where);
  158. log_register('PHONEBOOK UPDATE [' . $contactId . '] NAME `' . $_POST['editcontactname'] . '`');
  159. }
  160. }
  161. }
  162. }
  163. /**
  164. * Renders phone data container
  165. *
  166. * @return string
  167. */
  168. public function renderContactsContainer() {
  169. $result = '';
  170. if (cfr('PHONEBOOKEDIT')) {
  171. $columns = array('Phone', 'Name', 'Actions');
  172. } else {
  173. $columns = array('Phone', 'Name');
  174. }
  175. $opts = '';
  176. $result .= wf_JqDtLoader($columns, self::URL_ME . '&ajax=true', false, 'Phones', 100, $opts);
  177. return ($result);
  178. }
  179. /**
  180. * Renders phone data with available controls
  181. *
  182. * @return void
  183. */
  184. public function renderAjaxContacts() {
  185. $result = '';
  186. $json = new wf_JqDtHelper();
  187. $messages = new UbillingMessageHelper();
  188. if ((!empty($this->allContacts)) OR ( !empty($this->allBuildContacts))) {
  189. //normal contacts processing
  190. if (!empty($this->allContacts)) {
  191. foreach ($this->allContacts as $io => $each) {
  192. $data[] = $each['phone'];
  193. $data[] = $each['name'];
  194. if (cfr('PHONEBOOKEDIT')) {
  195. $actLinks = wf_JSAlert(self::URL_ME . '&deletecontactid=' . $io, web_delete_icon(), $messages->getDeleteAlert());
  196. $actLinks .= wf_modalAuto(web_edit_icon(), __('Edit'), $this->editForm($io));
  197. $data[] = $actLinks;
  198. }
  199. $json->addRow($data);
  200. unset($data);
  201. }
  202. }
  203. //build passport contacts processing
  204. if (!empty($this->allBuildContacts)) {
  205. foreach ($this->allBuildContacts as $io => $each) {
  206. $data[] = $each['phone'];
  207. $data[] = $each['name'];
  208. if (cfr('PHONEBOOKEDIT')) {
  209. $data[] = '';
  210. }
  211. $json->addRow($data);
  212. unset($data);
  213. }
  214. }
  215. }
  216. $json->getJson();
  217. }
  218. /**
  219. * Returns all available contacts as number=>contact
  220. *
  221. * @return array
  222. */
  223. public function getAllContacts() {
  224. $result = array();
  225. if (!empty($this->allContacts)) {
  226. foreach ($this->allContacts as $io => $each) {
  227. $result[$each['phone']] = $each['name'];
  228. }
  229. }
  230. return($result);
  231. }
  232. }