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