Merge pull request #17981 from eileenmcnaughton/merge_form
[civicrm-core.git] / CRM / SMS / Form / Provider.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * SMS Form.
20 */
21 class CRM_SMS_Form_Provider extends CRM_Core_Form {
22 protected $_id = NULL;
23
24 public function preProcess() {
25
26 $this->_id = $this->get('id');
27
28 $this->setPageTitle(ts('SMS Provider'));
29
30 if ($this->_id) {
31 $refreshURL = CRM_Utils_System::url('civicrm/admin/sms/provider',
32 "reset=1&action=update&id={$this->_id}",
33 FALSE, NULL, FALSE
34 );
35 }
36 else {
37 $refreshURL = CRM_Utils_System::url('civicrm/admin/sms/provider',
38 "reset=1&action=add",
39 FALSE, NULL, FALSE
40 );
41 }
42
43 $this->assign('refreshURL', $refreshURL);
44 }
45
46 /**
47 * Build the form object.
48 */
49 public function buildQuickForm() {
50 parent::buildQuickForm();
51
52 $this->addButtons([
53 [
54 'type' => 'next',
55 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'),
56 'isDefault' => TRUE,
57 ],
58 [
59 'type' => 'cancel',
60 'name' => ts('Cancel'),
61 ],
62 ]);
63
64 if ($this->_action & CRM_Core_Action::DELETE) {
65 return;
66 }
67
68 $attributes = CRM_Core_DAO::getAttribute('CRM_SMS_DAO_Provider');
69
70 $providerNames = CRM_Core_OptionGroup::values('sms_provider_name', FALSE, FALSE, FALSE, NULL, 'label');
71 $apiTypes = CRM_Core_OptionGroup::values('sms_api_type', FALSE, FALSE, FALSE, NULL, 'label');
72
73 $this->add('select', 'name', ts('Name'), ['' => '- select -'] + $providerNames, TRUE);
74
75 $this->add('text', 'title', ts('Title'),
76 $attributes['title'], TRUE
77 );
78
79 $this->addRule('title', ts('This Title already exists in Database.'), 'objectExists', [
80 'CRM_SMS_DAO_Provider',
81 $this->_id,
82 ]);
83
84 $this->add('text', 'username', ts('Username'),
85 $attributes['username'], TRUE
86 );
87
88 $this->add('password', 'password', ts('Password'),
89 $attributes['password'], TRUE
90 );
91
92 $this->add('select', 'api_type', ts('API Type'), $apiTypes, TRUE);
93
94 $this->add('text', 'api_url', ts('API Url'), $attributes['api_url'], TRUE);
95
96 $this->add('textarea', 'api_params', ts('API Parameters'),
97 "cols=50 rows=6", TRUE
98 );
99
100 $this->add('checkbox', 'is_active', ts('Is this provider active?'));
101
102 $this->add('checkbox', 'is_default', ts('Is this a default provider?'));
103 }
104
105 /**
106 * Set the default values of various form elements.
107 *
108 * @return array
109 */
110 public function setDefaultValues() {
111 $defaults = [];
112
113 $name = CRM_Utils_Request::retrieve('key', 'String', $this, FALSE, NULL);
114 if ($name) {
115 $defaults['name'] = $name;
116 $provider = CRM_SMS_Provider::singleton(['provider' => $name]);
117 $defaults['api_url'] = $provider->_apiURL ?? '';
118 }
119
120 if (!$this->_id) {
121 $defaults['is_active'] = $defaults['is_default'] = 1;
122 return $defaults;
123 }
124
125 $dao = new CRM_SMS_DAO_Provider();
126 $dao->id = $this->_id;
127
128 if ($name) {
129 $dao->name = $name;
130 }
131
132 if (!$dao->find(TRUE)) {
133 return $defaults;
134 }
135
136 CRM_Core_DAO::storeValues($dao, $defaults);
137
138 return $defaults;
139 }
140
141 /**
142 * Process the form submission.
143 */
144 public function postProcess() {
145
146 CRM_Utils_System::flushCache();
147
148 if ($this->_action & CRM_Core_Action::DELETE) {
149 CRM_SMS_BAO_Provider::del($this->_id);
150 CRM_Core_Session::setStatus(ts('Selected Provider has been deleted.'), ts('Deleted'), 'success');
151 return;
152 }
153
154 $recData = $values = $this->controller->exportValues($this->_name);
155 $recData['is_active'] = CRM_Utils_Array::value('is_active', $recData, 0);
156 $recData['is_default'] = CRM_Utils_Array::value('is_default', $recData, 0);
157
158 if ($this->_action && (CRM_Core_Action::UPDATE || CRM_Core_Action::ADD)) {
159 if ($this->_id) {
160 $recData['id'] = $this->_id;
161 }
162 civicrm_api3('SmsProvider', 'create', $recData);
163 }
164 }
165
166 }