]> git.mxchange.org Git - core.git/blob - framework/main/classes/database/frontend/class_UserPointsDatabaseWrapper.php
Some updates:
[core.git] / framework / main / classes / database / frontend / class_UserPointsDatabaseWrapper.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Database\Frontend\Points;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Database\Frontend\BaseDatabaseWrapper;
7 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Registry\Registerable;
9 use Org\Mxchange\CoreFramework\Result\Update\UpdateableResult;
10
11 /**
12  * A database wrapper for user points classes
13  *
14  * @author              Roland Haeder <webmaster@shipsimu.org>
15  * @version             0.0.0
16 <<<<<<< HEAD:framework/main/classes/database/frontend/class_UserPointsDatabaseWrapper.php
17  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 =======
19  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
20 >>>>>>> Some updates::inc/main/classes/database/frontend/class_UserPointsDatabaseWrapper.php
21  * @license             GNU GPL 3.0 or any newer version
22  * @link                http://www.shipsimu.org
23  *
24  * This program is free software: you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation, either version 3 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with this program. If not, see <http://www.gnu.org/licenses/>.
36  */
37 class UserPointsDatabaseWrapper extends BaseDatabaseWrapper implements BookablePointsWrapper, Registerable {
38         /**
39          * Constants for database table names
40          */
41         const DB_TABLE_USER_POINTS = 'user_points';
42
43         /**
44          * Name of the user->points column
45          */
46         const DB_COLUMN_POINTS_UID = 'points_uid';
47
48         /**
49          * Name of the points column
50          */
51         const DB_COLUMN_POINTS = 'points';
52
53         /**
54          * Protected constructor
55          *
56          * @return      void
57          */
58         protected function __construct () {
59                 // Call parent constructor
60                 parent::__construct(__CLASS__);
61         }
62
63         /**
64          * Creates an instance of this database wrapper by a provided user class
65          *
66          * @return      $wrapperInstance        An instance of the created wrapper class
67          */
68         public static final function createUserPointsDatabaseWrapper () {
69                 // Get a new instance
70                 $wrapperInstance = new UserPointsDatabaseWrapper();
71
72                 // Set (primary!) table name
73                 $wrapperInstance->setTableName(self::DB_TABLE_USER_POINTS);
74
75                 // Return the instance
76                 return $wrapperInstance;
77         }
78
79         /**
80          * Inserts the given points for the given user in the database
81          *
82          * @param       $pointsInstance         An instance of a user class
83          * @return      void
84          */
85         public function insertUserPoints (BookablePoints $pointsInstance) {
86                 // Generate a data set for the request
87                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER_POINTS));
88
89                 // Set the primary key
90                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_POINTS_UID);
91
92                 // Add registration elements to the dataset
93                 $pointsInstance->addElementsToDataSet($dataSetInstance);
94
95                 // "Insert" this request instance completely into the database
96                 $this->queryInsertDataSet($dataSetInstance);
97         }
98
99         /**
100          * Updates an user database entry with given result
101          *
102          * @param       $resultInstance         An instance of a Updateable database result
103          * @return      void
104          */
105         public function doUpdateByResult (UpdateableResult $resultInstance) {
106                 // Generate a data set object
107                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER_POINTS));
108
109                 // Add all update criteria to the database set
110                 $resultInstance->addElementsToDataSet($dataSetInstance);
111
112                 // Add seach criteria
113                 $dataSetInstance->setSearchInstance($resultInstance->getUpdateInstance()->getSearchInstance());
114
115                 // Set the primary key
116                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_POINTS_UID);
117
118                 // "Update" this request with the database
119                 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
120         }
121
122 }