7b6721d7f52f23aa53276e68685c596fb7fbbb29
2 /***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 **************************************************************************/
22 /** A Wizard Page is a page of a wizard. It is a self-contained step which
23 * handles both the creation and initialisation of the step (by using the
24 * Wizard global state, if needed) and the processing of the action the
27 interface PlWizardPage
29 /** Build a new instance of the class
30 * associated with the given wizard master.
32 public function __construct(PlWizard
&$wiz);
34 /** Return the name of the templace describing the page.
36 public function template();
38 /** Prepare the page by assigning to it any useful value.
40 public function prepare(PlatalPage
&$page);
42 /** Process information resulting of the application of the page.
43 * This function must return a clue indicating the next page to show.
44 * This clue can be either a page id, a page number or a navigation
45 * id (PlWizard::FIRST_PAGE, PlWizard::NEXT_PAGE, PlWizard::CURRENT_PAGE
46 * PlWizard::PREVIOUS_PAGE, PlWizard::LAST_PAGE).
48 public function process();
51 /** A PlWizard is a set of pages through which the user can navigate,
52 * his action on a page determining which the next one will be.
54 * A Wizard can either a stateless wizard (which is only a set of
55 * independent pages through which the user can easily navigate) or
56 * stateful (a suite of steps where each step gives clue for the next
61 const FIRST_PAGE
= 'bt_first';
62 const NEXT_PAGE
= 'bt_next';
63 const CURRENT_PAGE
= 'bt_current';
64 const PREVIOUS_PAGE
= 'bt_previous';
65 const LAST_PAGE
= 'bt_last';
75 public function __construct($name, $layout, $stateless = false
)
77 $this->name
= 'wiz_' . $name;
78 $this->layout
= $layout;
79 $this->stateless
= $stateless;
80 $this->pages
= array();
81 $this->lookup
= array();
82 $this->titles
= array();
83 if (!isset($_SESSION[$this->name
])) {
84 $_SESSION[$this->name
] = array();
86 $_SESSION[$this->name
. '_page'] = null
;
87 $_SESSION[$this->name
. '_stack'] = array();
90 public function addPage($class, $title,$id = null
)
93 $this->lookup
[$id] = count($this->pages
);
95 $this->pages
[] = $class;
96 $this->titles
[] = $title;
99 public function set($varname, $value)
101 $_SESSION[$this->name
][$varname] = $value;
104 public function get($varname, $default = null
)
106 return isset($_SESSION[$this->name
][$varname]) ?
107 $_SESSION[$this->name
][$varname] : $default;
110 public function v($varname, $default = "")
112 return $this->get($varname, $default);
115 public function i($varname, $default = 0)
117 return (int)$this->get($varname, $default);
120 public function clear($varname = null
)
122 if (is_null($varname)) {
123 $_SESSION[$this->name
] = array();
125 unset($_SESSION[$this->name
][$varname]);
127 $_SESSION[$this->name
. '_page'] = null
;
130 private function getPage($id)
132 $page = $this->pages
[$id];
133 return new $page($this);
136 public function apply(PlatalPage
&$smarty, $baseurl, $pgid = null
)
138 $curpage =& $_SESSION[$this->name
. '_page'];
140 // Process the previous page
141 if (!is_null($curpage)) {
142 $page = $this->getPage($curpage);
143 $next = $page->process();
146 case PlWizard
::FIRST_PAGE
:
149 case PlWizard
::PREVIOUS_PAGE
:
150 if (!$this->stateless
&& count($_SESSION[$this->name
. '_stack'])) {
151 $curpage = array_pop($_SESSION[$this->name
. '_stack']);
152 } elseif ($curpage && $this->stateless
) {
158 case PlWizard
::NEXT_PAGE
:
159 if ($curpage < count($this->pages
) - 1) {
163 case PlWizard
::LAST_PAGE
:
164 $curpage = count($this->pages
) - 1;
166 case PlWizard
::CURRENT_PAGE
: break; // don't change the page
168 $curpage = is_numeric($next) ?
$next : $this->lookup
[$curpage];
171 if (!$this->stateless
) {
172 array_push($_SESSION[$this->name
. '_stack'], $last);
175 if ($this->stateless
&& (in_array($pgid, $this->lookup
) ||
isset($this->pages
[$pgid]))) {
178 if (is_null($curpage)) {
183 $page = $this->getPage($curpage);
184 $smarty->changeTpl($this->layout
);
185 $smarty->assign('pages', $this->titles
);
186 $smarty->assign('current', $curpage);
187 $smarty->assign('stateless', $this->stateless
);
188 $smarty->assign('wiz_baseurl', $baseurl);
189 $smarty->assign('tab_width', (int)(99 / count($this->pages
)));
190 $smarty->assign('wiz_page', $page->template());
191 $page->prepare($smarty);
195 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: