Merge pull request #4621 from atif-shaikh/CRM-15589
[civicrm-core.git] / CRM / Campaign / Form / Survey / TabHeader.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * Helper class to build navigation links
38 */
39 class CRM_Campaign_Form_Survey_TabHeader {
40
41 /**
42 * @param CRM_Core_Form $form
43 *
44 * @return array
45 */
46 static function build(&$form) {
47 $tabs = $form->get('tabHeader');
48 if (!$tabs || empty($_GET['reset'])) {
49 $tabs = self::process($form);
50 $form->set('tabHeader', $tabs);
51 }
52 $form->assign_by_ref('tabHeader', $tabs);
53 CRM_Core_Resources::singleton()
54 ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header')
55 ->addSetting(array('tabSettings' => array(
56 'active' => self::getCurrentTab($tabs),
57 )));
58 return $tabs;
59 }
60
61 /**
62 * @param CRM_Core_Form $form
63 *
64 * @return array
65 */
66 static function process(&$form) {
67 if ($form->getVar('_surveyId') <= 0) {
68 return NULL;
69 }
70
71 $tabs = array(
72 'main' => array('title' => ts('Main Information'),
73 'link' => NULL,
74 'valid' => FALSE,
75 'active' => FALSE,
76 'current' => FALSE,
77 ),
78 'questions' => array('title' => ts('Questions'),
79 'link' => NULL,
80 'valid' => FALSE,
81 'active' => FALSE,
82 'current' => FALSE,
83 ),
84 'results' => array('title' => ts('Results'),
85 'link' => NULL,
86 'valid' => FALSE,
87 'active' => FALSE,
88 'current' => FALSE,
89 ),
90 );
91
92 $surveyID = $form->getVar('_surveyId');
93 $class = $form->getVar('_name');
94 $class = CRM_Utils_String::getClassName($class);
95 $class = strtolower($class);
96
97 if (array_key_exists($class, $tabs)) {
98 $tabs[$class]['current'] = TRUE;
99 $qfKey = $form->get('qfKey');
100 if ($qfKey) {
101 $tabs[$class]['qfKey'] = "&qfKey={$qfKey}";
102 }
103 }
104
105 if ($surveyID) {
106 $reset = !empty($_GET['reset']) ? 'reset=1&' : '';
107
108 foreach ($tabs as $key => $value) {
109 if (!isset($tabs[$key]['qfKey'])) {
110 $tabs[$key]['qfKey'] = NULL;
111 }
112
113 $tabs[$key]['link'] = CRM_Utils_System::url("civicrm/survey/configure/{$key}",
114 "{$reset}action=update&id={$surveyID}{$tabs[$key]['qfKey']}"
115 );
116 $tabs[$key]['active'] = $tabs[$key]['valid'] = TRUE;
117 }
118 }
119 return $tabs;
120 }
121
122 /**
123 * @param CRM_Core_Form $form
124 */
125 static function reset(&$form) {
126 $tabs = self::process($form);
127 $form->set('tabHeader', $tabs);
128 }
129
130 /**
131 * @param $tabs
132 *
133 * @return int|string
134 */
135 static function getCurrentTab($tabs) {
136 static $current = FALSE;
137
138 if ($current) {
139 return $current;
140 }
141
142 if (is_array($tabs)) {
143 foreach ($tabs as $subPage => $pageVal) {
144 if ($pageVal['current'] === TRUE) {
145 $current = $subPage;
146 break;
147 }
148 }
149 }
150
151 $current = $current ? $current : 'main';
152 return $current;
153 }
154
155 /**
156 * @param $form
157 *
158 * @return int|string
159 */
160 static function getNextTab(&$form) {
161 static $next = FALSE;
162 if ($next)
163 return $next;
164
165 $tabs = $form->get('tabHeader');
166 if (is_array($tabs)) {
167 $current = false;
168 foreach ($tabs as $subPage => $pageVal) {
169 if ($current) {
170 $next = $subPage;
171 break;
172 }
173 if ($pageVal['current'] === TRUE) {
174 $current = $subPage;
175 }
176 }
177 }
178
179 $next = $next ? $next : 'main';
180 return $next;
181 }
182 }