]> git.mxchange.org Git - city.git/blob - application/city/classes/database/frontend/region/class_RegionInformationDatabaseWrapper.php
53b2e8583be24b7021b0da12acb78dad9600a61d
[city.git] / application / city / classes / 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                 // Make sure the instance is valid
96                 assert($managerInstance instanceof ManageableRegion);
97
98                 // Set result instance
99                 $managerInstance->setResultInstance($resultInstance);
100
101                 // Has it been founded?
102                 $hasFounded = $resultInstance->valid();
103
104                 // Return result
105                 return $hasFounded;
106         }
107
108         /**
109          * Checks whether the given region name is taken
110          *
111          * @param       $regionName             Name of region
112          * @return      $isTaken                Whether the given region name is taken
113          */
114         public function ifRegionExists ($regionName) {
115                 // Now get a search criteria instance
116                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
117
118                 // Search for the region number one which is hard-coded the default
119                 $searchInstance->addCriteria(RegionInformationDatabaseWrapper::DB_COLUMN_REGION_NAME, $regionName);
120                 $searchInstance->setLimit(1);
121
122                 // Get a result back
123                 $resultInstance = $this->doSelectByCriteria($searchInstance);
124
125                 // Check it
126                 $isTaken = $resultInstance->next();
127                 //* NOISY-DEBUG: */ $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] isTaken[' . gettype($isTaken) . ']=' . intval($isTaken));
128
129                 // Get manger instance
130                 $managerInstance = ManagerFactory::createManagerByType('region');
131
132                 // Make sure the instance is valid
133                 assert($managerInstance instanceof ManageableRegion);
134
135                 // Set result instance
136                 $managerInstance->setResultInstance($resultInstance);
137
138                 // Return result
139                 return $isTaken;
140         }
141
142         /**
143          * Creates a region by given name
144          *
145          * @param       $regionName             Name of region
146          * @return      void
147          */
148         public function createRegionByName ($regionName) {
149                 // Pre-check name
150                 assert(!$this->ifRegionExists($regionName));
151
152                 // Get user instance
153                 $userInstance = Registry::getRegistry()->getInstance('user');
154
155                 // Get a dataset instance
156                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_REGION_INFORMATION));
157
158                 // Set the primary key
159                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_REGION_ID);
160
161                 // Add region name and assign user id
162                 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_ID     , ($this->countTotalRows() + 1));
163                 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_NAME   , $regionName);
164                 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_USER_ID, $userInstance->getUserId());
165
166                 // "Insert" this dataset instance completely into the database
167                 $this->queryInsertDataSet($dataSetInstance);
168
169                 // Post-check name
170                 assert($this->ifRegionExists($regionName));
171         }
172 }
173
174 // [EOF]
175 ?>