voip_json2sql.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. $unit_code = $argv[1];
  3. $api_key = $argv[2];
  4. processUnit($unit_code, 0);
  5. function createPersonBySubscriber($appointment) {
  6. static $setPersons = array();
  7. static $personId = 1;
  8. if (!isset($appointment['Subscriber'])) {
  9. return NULL;
  10. }
  11. $subscriber = $appointment['Subscriber'];
  12. if (!isset($subscriber['Key'])) {
  13. return NULL;
  14. }
  15. $key = $subscriber['Key'];
  16. if (isset($setPersons[$key])) {
  17. return $setPersons[$key]['id'];
  18. }
  19. /*
  20. [Subscriber] => Array
  21. (
  22. [Firstname] => Татьяна
  23. [Lastname] => Воропаева
  24. [Patronymic] => Михайловна
  25. [Key] => -19527
  26. [DisplayName] => Воропаева Т М
  27. [DisplayNameLatin] => Voropaeva T M
  28. [PhotoUrl] => /public/photos/oldvoip/19527-Voropaeva Tat'yana Mihaylovna.jpg
  29. )
  30. */
  31. $login = 'NULL';
  32. if (isset($subscriber['Login'])) {
  33. $login = '"'.$subscriber['Login'].'"';
  34. }
  35. $person = $subscriber;
  36. $person['id'] = $personId;
  37. if (substr($person['PhotoUrl'], 0, 1) == '/') {
  38. $person['PhotoUrl'] = 'https://voip.mephi.ru'.$person['PhotoUrl'];
  39. }
  40. print 'INSERT INTO `people` SET id='.$person['id'].', login='.$login.', `firstname`="'.$person['Firstname'].'", `lastname`="'.$person['Lastname'].'", `patronymic`="'.$person['Patronymic'].'", `photo_url`="'.$person['PhotoUrl'].'"'.", created_at=NOW(), updated_at=NOW();\n";
  41. $personId++;
  42. $setPersons[$key] = $person;
  43. return $person['id'];
  44. }
  45. function processUnit($unit_code, $parent_id) {
  46. global $api_key;
  47. static $unitId = 1;
  48. static $appointmentId = 1;
  49. $unit = json_decode(file_get_contents('https://voip.mephi.ru/units/'.$unit_code.'.json?renderFilter=unit_selected&apiKey='.$api_key.'&effectiveSecurityLevel=1024'), 1)['unit_selected'];
  50. /*
  51. [Unit] => Array
  52. (
  53. [ExtId] => -1568
  54. [CodeStr] => 01 250 05
  55. [Name] => Управления
  56. [Fullname] => Управления (Обнинский Институт Атомной Энергетики)
  57. [Shortname] => Управления
  58. [AccountCode] => 250
  59. [ParentsCodeStr] => 01 250 00
  60. [DontInflect] => 1
  61. [AppointmentsOrderStr] => -25200451
  62. [AppointmentsOrder] => Array
  63. (
  64. [0] => -25200451
  65. )
  66. [Code] => Array
  67. (
  68. [Maj] => 1
  69. [Mid] => 250
  70. [Min] => 5
  71. )
  72. )
  73. */
  74. //print_r($unit);
  75. $unit['id'] = $unitId++;
  76. $code = @$unit['Code']['Min'];
  77. if (empty($code)) {
  78. $code = 'NULL';
  79. } else {
  80. $code = '"'.str_pad($code, 2, '0', STR_PAD_LEFT).'"';
  81. }
  82. print 'INSERT INTO `units` SET id='.$unit['id'].', code='.$code.', `name`="'.$unit['Name'].'", `parent_id` = "'.$parent_id.'"'.", created_at=NOW(), updated_at=NOW();\n";
  83. if (isset($unit['AppointmentsForRender'])) {
  84. foreach ($unit['AppointmentsForRender'] as $appointment) {
  85. $personId = createPersonBySubscriber($appointment);
  86. if (is_null($personId)) {
  87. continue;
  88. }
  89. $appointment['id'] = $appointmentId++;
  90. if (!isset($appointment['Name'])) {
  91. continue;
  92. }
  93. print 'INSERT INTO `appointments` SET id='.$appointment['id'].', person_id='.$personId.', `unit_id`="'.$unit['id'].'", `name`="'.$appointment['Name'].'", `description`="'.@$appointment['Description'].'", `line`="'.@$appointment['EffectiveExtension'].'", `room`="'.@$appointment['Room']['Name'].'"'.", created_at=NOW(), updated_at=NOW();\n";
  94. }
  95. }
  96. if (isset($unit['Children'])) {
  97. foreach ($unit['Children'] as $child) {
  98. processUnit($child['CodeStr'], $unit['id']);
  99. }
  100. }
  101. }
  102. ?>