*
* @author Roland Haeder <webmaster@ship-simu.org>
* @version 0.0.0
- * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software
* @license GNU GPL 3.0 or any newer version
* @link http://www.ship-simu.org
*
*/
class BaseHelper extends BaseFrameworkSystem {
/**
- * Template engine instance
+ * Rendered content created by the helper class
*/
- private $templateInstance = null;
+ private $content = "";
/**
- * Rendered content created by the helper class
+ * Instance to the class which provides field values
*/
- private $content = "";
+ private $valueInstance = null;
+
+ // Exception constants
+ const EXCEPTION_XML_PARSER_ERROR = 0x1e0;
+ const EXCEPTION_XML_NODE_UNKNOWN = 0x1e1;
+ const EXCEPTION_XML_NODE_MISMATCH = 0x1e2;
/**
* Protected constructor
}
/**
- * Setter for template engine instances
+ * Add content
*
- * @param $templateInstance An instance of a template engine class
+ * @param $newContent New content to add
* @return void
*/
- protected final function setTemplateInstance (CompileableTemplate $templateInstance) {
- $this->templateInstance = $templateInstance;
+ protected final function addContent ($newContent) {
+ $this->content .= (string) trim($newContent)."\r\n";
}
/**
- * Getter for template engine instances
+ * Getter for content
*
- * @return $templateInstance An instance of a template engine class
+ * @return $content The rendered content by this helper
*/
- protected final function getTemplateInstance () {
- return $this->templateInstance;
+ protected final function getContent () {
+ return $this->content;
}
/**
- * Add content
+ * Assigns a field from the value instance with a template variable
*
- * @param $newContent New content to add
+ * @param $fieldName Name of the field to assign
* @return void
*/
- protected final function addContent ($newContent) {
- $this->content .= (string) trim($newContent)."\r\n";
+ public function assignField ($fieldName) {
+ // Get the value from value instance
+ $fieldValue = $this->getValueField($fieldName);
+
+ // Assign it with a template variable
+ $this->getTemplateInstance()->assignVariable("block_" . $fieldName, $fieldValue);
}
/**
- * Getter for content
+ * Assigns a field from the value instance with a template variable but
+ * parses its content through a given filter method of the value instance
*
- * @return $content The rendered content by this helper
+ * @param $fieldName Name of the field to assign
+ * @param $filterMethod Method name to call of the value instance
+ * @return void
+ * @todo Rewrite this method using a helper class for filtering data
*/
- protected final function getContent () {
- return $this->content;
+ public function assignFieldWithFilter ($fieldName, $filterMethod) {
+ // Get the value
+ $fieldValue = $this->getValueField($fieldName);
+
+ // Now filter it through the value through the filter method
+ $filteredValue = call_user_func_array(array($this, "doFilter" . $this->convertToClassName($filterMethod)), array($fieldValue));
+
+ // Assign it with a template variable
+ $this->getTemplateInstance()->assignVariable("block_" . $fieldName, $filteredValue);
+ }
+
+ /**
+ * 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 recovery of requested value instance failed
+ */
+ 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)) {
+ // 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
+ }
+
+ /**
+ * Getter for direct field values
+ *
+ * @param $fieldName Name of the field we shall fetch
+ * @return $fieldValue Value from field
+ */
+ public function getValueField ($fieldName) {
+ // Get the field value
+ $fieldValue = call_user_func_array(array($this->valueInstance, 'getField'), array($fieldName));
+
+ // Return it
+ return $fieldValue;
+ }
+
+ /**
+ * Getter for value instance
+ *
+ * @return $valueInstance Instance of the class holding our values
+ */
+ public final function getValueInstance () {
+ return $this->valueInstance;
}
}