Merge pull request #15241 from civicrm/5.18
[civicrm-core.git] / CRM / SMS / Form / Provider.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
57507ae6 35 * SMS Form.
6a488035
TO
36 */
37class CRM_SMS_Form_Provider extends CRM_Core_Form {
430ae6dd
TO
38 protected $_id = NULL;
39
00be9182 40 public function preProcess() {
6a488035
TO
41
42 $this->_id = $this->get('id');
43
e2046b33 44 $this->setPageTitle(ts('SMS Provider'));
6a488035
TO
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 /**
d8689418 63 * Build the form object.
6a488035
TO
64 */
65 public function buildQuickForm() {
66 parent::buildQuickForm();
67
be2fb01f
CW
68 $this->addButtons([
69 [
c5c263ca
AH
70 'type' => 'next',
71 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'),
72 'isDefault' => TRUE,
be2fb01f
CW
73 ],
74 [
c5c263ca
AH
75 'type' => 'cancel',
76 'name' => ts('Cancel'),
be2fb01f
CW
77 ],
78 ]);
185eb0fc 79
6a488035 80 if ($this->_action & CRM_Core_Action::DELETE) {
6a488035
TO
81 return;
82 }
6a488035
TO
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
be2fb01f 89 $this->add('select', 'name', ts('Name'), ['' => '- select -'] + $providerNames, TRUE);
6a488035
TO
90
91 $this->add('text', 'title', ts('Title'),
92 $attributes['title'], TRUE
93 );
94
be2fb01f 95 $this->addRule('title', ts('This Title already exists in Database.'), 'objectExists', [
c5c263ca
AH
96 'CRM_SMS_DAO_Provider',
97 $this->_id,
be2fb01f 98 ]);
6a488035
TO
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
fd0e0077 121 /**
d8689418 122 * Set the default values of various form elements.
fd0e0077 123 *
fd0e0077
EM
124 * @return array
125 */
00be9182 126 public function setDefaultValues() {
be2fb01f 127 $defaults = [];
6a488035
TO
128
129 $name = CRM_Utils_Request::retrieve('key', 'String', $this, FALSE, NULL);
130 if ($name) {
131 $defaults['name'] = $name;
be2fb01f 132 $provider = CRM_SMS_Provider::singleton(['provider' => $name]);
6a488035
TO
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
db7de9c1 144 if ($name) {
6a488035 145 $dao->name = $name;
db7de9c1 146 }
6a488035
TO
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 /**
ee0ce2ef 158 * Process the form submission.
6a488035
TO
159 */
160 public function postProcess() {
161
d7d6c461 162 CRM_Utils_System::flushCache();
6a488035
TO
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
fddb9113 174 if ($this->_action && (CRM_Core_Action::UPDATE || CRM_Core_Action::ADD)) {
59248c40
SL
175 if ($this->_id) {
176 $recData['id'] = $this->_id;
fddb9113 177 }
449fbe14 178 civicrm_api3('SmsProvider', 'create', $recData);
6a488035
TO
179 }
180 }
96025800 181
6a488035 182}