Merge pull request #22218 from colemanw/fixCanAggregate
[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
88aae6d4
A
24 /**
25 * @var bool
26 */
27 public $submitOnce = TRUE;
28
00be9182 29 public function preProcess() {
6a488035
TO
30
31 $this->_id = $this->get('id');
32
e2046b33 33 $this->setPageTitle(ts('SMS Provider'));
6a488035
TO
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 /**
d8689418 52 * Build the form object.
6a488035
TO
53 */
54 public function buildQuickForm() {
55 parent::buildQuickForm();
56
be2fb01f
CW
57 $this->addButtons([
58 [
c5c263ca
AH
59 'type' => 'next',
60 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'),
61 'isDefault' => TRUE,
be2fb01f
CW
62 ],
63 [
c5c263ca
AH
64 'type' => 'cancel',
65 'name' => ts('Cancel'),
be2fb01f
CW
66 ],
67 ]);
185eb0fc 68
6a488035 69 if ($this->_action & CRM_Core_Action::DELETE) {
6a488035
TO
70 return;
71 }
6a488035
TO
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
a2eb6152 78 $this->add('select', 'name', ts('Name'), $providerNames, TRUE, ['placeholder' => TRUE]);
6a488035
TO
79
80 $this->add('text', 'title', ts('Title'),
81 $attributes['title'], TRUE
82 );
83
be2fb01f 84 $this->addRule('title', ts('This Title already exists in Database.'), 'objectExists', [
c5c263ca
AH
85 'CRM_SMS_DAO_Provider',
86 $this->_id,
be2fb01f 87 ]);
6a488035
TO
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'),
4367e964 102 ['cols' => 50, 'rows' => 6], TRUE
6a488035
TO
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
fd0e0077 110 /**
d8689418 111 * Set the default values of various form elements.
fd0e0077 112 *
fd0e0077
EM
113 * @return array
114 */
00be9182 115 public function setDefaultValues() {
be2fb01f 116 $defaults = [];
6a488035
TO
117
118 $name = CRM_Utils_Request::retrieve('key', 'String', $this, FALSE, NULL);
119 if ($name) {
120 $defaults['name'] = $name;
be2fb01f 121 $provider = CRM_SMS_Provider::singleton(['provider' => $name]);
646b0eee 122 $defaults['api_url'] = $provider->_apiURL ?? '';
6a488035
TO
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
db7de9c1 133 if ($name) {
6a488035 134 $dao->name = $name;
db7de9c1 135 }
6a488035
TO
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 /**
ee0ce2ef 147 * Process the form submission.
6a488035
TO
148 */
149 public function postProcess() {
150
d7d6c461 151 CRM_Utils_System::flushCache();
6a488035
TO
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
fddb9113 163 if ($this->_action && (CRM_Core_Action::UPDATE || CRM_Core_Action::ADD)) {
59248c40
SL
164 if ($this->_id) {
165 $recData['id'] = $this->_id;
fddb9113 166 }
449fbe14 167 civicrm_api3('SmsProvider', 'create', $recData);
6a488035
TO
168 }
169 }
96025800 170
6a488035 171}