|
| 1 | +<?php /** @noinspection PhpUnused */ |
| 2 | + |
| 3 | +require_once __DIR__ . '/../../../../modules/AOW_Actions/actions/actionBase.php'; |
| 4 | + |
| 5 | +class actionSevenSms extends actionBase { |
| 6 | + public function __construct($id = '') { |
| 7 | + parent::__construct($id); |
| 8 | + } |
| 9 | + |
| 10 | + public function loadJS() { |
| 11 | + return []; |
| 12 | + } |
| 13 | + |
| 14 | + public function edit_display($line, SugarBean $bean = null, $params = []) { |
| 15 | + $foreignId = $params['foreign_id'] ?? ''; |
| 16 | + $label = $params['label'] ?? ''; |
| 17 | + $flash = isset($params['flash']) ? 'checked=checked' : ''; |
| 18 | + |
| 19 | + return "<label for='seven_from'>" . translate('LBL_SEVENSMS_FROM', 'AOW_Actions') . "</label> |
| 20 | + <input maxlength='16' id='seven_from' name='aow_actions_param[" . $line . "][from]' placeholder='" |
| 21 | + . translate('LBL_SEVENSMS_FROM', 'AOW_Actions') . "' value='" . $params['from'] |
| 22 | + . "'><br/>" |
| 23 | + . "<label for='seven_label'>" . translate('LBL_SEVENSMS_LABEL', 'AOW_Actions') . "</label> |
| 24 | + <input maxlength='100' id='seven_label' name='aow_actions_param[" . $line . "][label]' placeholder='" |
| 25 | + . translate('LBL_SEVENSMS_LABEL', 'AOW_Actions') . "' value='" . $label |
| 26 | + . "'><br/>" |
| 27 | + . "<label for='seven_foreign_id'>" . translate('LBL_SEVENSMS_FOREIGN_ID', 'AOW_Actions') . "</label> |
| 28 | + <input maxlength='64' id='seven_foreign_id' name='aow_actions_param[" . $line . "][foreign_id]' placeholder='" |
| 29 | + . translate('LBL_SEVENSMS_FOREIGN_ID', 'AOW_Actions') . "' value='" . $foreignId |
| 30 | + . "'><br/>" |
| 31 | + . "<label for='seven_flash'>" . translate('LBL_SEVENSMS_FLASH', 'AOW_Actions') . "</label> |
| 32 | + <input type='checkbox' id='seven_flash' name='aow_actions_param[" . $line . "][flash]' " . $flash |
| 33 | + . '><br/>' |
| 34 | + . "<label for='seven_text'>" . translate('LBL_SEVENSMS_TEXT', 'AOW_Actions') . "<span class='required'>*</span></label>" |
| 35 | + . "<textarea cols='110' id='seven_text' name='aow_actions_param[" . $line . "][text]' placeholder='" |
| 36 | + . translate('LBL_SEVENSMS_TEXT', 'AOW_Actions') . "' rows='5'>" . $params['text'] |
| 37 | + . '</textarea>'; |
| 38 | + } |
| 39 | + |
| 40 | + protected function getPhoneFromParams(SugarBean $bean, array $params): ?string { |
| 41 | + switch ($bean->module_name) { |
| 42 | + case 'Accounts': |
| 43 | + /** @var Account $bean */ |
| 44 | + |
| 45 | + return $bean->phone_alternate; |
| 46 | + case 'Contacts': |
| 47 | + /** @var Contact $bean */ |
| 48 | + |
| 49 | + return $bean->phone_mobile; |
| 50 | + case 'Employees': |
| 51 | + /** @var Employee $bean */ |
| 52 | + |
| 53 | + return $bean->phone_mobile; |
| 54 | + case 'Leads': |
| 55 | + /** @var Lead $bean */ |
| 56 | + |
| 57 | + return $bean->phone_mobile; |
| 58 | + case 'Users': |
| 59 | + /** @var User $bean */ |
| 60 | + |
| 61 | + return $bean->phone_mobile; |
| 62 | + default: |
| 63 | + return null; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Return true on success otherwise false. |
| 69 | + * Use actionSevenSms::getLastMessagesSuccess() and actionSevenSms::getLastMessagesFailed() |
| 70 | + * methods to get last SMS sending status |
| 71 | + * @param SugarBean $bean |
| 72 | + * @param array $params |
| 73 | + * @param bool $in_save |
| 74 | + * @return boolean |
| 75 | + */ |
| 76 | + public function run_action(SugarBean $bean, $params = [], $in_save = false) { |
| 77 | + global $sugar_config; |
| 78 | + |
| 79 | + $to = $this->getPhoneFromParams($bean, $params); |
| 80 | + if (!$to) return false; |
| 81 | + |
| 82 | + $apiKey = $sugar_config['seven_api_key']; |
| 83 | + if (!$apiKey) return false; |
| 84 | + |
| 85 | + $text = $params['text']; |
| 86 | + $from = $params['from']; |
| 87 | + $label = $params['label']; |
| 88 | + $foreign_id = $params['foreign_id']; |
| 89 | + $flash = 'yes' === $params['flash']; |
| 90 | + $json = compact('flash', 'foreign_id', 'from', 'label', 'text', 'to'); |
| 91 | + |
| 92 | + $ch = curl_init('https://gateway.seven.io/api/sms'); |
| 93 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($json)); |
| 94 | + curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
| 95 | + 'Accept: application/json', |
| 96 | + 'Content-type: application/json', |
| 97 | + 'X-Api-Key: ' . $apiKey, |
| 98 | + ]); |
| 99 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 100 | + $result = curl_exec($ch); |
| 101 | + $result = json_decode($result); |
| 102 | + curl_close($ch); |
| 103 | + |
| 104 | + return 100 == $result->success; |
| 105 | + } |
| 106 | +} |
0 commit comments