Update Copywrite year to be 2019
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / TabHeader.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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 CRM_Utils_Hook::tabset('civicrm/admin/contribute', $tabs, array('contribution_page_id' => $contribPageId));
138 $fullName = $form->getVar('_name');
139 $className = CRM_Utils_String::getClassName($fullName);
140
141 // Hack for special cases.
142 switch ($className) {
143 case 'Contribute':
144 $attributes = $form->getVar('_attributes');
145 $class = CRM_Utils_Request::retrieveComponent($attributes);
146 break;
147
148 case 'MembershipBlock':
149 $class = 'membership';
150 break;
151
152 default:
153 $class = strtolower($className);
154 break;
155 }
156
157 if (array_key_exists($class, $tabs)) {
158 $tabs[$class]['current'] = TRUE;
159 $qfKey = $form->get('qfKey');
160 if ($qfKey) {
161 $tabs[$class]['qfKey'] = "&qfKey={$qfKey}";
162 }
163 }
164
165 if ($contribPageId) {
166 $reset = !empty($_GET['reset']) ? 'reset=1&' : '';
167
168 foreach ($tabs as $key => $value) {
169 if (!isset($tabs[$key]['qfKey'])) {
170 $tabs[$key]['qfKey'] = NULL;
171 }
172
173 $tabs[$key]['link'] = CRM_Utils_System::url(
174 "civicrm/admin/contribute/{$key}",
175 "{$reset}action=update&id={$contribPageId}{$tabs[$key]['qfKey']}"
176 );
177 $tabs[$key]['active'] = $tabs[$key]['valid'] = TRUE;
178 }
179 //get all section info.
180 $contriPageInfo = CRM_Contribute_BAO_ContributionPage::getSectionInfo(array($contribPageId));
181
182 foreach ($contriPageInfo[$contribPageId] as $section => $info) {
183 if (!$info) {
184 $tabs[$section]['valid'] = FALSE;
185 }
186 }
187 }
188 return $tabs;
189 }
190
191 /**
192 * @param $form
193 */
194 public static function reset(&$form) {
195 $tabs = self::process($form);
196 $form->set('tabHeader', $tabs);
197 }
198
199 /**
200 * @param $tabs
201 *
202 * @return int|string
203 */
204 public static function getCurrentTab($tabs) {
205 static $current = FALSE;
206
207 if ($current) {
208 return $current;
209 }
210
211 if (is_array($tabs)) {
212 foreach ($tabs as $subPage => $pageVal) {
213 if ($pageVal['current'] === TRUE) {
214 $current = $subPage;
215 break;
216 }
217 }
218 }
219
220 $current = $current ? $current : 'settings';
221 return $current;
222 }
223
224 }