Moved database columns as constants in wrapper classes
[shipsimu.git] / application / ship-simu / main / user / class_ShipSimuBaseUser.php
1 <?php
2 /**
3  * A special member class for Ship-Simu
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 Ship-Simu Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.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 ShipSimuBaseUser extends BaseUser implements Registerable, Updateable {
25         /**
26          * Protected constructor
27          *
28          * @param       $className      Name of the class
29          * @return      void
30          */
31         protected function __construct ($className) {
32                 // Call parent constructor
33                 parent::__construct($className);
34         }
35
36         /**
37          * Checks wether the user has reached maximum allowed companies to found
38          *
39          * @return      $reached        Wether the user has reached maximum allowed companies to found
40          */
41         public function ifUserCreatedMaximumAllowedCompanies () {
42                 // Get max allowed companies to found
43                 $maxFound = $this->getConfigInstance()->getConfigEntry('max_allowed_companies_found');
44
45                 // Now get a search criteria and set the user's name as criteria
46                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
47                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
48                 $searchInstance->setLimit($maxFound);
49
50                 // Get a company wrapper
51                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('company_db_wrapper_class');
52
53                 // Do the count-select by criteria
54                 $totalRows = $wrapperInstance->doSelectCountByCriteria($searchInstance);
55
56                 // Does the user have reached maximum?
57                 $reached = ($totalRows >= $maxFound);
58
59                 // Return the result
60                 return $reached;
61         }
62
63         /**
64          * Checks wether the user has the required amount of points left for the specified action
65          *
66          * @param       $action                 The action or configuration entry plus prefix the user wants to perform
67          * @return      $hasRequired    Wether the user has the required points
68          */
69         public function ifUserHasRequiredPoints ($action) {
70                 // Default is that everyone is poor... ;-)
71                 $hasRequired = false;
72
73                 // Init instance
74                 $pointsInstance = null;
75
76                 try {
77                         // Get a points instance from registry
78                         $pointsInstance = Registry::getRegistry()->getInstance('points');
79                 } catch (NullPointerException $e) {
80                         // Instance not found in registry
81                         // @TODO We should log this exception later
82                 }
83
84                 // Is there an instance?
85                 if (is_null($pointsInstance)) {
86                         // Then create one
87                         $pointsInstance = ObjectFactory::createObjectByConfiguredName('user_points_class', array($this));
88
89                         // And store it in registry
90                         Registry::getRegistry()->addInstance('points', $pointsInstance);
91                 } // END - if
92
93                 // Just forward this request to the user points class
94                 $hasRequired = $pointsInstance->ifUserHasRequiredPoints($action);
95
96                 // Return the result
97                 return $hasRequired;
98         }
99
100         /**
101          * Determines if government can still pay a "virtual training course" in general
102          *
103          * @return      $ifGovHelps             Wether if government helps the user with a virtual training course
104          */
105         public function ifGovernmentPaysTraining () {
106                 // By default they want to help.
107                 $ifGovHelps = true;
108
109                 // Init instance
110                 $governmentInstance = null;
111
112                 try {
113                         // First get a government instance from registry
114                         $governmentInstance = Registry::getRegistry()->getInstance('government');
115                 } catch (NullPointerException $e) {
116                         // Instance not found in registry
117                         // @TODO We should log this exception later
118                 }
119
120                 // Is it there?
121                 if (is_null($governmentInstance)) {
122                         // Then create a new one
123                         $governmentInstance = ObjectFactory::createObjectByConfiguredName('government_class', array($this));
124
125                         // Store it in registry
126                         Registry::getRegistry()->addInstance('government', $governmentInstance);
127                 } // END - if
128
129                 // Then ask the government if they want to pay a "startup help" to the user
130                 if ($governmentInstance->ifGovernmentAlreadyPayedTraining()) {
131                         // Training already given!
132                         $ifGovHelps = false;
133                 } // END - if
134
135                 // Return result here
136                 return $ifGovHelps;
137         }
138
139         /**
140          * Determines if government can still pay a "startup help" to the user
141          *
142          * @return      $ifGovHelps             Wether if government helps the user with some startup money
143          */
144         public function ifGovernmentPaysStartupHelp () {
145                 // By default they want to help.
146                 $ifGovHelps = true;
147
148                 // Init instance
149                 $governmentInstance = null;
150
151                 try {
152                         // First get a government instance from registry
153                         $governmentInstance = Registry::getRegistry()->getInstance('government');
154                 } catch (NullPointerException $e) {
155                         // Instance not found in registry
156                         // @TODO We should log this exception later
157                 }
158
159                 // Is it there?
160                 if (is_null($governmentInstance)) {
161                         // Then create a new one
162                         $governmentInstance = ObjectFactory::createObjectByConfiguredName('government_class', array($this));
163
164                         // Store it in registry
165                         Registry::getRegistry()->addInstance('government', $governmentInstance);
166                 } // END - if
167
168                 // Then ask the government if they want to pay a "startup help" to the user
169                 if ($governmentInstance->ifGovernmentPayedMaxmimumStartupHelp()) {
170                         // They can't pay anymore to the user (excited amount)
171                         $ifGovHelps = false;
172                 } // END - if
173
174                 // Return result here
175                 return $ifGovHelps;
176         }
177
178         /**
179          * Checks wether the user can take points from the money bank
180          *
181          * @return      $bankLends      Wether the money bank is able to lend money
182          * @todo        Need to implement MoneyBank::ifBankLendsMoreMoney()
183          */
184         public function ifUserAllowedTakeCreditsFromMoneyBank () {
185                 // Per default the money bank cannot pay
186                 $bankLends = false;
187
188                 // Init instance
189                 $bankInstance = null;
190
191                 try {
192                         // Get a money bank instance from registry
193                         $bankInstance = Registry::getRegistry()->getInstance('money_bank');
194                 } catch (NullPointerException $e) {
195                         // Instance not found in registry
196                         // @TODO We should log this exception later
197                 }
198
199                 // Is it there?
200                 if (is_null($bankInstance)) {
201                         // Then create a new one
202                         $bankInstance = ObjectFactory::createObjectByConfiguredName('bank_class', array($this));
203
204                         // Store it in registry
205                         Registry::getRegistry()->addInstance('money_bank', $bankInstance);
206                 } // END - if
207
208                 // Does the money bank lend more money?
209                 /* UNFINISED:
210                 if ($bankInstance->ifBankLendsMoreMoney()) {
211                         // Okay, that they will do
212                         $bankLends = true;
213                 } // END - if
214                 */
215
216                 // Return result
217                 return $bankLends;
218         }
219
220         /**
221          * Checks wether the user has maximum credits with the money bank. This
222          * should be done seperately from checking if the user is allowed to take
223          * credits from the bank.
224          *
225          * @return      $hasMaxCredits  Wether the user has maximum credits with the bank
226          * @todo        Need to check the bank if they can lend more money
227          */
228         public function ifUserHasMaximumCreditsWithMoneyBank () {
229                 // For default he can still get money
230                 $hasMaxCredits = false;
231
232                 // Init instance
233                 $bankInstance = null;
234
235                 try {
236                         // Get a money bank instance from registry
237                         $bankInstance = Registry::getRegistry()->getInstance('money_bank');
238                 } catch (NullPointerException $e) {
239                         // Instance not found in registry
240                         // @TODO We should log this exception later
241                 }
242
243                 // Is it there?
244                 if (is_null($bankInstance)) {
245                         // Then create a new one
246                         $bankInstance = ObjectFactory::createObjectByConfiguredName('bank_class', array($this));
247
248                         // Store it in registry
249                         Registry::getRegistry()->addInstance('money_bank', $bankInstance);
250                 } // END - if
251
252                 // Now check if the user has maximum credits
253                 /** UNFINISHED PART!
254                 if ($bankInstance->ifUserHasMaxCredits()) {
255                         // Yes, he does!
256                         $hasMaxCredits = true;
257                 } // END - if
258                 */
259
260                 // Return the result
261                 return $hasMaxCredits;
262         }
263 }
264
265 // [EOF]
266 ?>