hunt_group_export.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. include "root.php";
  3. require_once "resources/require.php";
  4. require_once "resources/check_auth.php";
  5. require_once "resources/paging.php";
  6. //check permissions
  7. if (permission_exists('hunt_group_add') || permission_exists('hunt_group_edit')) {
  8. //access granted
  9. }
  10. else {
  11. echo "access denied";
  12. exit;
  13. }
  14. //show debug
  15. echo "<pre>\n";
  16. //start the atomic transaction
  17. $db->exec("BEGIN;");
  18. //get the hunt groups
  19. $sql = "select d.domain_name, h.domain_uuid, h.dialplan_uuid, h.hunt_group_uuid, h.hunt_group_extension, h.hunt_group_name, h.hunt_group_type, h.hunt_group_context, ";
  20. $sql .= "h.hunt_group_timeout, h.hunt_group_timeout_destination, h.hunt_group_timeout_type, h.hunt_group_ringback, h.hunt_group_cid_name_prefix, ";
  21. $sql .= "h.hunt_group_pin, h.hunt_group_caller_announce, h.hunt_group_call_prompt, h.hunt_group_user_list, h.hunt_group_enabled, h.hunt_group_description ";
  22. $sql .= "from v_hunt_groups as h, v_domains as d ";
  23. $sql .= "where d.domain_uuid = h.domain_uuid ";
  24. $sql .= "and (h.hunt_group_type = 'simultaneous' or h.hunt_group_type = 'sequentially') ";
  25. //$sql .= "and h.domain_uuid = '".$_SESSION["domain_uuid"]."' ";
  26. //$sql .= "and hunt_group_enabled = 'true' ";
  27. //echo $sql."\n";
  28. $prep_statement = $db->prepare(check_sql($sql));
  29. $prep_statement->execute();
  30. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  31. //print_r($result);
  32. foreach ($result as &$row) {
  33. //get the hunt group information and set as variables
  34. $domain_name = $row["domain_name"];
  35. $domain_uuid = $row["domain_uuid"];
  36. $hunt_group_uuid = $row["hunt_group_uuid"];
  37. $dialplan_uuid = $row["dialplan_uuid"];
  38. $ring_group_extension = $row["hunt_group_extension"];
  39. $ring_group_name = check_str($row["hunt_group_name"]);
  40. $ring_group_strategy = $row["hunt_group_type"]; //simultaneous //sequentially
  41. $ring_group_timeout_sec = $row["hunt_group_timeout"];
  42. $ring_group_timeout_data = $row["hunt_group_timeout_destination"];
  43. $ring_group_timeout_type = $row["hunt_group_timeout_type"]; //extension //voicemail //sip uri
  44. $ring_group_ringback = $row["hunt_group_ringback"];
  45. $ring_group_cid_name_prefix = $row["hunt_group_cid_name_prefix"];
  46. //$hunt_group_pin = $row["hunt_group_pin"];
  47. $ring_group_users = $row["hunt_group_user_list"];
  48. $ring_group_enabled = $row["hunt_group_enabled"];
  49. $ring_group_description = check_str($row["hunt_group_description"]);
  50. if ($ring_group_strategy == "sequentially") {
  51. $ring_group_strategy = "sequence";
  52. }
  53. //set the ring group context
  54. if (count($_SESSION["domains"]) > 1) {
  55. $ring_group_context = $domain_name;
  56. }
  57. else {
  58. $ring_group_context = "default";
  59. }
  60. if ($ring_group_timeout_type == "extension") {
  61. $ring_group_timeout_app = "transfer";
  62. } elseif ($ring_group_timeout_type == "voicemail") {
  63. $ring_group_timeout_app = "transfer";
  64. $ring_group_timeout_data = "*99".$ring_group_timeout_data." XML ".$ring_group_context;
  65. } else {
  66. $ring_group_timeout_app = "transfer";
  67. }
  68. if (strlen($dialplan_uuid) > 0) {
  69. //delete from the dialplan details
  70. $sql = "delete from v_dialplan_details ";
  71. $sql .= "where domain_uuid = '".$domain_uuid."' ";
  72. $sql .= "and dialplan_uuid = '".$dialplan_uuid."' ";
  73. $db->exec(check_sql($sql));
  74. unset($sql);
  75. //delete from the dialplan
  76. $sql = "delete from v_dialplans ";
  77. $sql .= "where domain_uuid = '".$domain_uuid."' ";
  78. $sql .= "and dialplan_uuid = '".$dialplan_uuid."' ";
  79. $db->exec(check_sql($sql));
  80. unset($sql);
  81. }
  82. else {
  83. $dialplan_uuid = uuid();
  84. }
  85. //add the ring group
  86. //prepare the uuids
  87. $ring_group_uuid = uuid();
  88. //add the ring group
  89. $sql = "insert into v_ring_groups ";
  90. $sql .= "(";
  91. $sql .= "domain_uuid, ";
  92. $sql .= "ring_group_uuid, ";
  93. $sql .= "ring_group_name, ";
  94. $sql .= "ring_group_extension, ";
  95. $sql .= "ring_group_context, ";
  96. $sql .= "ring_group_strategy, ";
  97. $sql .= "ring_group_timeout_sec, ";
  98. $sql .= "ring_group_timeout_app, ";
  99. $sql .= "ring_group_timeout_data, ";
  100. $sql .= "ring_group_cid_name_prefix, ";
  101. $sql .= "ring_group_ringback, ";
  102. $sql .= "ring_group_enabled, ";
  103. $sql .= "ring_group_description, ";
  104. $sql .= "dialplan_uuid ";
  105. $sql .= ")";
  106. $sql .= "values ";
  107. $sql .= "(";
  108. $sql .= "'".$domain_uuid."', ";
  109. $sql .= "'".$ring_group_uuid."', ";
  110. $sql .= "'$ring_group_name', ";
  111. $sql .= "'$ring_group_extension', ";
  112. $sql .= "'$ring_group_context', ";
  113. $sql .= "'$ring_group_strategy', ";
  114. $sql .= "'$ring_group_timeout_sec', ";
  115. $sql .= "'$ring_group_timeout_app', ";
  116. $sql .= "'$ring_group_timeout_data', ";
  117. $sql .= "'$ring_group_cid_name_prefix', ";
  118. $sql .= "'$ring_group_ringback', ";
  119. $sql .= "'$ring_group_enabled', ";
  120. $sql .= "'$ring_group_description', ";
  121. $sql .= "'$dialplan_uuid' ";
  122. $sql .= ");";
  123. echo $sql."\n";
  124. $db->exec(check_sql($sql));
  125. unset($sql);
  126. //add the dialplan
  127. require_once "resources/classes/database.php";
  128. $database = new database;
  129. $database->db = $db;
  130. $database->table = "v_dialplans";
  131. $database->fields['domain_uuid'] = $domain_uuid;
  132. $database->fields['dialplan_uuid'] = $dialplan_uuid;
  133. $database->fields['dialplan_name'] = $ring_group_name;
  134. $database->fields['dialplan_order'] = '333';
  135. $database->fields['dialplan_context'] = $ring_group_context;
  136. $database->fields['dialplan_enabled'] = 'true';
  137. $database->fields['dialplan_description'] = $ring_group_description;
  138. $database->fields['app_uuid'] = '1d61fb65-1eec-bc73-a6ee-a6203b4fe6f2';
  139. $database->add();
  140. //add the dialplan details
  141. $database->table = "v_dialplan_details";
  142. $database->fields['domain_uuid'] = $domain_uuid;
  143. $database->fields['dialplan_uuid'] = $dialplan_uuid;
  144. $database->fields['dialplan_detail_uuid'] = uuid();
  145. $database->fields['dialplan_detail_tag'] = 'condition'; //condition, action, antiaction
  146. $database->fields['dialplan_detail_type'] = 'destination_number';
  147. $database->fields['dialplan_detail_data'] = '^'.$ring_group_extension.'$';
  148. $database->fields['dialplan_detail_order'] = '000';
  149. $database->add();
  150. //add the dialplan details
  151. $database->table = "v_dialplan_details";
  152. $database->fields['domain_uuid'] = $domain_uuid;
  153. $database->fields['dialplan_uuid'] = $dialplan_uuid;
  154. $database->fields['dialplan_detail_uuid'] = uuid();
  155. $database->fields['dialplan_detail_tag'] = 'action'; //condition, action, antiaction
  156. $database->fields['dialplan_detail_type'] = 'set';
  157. $database->fields['dialplan_detail_data'] = 'ring_group_uuid='.$ring_group_uuid;
  158. $database->fields['dialplan_detail_order'] = '025';
  159. $database->add();
  160. //add the dialplan details
  161. $database->table = "v_dialplan_details";
  162. $database->fields['domain_uuid'] = $domain_uuid;
  163. $database->fields['dialplan_uuid'] = $dialplan_uuid;
  164. $database->fields['dialplan_detail_uuid'] = uuid();
  165. $database->fields['dialplan_detail_tag'] = 'action'; //condition, action, antiaction
  166. $database->fields['dialplan_detail_type'] = 'lua';
  167. $database->fields['dialplan_detail_data'] = 'app.lua ring_groups';
  168. $database->fields['dialplan_detail_order'] = '030';
  169. $database->add();
  170. //get the hunt group destinations
  171. $sql = "select * from v_hunt_group_destinations ";
  172. $sql .= "where domain_uuid = '$domain_uuid' ";
  173. $sql .= "and hunt_group_uuid = '$hunt_group_uuid' ";
  174. $sql .= "order by destination_order, destination_data asc";
  175. $sub_prep_statement = $db->prepare(check_sql($sql));
  176. $sub_prep_statement->execute();
  177. $sub_result = $sub_prep_statement->fetchAll(PDO::FETCH_NAMED);
  178. //print_r($sub_result);
  179. unset ($sub_prep_statement, $sql);
  180. if (count($sub_result) > 0) {
  181. foreach($sub_result as $field) {
  182. //get the hunt group destinations and set them as variables
  183. //$hunt_group_uuid = $field["hunt_group_uuid"];
  184. $destination_number = $field["destination_data"];
  185. $destination_type = $field["destination_type"];
  186. $destination_timeout = $field["destination_timeout"];
  187. $destination_order = $field["destination_order"];
  188. //$destination_enabled = $field["destination_enabled"];
  189. //$destination_description = $field["destination_description"];
  190. if (strlen($destination_timeout) == 0) {
  191. $destination_timeout = "30";
  192. }
  193. $destination_delay = "0";
  194. if ($ring_group_strategy == "sequence") {
  195. $destination_delay = $destination_order;
  196. }
  197. if ($destination_type == "extension") {
  198. //do nothing
  199. }
  200. elseif ($destination_type == "voicemail") {
  201. $destination_number = "*99".$destination_number." XML ".$ring_group_context;
  202. }
  203. elseif ($destination_type == "sip uri") {
  204. //do nothing
  205. }
  206. else {
  207. //do nothing
  208. }
  209. //add the ring group destinations
  210. $sql = "insert into v_ring_group_destinations ";
  211. $sql .= "(";
  212. $sql .= "domain_uuid, ";
  213. $sql .= "ring_group_destination_uuid, ";
  214. $sql .= "ring_group_uuid, ";
  215. $sql .= "destination_number, ";
  216. $sql .= "destination_delay, ";
  217. $sql .= "destination_timeout, ";
  218. $sql .= "destination_prompt ";
  219. $sql .= ") ";
  220. $sql .= "values ";
  221. $sql .= "(";
  222. $sql .= "'$domain_uuid', ";
  223. $sql .= "'".uuid()."', ";
  224. $sql .= "'$ring_group_uuid', ";
  225. $sql .= "'$destination_number', ";
  226. $sql .= "'$destination_delay', ";
  227. $sql .= "'$destination_timeout', ";
  228. $sql .= "null ";
  229. $sql .= ");";
  230. echo $sql."\n";
  231. $db->exec(check_sql($sql));
  232. unset($sql);
  233. } //end foreach
  234. unset($sql, $result, $row_count);
  235. } //end if results
  236. }
  237. unset ($prep_statement);
  238. //assign the users
  239. if (strlen($ring_group_users) > 0) {
  240. $ring_group_user_array = explode("|", $ring_group_users);
  241. if (count($ring_group_user_array) > 0) {
  242. foreach($ring_group_user_array as $username){
  243. if (strlen(trim($username)) > 0) {
  244. //get the user_uuid
  245. $sql = "select * from v_users ";
  246. $sql .= "where domain_uuid = '$domain_uuid' ";
  247. $sql .= "and username = '".trim($username)."' ";
  248. $tmp_prep_statement = $db->prepare(check_sql($sql));
  249. $tmp_prep_statement->execute();
  250. $tmp_result = $tmp_prep_statement->fetchAll(PDO::FETCH_NAMED);
  251. foreach($tmp_result as $field) {
  252. $user_uuid = $field["user_uuid"];
  253. }
  254. unset ($tmp_prep_statement, $sql,$tmp_result);
  255. //assign the user to the ring group
  256. $sql_insert = "insert into v_ring_group_users ";
  257. $sql_insert .= "(";
  258. $sql_insert .= "ring_group_user_uuid, ";
  259. $sql_insert .= "domain_uuid, ";
  260. $sql_insert .= "ring_group_uuid, ";
  261. $sql_insert .= "user_uuid ";
  262. $sql_insert .= ")";
  263. $sql_insert .= "values ";
  264. $sql_insert .= "(";
  265. $sql_insert .= "'".uuid()."', ";
  266. $sql_insert .= "'$domain_uuid', ";
  267. $sql_insert .= "'".$ring_group_uuid."', ";
  268. $sql_insert .= "'".$user_uuid."' ";
  269. $sql_insert .= ");";
  270. echo $sql_insert."\n";
  271. $db->exec($sql_insert);
  272. }
  273. }
  274. }
  275. }
  276. //commit the atomic transaction
  277. $count = $db->exec("COMMIT;"); //returns affected rows
  278. //save the xml
  279. save_dialplan_xml();
  280. //apply settings reminder
  281. $_SESSION["reload_xml"] = true;
  282. //delete the dialplan context from memcache
  283. $fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
  284. if ($fp) {
  285. $switch_cmd = "memcache delete dialplan:".$ring_group_context;
  286. $switch_result = event_socket_request($fp, 'api '.$switch_cmd);
  287. }
  288. //show debug
  289. echo "</pre>\n";
  290. //send a message
  291. echo "completed";
  292. ?>