Merge pull request #23764 from eileenmcnaughton/activity_update_mutli
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / TabHeader.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Helper class to build navigation links.
20 */
21 class CRM_Contribute_Form_ContributionPage_TabHeader {
22
23 /**
24 * @param CRM_Core_Form $form
25 *
26 * @return array
27 */
28 public static function build(&$form) {
29 $tabs = $form->get('tabHeader');
30 if (!$tabs || empty($_GET['reset'])) {
31 $tabs = self::process($form);
32 $form->set('tabHeader', $tabs);
33 }
34 $form->assign_by_ref('tabHeader', $tabs);
35 CRM_Core_Resources::singleton()
36 ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header')
37 ->addSetting([
38 'tabSettings' => [
39 'active' => self::getCurrentTab($tabs),
40 ],
41 ]);
42 return $tabs;
43 }
44
45 /**
46 * @param CRM_Core_Form $form
47 *
48 * @return array|null
49 */
50 public static function process(&$form) {
51 if ($form->getVar('_id') <= 0) {
52 return NULL;
53 }
54
55 $default = [
56 'link' => NULL,
57 'valid' => FALSE,
58 'active' => FALSE,
59 'current' => FALSE,
60 'class' => FALSE,
61 'extra' => FALSE,
62 'template' => FALSE,
63 'count' => FALSE,
64 'icon' => FALSE,
65 ];
66
67 $tabs = [
68 'settings' => [
69 'title' => ts('Title'),
70 ] + $default,
71 'amount' => [
72 'title' => ts('Amounts'),
73 ] + $default,
74 'membership' => [
75 'title' => ts('Memberships'),
76 ] + $default,
77 'thankyou' => [
78 'title' => ts('Receipt'),
79 ] + $default,
80 'friend' => [
81 'title' => ts('Tell a Friend'),
82 ] + $default,
83 'custom' => [
84 'title' => ts('Profiles'),
85 ] + $default,
86 'premium' => [
87 'title' => ts('Premiums'),
88 ] + $default,
89 'widget' => [
90 'title' => ts('Widgets'),
91 ] + $default,
92 'pcp' => [
93 'title' => ts('Personal Campaigns'),
94 ] + $default,
95 ];
96
97 $contribPageId = $form->getVar('_id');
98 // Call tabset hook to add/remove custom tabs
99 CRM_Utils_Hook::tabset('civicrm/admin/contribute', $tabs, ['contribution_page_id' => $contribPageId]);
100 $fullName = $form->getVar('_name');
101 $className = CRM_Utils_String::getClassName($fullName);
102
103 // Hack for special cases.
104 switch ($className) {
105 case 'Contribute':
106 $attributes = $form->getVar('_attributes');
107 $class = CRM_Utils_Request::retrieveComponent($attributes);
108 break;
109
110 case 'MembershipBlock':
111 $class = 'membership';
112 break;
113
114 default:
115 $class = strtolower($className);
116 break;
117 }
118
119 if (array_key_exists($class, $tabs)) {
120 $tabs[$class]['current'] = TRUE;
121 $qfKey = $form->get('qfKey');
122 if ($qfKey) {
123 $tabs[$class]['qfKey'] = "&qfKey={$qfKey}";
124 }
125 }
126
127 if ($contribPageId) {
128 $reset = !empty($_GET['reset']) ? 'reset=1&' : '';
129
130 foreach ($tabs as $key => $value) {
131 if (!isset($tabs[$key]['qfKey'])) {
132 $tabs[$key]['qfKey'] = NULL;
133 }
134
135 $tabs[$key]['link'] = CRM_Utils_System::url(
136 "civicrm/admin/contribute/{$key}",
137 "{$reset}action=update&id={$contribPageId}{$tabs[$key]['qfKey']}"
138 );
139 $tabs[$key]['active'] = $tabs[$key]['valid'] = TRUE;
140 }
141 //get all section info.
142 $contriPageInfo = CRM_Contribute_BAO_ContributionPage::getSectionInfo([$contribPageId]);
143
144 foreach ($contriPageInfo[$contribPageId] as $section => $info) {
145 if (!$info) {
146 $tabs[$section]['valid'] = FALSE;
147 }
148 }
149 }
150 return $tabs;
151 }
152
153 /**
154 * @param $form
155 */
156 public static function reset(&$form) {
157 $tabs = self::process($form);
158 $form->set('tabHeader', $tabs);
159 }
160
161 /**
162 * @param $tabs
163 *
164 * @return int|string
165 */
166 public static function getCurrentTab($tabs) {
167 static $current = FALSE;
168
169 if ($current) {
170 return $current;
171 }
172
173 if (is_array($tabs)) {
174 foreach ($tabs as $subPage => $pageVal) {
175 if ($pageVal['current'] === TRUE) {
176 $current = $subPage;
177 break;
178 }
179 }
180 }
181
182 $current = $current ? $current : 'settings';
183 return $current;
184 }
185
186 }