Listing 4-4 (Continued) function setDefault($value, $default) { return (isset($value)) ? $value : $default; } function fileextension($filename) { return substr(basename($filename), strrpos(basename($filename), “.”) + 1); } function outputTemplate(&$t) { $t->parse(‘main’, ‘mainBlock’, false); return $t->parse(‘output’, ‘fh’); } function showScreen($templateFile = null, $func = null, $app_name) { $menuTemplate = new Template($this->getTemplateDir()); $this->doCommonTemplateWork($menuTemplate, $templateFile, $app_name); if ($func != null) { $status = $this->$func($menuTemplate); } if ($status) { return $this->outputTemplate($menuTemplate); } else { return null; } } function doCommonTemplateWork(&$t, $templateFile, $app_name) { 106 Part II: Developing Intranet Solutions 07 549669 ch04.qxd 4/4/03 9:24 AM Page 106 $t->set_file(‘fh’, $templateFile); $t->set_block(‘fh’,’mainBlock’, ‘main’); $t->set_var(array( ‘APP_PATH’ => $this->getAppPath(), ‘APP_NAME’ => $app_name, ‘BASE_URL’ => $this->getBaseURL() ) ); } function getEnvironment($key) { return $_SERVER[$key]; } function showPage($contents = null) { global $THEME_TEMPLATE; global $THEME_TEMPLATE_DIR, $REL_TEMPLATE_DIR; global $REL_TEMPLATE_DIR; global $PHOTO_DIR, $DEFAULT_PHOTO, $REL_PHOTO_DIR; $themeObj = new Theme($this->dbi, null,’home’); $this->themeObj = $themeObj; $this->theme = $themeObj->getUserTheme($this->getUID()); $themeTemplate = new Template($THEME_TEMPLATE_DIR); $themeTemplate->set_file(‘fh’, $THEME_TEMPLATE[$this->theme]); $themeTemplate->set_block(‘fh’, ‘mmainBlock’, ‘mmblock’); $themeTemplate->set_block(‘mmainBlock’, ‘contentBlock’, ‘cnblock’); $themeTemplate->set_block(‘mmainBlock’, ‘printBlock’, ‘prnblock’); $themeTemplate->set_var(‘printBlock’, ‘ ’); $themeTemplate->parse(‘prnblock’, ‘printBlock’,false); $themeTemplate->set_block(‘mmainBlock’, ‘pageBlock’, ‘pblock’); $themeTemplate->set_var(‘pblock’, null); $photoFile = sprintf(“%s/photo%003d.jpg”,$PHOTO_DIR, $this->getUID()); $defaultPhoto = sprintf(“%s/%s”,$REL_PHOTO_DIR,$DEFAULT_PHOTO); $userPhoto = sprintf(“%s/photo%003d.jpg”,$REL_PHOTO_DIR,$this->getUID()); $photo = file_exists($photoFile) ? $userPhoto : $defaultPhoto; Continued Chapter 4: Architecture of an Intranet Application 107 07 549669 ch04.qxd 4/4/03 9:24 AM Page 107 Listing 4-4 (Continued) $themeTemplate->set_var(‘PHOTO’, $photo); $themeTemplate->set_var(‘TEMPLATE_DIR’, $REL_TEMPLATE_DIR); $themeDir = $THEME_TEMPLATE_DIR . ‘/’ . dirname($THEME_TEMPLATE[$this- >theme]); $leftNavigation = $this->themeObj->getLeftNavigation($themeDir); $themeTemplate->set_var(‘LEFT_NAVIGATION’, $leftNavigation); $themeTemplate->set_var(‘SERVER_NAME’, $this->get_server()); $themeTemplate->set_var(‘BASE_HREF’, $REL_TEMPLATE_DIR); $themeTemplate->set_var(‘CONTENT_BLOCK’, $contents); $themeTemplate->parse(‘cnblock’, ‘contentBlock’); $themeTemplate->parse(‘mmblock’, ‘mmainBlock’); $themeTemplate->pparse(‘output’, ‘fh’); } } ?> The methods in the class.PHPApplication.php class, which implements the base application in our framework, are discussed in detail in Table 4-1. TABLE 4-1 METHODS IN CLASS.PHPAPPLICATION.PHP Function Description PHPApplication() The constructor function for PHPApplication (class.PHPApplication.php) class. Sets app_name, app_version, app_type, debug_mode, error, authorized, and has_error member variables. If debug_mode is set to $ON (1), a debugger object called debugger is created. It also creates an error handler from ErrorHandler class. The constructor starts the session using session_start(), and also sets self_url by calling set_url(). check_session() Checks if the session username variable is set, or calls the reauthenticate() function method. 108 Part II: Developing Intranet Solutions 07 549669 ch04.qxd 4/4/03 9:24 AM Page 108 Function Description reauthenticate() Redirects the application user to the authentication application pointed by the global $AUTHENTICATION_URL variable. set_url() Creates a URL that points to the application itself. terminate() Terminates the application. If the application is connected to a database, the database connection is first closed and then the application session is destroyed. authorize() A blank authorized function method that should be overridden by the application. The abstract application object cannot authorize access to the application itself. set_error_handler() Creates an error handler object and stores the object in errHandler member variable. alert() Calls the alert function method from the ErrorHandler class. get_error_message() Gets the error message from the ErrorHandler class. show_debugger_banner() Displays the debug banner if debugging is enabled. (The banner display is done by the debugger class.) buffer_debugging() Sets the debug message buffering in the built-in Debugger object if the debugging is turned on. dump_debuginfo() Flushes the debug buffer if debugging was turned on. debug() Provides a wrapper for the write() method in the built- in debugger. run() Should be overridden by the instance of the PHPApplication to run it. connect() Creates a DBI object and connects the application to the desired relational database. disconnect() Disconnects the application from the database. get_error_message() Returns the error message for a given error code (calls the get_error_message of the ErrorHandler). show_debugger_banner() Prints the debugger banner if debugging is turned on. buffer_debugging() Enables you to buffer debugging so that it can be printed later. Continued Chapter 4: Architecture of an Intranet Application 109 07 549669 ch04.qxd 4/4/03 9:24 AM Page 109 TABLE 4-1 METHODS IN CLASS.PHPAPPLICATION.PHP (Continued) Function Description dump_debuginfo() Dumps all debug information if it was buffered in the built-in debugger object. debug() Writes the debug message using the debugger object’s write() function method. run() A dummy function method that must be overridden by each application to run the application. An application usually has its business logic driver in this method. connect() Creates a DBI object and connects the application to a given database. The database URL is passed as a parameter, and the DBI object is stored as a member variable called dbi in the PHPApplication class. disconnect() Disconnects the database connection for the application by calling the DBI disconnect()method. get_version() Returns the version of the application. The version is supplied as a parameter during PHPApplication object creation. get_name() Returns the name of the application (supplied as a parameter during PHPApplication object creation). get_type() Returns the type of the application (supplied as a parameter during PHPApplication object creation). set_error() Sets error code for the application and also sets the has_error flag to TRUE. (When used to set error code, the error codes are stored in an array called error.) When application needs to generate an error message, you use this function method to set the error code first, and then call get_error_message(). has_error() Returns TRUE if the application has error(s); otherwise it returns FALSE. reset_error() Resets has_error flag to FALSE. get_error() Returns an error code from the error array. get_error_array() Returns the entire error code array. You can get the error code array and use the get_error_message() method to return the appropriate error messages. 110 Part II: Developing Intranet Solutions 07 549669 ch04.qxd 4/4/03 9:24 AM Page 110 . class.PHPApplication .php class, which implements the base application in our framework, are discussed in detail in Table 4-1. TABLE 4-1 METHODS IN CLASS.PHPAPPLICATION .PHP Function Description PHPApplication(). CLASS.PHPAPPLICATION .PHP Function Description PHPApplication() The constructor function for PHPApplication (class.PHPApplication .php) class. Sets app_name, app_version, app_type, debug_mode, error, authorized,. Intranet Application 109 07 549669 ch04.qxd 4/4/03 9:24 AM Page 109 TABLE 4-1 METHODS IN CLASS.PHPAPPLICATION .PHP (Continued) Function Description dump_debuginfo() Dumps all debug information if it