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