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