Merge pull request #15144 from JKingsnorth/copying-events-contribution-pages
[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 /**
40 * @param CRM_Core_Form $form
41 *
42 * @return array
43 */
44 public static function build(&$form) {
45 $tabs = $form->get('tabHeader');
46 if (!$tabs || empty($_GET['reset'])) {
47 $tabs = self::process($form);
48 $form->set('tabHeader', $tabs);
49 }
50 $form->assign_by_ref('tabHeader', $tabs);
51 CRM_Core_Resources::singleton()
52 ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header')
53 ->addSetting([
54 'tabSettings' => [
55 'active' => self::getCurrentTab($tabs),
56 ],
57 ]);
58 return $tabs;
59 }
60
61 /**
62 * @param CRM_Core_Form $form
63 *
64 * @return array
65 */
66 public static function process(&$form) {
67 if ($form->getVar('_id') <= 0) {
68 return NULL;
69 }
70
71 $default = [
72 'link' => NULL,
73 'valid' => FALSE,
74 'active' => FALSE,
75 'current' => FALSE,
76 ];
77
78 $tabs = [
79 'settings' => [
80 'title' => ts('Title'),
81 ] + $default,
82 'amount' => [
83 'title' => ts('Amounts'),
84 ] + $default,
85 'membership' => [
86 'title' => ts('Memberships'),
87 ] + $default,
88 'thankyou' => [
89 'title' => ts('Receipt'),
90 ] + $default,
91 'friend' => [
92 'title' => ts('Tell a Friend'),
93 ] + $default,
94 'custom' => [
95 'title' => ts('Profiles'),
96 ] + $default,
97 'premium' => [
98 'title' => ts('Premiums'),
99 ] + $default,
100 'widget' => [
101 'title' => ts('Widgets'),
102 ] + $default,
103 'pcp' => [
104 'title' => ts('Personal Campaigns'),
105 ] + $default,
106 ];
107
108 $contribPageId = $form->getVar('_id');
109 // Call tabset hook to add/remove custom tabs
110 CRM_Utils_Hook::tabset('civicrm/admin/contribute', $tabs, ['contribution_page_id' => $contribPageId]);
111 $fullName = $form->getVar('_name');
112 $className = CRM_Utils_String::getClassName($fullName);
113
114 // Hack for special cases.
115 switch ($className) {
116 case 'Contribute':
117 $attributes = $form->getVar('_attributes');
118 $class = CRM_Utils_Request::retrieveComponent($attributes);
119 break;
120
121 case 'MembershipBlock':
122 $class = 'membership';
123 break;
124
125 default:
126 $class = strtolower($className);
127 break;
128 }
129
130 if (array_key_exists($class, $tabs)) {
131 $tabs[$class]['current'] = TRUE;
132 $qfKey = $form->get('qfKey');
133 if ($qfKey) {
134 $tabs[$class]['qfKey'] = "&qfKey={$qfKey}";
135 }
136 }
137
138 if ($contribPageId) {
139 $reset = !empty($_GET['reset']) ? 'reset=1&' : '';
140
141 foreach ($tabs as $key => $value) {
142 if (!isset($tabs[$key]['qfKey'])) {
143 $tabs[$key]['qfKey'] = NULL;
144 }
145
146 $tabs[$key]['link'] = CRM_Utils_System::url(
147 "civicrm/admin/contribute/{$key}",
148 "{$reset}action=update&id={$contribPageId}{$tabs[$key]['qfKey']}"
149 );
150 $tabs[$key]['active'] = $tabs[$key]['valid'] = TRUE;
151 }
152 //get all section info.
153 $contriPageInfo = CRM_Contribute_BAO_ContributionPage::getSectionInfo([$contribPageId]);
154
155 foreach ($contriPageInfo[$contribPageId] as $section => $info) {
156 if (!$info) {
157 $tabs[$section]['valid'] = FALSE;
158 }
159 }
160 }
161 return $tabs;
162 }
163
164 /**
165 * @param $form
166 */
167 public static function reset(&$form) {
168 $tabs = self::process($form);
169 $form->set('tabHeader', $tabs);
170 }
171
172 /**
173 * @param $tabs
174 *
175 * @return int|string
176 */
177 public 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 }