00001 <?php
00012 class Page_Base
00013 {
00014 var $web;
00015 var $db;
00016 var $validator;
00017 var $sql;
00018 var $models;
00019 var $plugins;
00020 var $ajax;
00021
00022 var $module_name;
00023
00024 var $template;
00025 var $template_layout;
00026
00031 function Page_Base()
00032 {
00033 $this->web =& Registry::get('pronto:web');
00034 $this->db =& Registry::get('pronto:db:main');
00035 $this->sql = new SQL_Generator($this);
00036 $this->models = new stdClass;
00037 $this->plugins = new stdClass;
00038
00039 $this->validator =& Registry::get('pronto:validator');
00040
00041 $this->template = new Template();
00042 $this->set_module('');
00043 $this->set_layout('layout.php');
00044
00045
00046 $this->set_ajax(!!$this->param('_ajax', $this->web->ajax));
00047 $this->template->set('_ajax', $this->ajax);
00048
00049
00050
00051 if(isset($_SESSION['form_data'])) {
00052 $this->template->set('data', $_SESSION['form_data']);
00053 $this->template->set('errors', $_SESSION['form_errors']);
00054 unset($_SESSION['form_data'], $_SESSION['form_errors']);
00055 }
00056
00057
00058 $this->plugins =& Registry::get('pronto:plugins');
00059 }
00060
00067 function set_module($name)
00068 {
00069 $this->module_name = $name;
00070 $this->template->set_module($name);
00071 }
00072
00080 function set_ajax($enable=true)
00081 {
00082 $this->ajax = $enable;
00083 if(is_object($this->web)) $this->web->ajax = $this->ajax;
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00098 function import_model($name)
00099 {
00100 foreach(func_get_args() as $name) {
00101 $this->models->$name =& Factory::model($name);
00102 if($this->models->$name === false) {
00103 trigger_error("Model $name does not exist");
00104 die;
00105 }
00106 }
00107 }
00108
00113 function &import_plugin($name)
00114 {
00115 foreach(func_get_args() as $name) {
00116 $class =& Factory::plugin($name, 'page');
00117 if($class === false) {
00118 trigger_error("Plugin $name does not exist");
00119 die;
00120 }
00121 }
00122
00123 $this->plugins =& Registry::get('pronto:plugins');
00124 if($class) return $class;
00125 }
00126
00127
00128
00129
00130
00131
00132
00138 function redirect($url)
00139 {
00140 $this->web->redirect($url);
00141 }
00142
00149 function redirect_to_referrer($default_url='')
00150 {
00151 if(!$default_url) $default_url = url('/');
00152 $url = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : $default_url;
00153 if($this->ajax) {
00154 $this->ajax_render('', array('redirect_url'=>$url));
00155 } else {
00156 $this->web->redirect($url);
00157 }
00158 }
00159
00168 function return_to_form($url, $data, $errors=array()) {
00169 if($this->ajax) {
00170 $vars = array('errors' => $errors);
00171 $this->ajax_render('', $vars);
00172 } else {
00173 $_SESSION['form_data'] = $data;
00174 $_SESSION['form_errors'] = $errors;
00175 if($url) {
00176 $this->redirect($url);
00177 } else {
00178 $this->redirect_to_referrer();
00179 }
00180 }
00181 }
00182
00183
00184
00185
00186
00187
00188
00194 function flash($message)
00195 {
00196 if(!empty($message)) $_SESSION['_FLASH_MESSAGE'] = $message;
00197 }
00198
00204 function flash_get()
00205 {
00206 return $_SESSION['_FLASH_MESSAGE'];
00207 }
00208
00214 function flash_isset()
00215 {
00216 return !empty($_SESSION['_FLASH_MESSAGE']);
00217 }
00218
00222 function flash_clear()
00223 {
00224 unset($_SESSION['_FLASH_MESSAGE']);
00225 }
00226
00227
00228
00229
00230
00231
00232
00243 function param($key, $default='')
00244 {
00245 $req = Registry::get('pronto:request_args');
00246 if(isset($req[$key])) {
00247 return $this->validator->prepare_input($req[$key]);
00248 }
00249 return $default;
00250 }
00251
00258 function params()
00259 {
00260 $a = array();
00261 foreach(func_get_args() as $k) {
00262 $a[] = $this->param($k);
00263 }
00264 return $a;
00265 }
00266
00274 function load_input($req='')
00275 {
00276 if(!is_array($req)) $req = Registry::get('pronto:request_args');
00277 $ret = array();
00278 foreach($req as $k=>$v) {
00279 $ret[$k] = $this->validator->prepare_input($v);
00280 }
00281 return $ret;
00282 }
00283
00291 function path_args($shift=0)
00292 {
00293 return array_slice(explode('/', $this->web->context->path), $shift);
00294 }
00295
00302 function param_set($key, $val)
00303 {
00304 $ra =& Registry::get('pronto:request_args');
00305 $ra[$key] = $val;
00306 Registry::set('pronto:request_args', $ra);
00307 }
00308
00314 function param_unset($key)
00315 {
00316 $ra =& Registry::get('pronto:request_args');
00317 unset($ra[$key]);
00318 Registry::set('pronto:request_args', $ra);
00319 }
00320
00321
00322
00323
00324
00325
00326
00332 function tget($key)
00333 {
00334 return $this->template->get($key);
00335 }
00341 function tset($key, $var)
00342 {
00343 $this->template->set($key, $var);
00344 }
00349 function tunset($key)
00350 {
00351 $this->template->un_set($key, $var);
00352 }
00358 function tisset($key)
00359 {
00360 return $this->template->is_set($key);
00361 }
00362
00367 function set_layout($filename)
00368 {
00369 $this->template_layout = $filename;
00370 }
00371
00389 function render_element($pagename, $element, $args=array(), $merge_vars=true)
00390 {
00391 $page =& Factory::page($pagename);
00392 if(!is_object($page)) {
00393 trigger_error("$pagename is not a valid Page name");
00394 }
00395 if(method_exists($page, '__init__')) {
00396 call_user_func(array(&$page, '__init__'));
00397 }
00398 $content = call_user_func_array(array(&$page, "ELEM_$element"), $args);
00399
00400
00401 if($this && $merge_vars) {
00402
00403 foreach($page->template->variables as $k=>$v) $this->tset($k, $v);
00404 }
00405
00406 return $content;
00407 }
00408
00415 function fetch($filename, $vars=array(), $language=false)
00416 {
00417 return $this->template->fetch($filename, $vars, $language);
00418 }
00419
00426 function render($filename, $vars=array(), $layout='')
00427 {
00428 if($layout === '') $layout = $this->template_layout;
00429
00430 if($this->ajax) {
00431 $this->ajax_render($filename);
00432 } else {
00433 $this->web->render($this->template, $filename, $vars, $layout);
00434 }
00435 }
00436
00443 function render_string($content, $vars=array(), $layout='')
00444 {
00445 if($layout === '') $layout = $this->template_layout;
00446
00447 $vars['CONTENT_FOR_LAYOUT'] = $content;
00448 $this->web->render($this->template, DIR_FS_APP.DS.'templates'.DS.$layout, $vars);
00449 }
00450
00457 function ajax_render($filename, $jsvars=array())
00458 {
00459 $this->web->ajax_render($this->template, $filename, $jsvars);
00460 }
00461
00467 function ajax_exec($js='')
00468 {
00469 $this->web->ajax_exec($js);
00470 }
00471
00472
00473
00474
00475
00476
00477
00481 function GET()
00482 {
00483 $this->web->notfound();
00484 }
00485
00486
00487
00488
00489 function POST()
00490 {
00491 $this->web->notfound();
00492 }
00493
00494
00495
00496
00497 function PUT()
00498 {
00499 $this->web->notfound();
00500 }
00501
00502
00503
00504
00505 function DELETE()
00506 {
00507 $this->web->notfound();
00508 }
00509
00510
00511
00512
00513 function HEAD()
00514 {
00515 $this->web->notfound();
00516 }
00517 }
00518
00519 ?>