Merge pull request #7181 from eileenmcnaughton/CRM-17335-2
[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 */
33 class CRM_SMS_BAO_Provider extends CRM_SMS_DAO_Provider {
34
35 /**
36 * Class constructor.
37 *
38 * @return \CRM_SMS_DAO_Provider
39 */
40 public function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * @return null|string
46 */
47 public static function activeProviderCount() {
48 $activeProviders = CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_sms_provider WHERE is_active = 1');
49 return $activeProviders;
50 }
51
52 /**
53 * Retrieves the list of providers from the database.
54 *
55 * $selectArr array of coloumns to fetch
56 * $getActive boolean to get active providers
57 *
58 * @param null $selectArr
59 * @param null $filter
60 * @param bool $getActive
61 * @param string $orderBy
62 *
63 * @return array
64 */
65 public static function getProviders($selectArr = NULL, $filter = NULL, $getActive = TRUE, $orderBy = 'id') {
66
67 $providers = array();
68 $temp = array();
69 $dao = new CRM_SMS_DAO_Provider();
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
91 /**
92 * @param $values
93 */
94 public static function saveRecord($values) {
95 $dao = new CRM_SMS_DAO_Provider();
96 $dao->copyValues($values);
97 $dao->save();
98 }
99
100 /**
101 * @param $values
102 * @param int $providerId
103 */
104 public static function updateRecord($values, $providerId) {
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
113 /**
114 * @param int $id
115 * @param $is_active
116 *
117 * @return bool
118 */
119 public static function setIsActive($id, $is_active) {
120 return CRM_Core_DAO::setFieldValue('CRM_SMS_DAO_Provider', $id, 'is_active', $is_active);
121 }
122
123 /**
124 * @param int $providerID
125 *
126 * @return null
127 * @throws Exception
128 */
129 public static function del($providerID) {
130 if (!$providerID) {
131 CRM_Core_Error::fatal(ts('Invalid value passed to delete function.'));
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
142 /**
143 * @param int $providerID
144 * @param null $returnParam
145 * @param null $returnDefaultString
146 *
147 * @return mixed
148 */
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;
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);
173 }
174 }
175
176 if ($returnParam) {
177 return CRM_Utils_Array::value($returnParam, $providerInfo[$providerID], $returnDefaultString);
178 }
179 return $providerInfo[$providerID];
180 }
181
182 }