Merge pull request #9566 from totten/master-19690-layout
[civicrm-core.git] / CRM / Admin / Form.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2017
32 */
33
34 /**
35 * Base class for admin forms.
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 /**
47 * The default values for form fields.
48 *
49 * @var int
50 */
51 protected $_values;
52
53 /**
54 * The name of the BAO object for this form.
55 *
56 * @var string
57 */
58 protected $_BAOName;
59
60 /**
61 * Explicitly declare the form context.
62 */
63 public function getDefaultContext() {
64 return 'create';
65 }
66
67 /**
68 * Basic setup.
69 */
70 public function preProcess() {
71 Civi::resources()->addStyleFile('civicrm', 'css/admin.css');
72 Civi::resources()->addScriptFile('civicrm', 'js/crm.admin.js');
73
74 $this->_id = $this->get('id');
75 $this->_BAOName = $this->get('BAOName');
76 $this->_values = array();
77 if (isset($this->_id)) {
78 $params = array('id' => $this->_id);
79 // this is needed if the form is outside the CRM name space
80 $baoName = $this->_BAOName;
81 $baoName::retrieve($params, $this->_values);
82 }
83 }
84
85 /**
86 * Set default values for the form. Note that in edit/view mode
87 * the default values are retrieved from the database
88 *
89 *
90 * @return array
91 */
92 public function setDefaultValues() {
93 if (isset($this->_id) && empty($this->_values)) {
94 $this->_values = array();
95 $params = array('id' => $this->_id);
96 $baoName = $this->_BAOName;
97 $baoName::retrieve($params, $this->_values);
98 }
99 $defaults = $this->_values;
100
101 if ($this->_action == CRM_Core_Action::DELETE &&
102 isset($defaults['name'])
103 ) {
104 $this->assign('delName', $defaults['name']);
105 }
106
107 // its ok if there is no element called is_active
108 $defaults['is_active'] = ($this->_id) ? CRM_Utils_Array::value('is_active', $defaults) : 1;
109 if (!empty($defaults['parent_id'])) {
110 $this->assign('is_parent', TRUE);
111 }
112 return $defaults;
113 }
114
115 /**
116 * Add standard buttons.
117 */
118 public function buildQuickForm() {
119 if ($this->_action & CRM_Core_Action::VIEW || $this->_action & CRM_Core_Action::PREVIEW) {
120 $this->addButtons(array(
121 array(
122 'type' => 'cancel',
123 'name' => ts('Done'),
124 'isDefault' => TRUE,
125 ),
126 )
127 );
128 }
129 else {
130 $this->addButtons(array(
131 array(
132 'type' => 'next',
133 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'),
134 'isDefault' => TRUE,
135 ),
136 array(
137 'type' => 'cancel',
138 'name' => ts('Cancel'),
139 ),
140 )
141 );
142 }
143 }
144
145 }