Introduced isFieldSet().
[core.git] / inc / classes / main / helper / class_BaseHelper.php
index 984475ea9ab5edddba50415fcee5603a1d0fa37d..f0d436f0fa8bf25518f44b2559c3daeae278d651 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.shipsimu.org
  *
@@ -149,6 +149,16 @@ class BaseHelper extends BaseFrameworkSystem {
                return $this->content;
        }
 
+       /**
+        * Public setter for extra instance
+        *
+        * @param       $extraInstance  An extra instance of FrameworkInterface to set
+        * @return      void
+        */
+       public final function setExtraInstance (FrameworkInterface $extraInstance) {
+               $this->extraInstance = $extraInstance;
+       }
+
        /**
         *  Assigns a field from the value instance with a template variable
         *
@@ -217,13 +227,8 @@ class BaseHelper extends BaseFrameworkSystem {
 
                // Is the value instance valid?
                if (is_null($this->valueInstance)) {
-                       try {
-                               // Get the requested instance
-                               $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($this->extraInstance));
-                       } catch (FrameworkException $e) {
-                               // Okay, nothing found so throw a null pointer exception here
-                               throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
-                       }
+                       // Get the requested instance
+                       $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($this->extraInstance));
                } // END - if
        }
 
@@ -253,7 +258,7 @@ class BaseHelper extends BaseFrameworkSystem {
                $this->groups[$this->totalCounter] = $groupId;
                $this->groups[$groupId]['opened']  = TRUE;
                $this->groups[$groupId]['content'] = sprintf(
-                       "<!-- group %s opened (length: %s, tag: %s) //-->%s\n",
+                       '<!-- group %s opened (length: %s, tag: %s) //-->%s' . PHP_EOL,
                        $groupId,
                        strlen($content),
                        $tag,
@@ -367,11 +372,11 @@ class BaseHelper extends BaseFrameworkSystem {
                // Is the content empty?
                if ((empty($content)) && (!empty($this->subGroups[$subGroupId]['tag']))) {
                        // Get it from opener
-                       $content = sprintf("<!-- sub-group %s auto-closed //--></%s>", $subGroupId, $this->subGroups[$subGroupId]['tag']);
+                       $content = sprintf('<!-- sub-group %s auto-closed //--></%s>', $subGroupId, $this->subGroups[$subGroupId]['tag']);
                } // END - if
 
                // Add content to it and mark it as closed
-               $this->subGroups[$subGroupId]['content'] .= sprintf("<!-- sub-group %s closed (length: %s, tag: %s) //-->%s\n", $subGroupId, strlen($content), $this->subGroups[$subGroupId]['tag'], $content);
+               $this->subGroups[$subGroupId]['content'] .= sprintf('<!-- sub-group %s closed (length: %s, tag: %s) //-->%s' . PHP_EOL, $subGroupId, strlen($content), $this->subGroups[$subGroupId]['tag'], $content);
                $this->subGroups[$subGroupId]['opened'] = FALSE
                ;
 
@@ -413,7 +418,7 @@ class BaseHelper extends BaseFrameworkSystem {
                                $content .= trim($subGroupContent);
                        } else {
                                // Something went wrong
-                               $this->debugInstance(__METHOD__."(): Something unexpected happened here.");
+                               $this->debugInstance(__METHOD__ . '(): Something unexpected happened here.');
                        }
                } // END - for
 
@@ -450,20 +455,28 @@ class BaseHelper extends BaseFrameworkSystem {
         * @throws      NullPointerException    Thrown if $valueInstance is null
         */
        public function getValueField ($fieldName) {
+               // Init value
+               $fieldValue = NULL;
+
                // The $valueInstance attribute should not be null!
                if (is_null($this->getValueInstance())) {
                        // Throws an exception here
                        throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
                } // END - if
 
-               // Get the field value
-               $fieldValue = $this->getValueInstance()->getField($fieldName);
-               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).')');
-
-               // Is it null?
-               if ((is_null($fieldValue)) && (!is_null($this->extraInstance))) {
+               // Is the field set?
+               if ($this->getValueInstance()->isFieldSet($fieldName)) {
+                       // Get the field value
+                       $fieldValue = $this->getValueInstance()->getField($fieldName);
+                       //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - Value instance!');
+               } elseif ((!is_null($this->extraInstance)) && ($this->extraInstance->isFieldSet($fieldName))) {
                        // So try the extra instance
                        $fieldValue = $this->extraInstance->getField($fieldName);
+                       //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - Extra instance!');
+               } else {
+                       // Field is not set
+                       if ((!is_null($this->extraInstance)) && ($fieldName == 'city_name')) die('<pre>'.print_r($this->extraInstance, TRUE).'</pre>');
+                       $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] fieldName=' . $fieldName . ' is not set! - @TODO');
                } // END - if
 
                // Return it