Happy new year!
[platal.git] / classes / plwizard.php
index 9a58afe..3667268 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
+ *  Copyright (C) 2003-2008 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -37,7 +37,7 @@ interface PlWizardPage
 
     /** Prepare the page by assigning to it any useful value.
      */
-    public function prepare(PlatalPage &$page);
+    public function prepare(PlatalPage &$page, $id);
 
     /** Process information resulting of the application of the page.
      * This function must return a clue indicating the next page to show.
@@ -67,29 +67,38 @@ class PlWizard
     protected $name;
     protected $layout;
     protected $stateless;
+    protected $ajax;
 
     protected $pages;
+    protected $titles;
     protected $lookup;
+    protected $inv_lookup;
 
-    public function __construct($name, $layout, $stateless = false)
+    public function __construct($name, $layout, $stateless = false, $ajax = true)
     {
-        $this->name   = 'wiz_' . $name;
-        $this->layout = $layout;
+        $this->name      = 'wiz_' . $name;
+        $this->layout    = $layout;
         $this->stateless = $stateless;
         $this->pages  = array();
         $this->lookup = array();
+        $this->titles = array();
+        $this->ajax   = $ajax;
         if (!isset($_SESSION[$this->name])) {
             $_SESSION[$this->name] = array();
+            $_SESSION[$this->name . '_page']  = null;
+            $_SESSION[$this->name . '_stack'] = array();
         }
-        $_SESSION[$this->name . '_page'] = null;
     }
 
-    public function addPage($class, $id = null)
+    public function addPage($class, $title, $id = null)
     {
-        if ($id != null) {
-            $this->lookup[$id] = count($this->pages);
+        if ($id == null) {
+            $id = count($this->pages);
         }
-        $this->pages[] = $class;
+        $this->lookup[$id]  = count($this->pages);
+        $this->inv_lookup[] = $id;
+        $this->pages[]      = $class;
+        $this->titles[]     = $title;
     }
 
     public function set($varname, $value)
@@ -123,49 +132,86 @@ class PlWizard
         $_SESSION[$this->name . '_page'] = null;
     }
 
-    private function &getPage($id)
+    private function getPage($id)
     {
         $page = $this->pages[$id];
         return new $page($this);
     }
 
-    public function apply(PlatalPage &$smarty, $baseurl, $pgid = null)
+    public function apply(PlatalPage &$smarty, $baseurl, $pgid = null, $mode = 'normal')
     {
-        $curpage =& $_SESSION[$this->name . '_page'];
+        if ($this->stateless && (isset($this->lookup[$pgid]) || isset($this->pages[$pgid]))) { 
+            $curpage = is_numeric($pgid) ? $pgid : $this->lookup[$pgid]; 
+        } else if ($this->stateless && is_null($pgid)) {
+            $curpage = 0;
+        } else {
+            $curpage = $_SESSION[$this->name . '_page'];
+        }
+        $oldpage = $curpage;
 
         // Process the previous page
-        if (!is_null($curpage)) {
-            $page = $this->getPage($curpage);
+        if (Post::has('valid_page')) {
+            $page = $this->getPage(Post::i('valid_page'));
+            $curpage = Post::i('valid_page');
             $next = $page->process();
+            $last = $curpage;
             switch ($next) {
               case PlWizard::FIRST_PAGE:
                 $curpage = 0;
                 break;
               case PlWizard::PREVIOUS_PAGE:
-                $curpage--;
+                if (!$this->stateless && count($_SESSION[$this->name . '_stack'])) {
+                    $curpage = array_pop($_SESSION[$this->name . '_stack']);
+                } elseif ($curpage && $this->stateless) {
+                    $curpage--;
+                } else {
+                    $curpage = 0;
+                }
                 break;
               case PlWizard::NEXT_PAGE:
-                $curpage++;
+                if ($curpage < count($this->pages) - 1) {
+                    $curpage++;
+                }
                 break;
               case PlWizard::LAST_PAGE:
                 $curpage = count($this->pages) - 1;
                 break;
               case PlWizard::CURRENT_PAGE: break; // don't change the page
               default:
-                $curpage = is_numeric($next) ? $next : $this->lookup[$curpage];
+                $curpage = is_numeric($next) ? $next : $this->lookup[$next];
                 break;
             }
-        } else {
-            $curpage = 0;
+            if (!$this->stateless) {
+                array_push($_SESSION[$this->name . '_stack'], $last);
+            }
         }
-        if ($this->stateless && (in_array($pgid, $this->lookup) || isset($this->pages[$pgid]))) {
-            $curpage = $pgid;
+        if (is_null($curpage)) {
+            $curpage = 0;
         }
 
         // Prepare the page
-        $page = $this->getPage($curpage);
+        $_SESSION[$this->name . '_page'] = $curpage;
+        if ($curpage != $oldpage) {
+            pl_redirect($baseurl . '/' . $this->inv_lookup[$curpage]);
+        } else if (!isset($page)) {
+            $page = $this->getPage($curpage);
+        }
+        if ($mode == 'ajax') {
+            header('Content-Type: text/html; charset=utf-8');
+            $smarty->changeTpl($page->template(), NO_SKIN);
+        } else {
+            $smarty->changeTpl($this->layout);
+        }
+        $smarty->assign('pages', $this->titles);
+        $smarty->assign('current', $curpage);
+        $smarty->assign('lookup', $this->inv_lookup);
+        $smarty->assign('stateless', $this->stateless);
+        $smarty->assign('wiz_baseurl', $baseurl);
+        $smarty->assign('wiz_ajax', $this->ajax);
+        $smarty->assign('tab_width', (int)(99 / count($this->pages)));
         $smarty->assign('wiz_page', $page->template());
-        $page->prepare($smarty);
+        $smarty->assign('xorg_no_errors', true);
+        $page->prepare($smarty, isset($this->inv_lookup[$curpage]) ? $this->inv_lookup[$curpage] : $curpage);
     }
 }