* @copyright Copyright © 2006 Peter Adams * @license http://www.gnu.org/copyleft/gpl.html GPL v2.0 * @category owa * @package owa * @version $Revision$ * @since owa 1.0.0 */ class owa_view extends owa_base { /** * Main view template object * * @var object */ var $t; /** * Body content template object * * @var object */ var $body; /** * Sub View object * * @var object */ var $subview; /** * Rednered subview * * @var string */ var $subview_rendered; /** * CSS file for main template * * @var unknown_type */ var $css_file; /** * The priviledge level required to access this view * @depricated * @var string */ var $priviledge_level; /** * Type of page * * @var unknown_type */ var $page_type; /** * Request Params * * @var unknown_type */ var $params; /** * Authorization object * * @var object */ var $auth; var $module; // set by factory. var $data; var $default_subview; var $is_subview; var $js = array(); var $css = array(); var $postProcessView = false; var $renderJsInline; /** * Constructor * */ function __construct($params = null) { parent::__construct($params); $this->t = new owa_template(); $this->body = new owa_template($this->module); $this->setTheme(); //header('Content-type: text/html; charset=utf-8'); } /** * Assembles the view using passed model objects * * @param unknown_type $data * @return unknown */ function assembleView($data) { $this->e->debug('Assembling view: '.get_class($this)); // set view name in template class. used for navigation. if (array_key_exists('view', $this->data)) { $this->body->caller_params['view'] = $this->data['view']; } if (array_key_exists('params', $this->data)): $this->body->set('params', $this->data['params']); endif; if (array_key_exists('subview', $this->data)): $this->body->caller_params['subview'] = $this->data['subview']; endif; // Assign status msg if (array_key_exists('status_msg', $this->data)): $this->t->set('status_msg', $this->data['status_msg']); endif; // get status msg from code passed on the query string from a redirect. if (array_key_exists('status_code', $this->data)): $this->t->set('status_msg', $this->getMsg($this->data['status_code'])); endif; // set error msg directly if passed from constructor if (array_key_exists('error_msg', $this->data)): $this->t->set('error_msg', $this->data['error_msg']); endif; // authentication status if (array_key_exists('auth_status', $this->data)): $this->t->set('authStatus', $this->data['auth_status']); endif; // get error msg from error code passed on the query string from a redirect. if (array_key_exists('error_code', $this->data)): $this->t->set('error_msg', $this->getMsg($this->data['error_code'])); endif; // load subview if (!empty($this->data['subview']) || !empty($this->default_subview)): // Load subview $this->loadSubView($this->data['subview']); endif; // construct main view. This might set some properties of the subview. if (method_exists($this, 'render')) { $this->render($this->data); } else { // old style $this->construct($this->data); } //array of errors usually used for field validations if (array_key_exists('validation_errors', $this->data)): $this->body->set('validation_errors', $this->data['validation_errors']); endif; // pagination if (array_key_exists('pagination', $this->data)): $this->body->set('pagination', $this->data['pagination']); endif; $this->_setLinkState(); // assemble subview if (!empty($this->data['subview'])): // set view name in template. used for navigation. $this->subview->body->caller_params['view'] = $this->data['subview']; // Set validation errors $this->subview->body->set('validation_errors', $this->get('validation_errors')); // pagination if (array_key_exists('pagination', $this->data)): $this->subview->body->set('pagination', $this->data['pagination']); endif; if (array_key_exists('params', $this->data)): $this->subview->body->set('params', $this->data['params']); $this->subview->body->set('do', $this->data['params']['do']); endif; // Load subview $this->renderSubView($this->data); // assign subview to body template $this->body->set('subview', $this->subview_rendered); endif; // assign validation errors if (!empty($this->data['validation_errors'])) { $ves = new owa_template('base'); $ves->set_template('error_validation_summary.tpl'); $ves->set('validation_errors', $this->data['validation_errors']); $validation_errors_summary = $ves->fetch(); $this->t->set('error_msg', $validation_errors_summary); } // fire post method $this->post(); // assign css and js ellements if the view is not a subview. // subview css/js have been merged/pulls from subview and assigned here. if ($this->is_subview != true) { if (!empty($this->css)) { $this->t->set('css', $this->css); } if (!empty($this->js)) { $this->t->set('js', $this->js); } } //Assign body to main template $this->t->set('config', $this->config); //Assign body to main template $this->t->set('body', $this->body); if ($this->postProcessView === true){ return $this->postProcess(); } else { // Return fully asembled View return $this->t->fetch(); } } /** * Abstract Alternative rendering method reuires the setting of $this->postProcessView to fire * */ function postProcess() { return false; } /** * Post method fired right before view is rendered and returned * as output */ function post() { return false; } /** * Sets the theme to be used by a view * */ function setTheme() { $this->t->set_template($this->config['report_wrapper']); return; } /** * Abstract method for assembling a view * @depricated * @param array $data */ function construct($data) { return; } /** * Assembles subview * * @param array $data */ function loadSubView($subview) { if (empty($subview)): if (!empty($this->default_subview)): $subview = $this->default_subview; $this->data['subview'] = $this->default_subview; else: return $this->e->debug("No Subview was specified by caller."); endif; endif; $this->subview = owa_coreAPI::subViewFactory($subview); //print_r($subview.'///'); $this->subview->setData($this->data); return; } /** * Assembles subview * * @param array $data */ function renderSubView($data) { // Stores subview as string into $this->subview $this->subview_rendered = $this->subview->assembleSubView($data); // pull css and jas elements needed by subview $this->css = array_merge($this->css, $this->subview->css); $this->js = array_merge($this->js, $this->subview->js); return; } /** * Assembles the view using passed model objects * * @param unknown_type $data * @return unknown */ function assembleSubView($data) { // construct main view. This might set some properties of the subview. if (method_exists($this, 'render')) { $this->render($data); } else { // old style $this->construct($data); } $this->t->set_template('wrapper_subview.tpl'); //Assign body to main template $this->t->set('body', $this->body); // Return fully asembled View $page = $this->t->fetch(); return $page; } function setCss($path) { $url = owa_coreAPI::getSetting('base', 'modules_url').$path; $this->css[] = $url; return; } function setJs($name, $path, $version ='', $deps = array(), $ie_only = false) { if (empty($version)) { $version = OWA_VERSION; } $uid = $name.$version; $url = sprintf('%s?version=%s', owa_coreAPI::getSetting('base', 'modules_url').$path, $version); $this->js[$uid]['url'] = $url; // build file system path just in case we need to concatenate the JS into a single file. $fs_path = OWA_MODULES_DIR.$path; $this->js[$uid]['path'] = $fs_path; $this->js[$uid]['deps'] = $deps; $this->js[$uid]['version'] = $version; $this->js[$uid]['ie_only'] = $ie_only; return; } function concatinateJs() { $js_libs = ''; foreach ($this->js as $lib) { $js_libs .= file_get_contents($lib['path']); $js_libs .= "\n\n"; } $this->body->set('js_includes', $js_libs); return; } /** * Sets flag to tell view to render the JS inline as