INFRA-132 comments to end with full stops
[civicrm-core.git] / CRM / Custom / Form / CustomData.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * this class builds custom data
38 */
39 class 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
46 * @param string $type
47 * @param null|int $entityID
48 * @param null $onlySubType
49 *
50 * @return void
51 */
52 public static function preProcess(
53 &$form, $subName = NULL, $subType = NULL,
54 $groupCount = NULL, $type = NULL, $entityID = NULL, $onlySubType = NULL
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);
108 if (isset($typeCheck) && $urlGroupId) {
109 $form->_groupID = $urlGroupId;
110 }
111 else {
112 $form->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $form);
113 }
114
115 $gid = (isset($form->_groupID)) ? $form->_groupID : NULL;
116 $getCachedTree = isset($form->_getCachedTree) ? $form->_getCachedTree : TRUE;
117
118 $subType = $form->_subType;
119 if (!is_array($subType) && strstr($subType, CRM_Core_DAO::VALUE_SEPARATOR)) {
120 $subType = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($subType, CRM_Core_DAO::VALUE_SEPARATOR));
121 }
122
123 $groupTree = &CRM_Core_BAO_CustomGroup::getTree($form->_type,
124 $form,
125 $form->_entityId,
126 $gid,
127 $subType,
128 $form->_subName,
129 $getCachedTree,
130 $onlySubType
131 );
132
133 if (property_exists($form, '_customValueCount') && !empty($groupTree)) {
134 $form->_customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($form, $groupTree, TRUE, NULL, NULL);
135 }
136 // we should use simplified formatted groupTree
137 $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $form->_groupCount, $form);
138
139 if (isset($form->_groupTree) && is_array($form->_groupTree)) {
140 $keys = array_keys($groupTree);
141 foreach ($keys as $key) {
142 $form->_groupTree[$key] = $groupTree[$key];
143 }
144 }
145 else {
146 $form->_groupTree = $groupTree;
147 }
148 }
149
150 /**
151 * @param CRM_Core_Form $form
152 *
153 * @return array
154 */
155 public static function setDefaultValues(&$form) {
156 $defaults = array();
157 CRM_Core_BAO_CustomGroup::setDefaults($form->_groupTree, $defaults, FALSE, FALSE, $form->get('action'));
158 return $defaults;
159 }
160
161 /**
162 * @param CRM_Core_Form $form
163 * @return void
164 */
165 public static function buildQuickForm(&$form) {
166 $form->addElement('hidden', 'hidden_custom', 1);
167 $form->addElement('hidden', "hidden_custom_group_count[{$form->_groupID}]", $form->_groupCount);
168 CRM_Core_BAO_CustomGroup::buildQuickForm($form, $form->_groupTree);
169 }
170
171 }