[ Index ] |
|
Code source de Plume CMS 1.2.2 |
1 <?php 2 /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 3 /* 4 # ***** BEGIN LICENSE BLOCK ***** 5 # This file is part of Plume CMS, a website management application. 6 # Copyright (C) 2001-2005 Loic d'Anterroches and contributors. 7 # 8 # Plume CMS is free software; you can redistribute it and/or modify 9 # it under the terms of the GNU General Public License as published by 10 # the Free Software Foundation; either version 2 of the License, or 11 # (at your option) any later version. 12 # 13 # Plume CMS is distributed in the hope that it will be useful, 14 # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 # GNU General Public License for more details. 17 # 18 # You should have received a copy of the GNU General Public License 19 # along with this program; if not, write to the Free Software 20 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 # 22 # ***** END LICENSE BLOCK ***** */ 23 24 require_once dirname(__FILE__).'/class.l10n.php'; 25 26 define('PX_TEST_GOOD', 1); 27 define('PX_TEST_WARNING', 2); 28 define('PX_TEST_BAD', 3); 29 30 /** 31 * Class managing the installation process. 32 * 33 * 34 */ 35 class Installer extends CError 36 { 37 var $pages = array(); /**< Pages in the installation. */ 38 var $page_order = array(); /**< Order in the pages. */ 39 var $current_page = ''; 40 var $validated_steps = array(); /**< Steps that went through. */ 41 var $conf_exists = false; /**< Does the config.php exists. */ 42 var $answers = array(); /**< The answers to the questions. */ 43 var $tests = array(); /**< Results of the tests. */ 44 45 /** 46 * Constructor. 47 */ 48 function Installer() 49 { 50 $this->loadConfigFile(); 51 $this->load(); 52 } 53 54 /** 55 * Save the answers in the session. 56 * 57 * @return bool Success 58 */ 59 function save() 60 { 61 $_SESSION['install_ans'] = $this->answers; 62 $_SESSION['install_tests'] = $this->tests; 63 return true; 64 } 65 66 /** 67 * Load the answers from the session. 68 * 69 * @return bool Success 70 */ 71 function load() 72 { 73 if (isset($_SESSION['install_ans'])) { 74 $this->answers = $_SESSION['install_ans']; 75 } 76 if (isset($_SESSION['install_tests'])) { 77 $this->tests = $_SESSION['install_tests']; 78 } 79 return true; 80 } 81 82 83 /** 84 * Add a page in the installer. 85 * 86 * Set the cursor to the current page. 87 * 88 * @param string Id of the page. It must be unique 89 * @param string Title of the page ('') 90 * @param string Description of the page ('') 91 * @return bool Success 92 */ 93 function addPage($id, $title='', $desc='') 94 { 95 if (isset($this->pages[$id])) { 96 $this->setError(sprintf(__('The page %s already exists in the installer'), $id), 500); 97 return false; 98 } 99 $this->page_order[] = $id; 100 $this->current_page = $id; 101 102 $this->pages[$id]['title'] = $title; 103 $this->pages[$id]['description'] = $description; 104 105 return true; 106 } 107 108 /** 109 * Add a question to the current page. 110 * 111 * @param string Id of the question. Must be unique. 112 * @param string The question 113 * @param string Type of answer ('text'), 'combobox', 'password', 114 * 'textarea' 115 * @param mixed Values for answers, combobox values with default choice 116 * or simple text string (''). 117 * @param string Help id, to display corresponding help ('') 118 * @return bool Success 119 */ 120 function addQuestion($id, $question, $type='text', $answer='', $helpid='') 121 { 122 $this->pages[$this->current_page]['q'][] = array($id, $question, $type, 123 $answer, $helpid); 124 return true; 125 } 126 127 128 /** 129 * Add a test to the current page. 130 * 131 * To validate a page, all the tests of the page are run. A test has 132 * 3 possible results success, failure or warning. 133 * 134 * The test function is used to run tests but also to perform installation 135 * action. The test is not "run" at the time the test is added, but when 136 * when the runPageTests() or runAllTests() are called. 137 * A test function must have this declaration: 138 * array(test result, message) = func(&$installer instance, $messageok, 139 * $messagebad, $messagewrning) 140 * Through the Installer instance a test function can access the answers 141 * to all the questions given during the installation procedure. 142 * 143 * @param string Test id. Must be unique. 144 * @param string Test function to call. 145 * @param string Good message ('') 146 * @param string Bad message ('') 147 * @param string Warning message ('') 148 * @return bool Success of the test addition 149 */ 150 function addTest($id, $func, $good='', $bad='', $warn='') 151 { 152 if (isset($this->tests[$id])) { 153 $this->setError(sprintf(__('The test %s is already defined. Each test id must be unique.'), $id), 500); 154 return false; 155 } 156 if (!function_exists($func)) { 157 $this->setError(sprintf(__('The test function %s is not defined.'), 158 $func), 500); 159 return false; 160 161 } 162 $this->pages[$this->current_page]['t'][] 163 = array($id, $func, $good, $bad, $warn); 164 $this->tests[$id] = array(null, ''); // test not run yet 165 return true; 166 } 167 168 /** 169 * Run the tests of a given page. 170 * 171 * @param string Id of the page 172 * @param int Return false on warning (PX_TEST_WARNING) or only errors 173 * PX_TEST_BAD 174 * @return bool Success 175 */ 176 function runPageTests($pageid, $level=PX_TEST_WARNING) 177 { 178 if (!isset($this->pages[$pageid]) 179 || !isset($this->pages[$pageid]['t'])) { 180 $this->setError(sprintf(__('The page %s is not available.'), $pageid), 500); 181 } 182 $success = true; 183 foreach ($this->pages[$pageid]['t'] as $test) { 184 // Here $test[1] is a function that is called 185 $res = $test[1]($this, $test[2], $test[3], $test[4]); 186 if ($res[0] >= $level) { 187 $success = false; 188 } 189 $this->tests[$test[0]] = $res; 190 } 191 return $success; 192 } 193 194 195 /** 196 * Get percentage done. 197 * 198 * @return int Percentage of the steps done. 199 */ 200 function getPercentage() 201 { 202 $n = count($this->pages); 203 for ($i=0; $i<$n; $i++) { 204 if ($this->page_order[$i] == $this->current_step) { 205 return (int) ($i / $n); 206 } 207 } 208 return 0; 209 } 210 211 /** 212 * Get next page id. 213 * 214 * @return mixed Id of the next page or false 215 */ 216 function getNextPage() 217 { 218 $n = count($this->pages); 219 for ($i=0; $i<$n; $i++) { 220 if ($this->page_order[$i] == $this->current_step) { 221 if (isset($this->page_order[$i+1])) { 222 return $this->page_order[$i+1]; 223 } 224 } 225 } 226 return false; 227 } 228 229 /** 230 * Get previous page id. 231 * 232 * @return mixed Id of the previous page or false 233 */ 234 function getPrevPage() 235 { 236 $n = count($this->pages); 237 for ($i=0; $i<$n; $i++) { 238 if ($this->page_order[$i] == $this->current_step) { 239 if ($i>1) { 240 return $this->page_order[$i-1]; 241 } 242 } 243 } 244 return false; 245 } 246 247 /** 248 * Get an answer. 249 * 250 * @param string What to get 251 * @return string The answer 252 */ 253 function getAns($what) 254 { 255 if (isset($this->answers[$what])) { 256 return $this->answers[$what]; 257 } 258 return ''; 259 } 260 261 /** 262 * Set an answer. 263 * 264 * @param string What to set 265 * @param string The answer 266 */ 267 function setAns($what, $ans) 268 { 269 $this->answers[$what] = $ans; 270 } 271 272 /** 273 * Load config file if exists. 274 * 275 * @return bool Success to load it. 276 */ 277 function loadConfigFile() 278 { 279 if (file_exists(config::f('manager_path').'/conf/config.php')) { 280 include_once config::f('manager_path').'/conf/config.php'; 281 $this->conf_exists = true; 282 return true; 283 } else { 284 $this->conf_exists = false; 285 return false; 286 } 287 } 288 289 /** 290 * Return the HTML of the questions. 291 * 292 * @param string Page id 293 * @return string HTML of the page content 294 */ 295 function getPageQuestions($id) 296 { 297 if (!isset($this->pages[$id])) { 298 $this->setError(sprintf(__('The page %s is not available.'), $id), 500); 299 return ''; 300 } 301 $html = ''; 302 foreach ($this->pages[$id]['q'] as $q) { 303 $html .= $this->getQuestionHtml($q); 304 } 305 return $html; 306 } 307 308 /** 309 * Return the HTML of a question 310 * 311 * @param array Definition of the question 312 * @return string HTML of the question 313 */ 314 function getQuestionHtml($q) 315 { 316 $id = $q[0]; 317 $question = $q[1]; 318 $type = $q[2]; 319 $answer = $q[3]; 320 321 $html = '<p><label for="'.$id.'">'.$question.'</label> '; 322 switch ($type) { 323 case 'combobox': 324 $html .= form::combobox($id, $answer['choices'], 325 $this->getAns($id), $answer['default']); 326 break; 327 case 'textarea': 328 $html .= form::textArea($id, 60, 5, $this->getAns($id), '', 329 'style="width:100%"'); 330 break; 331 case 'password': 332 $html .= form::passwordField($id, 50, 255, $this->getAns($id)); 333 $html .= '</p><p><label for="'.$id.'-confirm">' 334 .__('Please confirm the password:').'</label> '; 335 $html .= form::passwordField($id.'-confirm', 50, 255, 336 $this->getAns($id.'-confirm')); 337 break; 338 case 'text': 339 default: 340 $html .= form::textField($id, 50, 255, $this->getAns($id)); 341 } 342 $html .= '</p>'."\n\n"; 343 return $html; 344 } 345 346 } 347 348 /** 349 * Ce que doit faire l'installer. 350 * - stocker les reponses de l'utilisateur 351 * - trouver/demander la langue de l'utilisateur 352 * - pouvoir se "serialiser" dans une session 353 * - collecte et contrôle des informations dans tous les étapes 354 * - écriture réelle uniquement à la fin avec possibilité de répéter une 355 * étape foireuse 356 * - explication avan et après de ce qui va se faire 357 * - possibilité de retoure en arrière et de passage d'une étape à l'autre. 358 * - affichage des étapes dans un "menu" sur le côté 359 * - possibilités d'un scénario selon l'hébergeur ?? (depuis fichier xml) 360 * 361 */ 362 /* 363 - Pour chaque page les boutons [<<Previous] [Next>>] 364 - Stockage des infos via des champs "hidden" pour éviter les problèmes 365 avec les sessions (free.fr) 366 - On garde les différentes pages .php, ce n'est pas un problème 367 368 369 */ 370 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 11:57:01 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |