3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class generates form components for custom data
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.
44 class CRM_Contact_Form_CustomData
extends CRM_Core_Form
{
47 * The table id, used when editing/creating custom data
54 * entity type of the table id
58 protected $_entityType;
61 * entity sub type of the table id
66 protected $_entitySubType;
73 //protected $_groupTree;
76 * Which blocks should we show and hide.
78 * @var CRM_Core_ShowHideBlocks
87 protected $_groupTitle;
90 * Array group display status.
94 protected $_groupCollapseDisplay;
104 public $_multiRecordDisplay;
106 public $_copyValueId;
108 * pre processing work done here.
110 * gets session variables for table name, id of entity in table, type of entity and stores them.
119 function preProcess() {
120 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
121 $this->assign('cdType', FALSE);
122 $this->_multiRecordDisplay
= CRM_Utils_Request
::retrieve('multiRecordDisplay', 'String', $this);
123 if ($this->_cdType ||
$this->_multiRecordDisplay
== 'single') {
124 if ($this->_cdType
) {
125 $this->assign('cdType', TRUE);
127 // NOTE : group id is not stored in session from within CRM_Custom_Form_CustomData::preProcess func
128 // this is due to some condition inside it which restricts it from saving in session
129 // so doing this for multi record edit action
130 CRM_Custom_Form_CustomData
::preProcess($this);
131 if ($this->_multiRecordDisplay
) {
132 $this->_groupID
= CRM_Utils_Request
::retrieve('groupID', 'Positive', $this);
133 $this->_tableID
= $this->_entityId
;
134 $this->_copyValueId
= CRM_Utils_Request
::retrieve('copyValueId', 'Positive', $this);
136 if (!empty($_POST['hidden_custom'])) {
137 $this->assign('postedInfo', TRUE);
142 $this->_groupID
= CRM_Utils_Request
::retrieve('groupID', 'Positive', $this, TRUE);
143 $this->_tableID
= CRM_Utils_Request
::retrieve('tableId', 'Positive', $this, TRUE);
145 $this->_contactType
= CRM_Contact_BAO_Contact
::getContactType($this->_tableID
);
146 $this->_contactSubType
= CRM_Contact_BAO_Contact
::getContactSubType($this->_tableID
, ',');
147 $this->assign('contact_type', $this->_contactType
);
148 $this->assign('contact_subtype', $this->_contactSubType
);
149 list($displayName, $contactImage) = CRM_Contact_BAO_Contact
::getDisplayAndImage($this->_tableID
);
150 CRM_Utils_System
::setTitle($displayName, $contactImage . ' ' . $displayName);
152 // when custom data is included in this page
153 if (!empty($_POST['hidden_custom'])) {
154 for ($i = 0; $i <= $_POST['hidden_custom_group_count'][$this->_groupID
]; $i++
) {
155 CRM_Custom_Form_CustomData
::preProcess($this, NULL, NULL, $i);
156 CRM_Custom_Form_CustomData
::buildQuickForm($this);
157 CRM_Custom_Form_CustomData
::setDefaultValues($this);
163 * Function to actually build the form
168 public function buildQuickForm() {
169 if ($this->_cdType ||
$this->_multiRecordDisplay
== 'single') {
170 // buttons display for multi-valued fields to perform independednt actions
171 if ($this->_multiRecordDisplay
) {
172 $isMultiple = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup',
177 $this->assign('multiRecordDisplay', $this->_multiRecordDisplay
);
178 $saveButtonName = $this->_copyValueId ?
'Save a Copy': 'Save';
179 $this->addButtons(array(
182 'name' => ts('%1', array(1 => $saveButtonName)),
187 'name' => ts('Cancel'),
193 return CRM_Custom_Form_CustomData
::buildQuickForm($this);
196 //need to assign custom data type and subtype to the template
197 $this->assign('entityID', $this->_tableID
);
198 $this->assign('groupID', $this->_groupID
);
200 // make this form an upload since we dont know if the custom data injected dynamically
201 // is of type file etc
202 $this->addButtons(array(
205 'name' => ts('Save'),
210 'name' => ts('Cancel'),
217 * Set the default form values
221 * @return array the default array reference
223 function setDefaultValues() {
224 if ($this->_cdType ||
$this->_multiRecordDisplay
== 'single') {
225 if ($this->_copyValueId
) {
226 // cached tree is fetched
227 $groupTree = &CRM_Core_BAO_CustomGroup
::getTree($this->_type
,
232 $valueIdDefaults = array();
233 $groupTreeValueId = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, $this->_copyValueId
, $this);
234 CRM_Core_BAO_CustomGroup
::setDefaults($groupTreeValueId, $valueIdDefaults, FALSE, FALSE, $this->get('action'));
235 $tableId = $groupTreeValueId[$this->_groupID
]['table_id'];
236 foreach ($valueIdDefaults as $valueIdElementName => $value) {
237 // build defaults for COPY action for new record saving
238 $valueIdElementNamePieces = explode('_', $valueIdElementName);
239 $valueIdElementNamePieces[2] = "-{$this->_groupCount}";
240 $elementName = implode('_', $valueIdElementNamePieces);
241 $customDefaultValue[$elementName] = $value;
245 $customDefaultValue = CRM_Custom_Form_CustomData
::setDefaultValues($this);
247 return $customDefaultValue;
250 $groupTree = &CRM_Core_BAO_CustomGroup
::getTree($this->_contactType
,
254 $this->_contactSubType
257 if (empty($_POST['hidden_custom_group_count'])) {
258 // custom data building in edit mode (required to handle multi-value)
259 $groupTree = &CRM_Core_BAO_CustomGroup
::getTree($this->_contactType
, $this, $this->_tableID
,
260 $this->_groupID
, $this->_contactSubType
262 $customValueCount = CRM_Core_BAO_CustomGroup
::buildCustomDataView($this, $groupTree, TRUE, $this->_groupID
);
265 $customValueCount = $_POST['hidden_custom_group_count'][$this->_groupID
];
268 $this->assign('customValueCount', $customValueCount);
275 * Process the user submitted custom data values.
281 public function postProcess() {
282 // Get the form values and groupTree
283 $params = $this->controller
->exportValues($this->_name
);
284 CRM_Core_BAO_CustomValueTable
::postProcess($params,
285 $this->_groupTree
[$this->_groupID
]['fields'],
291 // reset the group contact cache for this group
292 CRM_Contact_BAO_GroupContactCache
::remove();