Another set of PHPdoc corrections
[civicrm-core.git] / CRM / Campaign / Form / Survey / 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_Campaign_Form_Survey_TabHeader {
22
23 /**
24 * Build tab header.
25 *
26 * @param CRM_Core_Form $form
27 *
28 * @return array
29 */
30 public static function build(&$form) {
31 $tabs = $form->get('tabHeader');
32 if (!$tabs || empty($_GET['reset'])) {
33 $tabs = self::process($form);
34 $form->set('tabHeader', $tabs);
35 }
36 $form->assign_by_ref('tabHeader', $tabs);
37 CRM_Core_Resources::singleton()
38 ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header')
39 ->addSetting([
40 'tabSettings' => [
41 'active' => self::getCurrentTab($tabs),
42 ],
43 ]);
44 return $tabs;
45 }
46
47 /**
48 * @param CRM_Core_Form $form
49 *
50 * @return array
51 */
52 public static function process(&$form) {
53 if ($form->getVar('_surveyId') <= 0) {
54 return NULL;
55 }
56
57 $tabs = [
58 'main' => [
59 'title' => ts('Main Information'),
60 'link' => NULL,
61 'valid' => FALSE,
62 'active' => FALSE,
63 'current' => FALSE,
64 ],
65 'questions' => [
66 'title' => ts('Questions'),
67 'link' => NULL,
68 'valid' => FALSE,
69 'active' => FALSE,
70 'current' => FALSE,
71 ],
72 'results' => [
73 'title' => ts('Results'),
74 'link' => NULL,
75 'valid' => FALSE,
76 'active' => FALSE,
77 'current' => FALSE,
78 ],
79 ];
80
81 $surveyID = $form->getVar('_surveyId');
82 $class = $form->getVar('_name');
83 $class = CRM_Utils_String::getClassName($class);
84 $class = strtolower($class);
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) {
95 $reset = !empty($_GET['reset']) ? 'reset=1&' : '';
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}",
103 "{$reset}action=update&id={$surveyID}{$tabs[$key]['qfKey']}"
104 );
105 $tabs[$key]['active'] = $tabs[$key]['valid'] = TRUE;
106 }
107 }
108 return $tabs;
109 }
110
111 /**
112 * @param CRM_Core_Form $form
113 */
114 public static function reset(&$form) {
115 $tabs = self::process($form);
116 $form->set('tabHeader', $tabs);
117 }
118
119 /**
120 * @param array $tabs
121 *
122 * @return int|string
123 */
124 public static function getCurrentTab($tabs) {
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
144 /**
145 * @param CRM_Core_Form $form
146 *
147 * @return int|string
148 */
149 public static function getNextTab(&$form) {
150 static $next = FALSE;
151 if ($next) {
152 return $next;
153 }
154
155 $tabs = $form->get('tabHeader');
156 if (is_array($tabs)) {
157 $current = FALSE;
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 }
172
173 }