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