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