Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id: $ |
33 | * | |
34 | */ | |
35 | class CRM_SMS_BAO_Provider extends CRM_SMS_DAO_Provider { | |
36 | ||
fd0e0077 | 37 | /** |
fe482240 | 38 | * Class constructor. |
fd0e0077 | 39 | * |
fd0e0077 EM |
40 | * @return \CRM_SMS_DAO_Provider |
41 | */ | |
42 | /** | |
fd0e0077 | 43 | */ |
00be9182 | 44 | public function __construct() { |
6a488035 TO |
45 | parent::__construct(); |
46 | } | |
47 | ||
fd0e0077 EM |
48 | /** |
49 | * @return null|string | |
50 | */ | |
00be9182 | 51 | public static function activeProviderCount() { |
6a488035 TO |
52 | $activeProviders = CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_sms_provider WHERE is_active = 1'); |
53 | return $activeProviders; | |
54 | } | |
55 | ||
d424ffde | 56 | /** |
fe482240 | 57 | * Retrieves the list of providers from the database. |
f813f78e | 58 | * |
6a488035 TO |
59 | * $selectArr array of coloumns to fetch |
60 | * $getActive boolean to get active providers | |
d424ffde | 61 | * |
fd0e0077 EM |
62 | * @param null $selectArr |
63 | * @param null $filter | |
64 | * @param bool $getActive | |
65 | * @param string $orderBy | |
66 | * | |
67 | * @return array | |
68 | */ | |
00be9182 | 69 | public static function getProviders($selectArr = NULL, $filter = NULL, $getActive = TRUE, $orderBy = 'id') { |
6a488035 TO |
70 | |
71 | $providers = array(); | |
353ffa53 TO |
72 | $temp = array(); |
73 | $dao = new CRM_SMS_DAO_Provider(); | |
6a488035 TO |
74 | if ($filter && !array_key_exists('is_active', $filter) && $getActive) { |
75 | $dao->is_active = 1; | |
76 | } | |
77 | if ($filter && is_array($filter)) { | |
78 | foreach ($filter as $key => $value) { | |
79 | $dao->$key = $value; | |
80 | } | |
81 | } | |
82 | if ($selectArr && is_array($selectArr)) { | |
83 | $select = implode(',', $selectArr); | |
84 | $dao->selectAdd($select); | |
85 | } | |
86 | $dao->orderBy($orderBy); | |
87 | $dao->find(); | |
88 | while ($dao->fetch()) { | |
89 | CRM_Core_DAO::storeValues($dao, $temp); | |
90 | $providers[] = $temp; | |
91 | } | |
92 | return $providers; | |
93 | } | |
94 | ||
fd0e0077 EM |
95 | /** |
96 | * @param $values | |
97 | */ | |
00be9182 | 98 | public static function saveRecord($values) { |
6a488035 TO |
99 | $dao = new CRM_SMS_DAO_Provider(); |
100 | $dao->copyValues($values); | |
101 | $dao->save(); | |
102 | } | |
103 | ||
fd0e0077 EM |
104 | /** |
105 | * @param $values | |
100fef9d | 106 | * @param int $providerId |
fd0e0077 | 107 | */ |
00be9182 | 108 | public static function updateRecord($values, $providerId) { |
6a488035 TO |
109 | $dao = new CRM_SMS_DAO_Provider(); |
110 | $dao->id = $providerId; | |
111 | if ($dao->find(TRUE)) { | |
112 | $dao->copyValues($values); | |
113 | $dao->save(); | |
114 | } | |
115 | } | |
116 | ||
fd0e0077 | 117 | /** |
100fef9d | 118 | * @param int $id |
fd0e0077 EM |
119 | * @param $is_active |
120 | * | |
121 | * @return bool | |
122 | */ | |
00be9182 | 123 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
124 | return CRM_Core_DAO::setFieldValue('CRM_SMS_DAO_Provider', $id, 'is_active', $is_active); |
125 | } | |
126 | ||
fd0e0077 | 127 | /** |
100fef9d | 128 | * @param int $providerID |
fd0e0077 EM |
129 | * |
130 | * @return null | |
131 | * @throws Exception | |
132 | */ | |
00be9182 | 133 | public static function del($providerID) { |
6a488035 | 134 | if (!$providerID) { |
1f21f2cf | 135 | CRM_Core_Error::fatal(ts('Invalid value passed to delete function.')); |
6a488035 TO |
136 | } |
137 | ||
138 | $dao = new CRM_SMS_DAO_Provider(); | |
139 | $dao->id = $providerID; | |
140 | if (!$dao->find(TRUE)) { | |
141 | return NULL; | |
142 | } | |
143 | $dao->delete(); | |
144 | } | |
145 | ||
fd0e0077 | 146 | /** |
100fef9d | 147 | * @param int $providerID |
fd0e0077 EM |
148 | * @param null $returnParam |
149 | * @param null $returnDefaultString | |
150 | * | |
151 | * @return mixed | |
152 | */ | |
6a488035 TO |
153 | public static function getProviderInfo($providerID, $returnParam = NULL, $returnDefaultString = NULL) { |
154 | static $providerInfo = array(); | |
155 | ||
156 | if (!array_key_exists($providerID, $providerInfo)) { | |
157 | $providerInfo[$providerID] = array(); | |
158 | ||
159 | $dao = new CRM_SMS_DAO_Provider(); | |
160 | $dao->id = $providerID; | |
161 | if ($dao->find(TRUE)) { | |
162 | CRM_Core_DAO::storeValues($dao, $providerInfo[$providerID]); | |
163 | $inputLines = explode("\n", $providerInfo[$providerID]['api_params']); | |
164 | $inputVals = array(); | |
165 | foreach ($inputLines as $value) { | |
166 | if ($value) { | |
167 | list($key, $val) = explode("=", $value); | |
168 | $inputVals[trim($key)] = trim($val); | |
169 | } | |
170 | } | |
171 | $providerInfo[$providerID]['api_params'] = $inputVals; | |
503fc58c | 172 | |
173 | // Replace the api_type ID with the string value | |
174 | $apiTypes = CRM_Core_OptionGroup::values('sms_api_type'); | |
175 | $apiTypeId = $providerInfo[$providerID]['api_type']; | |
176 | $providerInfo[$providerID]['api_type'] = CRM_Utils_Array::value($apiTypeId, $apiTypes, $apiTypeId); | |
6a488035 TO |
177 | } |
178 | } | |
179 | ||
180 | if ($returnParam) { | |
181 | return CRM_Utils_Array::value($returnParam, $providerInfo[$providerID], $returnDefaultString); | |
182 | } | |
183 | return $providerInfo[$providerID]; | |
184 | } | |
96025800 | 185 | |
6a488035 | 186 | } |