Merge pull request #4863 from totten/master-phpcbf4
[civicrm-core.git] / CRM / Custom / Form / CustomData.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * this class builds custom data
38 */
39class CRM_Custom_Form_CustomData {
40
41 /**
42 * @param CRM_Core_Form $form
43 * @param null|string $subName
44 * @param null|string $subType
45 * @param null|int $groupCount
e8646905 46 * @param string $type
6a488035 47 * @param null|int $entityID
77b97be7
EM
48 * @param null $onlySubType
49 *
6a488035
TO
50 * @return void
51 */
40b55be5
TO
52 static function preProcess(
53 &$form, $subName = NULL, $subType = NULL,
63dbed83 54 $groupCount = NULL, $type = NULL, $entityID = NULL, $onlySubType = NULL
6a488035
TO
55 ) {
56 if ($type) {
57 $form->_type = $type;
58 }
59 else {
60 $form->_type = CRM_Utils_Request::retrieve('type', 'String', $form);
61 }
62
63 if (isset($subType)) {
64 $form->_subType = $subType;
65 }
66 else {
67 $form->_subType = CRM_Utils_Request::retrieve('subType', 'String', $form);
68 }
69
70 if ($form->_subType == 'null') {
71 $form->_subType = NULL;
72 }
73
74 if (isset($subName)) {
75 $form->_subName = $subName;
76 }
77 else {
78 $form->_subName = CRM_Utils_Request::retrieve('subName', 'String', $form);
79 }
80
81 if ($form->_subName == 'null') {
82 $form->_subName = NULL;
83 }
84
85 if ($groupCount) {
86 $form->_groupCount = $groupCount;
87 }
88 else {
89 $form->_groupCount = CRM_Utils_Request::retrieve('cgcount', 'Positive', $form);
90 }
91
92 $form->assign('cgCount', $form->_groupCount);
93
94 //carry qf key, since this form is not inhereting core form.
95 if ($qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject)) {
96 $form->assign('qfKey', $qfKey);
97 }
98
99 if ($entityID) {
100 $form->_entityId = $entityID;
101 }
102 else {
103 $form->_entityId = CRM_Utils_Request::retrieve('entityID', 'Positive', $form);
104 }
105
106 $typeCheck = CRM_Utils_Request::retrieve( 'type', 'String', CRM_Core_DAO::$_nullObject );
107 $urlGroupId = CRM_Utils_Request::retrieve('groupID', 'Positive', CRM_Core_DAO::$_nullObject);
8ef12e64 108 if ( isset($typeCheck) && $urlGroupId) {
6a488035
TO
109 $form->_groupID = $urlGroupId;
110 } else {
111 $form->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $form);
112 }
113
114 $gid = (isset($form->_groupID)) ? $form->_groupID : NULL;
115 $getCachedTree = isset($form->_getCachedTree) ? $form->_getCachedTree : TRUE;
116
117 $subType = $form->_subType;
118 if (!is_array($subType) && strstr($subType, CRM_Core_DAO::VALUE_SEPARATOR)) {
119 $subType = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($subType, CRM_Core_DAO::VALUE_SEPARATOR));
120 }
8ef12e64 121
6a488035
TO
122 $groupTree = &CRM_Core_BAO_CustomGroup::getTree($form->_type,
123 $form,
124 $form->_entityId,
125 $gid,
126 $subType,
127 $form->_subName,
63dbed83 128 $getCachedTree,
129 $onlySubType
6a488035
TO
130 );
131
132 if (property_exists($form, '_customValueCount') && !empty($groupTree)) {
133 $form->_customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($form, $groupTree, TRUE, NULL, NULL);
134 }
135 // we should use simplified formatted groupTree
136 $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $form->_groupCount, $form);
137
138 if (isset($form->_groupTree) && is_array($form->_groupTree)) {
139 $keys = array_keys($groupTree);
140 foreach ($keys as $key) {
141 $form->_groupTree[$key] = $groupTree[$key];
142 }
143 }
144 else {
145 $form->_groupTree = $groupTree;
146 }
147 }
148
e0ef6999 149 /**
c490a46a 150 * @param CRM_Core_Form $form
e0ef6999
EM
151 *
152 * @return array
153 */
00be9182 154 public static function setDefaultValues(&$form) {
6a488035
TO
155 $defaults = array();
156 CRM_Core_BAO_CustomGroup::setDefaults($form->_groupTree, $defaults, FALSE, FALSE, $form->get('action'));
157 return $defaults;
158 }
159
160 /**
161 * @param CRM_Core_Form $form
162 * @return void
163 */
00be9182 164 public static function buildQuickForm(&$form) {
6a488035
TO
165 $form->addElement('hidden', 'hidden_custom', 1);
166 $form->addElement('hidden', "hidden_custom_group_count[{$form->_groupID}]", $form->_groupCount);
7b226831 167 CRM_Core_BAO_CustomGroup::buildQuickForm($form, $form->_groupTree);
6a488035
TO
168 }
169}