Code

Added new setup routine.
[gosa.git] / setup / class_setup.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2007 Fabian Hickert
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
22 /* Returns contents of the given POST variable and check magic quotes settings */
23 function get_post($name)
24 {
25   if(!isset($_POST[$name])){
26     trigger_error("Requested POST value (".$name.") does not exists, you should add a check to prevent this message.");
27     return(FALSE);
28   }
29   if(get_magic_quotes_gpc()){
30     return(stripcslashes($_POST[$name]));
31   }else{
32     return($_POST[$name]);
33   }
34 }
36 require_once("class_setupStep.inc");
39 class setup 
40 {
41   var $i_steps  = 9;  // Number of setup steps 
42   var $i_current= 1;  // Current step
43   var $i_last   = 1;  // Last setup step;
44   var $o_steps  = array(); 
45   var $captured_values = array();
47   function setup()
48   {
49     $i = 1; 
50     $this->o_steps[$i++] = new Step_Welcome();
51     $this->o_steps[$i++] = new Step_Language();
52     $this->o_steps[$i++] = new Step_Checks();
53     $this->o_steps[$i++] = new Step_License();
54     $this->o_steps[$i++] = new Step_Ldap();
55     $this->o_steps[$i++] = new Step_Schema();
56     $this->o_steps[$i++] = new Step_Config1();
57     $this->o_steps[$i++] = new Step_Config2();
58     $this->o_steps[$i++] = new Step_Config3();
59     $this->o_steps[$i++] = new Step_Migrate();
60     $this->o_steps[$i++] = new Step_Feedback();
61     $this->o_steps[$i++] = new Step_Finish();
62     $this->i_steps = $i-1;
64     /* Ensure that setup is not reachable if gosa.conf (CONFIG_FILE) */
65     if(file_exists(CONFIG_DIR."/".CONFIG_FILE)){
66       session_destroy();
67       header("Location: index.php")    ;
68       exit();
69     }
70     
71     foreach($this->o_steps as $key => $step){
72       $this->o_steps[$key]->parent = $this;
73     }
74   }
76   function execute()
77   {
78     /* Display phpinfo() dialog when $_GET['info'] is set,
79      *  but only do this, if user is allowed to use the setup.
80      * If setupStep_Welcome is_completed, we are allowed to view those infos-
81      */
82     if(isset($_GET['info']) && get_class($this->o_steps[1]) == "Step_Welcome" && $this->o_steps[1]->is_completed()){
83       phpinfo();
84       exit();
85     }
87     /* display step error msgs */
88     $msgs = $this->o_steps[$this->i_current]->check();
89     foreach($msgs as $msg){
90       print_red($msg);
91     }
93     $this->o_steps[$this->i_last]->set_active(FALSE);
94     $this->o_steps[$this->i_current]->set_active();
95     $content = $this->o_steps[$this->i_current]->execute();
96     return($content);
97   }
100   /* Save posted attributes  */
101   function save_object()
102   {
103     /* Call save_object for current setup step */
104     $this->o_steps[$this->i_current] -> save_object();
106     /* Get attributes from setup step */
107     $tmp = $this->o_steps[$this->i_current]->get_attributes();
108     foreach($tmp as $name => $value){
109       $this->captured_values[$name] = $value;
110     }
112     /* Set parent */
113     foreach($this->o_steps as $key => $value){
114       $this->o_steps[$key]->parent = $this;
115     }
117     /* Check if image button requests next page */
118     foreach($_POST as $name => $value){
119       if(preg_match("/^next_(x|y)/",$name)){
120         $_POST['next'] = TRUE;
121       }
122       if(preg_match("/^last_(x|y)/",$name)){
123         $_POST['last'] = TRUE;
124       }
125     }
127     /* Check if step was selected */
128     if(isset($_GET['step']) || isset($_POST['next']) || isset($_POST['last'])){
130       /* check if current setup step is completed now 
131           and activate the next step if possible */
132       for($i = 1 ; $i <= $this->i_steps ; $i ++ ){
133         if($this->o_steps[$i]->is_completed()){
134           if(isset($this->o_steps[($i+1)])){
135             $this->o_steps[($i+1)]->set_enabled();
136           }
137         }else{
138           $this->disable_steps_from($i+1);
139         }
140       }
141     }
142  
143     /* Disable all following steps, if one step isn't compelted right now .*/
144     for($i = 1 ; $i <= $this->i_steps ; $i ++ ){
145       if($this->o_steps[$i]->is_completed()){
146       }else{
147         $this->disable_steps_from($i+1);
148       }
149     }
150  
151     $step = -1;
153     if(isset($_POST['setup_goto_step'])){
154       $step= $_POST['setup_goto_step'];
155     }
157     if(isset($_GET['step'])){
158       $step = $_GET['step'];
159     }elseif(isset($_POST['next'])){
160       $step = $this->i_current + 1;
161     }elseif(isset($_POST['last'])){
162       $step = $this->i_current - 1;
163     }
164   
165     $once = true;
166     foreach($_POST as $name => $value){
167       if(preg_match("/^step_[0-9]*$/",$name) && $once ){
168         $step = preg_replace("/^step_/","",$name);
169       }
170     }
172     if($this->selectable_step($step)){
173       $this->i_last    = $this->i_current;
174       $this->i_current = $step;
175     }
176   }
179   function disable_steps_from($start)
180   {
181     $found = false;
182     foreach($this->o_steps as $key => $step){
183       if($key == $start){
184         $found = true;
185       }
187       if($found){ 
188         $this->o_steps[$key]->set_enabled(false);
189         $this->o_steps[$key]->set_completed(false);
190       }
191     }
192   }
195   /* Create navigation menu */
196   function get_navigation_html()
197   {
198     $str = "";
199     foreach($this->o_steps as $key => $step){
201       $step -> update_strings();
203       $s_title    = $step -> get_title();
204       $s_info     = $step -> get_small_info();
205       $b_active   = $step -> is_active();
206       $b_enabled  = $step -> is_enabled();
207       $b_completed= $step -> is_completed();
209       if($b_completed){
210         $s = "<img src='images/true.png' alt='"._("Completed")."' class='center'>&nbsp;"; 
211       }else{
212         $s = "<img src='images/empty.png' alt=' ' class='center'>&nbsp;";
213       }
215       if($_SESSION['js']){
217         $str .="<div >";
218     
219         if($b_enabled){
220           if($b_active){
221             $str .= "<div class='navigation_element_active'>";
222             $str .= "<div class='navigation_title_active'>".$s.$s_title."</div>";
223             $str .= "<div class='navigation_info'>".$s_info."</div>";
224             $str .= "</div>";
225           }else{
226             $str .= "<div class='navigation_element'>";
227             $str .= "<div onClick='document.mainform.setup_goto_step.value=\"$key\";document.mainform.submit();'
228               class='navigation_title_inactive'>".$s.$s_title."</div>";
229             $str .= "</div>";
230           }
231         }else{
232           $str .= "<div class='navigation_element'>";
233           $str .= "<div class='navigation_title_disabled'>".$s.$s_title."</div>";
234           $str .= "</div>";
235         }
236         $str .= "</div>" ;
237       }else{
238         $str .="<div >";
239         if($b_enabled){
240           if($b_active){
241             $str .= "<div class='navigation_element_active'>";
242             $str .= "<input style='text-align: left; color: #00008F; font-weight: bold; width:100%;' 
243                         type='button' value='".$s_title."' name='step_".$key."'>";
244             $str .= "</div>";
245           }else{
246             $str .= "<div class='navigation_element'>";
247             $str .= "<input style='text-align: left; color: #00008F; font-weight: bold; width:100%;' 
248                         type='submit' value='".$s_title."' name='step_".$key."'>";
249             $str .= "</div>";
250           }
251         }else{
252           $str .= "<div class='navigation_element'>";
253           $str .= "<div class='navigation_title_disabled'>".$s_title."</div>";
254           $str .= "</div>";
255         }
256         $str .= "</div>" ;
257       }
258     }
259     return($str);
260   }
262   
264   function get_bottom_html()
265   {
266     /* Skip adding forward/backward button,   
267      *  if the currently opened step is a sub dialog 
268      */
269     if($this->o_steps[$this->i_current]->dialog){
270       $str ="";
271     }else{
272       $str ="<p class='seperator' style='margin-bottom:10px;'>&nbsp;</p>";
273       $str.="   <div style='text-align:right;float:top;'>";
274       if(isset($this->o_steps[$this->i_current -1]) && $this->o_steps[$this->i_current -1]->is_enabled()){
275         $str .= "<input type='submit' name='last' value='"._("Back")."'>";
276       }else{
277         $str .= "<input type='button' name='last' value='"._("Back")."' disabled>";
278       }
279       $str.= "&nbsp;";
280         $str .= "<input type='submit' name='next' value='"._("Continue")."'>";
281       $str .="</div>";
282     }
283     return($str);
284   }
286   
287   /* Create header entry */
288   function get_header_html()
289   {
290     $str=   $this->o_steps[$this->i_current]->print_header();
291     return ($str);
292   }
295   /* Check if the given step id is valid and selectable */
296   function selectable_step($id)
297   {
298     if(isset($this->o_steps[$id]) && $this->o_steps[$id]->is_enabled()){
299       return(true);
300     }
301     return(false);
302   }
304   function step_name_to_id($name)
305   {
306     foreach($this->o_steps as $id => $class){
307       if(get_class($class) == $name){
308         return($id);
309       }
310     }
311     return(0);
312   }
313   
319 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
320 ?>