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