Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
service.php
1<?php
10
14
20{
21 public static function canUse()
22 {
23 return Loader::includeModule('messageservice');
24 }
25
31 public static function getProviders()
32 {
33 $result = array();
34 $list = static::getSenderInfoList();
35 foreach ($list as $item)
36 {
37 if (!$item['canUse'])
38 {
39 // continue;
40 }
41
42 $item['from'] = static::getSenderFromList($item['id']);
43 if (count($item['from']) == 0)
44 {
45 // continue;
46 }
47
48 $result[] = $item;
49 }
50
51 return $result;
52 }
53
59 public static function getSenderNames()
60 {
61 $list = array();
62 foreach (self::getProviders() as $item)
63 {
64 if (!$item['canUse'] || count($item['from']) == 0)
65 {
66 continue;
67 }
68 foreach ($item['from'] as $number)
69 {
70 $id = $item['id'] . ':' . $number['id'];
71 $name = $item['id'] === 'rest' ? $number['name'] : ($item['shortName'] ?: $item['name']);
72 $list[$id] = $name;
73 }
74 }
75
76 return $list;
77 }
78
84 public static function getDailyLimits()
85 {
86 return MessageService\Sender\Limitation::getDailyLimits();
87 }
88
99 public static function send($senderId, $from, $to, $text, $authorId = 1)
100 {
101 if (!static::canUse())
102 {
103 return false;
104 }
105
106 $sender = MessageService\Sender\SmsManager::getSenderById($senderId);
107 if (!$sender)
108 {
109 return false;
110 }
111
112 $smsMessage = MessageService\Sender\SmsManager::createMessage(array(
113 'AUTHOR_ID' => $authorId,
114 'MESSAGE_TO' => $to,
115 'MESSAGE_BODY' => $text,
116 'MESSAGE_FROM' => $from,
117 'MESSAGE_HEADERS' => array(
118 'module_id' => 'sender'
119 )
120 ), $sender);
121
122 $sendResult = $smsMessage->sendDirectly();
123 return $sendResult->isSuccess();
124 }
125
131 public static function getManageUrl()
132 {
133 return MessageService\Sender\SmsManager::getManageUrl();
134 }
135
141 public static function getLimitsUrl()
142 {
143 return '/crm/configs/sms/?page=limits';
144 }
145
150 protected static function getSenderFromList($senderId)
151 {
152 $list = array();
153 if (static::canUse())
154 {
155 $sender = MessageService\Sender\SmsManager::getSenderById($senderId);
156 if ($sender)
157 {
158 $list = $sender->getFromList();
159 }
160 }
161 return $list;
162 }
163
168 protected static function getSenderInfoList($getFromList = false)
169 {
170 $info = array();
171 if (static::canUse())
172 {
173 $uri = new Uri(self::getManageUrl());
174 foreach (MessageService\Sender\SmsManager::getSenders() as $sender)
175 {
177 $uri->deleteParams(['sender'])->addParams(['sender' => $sender->getId()]);
178
179 $senderInfo = array(
180 'id' => $sender->getId(),
181 'isConfigurable' => $sender->isConfigurable(),
182 'name' => $sender->getName(),
183 'shortName' => $sender->getShortName(),
184 'canUse' => $sender->canUse(),
185 'isDemo' => $sender->isConfigurable() ? $sender->isDemo() : null,
186 'manageUrl' => $sender->isConfigurable() ?
187 $uri->getLocator() : ''
188 );
189
190 if ($getFromList)
191 {
192 $senderInfo['fromList'] = static::getSenderFromList($sender->getId());
193 }
194
195 $info[] = $senderInfo;
196 }
197 }
198
199 return $info;
200 }
201
202 public static function getFormattedOutputNumber($value)
203 {
204 static $numbers;
205 if (null === $numbers)
206 {
207 $numbers = [];
208 if (static::canUse())
209 {
210 $providers = static::getProviders();
211 foreach ($providers as $provider)
212 {
213 foreach ($provider['from'] as $number)
214 {
215 $numbers[$provider['id'] . ':'. $number['id']] = $number['name'];
216 }
217 }
218 }
219 }
220
221 return $numbers[$value] ?: $value;
222 }
223}
static send($senderId, $from, $to, $text, $authorId=1)
Definition service.php:99