(NFC) Update CRM/SMS/ CRM/UF/ CRM/Upgrade/ CRM/Tag/ to be up to speed with the new...
[civicrm-core.git] / CRM / SMS / BAO / Provider.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33class CRM_SMS_BAO_Provider extends CRM_SMS_DAO_Provider {
34
fd0e0077 35 /**
fe482240 36 * Class constructor.
fd0e0077 37 */
00be9182 38 public function __construct() {
6a488035
TO
39 parent::__construct();
40 }
41
fd0e0077
EM
42 /**
43 * @return null|string
44 */
00be9182 45 public static function activeProviderCount() {
39fab6d7 46 $activeProviders = CRM_Core_DAO::singleValueQuery('SELECT count(id) FROM civicrm_sms_provider WHERE is_active = 1 AND (domain_id = %1 OR domain_id IS NULL)',
be2fb01f 47 [1 => [CRM_Core_Config::domainID(), 'Positive']]);
6a488035
TO
48 return $activeProviders;
49 }
50
d424ffde 51 /**
fe482240 52 * Retrieves the list of providers from the database.
f813f78e 53 *
6a488035
TO
54 * $selectArr array of coloumns to fetch
55 * $getActive boolean to get active providers
d424ffde 56 *
fd0e0077
EM
57 * @param null $selectArr
58 * @param null $filter
59 * @param bool $getActive
60 * @param string $orderBy
61 *
62 * @return array
63 */
00be9182 64 public static function getProviders($selectArr = NULL, $filter = NULL, $getActive = TRUE, $orderBy = 'id') {
6a488035 65
be2fb01f
CW
66 $providers = [];
67 $temp = [];
353ffa53 68 $dao = new CRM_SMS_DAO_Provider();
6a488035
TO
69 if ($filter && !array_key_exists('is_active', $filter) && $getActive) {
70 $dao->is_active = 1;
71 }
72 if ($filter && is_array($filter)) {
73 foreach ($filter as $key => $value) {
74 $dao->$key = $value;
75 }
76 }
77 if ($selectArr && is_array($selectArr)) {
78 $select = implode(',', $selectArr);
79 $dao->selectAdd($select);
80 }
eca6c81c 81 $dao->whereAdd("(domain_id = " . CRM_Core_Config::domainID() . " OR domain_id IS NULL)");
6a488035
TO
82 $dao->orderBy($orderBy);
83 $dao->find();
84 while ($dao->fetch()) {
85 CRM_Core_DAO::storeValues($dao, $temp);
6eee0915 86 $providers[$dao->id] = $temp;
6a488035
TO
87 }
88 return $providers;
89 }
90
fd0e0077 91 /**
fddb9113
SL
92 * Create or Update an SMS provider
93 * @param array $params
fddb9113 94 * @return array saved values
fd0e0077 95 */
59248c40
SL
96 public static function create(&$params) {
97 $id = CRM_Utils_Array::value('id', $params);
6a488035 98
fddb9113
SL
99 if ($id) {
100 CRM_Utils_Hook::pre('edit', 'SmsProvider', $id, $params);
101 }
102 else {
103 CRM_Utils_Hook::pre('create', 'SmsProvider', NULL, $params);
104 }
105
6f192908 106 $provider = new CRM_SMS_DAO_Provider();
fddb9113
SL
107 if ($id) {
108 $provider->id = $id;
109 $provider->find(TRUE);
110 }
111 if ($id) {
112 $provider->domain_id = CRM_Utils_Array::value('domain_id', $params, $provider->domain_id);
113 }
114 else {
115 $provider->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
116 }
117 $provider->copyValues($params);
118 $result = $provider->save();
119 if ($id) {
120 CRM_Utils_Hook::post('edit', 'SmsProvider', $provider->id, $provider);
121 }
122 else {
123 CRM_Utils_Hook::post('create', 'SmsProvider', NULL, $provider);
6a488035 124 }
fddb9113 125 return $result;
6a488035
TO
126 }
127
fd0e0077 128 /**
100fef9d 129 * @param int $id
fd0e0077
EM
130 * @param $is_active
131 *
132 * @return bool
133 */
00be9182 134 public static function setIsActive($id, $is_active) {
6a488035
TO
135 return CRM_Core_DAO::setFieldValue('CRM_SMS_DAO_Provider', $id, 'is_active', $is_active);
136 }
137
fd0e0077 138 /**
100fef9d 139 * @param int $providerID
fd0e0077
EM
140 *
141 * @return null
142 * @throws Exception
143 */
00be9182 144 public static function del($providerID) {
6a488035 145 if (!$providerID) {
1f21f2cf 146 CRM_Core_Error::fatal(ts('Invalid value passed to delete function.'));
6a488035
TO
147 }
148
149 $dao = new CRM_SMS_DAO_Provider();
150 $dao->id = $providerID;
3655bea4 151 $dao->whereAdd = "(domain_id = " . CRM_Core_Config::domainID() . "OR domain_id IS NULL)";
6a488035
TO
152 if (!$dao->find(TRUE)) {
153 return NULL;
154 }
155 $dao->delete();
156 }
157
fd0e0077 158 /**
100fef9d 159 * @param int $providerID
fd0e0077
EM
160 * @param null $returnParam
161 * @param null $returnDefaultString
162 *
163 * @return mixed
164 */
6a488035 165 public static function getProviderInfo($providerID, $returnParam = NULL, $returnDefaultString = NULL) {
be2fb01f 166 static $providerInfo = [];
6a488035
TO
167
168 if (!array_key_exists($providerID, $providerInfo)) {
be2fb01f 169 $providerInfo[$providerID] = [];
6a488035
TO
170
171 $dao = new CRM_SMS_DAO_Provider();
172 $dao->id = $providerID;
173 if ($dao->find(TRUE)) {
174 CRM_Core_DAO::storeValues($dao, $providerInfo[$providerID]);
175 $inputLines = explode("\n", $providerInfo[$providerID]['api_params']);
be2fb01f 176 $inputVals = [];
6a488035
TO
177 foreach ($inputLines as $value) {
178 if ($value) {
179 list($key, $val) = explode("=", $value);
180 $inputVals[trim($key)] = trim($val);
181 }
182 }
183 $providerInfo[$providerID]['api_params'] = $inputVals;
503fc58c 184
185 // Replace the api_type ID with the string value
186 $apiTypes = CRM_Core_OptionGroup::values('sms_api_type');
187 $apiTypeId = $providerInfo[$providerID]['api_type'];
188 $providerInfo[$providerID]['api_type'] = CRM_Utils_Array::value($apiTypeId, $apiTypes, $apiTypeId);
6a488035
TO
189 }
190 }
191
192 if ($returnParam) {
193 return CRM_Utils_Array::value($returnParam, $providerInfo[$providerID], $returnDefaultString);
194 }
195 return $providerInfo[$providerID];
196 }
96025800 197
6a488035 198}