Merge pull request #5687 from relldoesphp/ckeditor_fix
[civicrm-core.git] / CRM / Campaign / Form / Survey / TabHeader.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 public 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(
56 'tabSettings' => array(
57 'active' => self::getCurrentTab($tabs),
58 ),
59 ));
60 return $tabs;
61 }
62
63 /**
64 * @param CRM_Core_Form $form
65 *
66 * @return array
67 */
68 public static function process(&$form) {
69 if ($form->getVar('_surveyId') <= 0) {
70 return NULL;
71 }
72
73 $tabs = array(
74 'main' => array(
75 'title' => ts('Main Information'),
76 'link' => NULL,
77 'valid' => FALSE,
78 'active' => FALSE,
79 'current' => FALSE,
80 ),
81 'questions' => array(
82 'title' => ts('Questions'),
83 'link' => NULL,
84 'valid' => FALSE,
85 'active' => FALSE,
86 'current' => FALSE,
87 ),
88 'results' => array(
89 'title' => ts('Results'),
90 'link' => NULL,
91 'valid' => FALSE,
92 'active' => FALSE,
93 'current' => FALSE,
94 ),
95 );
96
97 $surveyID = $form->getVar('_surveyId');
98 $class = $form->getVar('_name');
99 $class = CRM_Utils_String::getClassName($class);
100 $class = strtolower($class);
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) {
111 $reset = !empty($_GET['reset']) ? 'reset=1&' : '';
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}",
119 "{$reset}action=update&id={$surveyID}{$tabs[$key]['qfKey']}"
120 );
121 $tabs[$key]['active'] = $tabs[$key]['valid'] = TRUE;
122 }
123 }
124 return $tabs;
125 }
126
127 /**
128 * @param CRM_Core_Form $form
129 */
130 public static function reset(&$form) {
131 $tabs = self::process($form);
132 $form->set('tabHeader', $tabs);
133 }
134
135 /**
136 * @param $tabs
137 *
138 * @return int|string
139 */
140 public static function getCurrentTab($tabs) {
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
160 /**
161 * @param $form
162 *
163 * @return int|string
164 */
165 public static function getNextTab(&$form) {
166 static $next = FALSE;
167 if ($next) {
168 return $next;
169 }
170
171 $tabs = $form->get('tabHeader');
172 if (is_array($tabs)) {
173 $current = FALSE;
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 }
188
189 }