Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Custom / Form / CustomData.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
e70a7fc0 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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 48 * @param null $onlySubType
6a488035 49 */
2da40d21 50 public static function preProcess(
40b55be5 51 &$form, $subName = NULL, $subType = NULL,
63dbed83 52 $groupCount = NULL, $type = NULL, $entityID = NULL, $onlySubType = NULL
6a488035
TO
53 ) {
54 if ($type) {
55 $form->_type = $type;
56 }
57 else {
58 $form->_type = CRM_Utils_Request::retrieve('type', 'String', $form);
59 }
60
61 if (isset($subType)) {
62 $form->_subType = $subType;
63 }
64 else {
65 $form->_subType = CRM_Utils_Request::retrieve('subType', 'String', $form);
66 }
67
68 if ($form->_subType == 'null') {
69 $form->_subType = NULL;
70 }
71
72 if (isset($subName)) {
73 $form->_subName = $subName;
74 }
75 else {
76 $form->_subName = CRM_Utils_Request::retrieve('subName', 'String', $form);
77 }
78
79 if ($form->_subName == 'null') {
80 $form->_subName = NULL;
81 }
82
83 if ($groupCount) {
84 $form->_groupCount = $groupCount;
85 }
86 else {
87 $form->_groupCount = CRM_Utils_Request::retrieve('cgcount', 'Positive', $form);
88 }
89
90 $form->assign('cgCount', $form->_groupCount);
91
92 //carry qf key, since this form is not inhereting core form.
93 if ($qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject)) {
94 $form->assign('qfKey', $qfKey);
95 }
96
97 if ($entityID) {
98 $form->_entityId = $entityID;
99 }
100 else {
101 $form->_entityId = CRM_Utils_Request::retrieve('entityID', 'Positive', $form);
102 }
103
481a74f4 104 $typeCheck = CRM_Utils_Request::retrieve('type', 'String', CRM_Core_DAO::$_nullObject);
6a488035 105 $urlGroupId = CRM_Utils_Request::retrieve('groupID', 'Positive', CRM_Core_DAO::$_nullObject);
481a74f4 106 if (isset($typeCheck) && $urlGroupId) {
6a488035 107 $form->_groupID = $urlGroupId;
0db6c3e1
TO
108 }
109 else {
6a488035
TO
110 $form->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $form);
111 }
112
113 $gid = (isset($form->_groupID)) ? $form->_groupID : NULL;
114 $getCachedTree = isset($form->_getCachedTree) ? $form->_getCachedTree : TRUE;
115
116 $subType = $form->_subType;
117 if (!is_array($subType) && strstr($subType, CRM_Core_DAO::VALUE_SEPARATOR)) {
118 $subType = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($subType, CRM_Core_DAO::VALUE_SEPARATOR));
119 }
8ef12e64 120
5b15263a
EM
121 self::setGroupTree($form, $subType, $gid, $onlySubType, $getCachedTree);
122 }
123
124 /**
125 * @param CRM_Core_Form $form
126 *
127 * @return array
128 */
129 public static function setDefaultValues(&$form) {
130 $defaults = array();
131 CRM_Core_BAO_CustomGroup::setDefaults($form->_groupTree, $defaults, FALSE, FALSE, $form->get('action'));
132 return $defaults;
133 }
134
135 /**
136 * @param CRM_Core_Form $form
137 */
138 public static function buildQuickForm(&$form) {
139 $form->addElement('hidden', 'hidden_custom', 1);
140 $form->addElement('hidden', "hidden_custom_group_count[{$form->_groupID}]", $form->_groupCount);
141 CRM_Core_BAO_CustomGroup::buildQuickForm($form, $form->_groupTree);
142 }
143
144 /**
145 * @param $form
146 * @param $subType
147 * @param $gid
148 * @param $onlySubType
149 * @param $getCachedTree
150 *
151 * @return array
152 */
153 public static function setGroupTree(&$form, $subType, $gid, $onlySubType, $getCachedTree = FALSE) {
154
155 $groupTree = CRM_Core_BAO_CustomGroup::getTree($form->_type,
6a488035
TO
156 $form,
157 $form->_entityId,
158 $gid,
159 $subType,
160 $form->_subName,
63dbed83 161 $getCachedTree,
162 $onlySubType
6a488035
TO
163 );
164
165 if (property_exists($form, '_customValueCount') && !empty($groupTree)) {
166 $form->_customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($form, $groupTree, TRUE, NULL, NULL);
167 }
168 // we should use simplified formatted groupTree
169 $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $form->_groupCount, $form);
170
171 if (isset($form->_groupTree) && is_array($form->_groupTree)) {
172 $keys = array_keys($groupTree);
173 foreach ($keys as $key) {
174 $form->_groupTree[$key] = $groupTree[$key];
175 }
5b15263a 176 return array($form, $groupTree);
6a488035
TO
177 }
178 else {
179 $form->_groupTree = $groupTree;
5b15263a 180 return array($form, $groupTree);
6a488035
TO
181 }
182 }
183
6a488035 184}