]> git.mxchange.org Git - shipsimu.git/blobdiff - application/ship-simu/main/companies/class_ShippingCompany.php
Company stubs now finished, output of 'company_status' added
[shipsimu.git] / application / ship-simu / main / companies / class_ShippingCompany.php
index 381aa4ce8f665e37cbf4457d291d277da0b7b12e..841be84d0134d40f7d4ea03483c837367d7ff16b 100644 (file)
@@ -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,8 +62,11 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
         */
        private $contractList    = null;
 
+       // Exception constants
+       const EXCEPTION_USER_OWNS_NO_COMPANY = 0x200;
+
        /**
-        * Main constructor
+        * Protected constructor
         *
         * @return      void
         */
@@ -72,7 +75,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
                parent::__construct(__CLASS__);
 
                // Set description
-               $this->setObjectDescription("Reederei");
+               $this->setObjectDescription("A shipping company class");
 
                // Generate unique ID number
                $this->generateUniqueId();
@@ -81,73 +84,144 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
                $this->removeSystemArray();
        }
 
-       // Reederei gruenden (create wegen Namenskonvention)
-       public final static function createShippingCompany ($companyName, Harbor $hqInstance) {
+       /**
+        * Creates an instance of this company class or throws an exception if the
+        * given user owns no company.
+        *
+        * @param       $userInstance           A user class
+        * @return      $companyInstance        Prepared company instance
+        */
+       public final static function createShippingCompany (BaseUser $userInstance) {
                // Get new instance
                $companyInstance = new ShippingCompany();
 
-               if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $companyInstance->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> wird gegr&uuml;ndet.",
-                       __CLASS__,
-                       __LINE__,
-                       $companyName
-               ));
+               // Does the given user owns a 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&#39;t throw exceptions here.");
+               } // END - if
 
-               // Firmennamen setzen
-               $companyInstance->setCompanyName($companyName);
+               // Init all lists
+               $companyInstance->initCompanyLists();
 
-               // Kuerzel setzen
-               $companyInstance->createShortName();
+               // Return instance
+               return $companyInstance;
+       }
 
-               // Sitz festlegen
-               $companyInstance->setHQInstance($hqInstance);
+       /**
+        * 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
+        */
+       protected function ifUserParticipatesInCompany (BaseUser $userInstance)  {
+               // By default no user owns any company... ;)
+               $participates = false;
 
-               // Werftenliste erstellen
-               $companyInstance->createshipyardList();
+               // Get a company database wrapper class
+               $wrapperInstance = ObjectFactory::createObjectByConfiguredName('company_db_wrapper_class');
 
-               // Angestellten-Liste erstellen
-               $companyInstance->createEmployeeList();
+               // Ask the wrapper if this user participates
+               $participates = $wrapperInstance->ifUserParticipatesInCompany($userInstance);
 
-               // Auftragsliste erstellen
-               $companyInstance->createContractList();
+               // Get the result instance
+               $resultInstance = $wrapperInstance->getResultInstance();
 
-               // Clean up a little
-               $companyInstance->removeWidth();
-               $companyInstance->removeHeight();
-               $companyInstance->removeLength();
-               $companyInstance->removeDraught();
-               $companyInstance->removePartInstance();
+               // 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
 
-               // Instanz zurueckgeben
-               return $companyInstance;
+               // Return result
+               return $participates;
        }
 
-       // Angestellten-Liste erstellen
-       private function createEmployeeList () {
-               if ((defined('DEBUG_COMPANY_EMPLOYEE')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> erh&auml;lt eine Angestelltenliste.",
-                       __CLASS__,
-                       __LINE__,
-                       $this->getCompanyName()
-               ));
-               $this->employeeList = new FrameworkArrayObject("FakedEmployeeList");
+       /**
+        * Checks wether the current user in registry is the company founder
+        *
+        * @return      $isFounder      Wether the current user is the company founder
+        */
+       public function ifUserIsFounder () {
+               // Default is not the founder
+               $isFounder = false;
+
+               // Get result instance
+               $resultInstance = $this->getResultInstance();
+
+               // Is it set?
+               if ($resultInstance instanceof SearchableResult) {
+                       // Result found so analyse it
+                       $this->partialStub("Check if user is company founder.");
+               } // END - if
+
+               // Return result
+               return $isFounder;
        }
 
-       // Werftenliste erstellen
-       public function createShipyardList () {
-               if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> erh&auml;lt eine Werftsliste.",
-                       __CLASS__,
-                       __LINE__,
-                       $this->getCompanyName()
-               ));
-               $this->shipyardList = new FrameworkArrayObject("FakedShipyardList");
+       /**
+        * Checks wether the current user in registry is the company owner
+        *
+        * @return      $isOwner        Wether the current user is the company owner
+        */
+       public function ifUserIsOwner () {
+               // Default is not the owner
+               $isOwner = false;
+
+               // Get result instance
+               $resultInstance = $this->getResultInstance();
+
+               // Is it set?
+               if ($resultInstance instanceof SearchableResult) {
+                       // Result found so analyse it
+                       $this->partialStub("Check if user is company owner.");
+               } // END - if
+
+               // Return result
+               return $isOwner;
        }
 
-       // Auftragsliste erstellen
-       public function createContractList () {
-               if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> erh&auml;lt eine Auftragsliste.",
-                       __CLASS__,
-                       __LINE__,
-                       $this->getCompanyName()
-               ));
+       /**
+        * Checks wether the current user in registry is an employee in this company
+        *
+        * @return      $isOwner        Wether the current user is an employee in this company
+        */
+       public function ifUserIsEmployee () {
+               // Default is no employee
+               $isEmployee = false;
+
+               // Get result instance
+               $resultInstance = $this->getResultInstance();
+
+               // Is it set?
+               if ($resultInstance instanceof SearchableResult) {
+                       // Result found so he is employee
+                       $isEmployee = true;
+               } // END - if
+
+               // Return result
+               return $isEmployee;
+       }
+
+       //----------------------------------------------------------------------------
+       // 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
+               $this->employeeList = new FrameworkArrayObject("FakedEmployeeList");
+
+               // Ship yards
+               $this->shipyardList = new FrameworkArrayObject("FakedShipyardList");
+
+               // Contracts
                $this->contractList = new FrameworkArrayObject("FakedContractList");
        }
 
@@ -167,12 +241,12 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
        }
 
        // Kuerzel setzen
-       private function createShortName () {
+       private function initShortName () {
                // Mindestens eine Leerstelle?
                $dummy = explode(" ", $this->getCompanyName());
                foreach ($dummy as $part) {
                        $this->shortName .= substr($part, 0, 1);
-               }
+               } // END - if
        }
 
        // Reedereien Werften bauen lassen