CRM-14106 - Regex targeting the first part of if statements
[civicrm-core.git] / CRM / Contact / Form / CustomData.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for custom data
38 *
39 * It delegates the work to lower level subclasses and integrates the changes
40 * back in. It also uses a lot of functionality with the CRM API's, so any change
41 * made here could potentially affect the API etc. Be careful, be aware, use unit tests.
42 *
43 */
44 class CRM_Contact_Form_CustomData extends CRM_Core_Form {
45
46 /**
47 * The table id, used when editing/creating custom data
48 *
49 * @var int
50 */
51 protected $_tableId;
52
53 /**
54 * entity type of the table id
55 *
56 * @var string
57 */
58 protected $_entityType;
59
60 /**
61 * entity sub type of the table id
62 *
63 * @var string
64 * @access protected
65 */
66 protected $_entitySubType;
67
68 /**
69 * the group tree data
70 *
71 * @var array
72 */
73 //protected $_groupTree;
74
75 /**
76 * Which blocks should we show and hide.
77 *
78 * @var CRM_Core_ShowHideBlocks
79 */
80 protected $_showHide;
81
82 /**
83 * Array group titles.
84 *
85 * @var array
86 */
87 protected $_groupTitle;
88
89 /**
90 * Array group display status.
91 *
92 * @var array
93 */
94 protected $_groupCollapseDisplay;
95
96 /**
97 * custom group id
98 *
99 * @int
100 * @access public
101 */
102 public $_groupID;
103
104 /**
105 * pre processing work done here.
106 *
107 * gets session variables for table name, id of entity in table, type of entity and stores them.
108 *
109 * @param
110 *
111 * @return void
112 *
113 * @access public
114 *
115 */
116 function preProcess() {
117 $this->_cdType = CRM_Utils_Array::value('type', $_GET);
118
119 $this->assign('cdType', FALSE);
120 if ($this->_cdType) {
121 $this->assign('cdType', TRUE);
122 return CRM_Custom_Form_CustomData::preProcess($this);
123 }
124
125 $this->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $this, TRUE);
126 $this->_tableID = CRM_Utils_Request::retrieve('tableId', 'Positive', $this, TRUE);
127
128 $this->_contactType = CRM_Contact_BAO_Contact::getContactType($this->_tableID);
129 $this->_contactSubType = CRM_Contact_BAO_Contact::getContactSubType($this->_tableID, ',');
130 $this->assign('contact_type', $this->_contactType);
131 $this->assign('contact_subtype', $this->_contactSubType);
132 list($displayName, $contactImage) = CRM_Contact_BAO_Contact::getDisplayAndImage($this->_tableID);
133 CRM_Utils_System::setTitle($displayName, $contactImage . ' ' . $displayName);
134
135 // when custom data is included in this page
136 if (!empty($_POST['hidden_custom'])) {
137 for ($i = 0; $i <= $_POST['hidden_custom_group_count'][$this->_groupID]; $i++) {
138 CRM_Custom_Form_CustomData::preProcess($this, NULL, NULL, $i);
139 CRM_Custom_Form_CustomData::buildQuickForm($this);
140 CRM_Custom_Form_CustomData::setDefaultValues($this);
141 }
142 }
143 }
144
145 /**
146 * Function to actually build the form
147 *
148 * @return void
149 * @access public
150 */
151 public function buildQuickForm() {
152 if ($this->_cdType) {
153 return CRM_Custom_Form_CustomData::buildQuickForm($this);
154 }
155
156 //need to assign custom data type and subtype to the template
157 $this->assign('entityID', $this->_tableID);
158 $this->assign('groupID', $this->_groupID);
159
160 // make this form an upload since we dont know if the custom data injected dynamically
161 // is of type file etc
162 $this->addButtons(array(
163 array(
164 'type' => 'upload',
165 'name' => ts('Save'),
166 'isDefault' => TRUE,
167 ),
168 array(
169 'type' => 'cancel',
170 'name' => ts('Cancel'),
171 ),
172 )
173 );
174 }
175
176 /**
177 * Set the default form values
178 *
179 * @access protected
180 *
181 * @return array the default array reference
182 */
183 function setDefaultValues() {
184 if ($this->_cdType) {
185 $customDefaultValue = CRM_Custom_Form_CustomData::setDefaultValues($this);
186 return $customDefaultValue;
187 }
188
189 $groupTree = &CRM_Core_BAO_CustomGroup::getTree($this->_contactType,
190 $this,
191 $this->_tableID,
192 $this->_groupID,
193 $this->_contactSubType
194 );
195
196 if (empty($_POST['hidden_custom_group_count'])) {
197 // custom data building in edit mode (required to handle multi-value)
198 $groupTree = &CRM_Core_BAO_CustomGroup::getTree($this->_contactType, $this, $this->_tableID,
199 $this->_groupID, $this->_contactSubType
200 );
201 $customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, TRUE, $this->_groupID);
202 }
203 else {
204 $customValueCount = $_POST['hidden_custom_group_count'][$this->_groupID];
205 }
206
207 $this->assign('customValueCount', $customValueCount);
208
209 $defaults = array();
210 return $defaults;
211 }
212
213 /**
214 * Process the user submitted custom data values.
215 *
216 * @access public
217 *
218 * @return void
219 */
220 public function postProcess() {
221 // Get the form values and groupTree
222 $params = $this->controller->exportValues($this->_name);
223 CRM_Core_BAO_CustomValueTable::postProcess($params,
224 $this->_groupTree[$this->_groupID]['fields'],
225 'civicrm_contact',
226 $this->_tableID,
227 $this->_entityType
228 );
229
230 // reset the group contact cache for this group
231 CRM_Contact_BAO_GroupContactCache::remove();
232 }
233 }
234