Commit | Line | Data |
---|---|---|
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 | /** | |
b6c94f42 | 19 | * Base class for admin forms. |
6a488035 TO |
20 | */ |
21 | class CRM_Admin_Form extends CRM_Core_Form { | |
22 | ||
23 | /** | |
24 | * The id of the object being edited / created | |
25 | * | |
26 | * @var int | |
27 | */ | |
e57fc8ca | 28 | public $_id; |
6a488035 TO |
29 | |
30 | /** | |
eceb18cc | 31 | * The default values for form fields. |
6a488035 | 32 | * |
e0f5b841 | 33 | * @var array |
6a488035 TO |
34 | */ |
35 | protected $_values; | |
36 | ||
37 | /** | |
eceb18cc | 38 | * The name of the BAO object for this form. |
6a488035 TO |
39 | * |
40 | * @var string | |
41 | */ | |
42 | protected $_BAOName; | |
43 | ||
a4969aee TM |
44 | /** |
45 | * Explicitly declare the form context. | |
46 | */ | |
47 | public function getDefaultContext() { | |
48 | return 'create'; | |
49 | } | |
50 | ||
355ba699 | 51 | /** |
eceb18cc | 52 | * Basic setup. |
355ba699 | 53 | */ |
00be9182 | 54 | public function preProcess() { |
4ef8abc2 | 55 | Civi::resources()->addStyleFile('civicrm', 'css/admin.css'); |
d2b384e9 | 56 | Civi::resources()->addScriptFile('civicrm', 'js/jquery/jquery.crmIconPicker.js'); |
4ef8abc2 | 57 | |
353ffa53 | 58 | $this->_id = $this->get('id'); |
6a488035 | 59 | $this->_BAOName = $this->get('BAOName'); |
be2fb01f | 60 | $this->_values = []; |
6a488035 | 61 | if (isset($this->_id)) { |
be2fb01f | 62 | $params = ['id' => $this->_id]; |
6a488035 | 63 | // this is needed if the form is outside the CRM name space |
0e6e8724 | 64 | $baoName = $this->_BAOName; |
481a74f4 | 65 | $baoName::retrieve($params, $this->_values); |
6a488035 TO |
66 | } |
67 | } | |
68 | ||
69 | /** | |
c490a46a | 70 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
71 | * the default values are retrieved from the database |
72 | * | |
6a488035 | 73 | * |
355ba699 | 74 | * @return array |
6a488035 | 75 | */ |
00be9182 | 76 | public function setDefaultValues() { |
4324b8d7 | 77 | // Fetch defaults from the db |
d3cbd0a5 | 78 | if (!empty($this->_id) && empty($this->_values) && CRM_Utils_Rule::positiveInteger($this->_id)) { |
be2fb01f CW |
79 | $this->_values = []; |
80 | $params = ['id' => $this->_id]; | |
0e6e8724 | 81 | $baoName = $this->_BAOName; |
481a74f4 | 82 | $baoName::retrieve($params, $this->_values); |
6a488035 TO |
83 | } |
84 | $defaults = $this->_values; | |
85 | ||
4324b8d7 CW |
86 | // Allow defaults to be set from the url |
87 | if (empty($this->_id) && $this->_action & CRM_Core_Action::ADD) { | |
88 | foreach ($_GET as $key => $val) { | |
89 | if ($this->elementExists($key)) { | |
90 | $defaults[$key] = $val; | |
91 | } | |
92 | } | |
93 | } | |
94 | ||
6a488035 TO |
95 | if ($this->_action == CRM_Core_Action::DELETE && |
96 | isset($defaults['name']) | |
97 | ) { | |
98 | $this->assign('delName', $defaults['name']); | |
99 | } | |
100 | ||
101 | // its ok if there is no element called is_active | |
102 | $defaults['is_active'] = ($this->_id) ? CRM_Utils_Array::value('is_active', $defaults) : 1; | |
a7488080 | 103 | if (!empty($defaults['parent_id'])) { |
6a488035 TO |
104 | $this->assign('is_parent', TRUE); |
105 | } | |
106 | return $defaults; | |
107 | } | |
108 | ||
109 | /** | |
eceb18cc | 110 | * Add standard buttons. |
6a488035 TO |
111 | */ |
112 | public function buildQuickForm() { | |
e2046b33 | 113 | if ($this->_action & CRM_Core_Action::VIEW || $this->_action & CRM_Core_Action::PREVIEW) { |
be2fb01f | 114 | $this->addButtons([ |
0d48f1cc TO |
115 | [ |
116 | 'type' => 'cancel', | |
117 | 'name' => ts('Done'), | |
118 | 'isDefault' => TRUE, | |
119 | ], | |
120 | ]); | |
52395565 | 121 | } |
6a488035 | 122 | else { |
be2fb01f | 123 | $this->addButtons([ |
0d48f1cc TO |
124 | [ |
125 | 'type' => 'next', | |
126 | 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'), | |
127 | 'isDefault' => TRUE, | |
128 | ], | |
129 | [ | |
130 | 'type' => 'cancel', | |
131 | 'name' => ts('Cancel'), | |
132 | ], | |
133 | ]); | |
6a488035 TO |
134 | } |
135 | } | |
96025800 | 136 | |
6a488035 | 137 | } |