Merge pull request #3807 from totten/master-emptytest
[civicrm-core.git] / CRM / SMS / Form / Provider.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id: $
33 *
34 */
35
36 /**
37 *
38 */
39 class CRM_SMS_Form_Provider extends CRM_Core_Form {
40 protected $_id = NULL;
41
42 function preProcess() {
43
44 $this->_id = $this->get('id');
45
46 CRM_Utils_System::setTitle(ts('Manage - SMS Providers'));
47
48 if ($this->_id) {
49 $refreshURL = CRM_Utils_System::url('civicrm/admin/sms/provider',
50 "reset=1&action=update&id={$this->_id}",
51 FALSE, NULL, FALSE
52 );
53 }
54 else {
55 $refreshURL = CRM_Utils_System::url('civicrm/admin/sms/provider',
56 "reset=1&action=add",
57 FALSE, NULL, FALSE
58 );
59 }
60
61 $this->assign('refreshURL', $refreshURL);
62 }
63
64 /**
65 * Function to build the form
66 *
67 * @return void
68 * @access public
69 */
70 public function buildQuickForm() {
71 parent::buildQuickForm();
72
73 if ($this->_action & CRM_Core_Action::DELETE) {
74 $this->addButtons(array(
75 array(
76 'type' => 'next',
77 'name' => ts('Delete'),
78 'isDefault' => TRUE,
79 ),
80 array(
81 'type' => 'cancel',
82 'name' => ts('Cancel'),
83 ),
84 )
85 );
86 return;
87 }
88 else {
89 $this->addButtons(array(
90 array(
91 'type' => 'next',
92 'name' => ts('Save'),
93 'isDefault' => TRUE,
94 ),
95 array(
96 'type' => 'cancel',
97 'name' => ts('Cancel'),
98 ),
99 )
100 );
101 }
102
103 $attributes = CRM_Core_DAO::getAttribute('CRM_SMS_DAO_Provider');
104
105 $providerNames = CRM_Core_OptionGroup::values('sms_provider_name', FALSE, FALSE, FALSE, NULL, 'label');
106 $apiTypes = CRM_Core_OptionGroup::values('sms_api_type', FALSE, FALSE, FALSE, NULL, 'label');
107
108 $this->add('select', 'name', ts('Name'), array('' => '- select -') + $providerNames, TRUE, array('onchange' => "reload(true)"));
109
110 $this->add('text', 'title', ts('Title'),
111 $attributes['title'], TRUE
112 );
113
114 $this->addRule('title', ts('This Title already exists in Database.'), 'objectExists', array('CRM_SMS_DAO_Provider', $this->_id));
115
116 $this->add('text', 'username', ts('Username'),
117 $attributes['username'], TRUE
118 );
119
120 $this->add('password', 'password', ts('Password'),
121 $attributes['password'], TRUE
122 );
123
124 $this->add('select', 'api_type', ts('API Type'), $apiTypes, TRUE);
125
126 $this->add('text', 'api_url', ts('API Url'), $attributes['api_url'], TRUE);
127
128 $this->add('textarea', 'api_params', ts('API Parameters'),
129 "cols=50 rows=6", TRUE
130 );
131
132 $this->add('checkbox', 'is_active', ts('Is this provider active?'));
133
134 $this->add('checkbox', 'is_default', ts('Is this a default provider?'));
135 }
136
137 /**
138 * This virtual function is used to set the default values of
139 * various form elements
140 *
141 * access public
142 *
143 * @return array reference to the array of default values
144 *
145 */
146 /**
147 * @return array
148 */
149 function setDefaultValues() {
150 $defaults = array();
151
152 $name = CRM_Utils_Request::retrieve('key', 'String', $this, FALSE, NULL);
153 if ($name) {
154 $defaults['name'] = $name;
155 $provider = CRM_SMS_Provider::singleton(array('provider' => $name));
156 $defaults['api_url'] = $provider->_apiURL;
157 }
158
159 if (!$this->_id) {
160 $defaults['is_active'] = $defaults['is_default'] = 1;
161 return $defaults;
162 }
163
164 $dao = new CRM_SMS_DAO_Provider();
165 $dao->id = $this->_id;
166
167 if ($name)
168 $dao->name = $name;
169
170 if (!$dao->find(TRUE)) {
171 return $defaults;
172 }
173
174 CRM_Core_DAO::storeValues($dao, $defaults);
175
176 return $defaults;
177 }
178
179 /**
180 * Function to process the form
181 *
182 * @access public
183 *
184 * @return void
185 */
186 public function postProcess() {
187
188 CRM_Utils_System::flushCache('CRM_SMS_DAO_Provider');
189
190 if ($this->_action & CRM_Core_Action::DELETE) {
191 CRM_SMS_BAO_Provider::del($this->_id);
192 CRM_Core_Session::setStatus(ts('Selected Provider has been deleted.'), ts('Deleted'), 'success');
193 return;
194 }
195
196 $recData = $values = $this->controller->exportValues($this->_name);
197 $recData['is_active'] = CRM_Utils_Array::value('is_active', $recData, 0);
198 $recData['is_default'] = CRM_Utils_Array::value('is_default', $recData, 0);
199
200 if ($this->_action & CRM_Core_Action::UPDATE) {
201 CRM_SMS_BAO_Provider::updateRecord($recData, $this->_id);
202 }
203 elseif ($this->_action & CRM_Core_Action::ADD) {
204 CRM_SMS_BAO_Provider::saveRecord($recData);
205 }
206 }
207 }
208