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