/** * this is the main feature of the view, in the MVC paradigm the controller sends updates to the view, this is * the method which captures the updates. * * The uri is essentially the part of the system which we are updating so different output will be negotiated * depending on the value of the uri. * * The data are the things which have changed due to the controller. * * The message is optional, it is for notes, debug information or with json sending messages back alongside the data * * @param string $uri * @param array $data * @return void * @author Craig Ulliott */ public static function update($uri, $data = NULL) { // extract the base from the url, we use this to determine the type of output $uri_r = explode('/', trim($uri, '/'), 2); $base = array_val($uri_r, 0); $path = array_val($uri_r, 1); // for an error we try and determine the best format to send back the message if ($base == 'error') { // if the original request came from AJAX if (array_val($_SERVER, 'HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest') { // rewrite and use the json handler for this error $base = 'json'; $path = 'fail'; $data = array_val($data, 'message', 'Unknown Error'); } else { // pass back the appropriate http code for this error $code = array_val($data, 'code'); switch ($code) { case '404': header("HTTP/1.0 404 Not Found"); break; case '500': header("HTTP/1.0 500 Internal Server Error"); break; default: die('unknown error code "' . $code . '"'); } // use the page handler to display this error $base = 'page'; $path = 'error/' . $code; } } // for an error, we try to determine if we are // we handle the update differently depending on the base of the uri switch ($base) { // these are the different layout files, we are loading a whole page template and passing the result into these layouts case 'page': // we are preparing a full html page $tpl_vars = array(); // the part of the page being updated from teh controller (aka the page contents) $tpl_vars['page_content'] = Template::loadTemplate($path, $data); // script and css clien side includes $tpl_vars['css_url'] = ENV == 'dev' ? '/css/generate' : STATIC_BASE_URL . 'css/style.css'; $tpl_vars['js_url'] = ENV == 'dev' ? '/js/generate' : STATIC_BASE_URL . 'js/script.js'; // todo:: $tpl_vars['css_url'] = '/css/generate'; $tpl_vars['js_url'] = '/js/generate'; // the facebook API key $tpl_vars['fb_api_key'] = getConfiguration('facebook', 'api_key'); // user values $tpl_vars['current_uid'] = CURRENT_UID; $tpl_vars['current_session_key'] = CURRENT_SESSION_KEY; // the parts of the path $path_r = explode('/', $path); // the active section is the first part of the path $active_section = reset($path_r); // used to set an active class on the main tab $tpl_vars['active'] = $active_section; // we build body classes to target css more accurately, one whole class for each parent section $body_classes = array(); while ($path_r) { $body_classes[] = implode('-', $path_r); array_pop($path_r); } // the current login state if (CURRENT_UID) { $body_classes[] = 'logged-in'; } // the current browser (TODO:) if (true == false) { $body_classes[] = 'ie-7'; } // the body classes, used to determine the browser and login state $tpl_vars['body_class'] = implode(' ', $body_classes); // render the full page in either the base or admin layout file $output = Template::loadLayout($base, $tpl_vars); // complete the translations Translator::translate('en'); $output = Translator::parse($output); // useful headers for debugging self::outputDebugHeaders(); // output to the browser die($output); // partial means we are rendering a template (usualy html) but not passing it back into the page template // this is usually for partial page updates preformed by javascript // partial means we are rendering a template (usualy html) but not passing it back into the page template // this is usually for partial page updates preformed by javascript case 'partial': // render the template and output to the browser $output = Template::loadTemplate($path, $data); // complete the translations Translator::translate('en'); $translated_output = Translator::parse($output); // useful headers for debugging self::outputDebugHeaders(); // to hold the output $r = array(); // the rest of the params go into the data key $r['page'] = $translated_output; // the correct content type header('Content-type: application/json'); // build and send the json back to the browser $encoded_output = json_encode($r); die($encoded_output); // json is used by javascript for various AJAX functionality // json is used by javascript for various AJAX functionality case 'json': $r = array(); switch ($path) { // ouput raw json data case 'data': // the content type header('Content-type: application/json'); // build and send the json back to the browser $encoded_output = json_encode($data); die($encoded_output); // success means we simply set the success key to 1, javascript will capture this // success means we simply set the success key to 1, javascript will capture this case 'success': $r['success'] = 1; break; // fail means we simply set the success key to 0, javascript will capture this and handle is as a fail // fail means we simply set the success key to 0, javascript will capture this and handle is as a fail case 'fail': $r['success'] = 0; break; default: throw new exception($path . ' is not a valid path for json output'); } // the data variable is used for sending back a message // it is sent as a blank string if one wasnt provided $r['message'] = (string) $data; // the correct content type header('Content-type: application/json'); // build and send the json back to the browser $encoded_output = json_encode($r); die($encoded_output); // content pass through, with the uri as a content type // content pass through, with the uri as a content type case 'content': // the different content types we accept switch ($path) { // common image types case 'image/png': case 'image/gif': case 'image/jpeg': // css and js // css and js case 'text/css': case 'text/javascript': case 'text/html': // data // data case 'text/csv': // the content type header('Content-type: ' . $path); // other useful headers for debugging self::outputDebugHeaders(); // send to the browser die($data); default: throw new exception($path . ' is not a known safe content type'); } default: throw new exception($base . ' is not a valid base for updating this view'); } }