]> git.mxchange.org Git - city.git/blob - application/city/main/database/frontend/region/class_RegionInformationDatabaseWrapper.php
Updated 'core'.
[city.git] / application / city / main / database / frontend / region / class_RegionInformationDatabaseWrapper.php
1 <?php
2 /**
3  * A database wrapper for region informations
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2015 City Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class RegionInformationDatabaseWrapper extends BaseDatabaseWrapper implements RegionInformationWrapper, Registerable {
25         // Constants for database table names
26         const DB_TABLE_REGION_INFORMATION = 'region_data';
27
28         // Constants for database column names
29         const DB_COLUMN_REGION_ID          = 'region_id';
30         const DB_COLUMN_REGION_NAME        = 'region_name';
31         const DB_COLUMN_REGION_USER_ID     = 'region_user_id';
32
33         /**
34          * Protected constructor
35          *
36          * @return      void
37          */
38         protected function __construct () {
39                 // Call parent constructor
40                 parent::__construct(__CLASS__);
41         }
42
43         /**
44          * Creates an instance of this database wrapper by a provided user class
45          *
46          * @return      $wrapperInstance        An instance of the created wrapper class
47          */
48         public static final function createRegionInformationDatabaseWrapper () {
49                 // Get a new instance
50                 $wrapperInstance = new RegionInformationDatabaseWrapper();
51
52                 // Set (primary!) table name
53                 $wrapperInstance->setTableName(self::DB_TABLE_REGION_INFORMATION);
54
55                 // Return the instance
56                 return $wrapperInstance;
57         }
58
59         /**
60          * Removes non-public data from given array.
61          *
62          * @param       $data   An array with possible non-public data that needs to be removed.
63          * @return      $data   A cleaned up array with only public data.
64          */
65         public function removeNonPublicDataFromArray(array $data) {
66                 // Currently call only inner method
67                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGION-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
68                 $data = parent::removeNonPublicDataFromArray($data);
69
70                 // Return cleaned data
71                 return $data;
72         }
73
74         /**
75          * Checks whether the user has already founded a region
76          *
77          * @return      $hasFounded             Whether the user has already founded a region
78          */
79         public function ifUserHasCreatedRegion () {
80                 // Get user instance
81                 $userInstance = Registry::getRegistry()->getInstance('user');
82
83                 // Now get a search criteria instance
84                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
85
86                 // Search for user's cities
87                 $searchInstance->addCriteria(RegionInformationDatabaseWrapper::DB_COLUMN_REGION_USER_ID, $userInstance->getUserId());
88
89                 // Get a result back
90                 $resultInstance = $this->doSelectByCriteria($searchInstance);
91
92                 // Get region manager instance
93                 $managerInstance = ManagerFactory::createManagerByType('region');
94
95                 // Set result instance
96                 $managerInstance->setResultInstance($resultInstance);
97
98                 // Has it been founded?
99                 $hasFounded = $resultInstance->valid();
100
101                 // Return result
102                 return $hasFounded;
103         }
104
105         /**
106          * Checks whether the given region name is taken
107          *
108          * @param       $regionName             Name of region
109          * @return      $isTaken                Whether the given region name is taken
110          */
111         public function ifRegionExists ($regionName) {
112                 // Now get a search criteria instance
113                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
114
115                 // Search for the region number one which is hard-coded the default
116                 $searchInstance->addCriteria(RegionInformationDatabaseWrapper::DB_COLUMN_REGION_NAME, $regionName);
117                 $searchInstance->setLimit(1);
118
119                 // Get a result back
120                 $resultInstance = $this->doSelectByCriteria($searchInstance);
121
122                 // Check it
123                 $isTaken = $resultInstance->next();
124                 //* NOISY-DEBUG: */ $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] isTaken[' . gettype($isTaken) . ']=' . intval($isTaken));
125
126                 // Get manger instance
127                 $managerInstance = ManagerFactory::createManagerByType('region');
128
129                 // Set result instance
130                 $managerInstance->setResultInstance($resultInstance);
131
132                 // Return result
133                 return $isTaken;
134         }
135
136         /**
137          * Creates a region by given name
138          *
139          * @param       $regionName             Name of region
140          * @return      void
141          */
142         public function createRegionByName ($regionName) {
143                 // Pre-check name
144                 assert(!$this->ifRegionExists($regionName));
145
146                 // Get user instance
147                 $userInstance = Registry::getRegistry()->getInstance('user');
148
149                 // Get a dataset instance
150                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_REGION_INFORMATION));
151
152                 // Set the primary key
153                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_REGION_ID);
154
155                 // Add region name and assign user id
156                 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_ID     , ($this->countTotalRows() + 1));
157                 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_NAME   , $regionName);
158                 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_USER_ID, $userInstance->getUserId());
159
160                 // "Insert" this dataset instance completely into the database
161                 $this->queryInsertDataSet($dataSetInstance);
162
163                 // Post-check name
164                 assert($this->ifRegionExists($regionName));
165         }
166 }
167
168 // [EOF]
169 ?>