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