manual merge of fixes for CRM-13981
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / TabHeader.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 * Helper class to build navigation links
38 */
39 class CRM_Contribute_Form_ContributionPage_TabHeader {
40 static function build(&$form) {
41 $tabs = $form->get('tabHeader');
42 if (!$tabs || empty($_GET['reset'])) {
43 $tabs = self::process($form);
44 $form->set('tabHeader', $tabs);
45 }
46 $form->assign_by_ref('tabHeader', $tabs);
47 CRM_Core_Resources::singleton()
48 ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js')
49 ->addSetting(array('tabSettings' => array(
50 'active' => self::getCurrentTab($tabs),
51 )));
52 return $tabs;
53 }
54
55 static function process(&$form) {
56 if ($form->getVar('_id') <= 0) {
57 return NULL;
58 }
59
60 $tabs = array(
61 'settings' => array('title' => ts('Title'),
62 'link' => NULL,
63 'valid' => FALSE,
64 'active' => FALSE,
65 'current' => FALSE,
66 ),
67 'amount' => array('title' => ts('Amounts'),
68 'link' => NULL,
69 'valid' => FALSE,
70 'active' => FALSE,
71 'current' => FALSE,
72 ),
73 'membership' => array('title' => ts('Memberships'),
74 'link' => NULL,
75 'valid' => FALSE,
76 'active' => FALSE,
77 'current' => FALSE,
78 ),
79 'thankyou' => array('title' => ts('Receipt'),
80 'link' => NULL,
81 'valid' => FALSE,
82 'active' => FALSE,
83 'current' => FALSE,
84 ),
85 'friend' => array('title' => ts('Tell a Friend'),
86 'link' => NULL,
87 'valid' => FALSE,
88 'active' => FALSE,
89 'current' => FALSE,
90 ),
91 'custom' => array('title' => ts('Profiles'),
92 'link' => NULL,
93 'valid' => FALSE,
94 'active' => FALSE,
95 'current' => FALSE,
96 ),
97 'premium' => array('title' => ts('Premiums'),
98 'link' => NULL,
99 'valid' => FALSE,
100 'active' => FALSE,
101 'current' => FALSE,
102 ),
103 'widget' => array('title' => ts('Widgets'),
104 'link' => NULL,
105 'valid' => FALSE,
106 'active' => FALSE,
107 'current' => FALSE,
108 ),
109 'pcp' => array('title' => ts('Personal Campaigns'),
110 'link' => NULL,
111 'valid' => FALSE,
112 'active' => FALSE,
113 'current' => FALSE,
114 ),
115 );
116
117 $contribPageId = $form->getVar('_id');
118 $fullName = $form->getVar('_name');
119 $className = CRM_Utils_String::getClassName($fullName);
120
121 // Hack for special cases.
122 switch ($className) {
123 case 'Contribute':
124 $attributes = $form->getVar('_attributes');
125 $class = strtolower(basename(CRM_Utils_Array::value('action', $attributes)));
126 break;
127
128 case 'MembershipBlock':
129 $class = 'membership';
130 break;
131
132 default:
133 $class = strtolower($className);
134 break;
135 }
136
137 if (array_key_exists($class, $tabs)) {
138 $tabs[$class]['current'] = TRUE;
139 $qfKey = $form->get('qfKey');
140 if ($qfKey) {
141 $tabs[$class]['qfKey'] = "&qfKey={$qfKey}";
142 }
143 }
144
145 if ($contribPageId) {
146 $reset = !empty($_GET['reset']) ? 'reset=1&' : '';
147
148 foreach ($tabs as $key => $value) {
149 if (!isset($tabs[$key]['qfKey'])) {
150 $tabs[$key]['qfKey'] = NULL;
151 }
152
153 $tabs[$key]['link'] =
154 CRM_Utils_System::url(
155 "civicrm/admin/contribute/{$key}",
156 "{$reset}action=update&id={$contribPageId}{$tabs[$key]['qfKey']}"
157 );
158 $tabs[$key]['active'] = $tabs[$key]['valid'] = TRUE;
159 }
160 //get all section info.
161 $contriPageInfo = CRM_Contribute_BAO_ContributionPage::getSectionInfo(array($contribPageId));
162
163 foreach ($contriPageInfo[$contribPageId] as $section => $info) {
164 if (!$info) {
165 $tabs[$section]['valid'] = FALSE;
166 }
167 }
168 }
169 return $tabs;
170 }
171
172 static function reset(&$form) {
173 $tabs = self::process($form);
174 $form->set('tabHeader', $tabs);
175 }
176
177 static function getCurrentTab($tabs) {
178 static $current = FALSE;
179
180 if ($current) {
181 return $current;
182 }
183
184 if (is_array($tabs)) {
185 foreach ($tabs as $subPage => $pageVal) {
186 if ($pageVal['current'] === TRUE) {
187 $current = $subPage;
188 break;
189 }
190 }
191 }
192
193 $current = $current ? $current : 'settings';
194 return $current;
195 }
196 }
197