Merge pull request #10330 from colemanw/CRM-20540
[civicrm-core.git] / CRM / SMS / BAO / Provider.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2017
32 */
33 class CRM_SMS_BAO_Provider extends CRM_SMS_DAO_Provider {
34
35 /**
36 * Class constructor.
37 */
38 public function __construct() {
39 parent::__construct();
40 }
41
42 /**
43 * @return null|string
44 */
45 public static function activeProviderCount() {
46 $activeProviders = CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_sms_provider WHERE is_active = 1');
47 return $activeProviders;
48 }
49
50 /**
51 * Retrieves the list of providers from the database.
52 *
53 * $selectArr array of coloumns to fetch
54 * $getActive boolean to get active providers
55 *
56 * @param null $selectArr
57 * @param null $filter
58 * @param bool $getActive
59 * @param string $orderBy
60 *
61 * @return array
62 */
63 public static function getProviders($selectArr = NULL, $filter = NULL, $getActive = TRUE, $orderBy = 'id') {
64
65 $providers = array();
66 $temp = array();
67 $dao = new CRM_SMS_DAO_Provider();
68 if ($filter && !array_key_exists('is_active', $filter) && $getActive) {
69 $dao->is_active = 1;
70 }
71 if ($filter && is_array($filter)) {
72 foreach ($filter as $key => $value) {
73 $dao->$key = $value;
74 }
75 }
76 if ($selectArr && is_array($selectArr)) {
77 $select = implode(',', $selectArr);
78 $dao->selectAdd($select);
79 }
80 $dao->whereAdd("(domain_id = " . CRM_Core_Config::domainID() . " OR domain_id IS NULL)");
81 $dao->orderBy($orderBy);
82 $dao->find();
83 while ($dao->fetch()) {
84 CRM_Core_DAO::storeValues($dao, $temp);
85 $providers[] = $temp;
86 }
87 return $providers;
88 }
89
90 /**
91 * Create or Update an SMS provider
92 * @param array $params
93 * @return array saved values
94 */
95 public static function create(&$params) {
96 $id = CRM_Utils_Array::value('id', $params);
97
98 if ($id) {
99 CRM_Utils_Hook::pre('edit', 'SmsProvider', $id, $params);
100 }
101 else {
102 CRM_Utils_Hook::pre('create', 'SmsProvider', NULL, $params);
103 }
104
105 $provider = new CRM_SMS_DAO_Provider();
106 if ($id) {
107 $provider->id = $id;
108 $provider->find(TRUE);
109 }
110 if ($id) {
111 $provider->domain_id = CRM_Utils_Array::value('domain_id', $params, $provider->domain_id);
112 }
113 else {
114 $provider->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
115 }
116 $provider->copyValues($params);
117 $result = $provider->save();
118 if ($id) {
119 CRM_Utils_Hook::post('edit', 'SmsProvider', $provider->id, $provider);
120 }
121 else {
122 CRM_Utils_Hook::post('create', 'SmsProvider', NULL, $provider);
123 }
124 return $result;
125 }
126
127 /**
128 * @param int $id
129 * @param $is_active
130 *
131 * @return bool
132 */
133 public static function setIsActive($id, $is_active) {
134 return CRM_Core_DAO::setFieldValue('CRM_SMS_DAO_Provider', $id, 'is_active', $is_active);
135 }
136
137 /**
138 * @param int $providerID
139 *
140 * @return null
141 * @throws Exception
142 */
143 public static function del($providerID) {
144 if (!$providerID) {
145 CRM_Core_Error::fatal(ts('Invalid value passed to delete function.'));
146 }
147
148 $dao = new CRM_SMS_DAO_Provider();
149 $dao->id = $providerID;
150 $dao->whereAdd = "(domain_id = " . CRM_Core_Config::domainID() . "OR domain_id IS NULL)";
151 if (!$dao->find(TRUE)) {
152 return NULL;
153 }
154 $dao->delete();
155 }
156
157 /**
158 * @param int $providerID
159 * @param null $returnParam
160 * @param null $returnDefaultString
161 *
162 * @return mixed
163 */
164 public static function getProviderInfo($providerID, $returnParam = NULL, $returnDefaultString = NULL) {
165 static $providerInfo = array();
166
167 if (!array_key_exists($providerID, $providerInfo)) {
168 $providerInfo[$providerID] = array();
169
170 $dao = new CRM_SMS_DAO_Provider();
171 $dao->id = $providerID;
172 if ($dao->find(TRUE)) {
173 CRM_Core_DAO::storeValues($dao, $providerInfo[$providerID]);
174 $inputLines = explode("\n", $providerInfo[$providerID]['api_params']);
175 $inputVals = array();
176 foreach ($inputLines as $value) {
177 if ($value) {
178 list($key, $val) = explode("=", $value);
179 $inputVals[trim($key)] = trim($val);
180 }
181 }
182 $providerInfo[$providerID]['api_params'] = $inputVals;
183
184 // Replace the api_type ID with the string value
185 $apiTypes = CRM_Core_OptionGroup::values('sms_api_type');
186 $apiTypeId = $providerInfo[$providerID]['api_type'];
187 $providerInfo[$providerID]['api_type'] = CRM_Utils_Array::value($apiTypeId, $apiTypes, $apiTypeId);
188 }
189 }
190
191 if ($returnParam) {
192 return CRM_Utils_Array::value($returnParam, $providerInfo[$providerID], $returnDefaultString);
193 }
194 return $providerInfo[$providerID];
195 }
196
197 }