Merge pull request #3207 from totten/master-alerts
[civicrm-core.git] / CRM / SMS / Form / Provider.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id: $
33 *
34 */
35
36/**
37 *
38 */
39class CRM_SMS_Form_Provider extends CRM_Core_Form {
430ae6dd
TO
40 protected $_id = NULL;
41
42 function preProcess() {
6a488035
TO
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 *
355ba699 67 * @return void
6a488035
TO
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 function setDefaultValues() {
138 $defaults = array();
139
140 $name = CRM_Utils_Request::retrieve('key', 'String', $this, FALSE, NULL);
141 if ($name) {
142 $defaults['name'] = $name;
143 $provider = CRM_SMS_Provider::singleton(array('provider' => $name));
144 $defaults['api_url'] = $provider->_apiURL;
145 }
146
147 if (!$this->_id) {
148 $defaults['is_active'] = $defaults['is_default'] = 1;
149 return $defaults;
150 }
151
152 $dao = new CRM_SMS_DAO_Provider();
153 $dao->id = $this->_id;
154
155 if ($name)
156 $dao->name = $name;
157
158 if (!$dao->find(TRUE)) {
159 return $defaults;
160 }
161
162 CRM_Core_DAO::storeValues($dao, $defaults);
163
164 return $defaults;
165 }
166
167 /**
168 * Function to process the form
169 *
170 * @access public
171 *
355ba699 172 * @return void
6a488035
TO
173 */
174 public function postProcess() {
175
176 CRM_Utils_System::flushCache('CRM_SMS_DAO_Provider');
177
178 if ($this->_action & CRM_Core_Action::DELETE) {
179 CRM_SMS_BAO_Provider::del($this->_id);
180 CRM_Core_Session::setStatus(ts('Selected Provider has been deleted.'), ts('Deleted'), 'success');
181 return;
182 }
183
184 $recData = $values = $this->controller->exportValues($this->_name);
185 $recData['is_active'] = CRM_Utils_Array::value('is_active', $recData, 0);
186 $recData['is_default'] = CRM_Utils_Array::value('is_default', $recData, 0);
187
188 if ($this->_action & CRM_Core_Action::UPDATE) {
189 CRM_SMS_BAO_Provider::updateRecord($recData, $this->_id);
190 }
191 elseif ($this->_action & CRM_Core_Action::ADD) {
192 CRM_SMS_BAO_Provider::saveRecord($recData);
193 }
194 }
195}
196