version fixes
[civicrm-core.git] / CRM / SMS / BAO / Provider.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id: $
33 *
34 */
35 class CRM_SMS_BAO_Provider extends CRM_SMS_DAO_Provider {
36
37 /**
38 * Class constructor.
39 *
40 * @return \CRM_SMS_DAO_Provider
41 */
42 /**
43 */
44 public function __construct() {
45 parent::__construct();
46 }
47
48 /**
49 * @return null|string
50 */
51 public static function activeProviderCount() {
52 $activeProviders = CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_sms_provider WHERE is_active = 1');
53 return $activeProviders;
54 }
55
56 /**
57 * Retrieves the list of providers from the database.
58 *
59 * $selectArr array of coloumns to fetch
60 * $getActive boolean to get active providers
61 *
62 * @param null $selectArr
63 * @param null $filter
64 * @param bool $getActive
65 * @param string $orderBy
66 *
67 * @return array
68 */
69 public static function getProviders($selectArr = NULL, $filter = NULL, $getActive = TRUE, $orderBy = 'id') {
70
71 $providers = array();
72 $temp = array();
73 $dao = new CRM_SMS_DAO_Provider();
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
95 /**
96 * @param $values
97 */
98 public static function saveRecord($values) {
99 $dao = new CRM_SMS_DAO_Provider();
100 $dao->copyValues($values);
101 $dao->save();
102 }
103
104 /**
105 * @param $values
106 * @param int $providerId
107 */
108 public static function updateRecord($values, $providerId) {
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
117 /**
118 * @param int $id
119 * @param $is_active
120 *
121 * @return bool
122 */
123 public static function setIsActive($id, $is_active) {
124 return CRM_Core_DAO::setFieldValue('CRM_SMS_DAO_Provider', $id, 'is_active', $is_active);
125 }
126
127 /**
128 * @param int $providerID
129 *
130 * @return null
131 * @throws Exception
132 */
133 public static function del($providerID) {
134 if (!$providerID) {
135 CRM_Core_Error::fatal(ts('Invalid value passed to delete function.'));
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
146 /**
147 * @param int $providerID
148 * @param null $returnParam
149 * @param null $returnDefaultString
150 *
151 * @return mixed
152 */
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;
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);
177 }
178 }
179
180 if ($returnParam) {
181 return CRM_Utils_Array::value($returnParam, $providerInfo[$providerID], $returnDefaultString);
182 }
183 return $providerInfo[$providerID];
184 }
185
186 }