Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * Helper class to build navigation links | |
20 | */ | |
21 | class CRM_Campaign_Form_Survey_TabHeader { | |
22 | ||
30c4e065 | 23 | /** |
df371444 | 24 | * Build tab header. |
25 | * | |
c490a46a | 26 | * @param CRM_Core_Form $form |
30c4e065 EM |
27 | * |
28 | * @return array | |
29 | */ | |
00be9182 | 30 | public static function build(&$form) { |
6a488035 | 31 | $tabs = $form->get('tabHeader'); |
8cc574cf | 32 | if (!$tabs || empty($_GET['reset'])) { |
6a488035 TO |
33 | $tabs = self::process($form); |
34 | $form->set('tabHeader', $tabs); | |
35 | } | |
36 | $form->assign_by_ref('tabHeader', $tabs); | |
4165b7e5 | 37 | CRM_Core_Resources::singleton() |
96ed17aa | 38 | ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header') |
be2fb01f CW |
39 | ->addSetting([ |
40 | 'tabSettings' => [ | |
6ea503d4 | 41 | 'active' => self::getCurrentTab($tabs), |
be2fb01f CW |
42 | ], |
43 | ]); | |
6a488035 TO |
44 | return $tabs; |
45 | } | |
46 | ||
30c4e065 | 47 | /** |
c490a46a | 48 | * @param CRM_Core_Form $form |
30c4e065 EM |
49 | * |
50 | * @return array | |
51 | */ | |
00be9182 | 52 | public static function process(&$form) { |
6a488035 TO |
53 | if ($form->getVar('_surveyId') <= 0) { |
54 | return NULL; | |
55 | } | |
56 | ||
be2fb01f CW |
57 | $tabs = [ |
58 | 'main' => [ | |
6ea503d4 | 59 | 'title' => ts('Main Information'), |
6a488035 TO |
60 | 'link' => NULL, |
61 | 'valid' => FALSE, | |
62 | 'active' => FALSE, | |
63 | 'current' => FALSE, | |
be2fb01f CW |
64 | ], |
65 | 'questions' => [ | |
6ea503d4 | 66 | 'title' => ts('Questions'), |
6a488035 TO |
67 | 'link' => NULL, |
68 | 'valid' => FALSE, | |
69 | 'active' => FALSE, | |
70 | 'current' => FALSE, | |
be2fb01f CW |
71 | ], |
72 | 'results' => [ | |
6ea503d4 | 73 | 'title' => ts('Results'), |
6a488035 TO |
74 | 'link' => NULL, |
75 | 'valid' => FALSE, | |
76 | 'active' => FALSE, | |
77 | 'current' => FALSE, | |
be2fb01f CW |
78 | ], |
79 | ]; | |
6a488035 | 80 | |
353ffa53 TO |
81 | $surveyID = $form->getVar('_surveyId'); |
82 | $class = $form->getVar('_name'); | |
83 | $class = CRM_Utils_String::getClassName($class); | |
84 | $class = strtolower($class); | |
6a488035 TO |
85 | |
86 | if (array_key_exists($class, $tabs)) { | |
87 | $tabs[$class]['current'] = TRUE; | |
88 | $qfKey = $form->get('qfKey'); | |
89 | if ($qfKey) { | |
90 | $tabs[$class]['qfKey'] = "&qfKey={$qfKey}"; | |
91 | } | |
92 | } | |
93 | ||
94 | if ($surveyID) { | |
0d8afee2 | 95 | $reset = !empty($_GET['reset']) ? 'reset=1&' : ''; |
6a488035 TO |
96 | |
97 | foreach ($tabs as $key => $value) { | |
98 | if (!isset($tabs[$key]['qfKey'])) { | |
99 | $tabs[$key]['qfKey'] = NULL; | |
100 | } | |
101 | ||
102 | $tabs[$key]['link'] = CRM_Utils_System::url("civicrm/survey/configure/{$key}", | |
8547369d | 103 | "{$reset}action=update&id={$surveyID}{$tabs[$key]['qfKey']}" |
6a488035 TO |
104 | ); |
105 | $tabs[$key]['active'] = $tabs[$key]['valid'] = TRUE; | |
106 | } | |
107 | } | |
108 | return $tabs; | |
109 | } | |
110 | ||
30c4e065 | 111 | /** |
c490a46a | 112 | * @param CRM_Core_Form $form |
30c4e065 | 113 | */ |
00be9182 | 114 | public static function reset(&$form) { |
6a488035 TO |
115 | $tabs = self::process($form); |
116 | $form->set('tabHeader', $tabs); | |
117 | } | |
118 | ||
30c4e065 EM |
119 | /** |
120 | * @param $tabs | |
121 | * | |
122 | * @return int|string | |
123 | */ | |
00be9182 | 124 | public static function getCurrentTab($tabs) { |
6a488035 TO |
125 | static $current = FALSE; |
126 | ||
127 | if ($current) { | |
128 | return $current; | |
129 | } | |
130 | ||
131 | if (is_array($tabs)) { | |
132 | foreach ($tabs as $subPage => $pageVal) { | |
133 | if ($pageVal['current'] === TRUE) { | |
134 | $current = $subPage; | |
135 | break; | |
136 | } | |
137 | } | |
138 | } | |
139 | ||
140 | $current = $current ? $current : 'main'; | |
141 | return $current; | |
142 | } | |
143 | ||
30c4e065 EM |
144 | /** |
145 | * @param $form | |
146 | * | |
147 | * @return int|string | |
148 | */ | |
00be9182 | 149 | public static function getNextTab(&$form) { |
6a488035 | 150 | static $next = FALSE; |
6c552737 | 151 | if ($next) { |
6a488035 | 152 | return $next; |
6c552737 | 153 | } |
6a488035 TO |
154 | |
155 | $tabs = $form->get('tabHeader'); | |
156 | if (is_array($tabs)) { | |
ab8a593e | 157 | $current = FALSE; |
6a488035 TO |
158 | foreach ($tabs as $subPage => $pageVal) { |
159 | if ($current) { | |
160 | $next = $subPage; | |
161 | break; | |
162 | } | |
163 | if ($pageVal['current'] === TRUE) { | |
164 | $current = $subPage; | |
165 | } | |
166 | } | |
167 | } | |
168 | ||
169 | $next = $next ? $next : 'main'; | |
170 | return $next; | |
171 | } | |
96025800 | 172 | |
6a488035 | 173 | } |