From: Roland Häder Date: Sat, 28 Jun 2008 00:51:37 +0000 (+0000) Subject: More stubs added, code "made" generic: X-Git-Url: https://git.mxchange.org/?p=shipsimu.git;a=commitdiff_plain;h=d0b833b090c7cb50e075e4b08d052d415780faa0 More stubs added, code "made" generic: - Database results are now supported by all objects by moving setter/getter to the super class BaseFrameworkSystem - Login area with more stubs (still working on company data output) - Obsolete $content[some_var] replaced by {?some_var?} in header templates - app_die() slightly improved --- diff --git a/application/ship-simu/main/companies/class_ShippingCompany.php b/application/ship-simu/main/companies/class_ShippingCompany.php index d3ab698..e3bd591 100644 --- a/application/ship-simu/main/companies/class_ShippingCompany.php +++ b/application/ship-simu/main/companies/class_ShippingCompany.php @@ -38,14 +38,14 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner private $founderInstance = null; /** - * Employed people by this company + * Headquarter harbor instance */ - private $employeeList = null; + private $hqInstance = null; /** - * Headquarter harbor instance + * Employed people by this company */ - private $hqInstance = null; + private $employeeList = null; /** * List of all assigned shipyards @@ -62,11 +62,6 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner */ private $contractList = null; - /** - * Database result instance - */ - private $resultInstance = null; - // Exception constants const EXCEPTION_USER_OWNS_NO_COMPANY = 0x200; @@ -95,21 +90,18 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner * * @param $userInstance A user class * @return $companyInstance Prepared company instance - * @throws NoShippingCompanyOwnedException If the user owns no shipping companies */ public final static function createShippingCompany (BaseUser $userInstance) { // Get new instance $companyInstance = new ShippingCompany(); // Does the given user owns a company? - if (!$companyInstance->ifUserOwnsShippingCompany($userInstance)) { - // Throw an exception here - throw new NoShippingCompanyOwnedException(array($companyInstance, $userInstance), self::EXCEPTION_USER_OWNS_NO_COMPANY); + if ($companyInstance->ifUserParticipatesInCompany($userInstance)) { + // Then do some nastly caching here but don't throw an exception + // because then you will hurt our web helpers... :/ + $companyInstance->partialStub("Don't throw exceptions here."); } // END - if - // Set the user instance. We don't care here if he is founder or employee. - $companyInstance->setUserInstance($userInstance); - // Init all lists $companyInstance->initCompanyLists(); @@ -118,34 +110,43 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner } /** - * Checks wether the given user owns a company or not + * Checks wether the given user participates in a company * * @param $userInstance An instance of a user class - * @return $ownsCompany Wether the user owns at least one company + * @return $participates Wether the user participates at lease in one company */ - protected function ifUserOwnsShippingCompany (BaseUser $userInstance) { + protected function ifUserParticipatesInCompany (BaseUser $userInstance) { // By default no user owns any company... ;) - $ownsCompany = false; + $participates = false; // Get a company database wrapper class $wrapperInstance = ObjectFactory::createObjectByConfiguredName('company_db_wrapper_class'); - // Get a search criteria class - $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); + // Ask the wrapper if this user participates + $participates = $wrapperInstance->ifUserParticipatesInCompany($userInstance); - // Add the user primary key as a search criteria - $criteriaInstance->addCriteria('company_owner', $userInstance->getPrimaryKey()); - $criteriaInstance->setLimit(1); + // Get the result instance + $resultInstance = $wrapperInstance->getResultInstance(); - // Get the result back - $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance); - $resultInstance->debugInstance(); + // Caches the result instance here, if set (we don't the wrapper anymore!) + if ($resultInstance instanceof SearchableResult) { + // Set the result instance + $this->setResultInstance($resultInstance); + } // END - if + + // Return result + return $participates; } + //---------------------------------------------------------------------------- + // From here is very old code which needs to be translated and changed heavily + //---------------------------------------------------------------------------- + /** * Intialize all lists * * @return void + * @todo Maybe we don't need these big lists anymore?! So we can deprecate/remove it */ protected function initCompanyLists () { // Employees @@ -158,10 +159,6 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner $this->contractList = new FrameworkArrayObject("FakedContractList"); } - //---------------------------------------------------------------------------- - // From here is very old code which needs to be translated and changed heavily - //---------------------------------------------------------------------------- - // Setter-Methode fuer Firmennamen public final function setCompanyName ($companyName) { $this->companyName = (string) $companyName; diff --git a/application/ship-simu/main/wrapper/class_CompanyDatabaseWrapper.php b/application/ship-simu/main/wrapper/class_CompanyDatabaseWrapper.php index d403d18..b49b1d0 100644 --- a/application/ship-simu/main/wrapper/class_CompanyDatabaseWrapper.php +++ b/application/ship-simu/main/wrapper/class_CompanyDatabaseWrapper.php @@ -23,7 +23,8 @@ */ class CompanyDatabaseWrapper extends BaseDatabaseWrapper { // Constants for database tables - const DB_TABLE_COMPANY = "company"; + const DB_TABLE_COMPANY_DATA = "company"; + const DB_TABLE_COMPANY_USER = "company_user"; /** * Protected constructor @@ -51,11 +52,47 @@ class CompanyDatabaseWrapper extends BaseDatabaseWrapper { $wrapperInstance = new CompanyDatabaseWrapper(); // Set (primary!) table name - $wrapperInstance->setTableName(self::DB_TABLE_COMPANY); + $wrapperInstance->setTableName(self::DB_TABLE_COMPANY_DATA); // Return the instance return $wrapperInstance; } + + /** + * Checks wether the given user participates in a company + * + * @param $userInstance An instance of a user class + * @return $participates Wether the user participates at lease in one company + */ + public function ifUserParticipatesInCompany (BaseUser $userInstance) { + // By default no user owns any company... ;) + $participates = false; + + // Get a search criteria class + $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); + + // Add the user primary key as a search criteria + $criteriaInstance->addCriteria('participant_id', $userInstance->getPrimaryKey()); + $criteriaInstance->setLimit(1); + + // Set company->user table + $this->setTableName(self::DB_TABLE_COMPANY_USER); + + // Get the result back + $resultInstance = $this->doSelectByCriteria($criteriaInstance); + + // Is there a result? + if ($resultInstance->next()) { + // Then cache it + $this->setResultInstance($resultInstance); + + // Entry found for further analysis/processing + $participates = true; + } // END - if + + // Return the result + return $participates; + } } // [EOF] diff --git a/application/ship-simu/templates/de/code/header.ctp b/application/ship-simu/templates/de/code/header.ctp index 2134e43..3a508c0 100644 --- a/application/ship-simu/templates/de/code/header.ctp +++ b/application/ship-simu/templates/de/code/header.ctp @@ -2,7 +2,7 @@ - Ship-Simu Schiffsimulator - $content[title] + Ship-Simu Schiffsimulator - {?title?} diff --git a/application/ship-simu/templates/de/code/login_main.ctp b/application/ship-simu/templates/de/code/login_main.ctp index 3326c51..97fbbd3 100644 --- a/application/ship-simu/templates/de/code/login_main.ctp +++ b/application/ship-simu/templates/de/code/login_main.ctp @@ -28,14 +28,30 @@ $blockInstance->flushContent(); // Get a new instance for personal data $blockInstance = WebBlockHelper::createWebBlockHelper($this, 'company_data'); -// Get the user instance -$userInstance = Registry::getRegistry()->getInstance('user'); - -// Get a shipping company instance ready -$companyInstance = ObjectFactory::createObjectByConfiguredName('company_class', array($userInstance)); +// Set the data source instance +$blockInstance->prefetchValueInstance('company', 'user'); + +// Assign the company name +if ($blockInstance->getValueInstance()->ifUserIsFounder()) { + // User is the founder of the company + $blockInstance->assignMessageField('company_stats', "user_is_company_founder"); + $blockInstance->assignLinkFieldWithAction('company_link' , "company_overview"); +} elseif ($blockInstance->getValueInstance()->ifUserIsOwner()) { + // User owns the company + $blockInstance->assignMessageField('company_stats', "user_is_company_owner"); + $blockInstance->assignLinkFieldWithAction('company_link' , "company_overview"); +} elseif ($blockInstance->getValueInstance()->ifUserIsEmployee()) { + // User is employed in company + $blockInstance->assignMessageField('company_stats', "user_is_employed_in_company"); + $blockInstance->assignLinkFieldWithAction('company_link' , "employee_overview"); +} else { + // No company participation! + $blockInstance->assignMessageField('company_stats', "user_not_assigned_company"); + $blockInstance->assignLinkFieldWithAction('company_link' , "found_new_company"); +} // Flush the content out to a template variable -//$blockInstance->flushContent(); +$blockInstance->flushContent(); // End of all PHP commands ?> diff --git a/inc/classes/main/class_BaseFrameworkSystem.php b/inc/classes/main/class_BaseFrameworkSystem.php index de854be..08abd1f 100644 --- a/inc/classes/main/class_BaseFrameworkSystem.php +++ b/inc/classes/main/class_BaseFrameworkSystem.php @@ -68,6 +68,11 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { */ private $templateInstance = null; + /** + * Database result instance + */ + private $resultInstance = null; + /** * The real class name */ @@ -201,6 +206,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { * Destructor reached... * * @return void + * @todo This is old code. Do we still need this old lost code? */ public function __destruct() { // Is this object already destroyed? @@ -211,7 +217,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { $this->resetUniqueID(); } elseif ((defined('DEBUG_DESTRUCTOR')) && (is_object($this->getDebugInstance()))) { // Already destructed object - $this->getDebugInstance()->output(sprintf("[%s:] Das Objekt %s wurde bereits zerstört.", + $this->getDebugInstance()->output(sprintf("[%s:] The object %s is already destroyed.", __CLASS__, $this->__toString() )); } @@ -320,6 +326,26 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { } } + /** + * Setter for database result instance + * + * @param $resultInstance An instance of a database result class + * @return void + * @todo SearchableResult and UpdateableResult shall have a super interface to use here + */ + protected final function setResultInstance (SearchableResult $resultInstance) { + $this->resultInstance = $resultInstance; + } + + /** + * Getter for database result instance + * + * @return $resultInstance An instance of a database result class + */ + public final function getResultInstance () { + return $this->resultInstance; + } + /** * Setter for template engine instances * diff --git a/inc/classes/main/helper/captcha/images/class_ImageHelper.php b/inc/classes/main/helper/captcha/images/class_ImageHelper.php index 6e28c9f..0f5af54 100644 --- a/inc/classes/main/helper/captcha/images/class_ImageHelper.php +++ b/inc/classes/main/helper/captcha/images/class_ImageHelper.php @@ -369,7 +369,7 @@ class ImageHelper extends BaseCaptcha implements HelpableTemplate { // Add all strings foreach ($this->imageStrings as $id=>$imageString) { - // Set current string id + // Set current string id to keep this helper in sync with template engine $this->currString = $id; // Set variable group diff --git a/inc/classes/main/helper/class_BaseHelper.php b/inc/classes/main/helper/class_BaseHelper.php index dab6791..a40193d 100644 --- a/inc/classes/main/helper/class_BaseHelper.php +++ b/inc/classes/main/helper/class_BaseHelper.php @@ -109,17 +109,33 @@ class BaseHelper extends BaseFrameworkSystem { * Pre-fetches field default values from the given registry key instance into this class * * @param $registryKey Registry key which holds an object with values + * @param $extraKey Extra value instance key used if registryKey is null * @return void - * @throws NullPointerException If an instance from registry is null + * @throws NullPointerException If recovery of requested value instance failed */ - public function prefetchValueInstance ($registryKey) { + public function prefetchValueInstance ($registryKey, $extraKey = null) { // Get the required instance $this->valueInstance = Registry::getRegistry()->getInstance($registryKey); // Is the instance valid? if (is_null($this->valueInstance)) { - // Throw an exception - throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + // Try to create it "from scratch", by first init extra instance + $extraInstance = null; + + // Shall we get an extra instance? + if (!is_null($extraKey)) { + // Get the extra instance. + $extraInstance = Registry::getRegistry()->getInstance($extraKey); + } // END - if + + // Get the requested instance + try { + $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($extraInstance)); + + } catch (FrameworkException $e) { + // Okay, nothing found so throw a null pointer exception here + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } } // END - if } @@ -136,6 +152,15 @@ class BaseHelper extends BaseFrameworkSystem { // Return it return $fieldValue; } + + /** + * Getter for value instance + * + * @return $valueInstance Instance of the class holding our values + */ + public final function getValueInstance () { + return $this->valueInstance; + } } // [EOF] diff --git a/inc/classes/main/reader/class_DefaultNewsReader.php b/inc/classes/main/reader/class_DefaultNewsReader.php index d6b8fc9..65b2e3f 100644 --- a/inc/classes/main/reader/class_DefaultNewsReader.php +++ b/inc/classes/main/reader/class_DefaultNewsReader.php @@ -22,11 +22,6 @@ * along with this program. If not, see . */ class DefaultNewsReader extends BaseFrameworkSystem implements ReadableNews, Registerable { - /** - * Result set with news - */ - private $resultInstance = null; - /** * Protected constructor * @@ -90,7 +85,7 @@ class DefaultNewsReader extends BaseFrameworkSystem implements ReadableNews, Reg $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance); // Save that resultInstance in this class - $this->resultInstance = $resultInstance; + $this->setResultInstance($resultInstance); } } diff --git a/inc/classes/main/user/class_BaseUser.php b/inc/classes/main/user/class_BaseUser.php index e7e4541..e9ce25c 100644 --- a/inc/classes/main/user/class_BaseUser.php +++ b/inc/classes/main/user/class_BaseUser.php @@ -22,11 +22,6 @@ * along with this program. If not, see . */ class BaseUser extends BaseFrameworkSystem { - /** - * Instance of the database result - */ - private $resultInstance = null; - /** * Username of current user */ @@ -71,7 +66,7 @@ class BaseUser extends BaseFrameworkSystem { // Do we have an entry? if (!$this->getResultInstance()->valid()) { - throw new InvalidDatabaseResultException(array($this, $this->resultInstance), DatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT); + throw new InvalidDatabaseResultException(array($this, $this->getResultInstance()), DatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT); } // END - if // Get next entry @@ -122,15 +117,6 @@ class BaseUser extends BaseFrameworkSystem { return $this->email; } - /** - * Getter for database result instance - * - * @return $resultInstance An instance of a database result class - */ - protected final function getResultInstance () { - return $this->resultInstance; - } - /** * Determines wether the username exists or not * @@ -153,7 +139,7 @@ class BaseUser extends BaseFrameworkSystem { $criteriaInstance->setLimit(1); // Get a search result - $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance); + $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance)); } else { // Rewind it $this->getResultInstance()->rewind(); @@ -191,7 +177,7 @@ class BaseUser extends BaseFrameworkSystem { $criteriaInstance->setLimit(1); // Get a search resultInstance - $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance); + $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance)); } else { // Rewind it $this->getResultInstance()->rewind(); @@ -229,7 +215,7 @@ class BaseUser extends BaseFrameworkSystem { $criteriaInstance->setLimit(1); // Get a search resultInstance - $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance); + $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance)); // Search for it if ($this->getResultInstance()->next()) { diff --git a/index.php b/index.php index 9ad3a00..9986c91 100644 --- a/index.php +++ b/index.php @@ -83,31 +83,19 @@ class ApplicationEntryPoint { // Use the template engine for putting out (nicer look) the message try { // Get the template instance from our object factory - $tplEngine = ObjectFactory::createObjectByConfiguredName('template_class', array(FrameworkConfiguration::getInstance()->readConfig('tpl_base_path'), $lang, $io)); - } catch (BasePathIsEmptyException $e) { - die(sprintf("[Main:] Could not initialize template engine for this reason: %s", - $e->getMessage() - )); - } catch (InvalidBasePathStringException $e) { - die(sprintf("[Main:] Could not initialize template engine for this reason: %s", - $e->getMessage() - )); - } catch (BasePathIsNoDirectoryException $e) { - die(sprintf("[Main:] Could not initialize template engine for this reason: %s", - $e->getMessage() - )); - } catch (BasePathReadProtectedException $e) { - die(sprintf("[Main:] Could not initialize template engine for this reason: %s", + $tplEngine = ObjectFactory::createObjectByName($tpl, array(FrameworkConfiguration::getInstance()->readConfig('tpl_base_path'), $lang, $io)); + } catch (FrameworkException $e) { + die(sprintf("[Main:] Could not initialize template engine for reason: %s", $e->getMessage() )); } - // Backtrace holen und aufbereiten + // Get and prepare backtrace for output $backtraceArray = debug_backtrace(); $backtrace = ""; foreach ($backtraceArray as $key=>$trace) { if (!isset($trace['file'])) $trace['file'] = __FILE__; - if (!isset($trace['line'])) $trace['line'] = 5; + if (!isset($trace['line'])) $trace['line'] = __LINE__; if (!isset($trace['args'])) $trace['args'] = array(); $backtrace .= "".basename($trace['file']).":".$trace['line'].", ".$trace['function']."(".count($trace['args']).")
"; } // END - foreach @@ -119,6 +107,7 @@ class ApplicationEntryPoint { $tplEngine->assignVariable('backtrace', $backtrace); $tplEngine->assignVariable('total_includes', ClassLoader::getInstance()->getTotal()); $tplEngine->assignVariable('total_objects', ObjectFactory::getTotal()); + $tplEngine->assignVariable('title', $lang->getMessage('emergency_exit_title')); // Load the template $tplEngine->loadCodeTemplate('emergency_exit'); @@ -168,7 +157,6 @@ class ApplicationEntryPoint { // Include the application selector require(PATH . 'inc/selector.php'); - } // END - main() } // END - class