From: Roland Häder Date: Sun, 21 Dec 2008 04:08:50 +0000 (+0000) Subject: Updating/inserting points finished (basicly), flushing needed database updates moved... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=08b1d39fa38b86cca6a0a6c968162d30ca171ae5;p=core.git Updating/inserting points finished (basicly), flushing needed database updates moved to BaseFrameworkSystem --- 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/class_DataSetCriteria.php b/inc/classes/main/criteria/class_DataSetCriteria.php deleted file mode 100644 index 34715c52..00000000 --- a/inc/classes/main/criteria/class_DataSetCriteria.php +++ /dev/null @@ -1,187 +0,0 @@ - - * @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 DataSetCriteria extends BaseFrameworkSystem implements StoreableCriteria { - /** - * Table name - */ - private $tableName = ""; - - /** - * Table columns (criteria) to store - */ - private $tableColumns = array(); - - /** - * Unique key - */ - private $uniqueKey = ""; - - /** - * Primary key - */ - private $primaryKey = ""; - - /** - * Protected constructor - * - * @return void - */ - protected function __construct() { - // Call parent constructor - parent::__construct(__CLASS__); - - // Clean up a little - $this->removeNumberFormaters(); - $this->removeSystemArray(); - } - - /** - * Creates an instance of this criteria - * - * @param $tableName Name of the table - * @return $criteriaInstance An instance of this criteria - */ - public final static function createDataSetCriteria ($tableName) { - // Get a new instance - $criteriaInstance = new DataSetCriteria(); - - // Set table name - $criteriaInstance->setTableName($tableName); - - // Return the instance - return $criteriaInstance; - } - - /** - * Add criteria - * - * @param $criteriaKey Criteria key - * @param $criteriaValue Criteria value - * @return void - */ - public function addCriteria ($criteriaKey, $criteriaValue) { - $this->tableColumns[(string) $criteriaKey] = $criteriaValue; - } - - /** - * Add configured criteria - * - * @param $criteriaKey Criteria key - * @param $configEntry Configuration entry - * @return void - */ - public function addConfiguredCriteria ($criteriaKey, $configEntry) { - // Add configuration entry as criteria - $value = $this->getConfigInstance()->readConfig($configEntry); - $this->addCriteria($criteriaKey, $value); - } - - /** - * Setter for table name - * - * @param $tableName Name of the table to set - * @return void - */ - public final function setTableName ($tableName) { - $this->tableName = (string) $tableName; - } - - /** - * Getter for table name - * - * @return $tableName Name of the table to set - */ - public final function getTableName () { - return $this->tableName; - } - - /** - * Setter for unique key - * - * @param $uniqueKey Column to use as unique key - * @return void - */ - public final function setUniqueKey ($uniqueKey) { - $this->uniqueKey = (string) $uniqueKey; - } - - /** - * Getter for unique key - * - * @return $uniqueKey Column to use as unique key - */ - public final function getUniqueKey () { - return $this->uniqueKey; - } - - /** - * Getter for unique key value - * - * @return $uniqueValue Value of the unique key - */ - public final function getUniqueValue () { - return $this->tableColumns[$this->getUniqueKey()]; - } - - /** - * Getter for criteria array - * - * @return $tableColumns - */ - public final function getCriteriaArray () { - return $this->tableColumns; - } - - /** - * Getter for primary key or unique key if not set - * - * @return $primaryKey Primary key or unique key if not set - */ - public final function getPrimaryKey () { - // Get primary key by default - $primaryKey = $this->primaryKey; - - if (empty($primaryKey)) { - // Get uniqueKey - $primaryKey = $this->getUniqueKey(); - } // END - if - - // Return it - return $primaryKey; - } - - /** - * Setter for primary key - * - * @param $primaryKey Primary key to set - * @return void - */ - public final function setPrimaryKey ($primaryKey) { - $this->primaryKey = (string) $primaryKey; - } -} - -// [EOF] -?> diff --git a/inc/classes/main/criteria/class_SearchCriteria.php b/inc/classes/main/criteria/class_SearchCriteria.php deleted file mode 100644 index faeb8be6..00000000 --- a/inc/classes/main/criteria/class_SearchCriteria.php +++ /dev/null @@ -1,213 +0,0 @@ - - * @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 SearchCriteria extends BaseFrameworkSystem implements LocalSearchCriteria { - /** - * Criteria to handle - */ - private $searchCriteria = array(); - - /** - * Limitation for the search - */ - private $limit = 0; - - /** - * Skip these entries before using them - */ - private $skip = 0; - - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Clean up a little - $this->removeNumberFormaters(); - $this->removeSystemArray(); - } - - /** - * Create an instance of this class - * - * @return $criteriaInstance An instance of this criteria - */ - public final static function createSearchCriteria () { - // Get a new instance - $criteriaInstance = new SearchCriteria(); - - // Return this instance - return $criteriaInstance; - } - - /** - * Add criteria - * - * @param $criteriaKey Criteria key - * @param $criteriaValue Criteria value - * @return void - */ - public final function addCriteria ($criteriaKey, $criteriaValue) { - $this->searchCriteria[(string)$criteriaKey] = (string)$criteriaValue; - } - - /** - * Add configured criteria - * - * @param $criteriaKey Criteria key - * @param $configEntry Configuration entry - * @return void - */ - public final function addConfiguredCriteria ($criteriaKey, $configEntry) { - // Add the configuration entry as a criteria - $value = $this->getConfigInstance()->readConfig($configEntry); - $this->addCriteria($criteriaKey, $value); - } - - /** - * Setter for limit - * - * @param $limit Search limit - * @return void - * @todo Find a nice casting here. (int) allows until and including 32766. - */ - public final function setLimit ($limit) { - $this->limit = $limit; - } - - /** - * Getter for limit - * - * @return $limit Search limit - */ - public final function getLimit () { - return $this->limit; - } - - /** - * Setter for skip - * - * @param $skip Search skip - * @return void - * @todo Find a nice casting here. (int) allows until and including 32766. - */ - public final function setSkip ($skip) { - $this->skip = $skip; - } - - /** - * Getter for skip - * - * @return $skip Search skip - */ - public final function getSkip () { - return $this->skip; - } - - /** - * "Getter" for a cache key - * - * @return $cacheKey The key suitable for the cache system - */ - public function getCacheKey () { - // Initialize the key - $cacheKey = ""; - - // Now walk through all criterias - foreach ($this->searchCriteria as $criteriaKey => $criteriaValue) { - // Add the value URL encoded to avoid any trouble with special characters - $cacheKey .= sprintf("%s=%s;", - $criteriaKey, - urlencode($criteriaValue) - ); - } - - // Add limit and skip values - $cacheKey .= sprintf("%%limit%%=%s;%%skip%%=%s", - $this->limit, - $this->skip - ); - - // Return the cache key - return $cacheKey; - } - - /** - * Get criteria element or null if not found - * - * @param $criteria The criteria we want to have - * @return $value Wether the value of the critera or null - */ - public function getCriteriaElemnent ($criteria) { - // Default is not found - $value = null; - - // Is the criteria there? - if (isset($this->searchCriteria[$criteria])) { - // Then use it - $value = $this->searchCriteria[$criteria]; - } - - // Return the value - return $value; - } - - /** - * Checks wether given array entry matches - * - * @param $entryArray Array with the entries to find - * @return $matches Wether the entry matches or not - */ - public function ifEntryMatches (array $entryArray) { - // First nothing matches and nothing is counted - $matches = false; - $counted = 0; - - // Walk through all entries - foreach ($entryArray as $key => $entry) { - // Then walk through all search criteria - foreach ($this->searchCriteria as $criteriaKey => $criteriaValue) { - // Is the element found and does it match? - if (($key == $criteriaKey) && ($criteriaValue == $entry)) { - // Then count this one up - $counted++; - } // END - if - } // END - foreach - } // END - foreach - - // Now check if expected criteria counts match - $matches = ($counted == count($this->searchCriteria)); - - // Return the result - return $matches; - } -} - -// [EOF] -?> diff --git a/inc/classes/main/criteria/class_UpdateCriteria.php b/inc/classes/main/criteria/class_UpdateCriteria.php deleted file mode 100644 index af3f6a57..00000000 --- a/inc/classes/main/criteria/class_UpdateCriteria.php +++ /dev/null @@ -1,104 +0,0 @@ - - * @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 UpdateCriteria extends BaseFrameworkSystem implements LocalUpdateCriteria { - /** - * Criteria to handle - */ - private $updateCriteria = array(); - - /** - * Limitation for the update - */ - private $limit = 0; - - /** - * Skip these entries before using them - */ - private $skip = 0; - - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - - // Clean up a little - $this->removeNumberFormaters(); - $this->removeSystemArray(); - } - - /** - * Create an instance of this class - * - * @return $criteriaInstance An instance of this criteria - */ - public final static function createUpdateCriteria () { - // Get a new instance - $criteriaInstance = new UpdateCriteria(); - - // Return this instance - return $criteriaInstance; - } - - /** - * Add criteria - * - * @param $criteriaKey Criteria key - * @param $criteriaValue Criteria value - * @return void - */ - public function addCriteria ($criteriaKey, $criteriaValue) { - $this->updateCriteria[$criteriaKey] = $criteriaValue; - } - - /** - * Add configured criteria - * - * @param $criteriaKey Criteria key - * @param $configEntry Configuration entry - * @return void - */ - public function addConfiguredCriteria ($criteriaKey, $configEntry) { - // Add the configuration entry as a criteria - $value = $this->getConfigInstance()->readConfig($configEntry); - $this->addCriteria($criteriaKey, $value); - } - - /** - * Getter for update criteria array - * - * @return $updateCriteria Array holding the update criteria - */ - public final function getUpdateCriteria () { - return $this->updateCriteria; - } -} - -// [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/dataset/class_DataSetCriteria.php b/inc/classes/main/criteria/dataset/class_DataSetCriteria.php new file mode 100644 index 00000000..f342606e --- /dev/null +++ b/inc/classes/main/criteria/dataset/class_DataSetCriteria.php @@ -0,0 +1,183 @@ + + * @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 DataSetCriteria extends BaseCriteria implements StoreableCriteria { + /** + * Table name + */ + private $tableName = ""; + + /** + * Table columns (criteria) to store + */ + private $tableColumns = array(); + + /** + * Unique key + */ + private $uniqueKey = ""; + + /** + * Primary key + */ + private $primaryKey = ""; + + /** + * Protected constructor + * + * @return void + */ + protected function __construct() { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Creates an instance of this criteria + * + * @param $tableName Name of the table + * @return $criteriaInstance An instance of this criteria + */ + public final static function createDataSetCriteria ($tableName) { + // Get a new instance + $criteriaInstance = new DataSetCriteria(); + + // Set table name + $criteriaInstance->setTableName($tableName); + + // Return the instance + return $criteriaInstance; + } + + /** + * Add criteria + * + * @param $criteriaKey Criteria key + * @param $criteriaValue Criteria value + * @return void + */ + public function addCriteria ($criteriaKey, $criteriaValue) { + $this->tableColumns[(string) $criteriaKey] = $criteriaValue; + } + + /** + * Add configured criteria + * + * @param $criteriaKey Criteria key + * @param $configEntry Configuration entry + * @return void + */ + public function addConfiguredCriteria ($criteriaKey, $configEntry) { + // Add configuration entry as criteria + $value = $this->getConfigInstance()->readConfig($configEntry); + $this->addCriteria($criteriaKey, $value); + } + + /** + * Setter for table name + * + * @param $tableName Name of the table to set + * @return void + */ + public final function setTableName ($tableName) { + $this->tableName = (string) $tableName; + } + + /** + * Getter for table name + * + * @return $tableName Name of the table to set + */ + public final function getTableName () { + return $this->tableName; + } + + /** + * Setter for unique key + * + * @param $uniqueKey Column to use as unique key + * @return void + */ + public final function setUniqueKey ($uniqueKey) { + $this->uniqueKey = (string) $uniqueKey; + } + + /** + * Getter for unique key + * + * @return $uniqueKey Column to use as unique key + */ + public final function getUniqueKey () { + return $this->uniqueKey; + } + + /** + * Getter for unique key value + * + * @return $uniqueValue Value of the unique key + */ + public final function getUniqueValue () { + return $this->tableColumns[$this->getUniqueKey()]; + } + + /** + * Getter for criteria array + * + * @return $tableColumns + */ + public final function getCriteriaArray () { + return $this->tableColumns; + } + + /** + * Getter for primary key or unique key if not set + * + * @return $primaryKey Primary key or unique key if not set + */ + public final function getPrimaryKey () { + // Get primary key by default + $primaryKey = $this->primaryKey; + + if (empty($primaryKey)) { + // Get uniqueKey + $primaryKey = $this->getUniqueKey(); + } // END - if + + // Return it + return $primaryKey; + } + + /** + * Setter for primary key + * + * @param $primaryKey Primary key to set + * @return void + */ + public final function setPrimaryKey ($primaryKey) { + $this->primaryKey = (string) $primaryKey; + } +} + +// [EOF] +?> 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/search/class_SearchCriteria.php b/inc/classes/main/criteria/search/class_SearchCriteria.php new file mode 100644 index 00000000..91baad35 --- /dev/null +++ b/inc/classes/main/criteria/search/class_SearchCriteria.php @@ -0,0 +1,209 @@ + + * @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 SearchCriteria extends BaseCriteria implements LocalSearchCriteria { + /** + * Criteria to handle + */ + private $searchCriteria = array(); + + /** + * Limitation for the search + */ + private $limit = 0; + + /** + * Skip these entries before using them + */ + private $skip = 0; + + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Create an instance of this class + * + * @return $criteriaInstance An instance of this criteria + */ + public final static function createSearchCriteria () { + // Get a new instance + $criteriaInstance = new SearchCriteria(); + + // Return this instance + return $criteriaInstance; + } + + /** + * Add criteria + * + * @param $criteriaKey Criteria key + * @param $criteriaValue Criteria value + * @return void + */ + public final function addCriteria ($criteriaKey, $criteriaValue) { + $this->searchCriteria[(string)$criteriaKey] = (string)$criteriaValue; + } + + /** + * Add configured criteria + * + * @param $criteriaKey Criteria key + * @param $configEntry Configuration entry + * @return void + */ + public final function addConfiguredCriteria ($criteriaKey, $configEntry) { + // Add the configuration entry as a criteria + $value = $this->getConfigInstance()->readConfig($configEntry); + $this->addCriteria($criteriaKey, $value); + } + + /** + * Setter for limit + * + * @param $limit Search limit + * @return void + * @todo Find a nice casting here. (int) allows until and including 32766. + */ + public final function setLimit ($limit) { + $this->limit = $limit; + } + + /** + * Getter for limit + * + * @return $limit Search limit + */ + public final function getLimit () { + return $this->limit; + } + + /** + * Setter for skip + * + * @param $skip Search skip + * @return void + * @todo Find a nice casting here. (int) allows until and including 32766. + */ + public final function setSkip ($skip) { + $this->skip = $skip; + } + + /** + * Getter for skip + * + * @return $skip Search skip + */ + public final function getSkip () { + return $this->skip; + } + + /** + * "Getter" for a cache key + * + * @return $cacheKey The key suitable for the cache system + */ + public function getCacheKey () { + // Initialize the key + $cacheKey = ""; + + // Now walk through all criterias + foreach ($this->searchCriteria as $criteriaKey => $criteriaValue) { + // Add the value URL encoded to avoid any trouble with special characters + $cacheKey .= sprintf("%s=%s;", + $criteriaKey, + urlencode($criteriaValue) + ); + } + + // Add limit and skip values + $cacheKey .= sprintf("%%limit%%=%s;%%skip%%=%s", + $this->limit, + $this->skip + ); + + // Return the cache key + return $cacheKey; + } + + /** + * Get criteria element or null if not found + * + * @param $criteria The criteria we want to have + * @return $value Wether the value of the critera or null + */ + public function getCriteriaElemnent ($criteria) { + // Default is not found + $value = null; + + // Is the criteria there? + if (isset($this->searchCriteria[$criteria])) { + // Then use it + $value = $this->searchCriteria[$criteria]; + } + + // Return the value + return $value; + } + + /** + * Checks wether given array entry matches + * + * @param $entryArray Array with the entries to find + * @return $matches Wether the entry matches or not + */ + public function ifEntryMatches (array $entryArray) { + // First nothing matches and nothing is counted + $matches = false; + $counted = 0; + + // Walk through all entries + foreach ($entryArray as $key => $entry) { + // Then walk through all search criteria + foreach ($this->searchCriteria as $criteriaKey => $criteriaValue) { + // Is the element found and does it match? + if (($key == $criteriaKey) && ($criteriaValue == $entry)) { + // Then count this one up + $counted++; + } // END - if + } // END - foreach + } // END - foreach + + // Now check if expected criteria counts match + $matches = ($counted == count($this->searchCriteria)); + + // Return the result + return $matches; + } +} + +// [EOF] +?> 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/update/class_UpdateCriteria.php b/inc/classes/main/criteria/update/class_UpdateCriteria.php new file mode 100644 index 00000000..9508c45f --- /dev/null +++ b/inc/classes/main/criteria/update/class_UpdateCriteria.php @@ -0,0 +1,100 @@ + + * @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 UpdateCriteria extends BaseCriteria implements LocalUpdateCriteria { + /** + * Criteria to handle + */ + private $updateCriteria = array(); + + /** + * Limitation for the update + */ + private $limit = 0; + + /** + * Skip these entries before using them + */ + private $skip = 0; + + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Create an instance of this class + * + * @return $criteriaInstance An instance of this criteria + */ + public final static function createUpdateCriteria () { + // Get a new instance + $criteriaInstance = new UpdateCriteria(); + + // Return this instance + return $criteriaInstance; + } + + /** + * Add criteria + * + * @param $criteriaKey Criteria key + * @param $criteriaValue Criteria value + * @return void + */ + public function addCriteria ($criteriaKey, $criteriaValue) { + $this->updateCriteria[$criteriaKey] = $criteriaValue; + } + + /** + * Add configured criteria + * + * @param $criteriaKey Criteria key + * @param $configEntry Configuration entry + * @return void + */ + public function addConfiguredCriteria ($criteriaKey, $configEntry) { + // Add the configuration entry as a criteria + $value = $this->getConfigInstance()->readConfig($configEntry); + $this->addCriteria($criteriaKey, $value); + } + + /** + * Getter for update criteria array + * + * @return $updateCriteria Array holding the update criteria + */ + public final function getUpdateCriteria () { + return $this->updateCriteria; + } +} + +// [EOF] +?> 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]