-
-
Notifications
You must be signed in to change notification settings - Fork 825
/
Copy pathForm.php
179 lines (161 loc) · 5.34 KB
/
Form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
use Civi\Api4\Utils\ReflectionUtils;
/**
* Base class for admin forms.
*/
class CRM_Admin_Form extends CRM_Core_Form {
/**
* The id of the object being edited / created
*
* @var int
*/
public $_id;
/**
* The default values for form fields.
*
* @var array
*/
protected $_values;
/**
* The name of the BAO object for this form.
*
* @var CRM_Core_DAO|string
*/
protected $_BAOName;
/**
* Whether to use the legacy `retrieve` method or APIv4 to load values.
* @var string
*/
protected $retrieveMethod = 'retrieve';
/**
* Explicitly declare the form context.
*/
public function getDefaultContext() {
return 'create';
}
/**
* Note: This type of form was traditionally embedded in a page, with values like _id and _action
* being `set()` by the page controller.
* Nowadays the preferred approach is to place these forms at their own url.
* This function can handle either scenario. It will retrieve `id` either from a value stored by the page controller
* if embedded, or from the url if standalone.
*/
public function preProcess() {
Civi::resources()->addStyleFile('civicrm', 'css/admin.css');
Civi::resources()->addScriptFile('civicrm', 'js/jquery/jquery.crmIconPicker.js');
// Lookup id from URL or stored value in controller
$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
// If embedded in a page, this will have been assigned
$this->_BAOName = $this->get('BAOName');
// Otherwise, look it up from the api entity name
if (!$this->_BAOName) {
$this->_BAOName = CRM_Core_DAO_AllCoreTables::getBAOClassName(CRM_Core_DAO_AllCoreTables::getDAONameForEntity($this->getDefaultEntity()));
}
$this->retrieveValues();
$this->setPageTitle($this->_BAOName::getEntityTitle());
// Once form is submitted, user should be redirected back to the "browse" page.
if (isset($this->_BAOName::getEntityPaths()['browse'])) {
$this->pushUrlToUserContext($this->_BAOName::getEntityPaths()['browse']);
}
}
/**
* Set default values for the form. Note that in edit/view mode
* the default values are retrieved from the database
*
* @return array
*/
public function setDefaultValues() {
// Fetch defaults from the db if not already retrieved
if (empty($this->_values)) {
$this->retrieveValues();
}
$defaults = $this->_values;
// Allow defaults to be set from the url
if (empty($this->_id) && $this->_action & CRM_Core_Action::ADD) {
foreach ($_GET as $key => $val) {
if ($this->elementExists($key)) {
$defaults[$key] = $val;
}
}
}
if ($this->_action == CRM_Core_Action::DELETE && isset($defaults['name'])) {
$this->assign('delName', $defaults['name']);
}
// Field is_active should default to TRUE (if there is no such field, this value will be ignored)
$defaults['is_active'] = ($this->_id) ? $defaults['is_active'] ?? 1 : 1;
if (!empty($defaults['parent_id'])) {
$this->assign('is_parent', TRUE);
}
return $defaults;
}
/**
* Add standard buttons.
*/
public function buildQuickForm() {
if ($this->_action & CRM_Core_Action::VIEW || $this->_action & CRM_Core_Action::PREVIEW) {
$this->addButtons([
[
'type' => 'cancel',
'name' => ts('Done'),
'isDefault' => TRUE,
],
]);
}
else {
$this->addButtons([
[
'type' => 'next',
'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'),
'isDefault' => TRUE,
],
[
'type' => 'cancel',
'name' => ts('Cancel'),
],
]);
}
}
/**
* Retrieve entity from the database using legacy retrieve method (default) or APIv4.
*
* @return array
*/
protected function retrieveValues(): array {
$this->_values = [];
if (isset($this->_id) && CRM_Utils_Rule::positiveInteger($this->_id)) {
if ($this->retrieveMethod === 'retrieve') {
$params = ['id' => $this->_id];
if (!empty(ReflectionUtils::getCodeDocs((new \ReflectionMethod($this->_BAOName, 'retrieve')), 'Method')['deprecated'])) {
CRM_Core_DAO::commonRetrieve($this->_BAOName, $params, $this->_values);
}
else {
// Are there still some out there?
$this->_BAOName::retrieve($params, $this->_values);
}
}
elseif ($this->retrieveMethod === 'api4') {
$this->_values = civicrm_api4($this->getDefaultEntity(), 'get', [
'where' => [['id', '=', $this->_id]],
])->single();
}
else {
throw new CRM_Core_Exception("Unknown retrieve method '$this->retrieveMethod' in " . get_class($this));
}
}
return $this->_values;
}
}