Merge pull request #17595 from eileenmcnaughton/settings
[civicrm-core.git] / CRM / SMS / Form / Provider.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
57507ae6 19 * SMS Form.
6a488035
TO
20 */
21class CRM_SMS_Form_Provider extends CRM_Core_Form {
430ae6dd
TO
22 protected $_id = NULL;
23
00be9182 24 public function preProcess() {
6a488035
TO
25
26 $this->_id = $this->get('id');
27
e2046b33 28 $this->setPageTitle(ts('SMS Provider'));
6a488035
TO
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 /**
d8689418 47 * Build the form object.
6a488035
TO
48 */
49 public function buildQuickForm() {
50 parent::buildQuickForm();
51
be2fb01f
CW
52 $this->addButtons([
53 [
c5c263ca
AH
54 'type' => 'next',
55 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'),
56 'isDefault' => TRUE,
be2fb01f
CW
57 ],
58 [
c5c263ca
AH
59 'type' => 'cancel',
60 'name' => ts('Cancel'),
be2fb01f
CW
61 ],
62 ]);
185eb0fc 63
6a488035 64 if ($this->_action & CRM_Core_Action::DELETE) {
6a488035
TO
65 return;
66 }
6a488035
TO
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
be2fb01f 73 $this->add('select', 'name', ts('Name'), ['' => '- select -'] + $providerNames, TRUE);
6a488035
TO
74
75 $this->add('text', 'title', ts('Title'),
76 $attributes['title'], TRUE
77 );
78
be2fb01f 79 $this->addRule('title', ts('This Title already exists in Database.'), 'objectExists', [
c5c263ca
AH
80 'CRM_SMS_DAO_Provider',
81 $this->_id,
be2fb01f 82 ]);
6a488035
TO
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
fd0e0077 105 /**
d8689418 106 * Set the default values of various form elements.
fd0e0077 107 *
fd0e0077
EM
108 * @return array
109 */
00be9182 110 public function setDefaultValues() {
be2fb01f 111 $defaults = [];
6a488035
TO
112
113 $name = CRM_Utils_Request::retrieve('key', 'String', $this, FALSE, NULL);
114 if ($name) {
115 $defaults['name'] = $name;
be2fb01f 116 $provider = CRM_SMS_Provider::singleton(['provider' => $name]);
646b0eee 117 $defaults['api_url'] = $provider->_apiURL ?? '';
6a488035
TO
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
db7de9c1 128 if ($name) {
6a488035 129 $dao->name = $name;
db7de9c1 130 }
6a488035
TO
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 /**
ee0ce2ef 142 * Process the form submission.
6a488035
TO
143 */
144 public function postProcess() {
145
d7d6c461 146 CRM_Utils_System::flushCache();
6a488035
TO
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
fddb9113 158 if ($this->_action && (CRM_Core_Action::UPDATE || CRM_Core_Action::ADD)) {
59248c40
SL
159 if ($this->_id) {
160 $recData['id'] = $this->_id;
fddb9113 161 }
449fbe14 162 civicrm_api3('SmsProvider', 'create', $recData);
6a488035
TO
163 }
164 }
96025800 165
6a488035 166}