Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
b6c94f42 | 35 | * Base class for admin forms. |
6a488035 TO |
36 | */ |
37 | class CRM_Admin_Form extends CRM_Core_Form { | |
38 | ||
39 | /** | |
40 | * The id of the object being edited / created | |
41 | * | |
42 | * @var int | |
43 | */ | |
44 | protected $_id; | |
45 | ||
46 | /** | |
eceb18cc | 47 | * The default values for form fields. |
6a488035 TO |
48 | * |
49 | * @var int | |
50 | */ | |
51 | protected $_values; | |
52 | ||
53 | /** | |
eceb18cc | 54 | * The name of the BAO object for this form. |
6a488035 TO |
55 | * |
56 | * @var string | |
57 | */ | |
58 | protected $_BAOName; | |
59 | ||
a4969aee TM |
60 | /** |
61 | * Explicitly declare the form context. | |
62 | */ | |
63 | public function getDefaultContext() { | |
64 | return 'create'; | |
65 | } | |
66 | ||
355ba699 | 67 | /** |
eceb18cc | 68 | * Basic setup. |
355ba699 | 69 | */ |
00be9182 | 70 | public function preProcess() { |
4ef8abc2 | 71 | Civi::resources()->addStyleFile('civicrm', 'css/admin.css'); |
d2b384e9 | 72 | Civi::resources()->addScriptFile('civicrm', 'js/jquery/jquery.crmIconPicker.js'); |
4ef8abc2 | 73 | |
353ffa53 | 74 | $this->_id = $this->get('id'); |
6a488035 | 75 | $this->_BAOName = $this->get('BAOName'); |
353ffa53 | 76 | $this->_values = array(); |
6a488035 TO |
77 | if (isset($this->_id)) { |
78 | $params = array('id' => $this->_id); | |
79 | // this is needed if the form is outside the CRM name space | |
0e6e8724 | 80 | $baoName = $this->_BAOName; |
481a74f4 | 81 | $baoName::retrieve($params, $this->_values); |
6a488035 TO |
82 | } |
83 | } | |
84 | ||
85 | /** | |
c490a46a | 86 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
87 | * the default values are retrieved from the database |
88 | * | |
6a488035 | 89 | * |
355ba699 | 90 | * @return array |
6a488035 | 91 | */ |
00be9182 | 92 | public function setDefaultValues() { |
4324b8d7 | 93 | // Fetch defaults from the db |
d3cbd0a5 | 94 | if (!empty($this->_id) && empty($this->_values) && CRM_Utils_Rule::positiveInteger($this->_id)) { |
6a488035 TO |
95 | $this->_values = array(); |
96 | $params = array('id' => $this->_id); | |
0e6e8724 | 97 | $baoName = $this->_BAOName; |
481a74f4 | 98 | $baoName::retrieve($params, $this->_values); |
6a488035 TO |
99 | } |
100 | $defaults = $this->_values; | |
101 | ||
4324b8d7 CW |
102 | // Allow defaults to be set from the url |
103 | if (empty($this->_id) && $this->_action & CRM_Core_Action::ADD) { | |
104 | foreach ($_GET as $key => $val) { | |
105 | if ($this->elementExists($key)) { | |
106 | $defaults[$key] = $val; | |
107 | } | |
108 | } | |
109 | } | |
110 | ||
6a488035 TO |
111 | if ($this->_action == CRM_Core_Action::DELETE && |
112 | isset($defaults['name']) | |
113 | ) { | |
114 | $this->assign('delName', $defaults['name']); | |
115 | } | |
116 | ||
117 | // its ok if there is no element called is_active | |
118 | $defaults['is_active'] = ($this->_id) ? CRM_Utils_Array::value('is_active', $defaults) : 1; | |
a7488080 | 119 | if (!empty($defaults['parent_id'])) { |
6a488035 TO |
120 | $this->assign('is_parent', TRUE); |
121 | } | |
122 | return $defaults; | |
123 | } | |
124 | ||
125 | /** | |
eceb18cc | 126 | * Add standard buttons. |
6a488035 TO |
127 | */ |
128 | public function buildQuickForm() { | |
e2046b33 | 129 | if ($this->_action & CRM_Core_Action::VIEW || $this->_action & CRM_Core_Action::PREVIEW) { |
52395565 CW |
130 | $this->addButtons(array( |
131 | array( | |
132 | 'type' => 'cancel', | |
133 | 'name' => ts('Done'), | |
134 | 'isDefault' => TRUE, | |
135 | ), | |
136 | ) | |
137 | ); | |
138 | } | |
6a488035 TO |
139 | else { |
140 | $this->addButtons(array( | |
141 | array( | |
142 | 'type' => 'next', | |
a1f552a9 | 143 | 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'), |
6a488035 TO |
144 | 'isDefault' => TRUE, |
145 | ), | |
146 | array( | |
147 | 'type' => 'cancel', | |
148 | 'name' => ts('Cancel'), | |
149 | ), | |
150 | ) | |
151 | ); | |
152 | } | |
153 | } | |
96025800 | 154 | |
6a488035 | 155 | } |