Update copyright date for 2020
[civicrm-core.git] / CRM / SMS / Form / Provider.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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-2020
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([
69 [
70 'type' => 'next',
71 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'),
72 'isDefault' => TRUE,
73 ],
74 [
75 'type' => 'cancel',
76 'name' => ts('Cancel'),
77 ],
78 ]);
79
80 if ($this->_action & CRM_Core_Action::DELETE) {
81 return;
82 }
83
84 $attributes = CRM_Core_DAO::getAttribute('CRM_SMS_DAO_Provider');
85
86 $providerNames = CRM_Core_OptionGroup::values('sms_provider_name', FALSE, FALSE, FALSE, NULL, 'label');
87 $apiTypes = CRM_Core_OptionGroup::values('sms_api_type', FALSE, FALSE, FALSE, NULL, 'label');
88
89 $this->add('select', 'name', ts('Name'), ['' => '- select -'] + $providerNames, TRUE);
90
91 $this->add('text', 'title', ts('Title'),
92 $attributes['title'], TRUE
93 );
94
95 $this->addRule('title', ts('This Title already exists in Database.'), 'objectExists', [
96 'CRM_SMS_DAO_Provider',
97 $this->_id,
98 ]);
99
100 $this->add('text', 'username', ts('Username'),
101 $attributes['username'], TRUE
102 );
103
104 $this->add('password', 'password', ts('Password'),
105 $attributes['password'], TRUE
106 );
107
108 $this->add('select', 'api_type', ts('API Type'), $apiTypes, TRUE);
109
110 $this->add('text', 'api_url', ts('API Url'), $attributes['api_url'], TRUE);
111
112 $this->add('textarea', 'api_params', ts('API Parameters'),
113 "cols=50 rows=6", TRUE
114 );
115
116 $this->add('checkbox', 'is_active', ts('Is this provider active?'));
117
118 $this->add('checkbox', 'is_default', ts('Is this a default provider?'));
119 }
120
121 /**
122 * Set the default values of various form elements.
123 *
124 * @return array
125 */
126 public function setDefaultValues() {
127 $defaults = [];
128
129 $name = CRM_Utils_Request::retrieve('key', 'String', $this, FALSE, NULL);
130 if ($name) {
131 $defaults['name'] = $name;
132 $provider = CRM_SMS_Provider::singleton(['provider' => $name]);
133 $defaults['api_url'] = $provider->_apiURL;
134 }
135
136 if (!$this->_id) {
137 $defaults['is_active'] = $defaults['is_default'] = 1;
138 return $defaults;
139 }
140
141 $dao = new CRM_SMS_DAO_Provider();
142 $dao->id = $this->_id;
143
144 if ($name) {
145 $dao->name = $name;
146 }
147
148 if (!$dao->find(TRUE)) {
149 return $defaults;
150 }
151
152 CRM_Core_DAO::storeValues($dao, $defaults);
153
154 return $defaults;
155 }
156
157 /**
158 * Process the form submission.
159 */
160 public function postProcess() {
161
162 CRM_Utils_System::flushCache();
163
164 if ($this->_action & CRM_Core_Action::DELETE) {
165 CRM_SMS_BAO_Provider::del($this->_id);
166 CRM_Core_Session::setStatus(ts('Selected Provider has been deleted.'), ts('Deleted'), 'success');
167 return;
168 }
169
170 $recData = $values = $this->controller->exportValues($this->_name);
171 $recData['is_active'] = CRM_Utils_Array::value('is_active', $recData, 0);
172 $recData['is_default'] = CRM_Utils_Array::value('is_default', $recData, 0);
173
174 if ($this->_action && (CRM_Core_Action::UPDATE || CRM_Core_Action::ADD)) {
175 if ($this->_id) {
176 $recData['id'] = $this->_id;
177 }
178 civicrm_api3('SmsProvider', 'create', $recData);
179 }
180 }
181
182 }