From 08b1d39fa38b86cca6a0a6c968162d30ca171ae5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 21 Dec 2008 04:08:50 +0000 Subject: [PATCH] Updating/inserting points finished (basicly), flushing needed database updates moved to BaseFrameworkSystem --- .gitattributes | 15 ++- .../interfaces/database/wrapper/.htaccess | 1 + .../wrapper/class_BookablePointsWrapper.php | 43 +++++++ .../class_ManageableAccountWrapper.php | 43 +++++++ inc/classes/interfaces/points/.htaccess | 1 + .../points/class_BookablePoints.php | 51 +++++++++ .../main/class_BaseFrameworkSystem.php | 49 ++++++++ .../main/criteria/class_BaseCriteria.php | 66 +++++++++++ inc/classes/main/criteria/dataset/.htaccess | 1 + .../{ => dataset}/class_DataSetCriteria.php | 6 +- inc/classes/main/criteria/search/.htaccess | 1 + .../{ => search}/class_SearchCriteria.php | 6 +- inc/classes/main/criteria/update/.htaccess | 1 + .../{ => update}/class_UpdateCriteria.php | 6 +- .../wrapper/class_UserDatabaseWrapper.php | 6 +- .../class_UserPointsDatabaseWrapper.php | 59 +++++++++- inc/classes/main/points/class_UserPoints.php | 107 ++++++++++++++++-- .../main/result/class_DatabaseResult.php | 10 +- inc/classes/main/user/class_BaseUser.php | 6 +- inc/classes/main/user/member/class_Member.php | 32 +----- 20 files changed, 445 insertions(+), 65 deletions(-) create mode 100644 inc/classes/interfaces/database/wrapper/.htaccess create mode 100644 inc/classes/interfaces/database/wrapper/class_BookablePointsWrapper.php create mode 100644 inc/classes/interfaces/database/wrapper/class_ManageableAccountWrapper.php create mode 100644 inc/classes/interfaces/points/.htaccess create mode 100644 inc/classes/interfaces/points/class_BookablePoints.php create mode 100644 inc/classes/main/criteria/class_BaseCriteria.php create mode 100644 inc/classes/main/criteria/dataset/.htaccess rename inc/classes/main/criteria/{ => dataset}/class_DataSetCriteria.php (96%) create mode 100644 inc/classes/main/criteria/search/.htaccess rename inc/classes/main/criteria/{ => search}/class_SearchCriteria.php (96%) create mode 100644 inc/classes/main/criteria/update/.htaccess rename inc/classes/main/criteria/{ => update}/class_UpdateCriteria.php (93%) diff --git a/.gitattributes b/.gitattributes index 4c0aebd5..0544e367 100644 --- a/.gitattributes +++ b/.gitattributes @@ -152,6 +152,9 @@ inc/classes/interfaces/database/frontend/.htaccess -text inc/classes/interfaces/database/frontend/class_DatabaseFrontendInterface.php -text inc/classes/interfaces/database/middleware/.htaccess -text inc/classes/interfaces/database/middleware/class_DatabaseConnector.php -text +inc/classes/interfaces/database/wrapper/.htaccess -text +inc/classes/interfaces/database/wrapper/class_BookablePointsWrapper.php -text +inc/classes/interfaces/database/wrapper/class_ManageableAccountWrapper.php -text inc/classes/interfaces/debug/.htaccess -text inc/classes/interfaces/debug/class_Debugger.php -text inc/classes/interfaces/discovery/.htaccess -text @@ -174,6 +177,8 @@ inc/classes/interfaces/login/.htaccess -text inc/classes/interfaces/login/class_LoginableUser.php -text inc/classes/interfaces/mailer/.htaccess -text inc/classes/interfaces/mailer/class_DeliverableMail.php -text +inc/classes/interfaces/points/.htaccess -text +inc/classes/interfaces/points/class_BookablePoints.php -text inc/classes/interfaces/reader/.htaccess -text inc/classes/interfaces/reader/class_ReadableNews.php -text inc/classes/interfaces/registration/.htaccess -text @@ -268,9 +273,13 @@ inc/classes/main/controller/web/class_WebLogoutDoneController.php -text inc/classes/main/controller/web/class_WebRegisterController.php -text inc/classes/main/controller/web/class_WebStatusController.php -text inc/classes/main/criteria/.htaccess -text -inc/classes/main/criteria/class_DataSetCriteria.php -text -inc/classes/main/criteria/class_SearchCriteria.php -text -inc/classes/main/criteria/class_UpdateCriteria.php -text +inc/classes/main/criteria/class_BaseCriteria.php -text +inc/classes/main/criteria/dataset/.htaccess -text +inc/classes/main/criteria/dataset/class_DataSetCriteria.php -text +inc/classes/main/criteria/search/.htaccess -text +inc/classes/main/criteria/search/class_SearchCriteria.php -text +inc/classes/main/criteria/update/.htaccess -text +inc/classes/main/criteria/update/class_UpdateCriteria.php -text inc/classes/main/crypto/.htaccess -text inc/classes/main/crypto/class_CryptoHelper.php -text inc/classes/main/database/.htaccess -text diff --git a/inc/classes/interfaces/database/wrapper/.htaccess b/inc/classes/interfaces/database/wrapper/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/interfaces/database/wrapper/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/interfaces/database/wrapper/class_BookablePointsWrapper.php b/inc/classes/interfaces/database/wrapper/class_BookablePointsWrapper.php new file mode 100644 index 00000000..00da33b6 --- /dev/null +++ b/inc/classes/interfaces/database/wrapper/class_BookablePointsWrapper.php @@ -0,0 +1,43 @@ + + * @version 0.0.0 + * @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 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +interface BookablePointsWrapper extends FrameworkInterface { + /** + * Inserts the given points for the given user in the database + * + * @param $pointsInstance An instance of a user class + * @return void + */ + function insertUserPoints (BookablePoints $pointsInstance); + + /** + * Updates an user database entry with given result + * + * @param $resultInstance An instance of a Updateable database result + * @return void + */ + function doUpdateByResult (UpdateableResult $resultInstance); +} + +// +?> diff --git a/inc/classes/interfaces/database/wrapper/class_ManageableAccountWrapper.php b/inc/classes/interfaces/database/wrapper/class_ManageableAccountWrapper.php new file mode 100644 index 00000000..f21abfcd --- /dev/null +++ b/inc/classes/interfaces/database/wrapper/class_ManageableAccountWrapper.php @@ -0,0 +1,43 @@ + + * @version 0.0.0 + * @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 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +interface ManageableAccountWrapper extends FrameworkInterface { + /** + * Handles inserting the registration data from a registration instance into the database + * + * @param $registrationInstance An instance of a registration class + * @return void + */ + function insertRegistrationObject (UserRegister $registrationInstance); + + /** + * Updates an user database entry with given result + * + * @param $resultInstance An instance of a Updateable database result + * @return void + */ + function doUpdateByResult (UpdateableResult $resultInstance); +} + +// +?> diff --git a/inc/classes/interfaces/points/.htaccess b/inc/classes/interfaces/points/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/interfaces/points/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/interfaces/points/class_BookablePoints.php b/inc/classes/interfaces/points/class_BookablePoints.php new file mode 100644 index 00000000..80de5b4f --- /dev/null +++ b/inc/classes/interfaces/points/class_BookablePoints.php @@ -0,0 +1,51 @@ + + * @version 0.0.0 + * @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 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +interface BookablePoints extends FrameworkInterface { + /** + * Checks wether the user has the required amount of points left for the specified action + * + * @param $action The action or configuration entry plus prefix the user wants to perform + * @return $hasRequired Wether the user has the required points + */ + function ifUserHasRequiredPoints ($action); + + /** + * "Books" the given points amount on the current user's account + * + * @param $amount Amount of points we shall book + * @return void + */ + function bookPointsDirectly ($amount); + + /** + * Adds registration elements to a given dataset instance + * + * @param $criteriaInstance An instance of a storeable criteria + * @return void + */ + function addElementsToDataSet (StoreableCriteria $criteriaInstance); +} + +// +?> diff --git a/inc/classes/main/class_BaseFrameworkSystem.php b/inc/classes/main/class_BaseFrameworkSystem.php index d4eabd79..d548e584 100644 --- a/inc/classes/main/class_BaseFrameworkSystem.php +++ b/inc/classes/main/class_BaseFrameworkSystem.php @@ -53,6 +53,11 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { */ private $searchInstance = null; + /** + * Update criteria instance + */ + private $updateInstance = null; + /** * The file I/O instance for the template loader */ @@ -211,6 +216,9 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { * @todo This is old code. Do we still need this old lost code? */ public function __destruct() { + // Flush any updated entries to the database + $this->flushPendingUpdates(); + // Is this object already destroyed? if ($this->__toString() != 'DestructedObject') { // Destroy all informations about this class but keep some text about it alive @@ -360,6 +368,25 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { return $this->searchInstance; } + /** + * Setter for update instance + * + * @param $updateInstance Searchable criteria instance + * @return void + */ + public final function setUpdateInstance (LocalUpdateCriteria $updateInstance) { + $this->updateInstance = $updateInstance; + } + + /** + * Getter for update instance + * + * @return $updateInstance Updateable criteria instance + */ + public final function getUpdateInstance () { + return $this->updateInstance; + } + /** * Setter for resolver instance * @@ -1071,6 +1098,28 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { public final function getControllerInstance () { return $this->controllerInstance; } + + /** + * Flushs all pending updates to the database layer + * + * @return void + */ + public function flushPendingUpdates () { + // Get result instance + $resultInstance = $this->getResultInstance(); + + // Do we have data to update? + if ((is_object($resultInstance)) && ($resultInstance->ifDataNeedsFlush())) { + // Get wrapper class name config entry + $configEntry = $resultInstance->getUpdateInstance()->getWrapperConfigEntry(); + + // Create object instance + $wrapperInstance = ObjectFactory::createObjectByConfiguredName($configEntry); + + // Yes, then send the whole result to the database layer + $wrapperInstance->doUpdateByResult($this->getResultInstance()); + } // END - if + } } // [EOF] diff --git a/inc/classes/main/criteria/class_BaseCriteria.php b/inc/classes/main/criteria/class_BaseCriteria.php new file mode 100644 index 00000000..d493bb16 --- /dev/null +++ b/inc/classes/main/criteria/class_BaseCriteria.php @@ -0,0 +1,66 @@ + + * @version 0.0.0 + * @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 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class BaseCriteria extends BaseFrameworkSystem { + /** + * Wrapper class name stored in config entry + */ + private $wrapperConfigEntry = ""; + + /** + * Protected constructor + * + * @param $className Name of the class + * @return void + */ + protected function __construct ($className) { + // Call parent constructor + parent::__construct($className); + + // Clean up a little + $this->removeNumberFormaters(); + $this->removeSystemArray(); + } + + /** + * Setter for wrapper class name + * + * @param $wrapperConfigEntry Configuration entry which hold the wrapper class' name + * @return void + */ + public final function setWrapperConfigEntry ($wrapperConfigEntry) { + $this->wrapperConfigEntry = (string) $wrapperConfigEntry; + } + + /** + * Getter for wrapper class name + * + * @return $wrapperConfigEntry Configuration entry which hold the wrapper class' name + */ + public final function getWrapperConfigEntry () { + return $this->wrapperConfigEntry; + } +} + +// [EOF] +?> diff --git a/inc/classes/main/criteria/dataset/.htaccess b/inc/classes/main/criteria/dataset/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/main/criteria/dataset/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/criteria/class_DataSetCriteria.php b/inc/classes/main/criteria/dataset/class_DataSetCriteria.php similarity index 96% rename from inc/classes/main/criteria/class_DataSetCriteria.php rename to inc/classes/main/criteria/dataset/class_DataSetCriteria.php index 34715c52..f342606e 100644 --- a/inc/classes/main/criteria/class_DataSetCriteria.php +++ b/inc/classes/main/criteria/dataset/class_DataSetCriteria.php @@ -22,7 +22,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class DataSetCriteria extends BaseFrameworkSystem implements StoreableCriteria { +class DataSetCriteria extends BaseCriteria implements StoreableCriteria { /** * Table name */ @@ -51,10 +51,6 @@ class DataSetCriteria extends BaseFrameworkSystem implements StoreableCriteria { protected function __construct() { // Call parent constructor parent::__construct(__CLASS__); - - // Clean up a little - $this->removeNumberFormaters(); - $this->removeSystemArray(); } /** diff --git a/inc/classes/main/criteria/search/.htaccess b/inc/classes/main/criteria/search/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/main/criteria/search/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/criteria/class_SearchCriteria.php b/inc/classes/main/criteria/search/class_SearchCriteria.php similarity index 96% rename from inc/classes/main/criteria/class_SearchCriteria.php rename to inc/classes/main/criteria/search/class_SearchCriteria.php index faeb8be6..91baad35 100644 --- a/inc/classes/main/criteria/class_SearchCriteria.php +++ b/inc/classes/main/criteria/search/class_SearchCriteria.php @@ -23,7 +23,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class SearchCriteria extends BaseFrameworkSystem implements LocalSearchCriteria { +class SearchCriteria extends BaseCriteria implements LocalSearchCriteria { /** * Criteria to handle */ @@ -47,10 +47,6 @@ class SearchCriteria extends BaseFrameworkSystem implements LocalSearchCriteria protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); - - // Clean up a little - $this->removeNumberFormaters(); - $this->removeSystemArray(); } /** diff --git a/inc/classes/main/criteria/update/.htaccess b/inc/classes/main/criteria/update/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/main/criteria/update/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/criteria/class_UpdateCriteria.php b/inc/classes/main/criteria/update/class_UpdateCriteria.php similarity index 93% rename from inc/classes/main/criteria/class_UpdateCriteria.php rename to inc/classes/main/criteria/update/class_UpdateCriteria.php index af3f6a57..9508c45f 100644 --- a/inc/classes/main/criteria/class_UpdateCriteria.php +++ b/inc/classes/main/criteria/update/class_UpdateCriteria.php @@ -23,7 +23,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class UpdateCriteria extends BaseFrameworkSystem implements LocalUpdateCriteria { +class UpdateCriteria extends BaseCriteria implements LocalUpdateCriteria { /** * Criteria to handle */ @@ -47,10 +47,6 @@ class UpdateCriteria extends BaseFrameworkSystem implements LocalUpdateCriteria protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); - - // Clean up a little - $this->removeNumberFormaters(); - $this->removeSystemArray(); } /** diff --git a/inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php b/inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php index 503fbadb..3d1cd78c 100644 --- a/inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php +++ b/inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class UserDatabaseWrapper extends BaseDatabaseWrapper { +class UserDatabaseWrapper extends BaseDatabaseWrapper implements ManageableAccountWrapper { // Constants for exceptions const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0x180; @@ -81,7 +81,7 @@ class UserDatabaseWrapper extends BaseDatabaseWrapper { $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER)); // Set the primary key - $dataSetInstance->setUniqueKey('username'); + $dataSetInstance->setUniqueKey(self::DB_COLUMN_USERNAME); // Add registration elements to the dataset $registrationInstance->addElementsToDataSet($dataSetInstance); @@ -107,7 +107,7 @@ class UserDatabaseWrapper extends BaseDatabaseWrapper { $dataSetInstance->setSearchInstance($resultInstance->getSearchInstance()); // Set the primary key - $dataSetInstance->setUniqueKey('username'); + $dataSetInstance->setUniqueKey(self::DB_COLUMN_USERNAME); // "Update" this request with the database $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance); diff --git a/inc/classes/main/database/wrapper/class_UserPointsDatabaseWrapper.php b/inc/classes/main/database/wrapper/class_UserPointsDatabaseWrapper.php index 0a8af18e..54beb6e3 100644 --- a/inc/classes/main/database/wrapper/class_UserPointsDatabaseWrapper.php +++ b/inc/classes/main/database/wrapper/class_UserPointsDatabaseWrapper.php @@ -21,10 +21,22 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class UserPointsDatabaseWrapper extends BaseDatabaseWrapper { - // Constants for database table names +class UserPointsDatabaseWrapper extends BaseDatabaseWrapper implements BookablePointsWrapper { + /** + * Constants for database table names + */ const DB_TABLE_USER_POINTS = "user_points"; + /** + * Name of the user->points column + */ + const DB_COLUMN_POINTS_UID = "points_uid"; + + /** + * Name of the points column + */ + const DB_COLUMN_POINTS = "points"; + /** * Protected constructor * @@ -50,6 +62,49 @@ class UserPointsDatabaseWrapper extends BaseDatabaseWrapper { // Return the instance return $wrapperInstance; } + + /** + * Inserts the given points for the given user in the database + * + * @param $pointsInstance An instance of a user class + * @return void + */ + public function insertUserPoints (BookablePoints $pointsInstance) { + // Generate a data set for the request + $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER_POINTS)); + + // Set the primary key + $dataSetInstance->setUniqueKey(self::DB_COLUMN_POINTS_UID); + + // Add registration elements to the dataset + $pointsInstance->addElementsToDataSet($dataSetInstance); + + // "Insert" this request instance completely into the database + $this->getDatabaseInstance()->queryInsertDataSet($dataSetInstance); + } + + /** + * Updates an user database entry with given result + * + * @param $resultInstance An instance of a Updateable database result + * @return void + */ + public function doUpdateByResult (UpdateableResult $resultInstance) { + // Generate a data set object + $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER_POINTS)); + + // Add all update criteria to the database set + $resultInstance->addElementsToDataSet($dataSetInstance); + + // Add seach criteria + $dataSetInstance->setSearchInstance($resultInstance->getUpdateInstance()->getSearchInstance()); + + // Set the primary key + $dataSetInstance->setUniqueKey(self::DB_COLUMN_POINTS_UID); + + // "Update" this request with the database + $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance); + } } // [EOF] diff --git a/inc/classes/main/points/class_UserPoints.php b/inc/classes/main/points/class_UserPoints.php index e7b636f4..1dd62ec8 100644 --- a/inc/classes/main/points/class_UserPoints.php +++ b/inc/classes/main/points/class_UserPoints.php @@ -21,7 +21,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class UserPoints extends BaseFrameworkSystem implements Registerable { +class UserPoints extends BaseFrameworkSystem implements Registerable, BookablePoints { + /** + * Amount of points + */ + private $amount = 0; + /** * Protected constructor * @@ -53,6 +58,25 @@ class UserPoints extends BaseFrameworkSystem implements Registerable { return $pointsInstance; } + /** + * Setter for amount + * + * @param $amount Amount of points to store + * @return void + */ + public final function setAmount ($amount) { + $this->amount = (float) $amount; + } + + /** + * Getter for amount + * + * @return $amount Amount of points to store + */ + public final function getAmount () { + return $this->amount; + } + /** * Checks wether the user has the required amount of points left for the specified action * @@ -67,16 +91,16 @@ class UserPoints extends BaseFrameworkSystem implements Registerable { // Get the required points entry $requiredPoints = $this->getConfigInstance()->readConfig($action . '_action_points'); + // Now get a search criteria and set the user's name as criteria + $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); + $searchInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS_UID, $this->getUserInstance()->getUserId()); + $searchInstance->setLimit(1); + // Get a wrapper instance $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_points_db_wrapper_class'); - // Now get a search criteria and set the user's name as criteria - $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); - $criteriaInstance->addCriteria("points_uid", $this->getUserInstance()->getUserName()); - $criteriaInstance->setLimit(1); - // Get result back - $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance); + $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance); // Do we have an entry? if ($resultInstance->next()) { @@ -87,6 +111,75 @@ class UserPoints extends BaseFrameworkSystem implements Registerable { // Return the result return $hasRequired; } + + /** + * "Books" the given points amount on the current user's account + * + * @param $amount Amount of points we shall book + * @return void + */ + function bookPointsDirectly ($amount) { + // Get a critieria instance + $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); + + // Add search criteria + $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); + $searchInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS_UID, $this->getUserInstance()->getUserId()); + $searchInstance->setLimit(1); + + // Get a wrapper instance + $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_points_db_wrapper_class'); + + // Get result back + $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance); + + // Do we have an entry? + if ($resultInstance->next()) { + // Get the entry + $entry = $resultInstance->current(); + + // Add the points + $amount += $entry[UserPointsDatabaseWrapper::DB_COLUMN_POINTS]; + + // Now get another criteria + $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class'); + + // And add our both entries + $updateInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS, $amount); + + // Add the search criteria for searching for the right entry + $updateInstance->setSearchInstance($searchInstance); + + // Set wrapper class name + $updateInstance->setWrapperConfigEntry('user_points_db_wrapper_class'); + + // Remember the update in database result + $resultInstance->add2UpdateQueue($updateInstance); + + // Set it + $this->setResultInstance($resultInstance); + } else { + // Set the amount in class + $this->setAmount($amount); + + // Create the new entry + $wrapperInstance->insertUserPoints($this); + } + } + + /** + * Adds registration elements to a given dataset instance + * + * @param $criteriaInstance An instance of a storeable criteria + * @return void + */ + public function addElementsToDataSet (StoreableCriteria $criteriaInstance) { + // Add user id + $criteriaInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS_UID, $this->getUserInstance()->getUserId()); + + // Add amount + $criteriaInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS, $this->getAmount()); + } } // [EOF] diff --git a/inc/classes/main/result/class_DatabaseResult.php b/inc/classes/main/result/class_DatabaseResult.php index 94fe3b5d..4909b349 100644 --- a/inc/classes/main/result/class_DatabaseResult.php +++ b/inc/classes/main/result/class_DatabaseResult.php @@ -265,17 +265,17 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Up } // END - if } // END - while - // Set affected rows - $this->setAffectedRows($foundEntries); - // If no entry is found/updated throw an exception if ($foundEntries == 0) { // Throw an exception here throw new ResultUpdateException($this, self::EXCEPTION_RESULT_UPDATE_FAILED); } // END - if - // Set search instance - $this->setSearchInstance($searchInstance); + // Set affected rows + $this->setAffectedRows($foundEntries); + + // Set update instance + $this->setUpdateInstance($criteriaInstance); } /** diff --git a/inc/classes/main/user/class_BaseUser.php b/inc/classes/main/user/class_BaseUser.php index b00a0730..ee8a1594 100644 --- a/inc/classes/main/user/class_BaseUser.php +++ b/inc/classes/main/user/class_BaseUser.php @@ -307,6 +307,7 @@ class BaseUser extends BaseFrameworkSystem { * @param $fieldValue New value to store * @return void * @throws DatabaseUpdateSupportException If this class does not support database updates + * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem */ public function updateDatabaseField ($fieldName, $fieldValue) { // Is updating database fields allowed by interface? @@ -325,12 +326,15 @@ class BaseUser extends BaseFrameworkSystem { // Now get another criteria $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class'); - // And add our both entries + // Add criteria entry which we shall update $updateInstance->addCriteria($fieldName, $fieldValue); // Add the search criteria for searching for the right entry $updateInstance->setSearchInstance($searchInstance); + // Set wrapper class name + $updateInstance->setWrapperConfigEntry('user_db_wrapper_class'); + // Remember the update in database result $this->getResultInstance()->add2UpdateQueue($updateInstance); } diff --git a/inc/classes/main/user/member/class_Member.php b/inc/classes/main/user/member/class_Member.php index b67ad88b..f5bf2e88 100644 --- a/inc/classes/main/user/member/class_Member.php +++ b/inc/classes/main/user/member/class_Member.php @@ -32,19 +32,6 @@ class Member extends BaseUser implements ManageableMember, Registerable, Updatea parent::__construct(__CLASS__); } - /** - * Destructor to always flush updates - * - * @return void - */ - public function __destruct () { - // Flush any updated entries to the database - $this->flushPendingUpdates(); - - // Call parent destructor - parent::__destruct(); - } - /** * Creates an instance of this user class by a provided username. This * factory method will check if username is already taken and if not so it @@ -150,25 +137,12 @@ class Member extends BaseUser implements ManageableMember, Registerable, Updatea // Add the search criteria for searching for the right entry $updateInstance->setSearchInstance($searchInstance); + // Set wrapper class name + $updateInstance->setWrapperConfigEntry('user_db_wrapper_class'); + // Remember the update in database result $this->getResultInstance()->add2UpdateQueue($updateInstance); } - - /** - * Flushs all pending updates to the database layer - * - * @return void - */ - public function flushPendingUpdates () { - // Do we have data to update? - if ($this->getResultInstance()->ifDataNeedsFlush()) { - // Get a database wrapper - $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class'); - - // Yes, then send the whole result to the database layer - $wrapperInstance->doUpdateByResult($this->getResultInstance()); - } // END - if - } } // [EOF] -- 2.39.5