api.smsdirections.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Flexible SMS routing implementation
  4. */
  5. class SMSDirections {
  6. /**
  7. * UbillingCache instance placeholder
  8. *
  9. * @var null
  10. */
  11. protected $ubCache = null;
  12. /**
  13. * $directionsCache array from UbillingCache
  14. *
  15. * @var array
  16. */
  17. protected $directionsCache = array();
  18. /**
  19. * Placeholder for SMS_SERVICES_BINDINGS_CACHE_LIFETIME from alter.ini
  20. *
  21. * @var int
  22. */
  23. protected $directionsCacheLifeTime = 1800;
  24. public function __construct() {
  25. global $ubillingConfig;
  26. $this->ubCache = new UbillingCache();
  27. if ($ubillingConfig->getAlterParam('SMS_SERVICES_BINDINGS_CACHE_LIFETIME')) {
  28. $this->directionsCacheLifeTime = $ubillingConfig->getAlterParam('SMS_SERVICES_BINDINGS_CACHE_LIFETIME');
  29. }
  30. $thisInstance = $this;
  31. $this->directionsCache = $this->ubCache->getCallback('SMS_SERVICES_DIRECTIONS', function () use ($thisInstance) {
  32. return ( $thisInstance->getSMSServicesDirectionsData() );
  33. }, $this->directionsCacheLifeTime);
  34. }
  35. /**
  36. * Returns SMS service ID as a direction from cache
  37. *
  38. * @param $keyType
  39. * @param $entity
  40. *
  41. * @return int
  42. */
  43. public function getDirection($keyType, $entity) {
  44. return ( isset($this->directionsCache[$keyType][$entity]) ) ? $this->directionsCache[$keyType][$entity] : 0;
  45. }
  46. /**
  47. * Returns SMS service name by it's ID from cache
  48. * Recommended to use in a big message sets instead of zb_getSMSServiceNameByID()
  49. *
  50. * @param int $smsServiceId
  51. *
  52. * @return string
  53. */
  54. public function getDirectionNameById($smsServiceId = 0) {
  55. return ( isset($this->directionsCache['service_id_name'][$smsServiceId]) ) ? $this->directionsCache['service_id_name'][$smsServiceId] : '';
  56. }
  57. /**
  58. * Returns SMS services bindings suitable for caching
  59. *
  60. * @return array
  61. */
  62. public function getSMSServicesDirectionsData() {
  63. $dirsCache = array();
  64. $queryBindings = 'SELECT * FROM sms_services_relations;';
  65. $queriedBindings = nr_query($queryBindings);
  66. if (!empty($queriedBindings)) {
  67. $fetch_assoc = ($queriedBindings instanceof mysqli_result) ? 'mysqli_fetch_assoc' : 'mysql_fetch_assoc';
  68. while ($row = $fetch_assoc($queriedBindings)) {
  69. if (!empty($row['user_login'])) {
  70. $dirsCache['user_login'][$row['user_login']] = $row['sms_srv_id'];
  71. }
  72. if (!empty($row['employee_id'])) {
  73. $dirsCache['employee_id'][$row['employee_id']] = $row['sms_srv_id'];
  74. }
  75. }
  76. }
  77. $queryServices = 'SELECT * FROM sms_services;';
  78. $queriedServices = nr_query($queryServices);
  79. if (!empty($queriedServices)) {
  80. $fetch_assoc = ($queriedServices instanceof mysqli_result) ? 'mysqli_fetch_assoc' : 'mysql_fetch_assoc';
  81. while ($row = $fetch_assoc($queriedServices)) {
  82. $dirsCache['service_id_name'][$row['id']] = $row['name'];
  83. if ($row['default_service']) {
  84. $dirsCache['service_id_name'][0] = $row['name'];
  85. }
  86. }
  87. }
  88. return ($dirsCache);
  89. }
  90. public function refreshCacheForced() {
  91. $this->ubCache->set('SMS_SERVICES_DIRECTIONS', $this->getSMSServicesDirectionsData(), $this->directionsCacheLifeTime);
  92. $this->directionsCache = $this->ubCache->get('SMS_SERVICES_DIRECTIONS', $this->directionsCacheLifeTime);
  93. }
  94. }
  95. ?>