From 2aa1e932d56ada938159b6955a27eed0b59bd9cd Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
Date: Sat, 22 Aug 2009 00:37:33 +0000
Subject: [PATCH] Misc fixes: - UserPoints does now load points on every object
 initialization - It does also now advance to first entry by default and
 rewind in many methods - Fixed getField() (?)

---
 .../main/class_BaseFrameworkSystem.php        |  1 +
 inc/classes/main/helper/class_BaseHelper.php  | 38 ++++++++++++++-----
 inc/classes/main/points/class_UserPoints.php  |  9 +++++
 3 files changed, 38 insertions(+), 10 deletions(-)

diff --git a/inc/classes/main/class_BaseFrameworkSystem.php b/inc/classes/main/class_BaseFrameworkSystem.php
index 5237b802..c085e8e4 100644
--- a/inc/classes/main/class_BaseFrameworkSystem.php
+++ b/inc/classes/main/class_BaseFrameworkSystem.php
@@ -966,6 +966,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
 
 		// Get current array
 		$fieldArray = $resultInstance->current();
+		//* DEBUG: */ $this->debugOutput($fieldName.':<pre>'.print_r($fieldArray, true).'</pre>');
 
 		// Does the field exist?
 		if (isset($fieldArray[$fieldName])) {
diff --git a/inc/classes/main/helper/class_BaseHelper.php b/inc/classes/main/helper/class_BaseHelper.php
index 5cd4ea88..81e4197c 100644
--- a/inc/classes/main/helper/class_BaseHelper.php
+++ b/inc/classes/main/helper/class_BaseHelper.php
@@ -27,6 +27,11 @@ class BaseHelper extends BaseFrameworkSystem {
 	 */
 	private $valueInstance = null;
 
+	/**
+	 * Extra instance to the class which provides field values
+	 */
+	private $extraInstance = null;
+
 	/**
 	 * Rendered content created by the helper class
 	 */
@@ -173,6 +178,7 @@ class BaseHelper extends BaseFrameworkSystem {
 	public function assignFieldWithFilter ($fieldName, $filterMethod) {
 		// Get the value
 		$fieldValue = $this->getValueField($fieldName);
+		//* DEBUG: */ $this->debugOutput($fieldName.'='.$fieldValue);
 
 		// Now filter it through the value through the filter method
 		$filteredValue = call_user_func_array(array($this, 'doFilter' . $this->convertToClassName($filterMethod)), array($fieldValue));
@@ -190,28 +196,33 @@ class BaseHelper extends BaseFrameworkSystem {
 	 * @throws	NullPointerException	If recovery of requested value instance failed
 	 */
 	public function prefetchValueInstance ($registryKey, $extraKey = null) {
+		//* DEBUG: */ $this->debugOutput('O:'.$registryKey.'/'.$extraKey);
 		try {
 			// Get the required instance
 			$this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
 		} catch (NullPointerException $e) {
 			// Not set in registry
 			// @TODO Try to log it here
+			//* DEBUG: */ $this->debugOutput($registryKey.'=NULL!');
 		}
 
-		// Is the value 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)) {
+		// Shall we get an extra instance?
+		if (!is_null($extraKey)) {
+			try {
 				// Get the extra instance.
-				$extraInstance = Registry::getRegistry()->getInstance($extraKey);
-			} // END - if
+				$this->extraInstance = Registry::getRegistry()->getInstance($extraKey);
+			} catch (NullPointerException $e) {
+				// Try to create it
+				$this->extraInstance = ObjectFactory::createObjectByConfiguredName($extraKey . '_class', array($this->valueInstance));
+			}
+			//* DEBUG: */ $this->debugOutput($extraKey.'='.$this->extraInstance.' - EXTRA!');
+		} // END - if
 
+		// Is the value instance valid?
+		if (is_null($this->valueInstance)) {
 			try {
 				// Get the requested instance
-				$this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($extraInstance));
+				$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);
@@ -449,6 +460,13 @@ class BaseHelper extends BaseFrameworkSystem {
 
 		// Get the field value
 		$fieldValue = $this->getValueInstance()->getField($fieldName);
+		//* DEBUG: */ $this->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).')');
+
+		// Is it null?
+		if ((is_null($fieldValue)) && (!is_null($this->extraInstance))) {
+			// So try the extra instance
+			$fieldValue = $this->extraInstance->getField($fieldName);
+		} // END - if
 
 		// Return it
 		return $fieldValue;
diff --git a/inc/classes/main/points/class_UserPoints.php b/inc/classes/main/points/class_UserPoints.php
index f4dbad4d..2673f055 100644
--- a/inc/classes/main/points/class_UserPoints.php
+++ b/inc/classes/main/points/class_UserPoints.php
@@ -63,6 +63,9 @@ class UserPoints extends BaseFrameworkSystem implements Registerable, BookablePo
 		// Get result back
 		$resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
 
+		// Advance to first entry by default
+		$resultInstance->next();
+
 		// Set it in this instance
 		$pointsInstance->setResultInstance($resultInstance);
 
@@ -103,6 +106,9 @@ class UserPoints extends BaseFrameworkSystem implements Registerable, BookablePo
 		// Get the required points entry
 		$requiredPoints = $this->getConfigInstance()->getConfigEntry($action . '_action_points');
 
+		// Rewind always
+		$this->getResultInstance()->rewind();
+
 		// Do we have an entry?
 		if ($this->getResultInstance()->next()) {
 			// Get the entry
@@ -123,6 +129,9 @@ class UserPoints extends BaseFrameworkSystem implements Registerable, BookablePo
 	 * @return	void
 	 */
 	function bookPointsDirectly ($amount) {
+		// Rewind always
+		$this->getResultInstance()->rewind();
+
 		// Do we have an entry?
 		if ($this->getResultInstance()->next()) {
 			// Get the entry
-- 
2.39.5