Copyright updated, menu class added for 'home'
[shipsimu.git] / application / ship-simu / main / personell / class_SimulatorPersonell.php
1 <?php
2 /**
3  * The general simulator personell class
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 SimulatorPersonell extends BasePersonell {
25         // Personell list
26         private $personellList = null;
27
28         // A cache for lists
29         private $cacheList = null;
30
31         // A string for cached conditions
32         private $cacheCond = null;
33
34         /**
35          * Protected constructor
36          *
37          * @return      void
38          */
39         protected function __construct () {
40                 // Call parent constructor
41                 parent::__construct(__CLASS__);
42         }
43
44         /**
45          * Magic wake-up method called when unserialize() is called. This is
46          * neccessary because in this case a personell does not need to know the
47          * min/max ages range and system classes. This would anyway use more RAM
48          * what is not required.
49          *
50          * @return      void
51          */
52         public function __wakeup () {
53                 // Tidy up a little
54                 $this->removePersonellList();
55                 $this->removeMinMaxAge();
56                 $this->removeCache();
57                 $this->removeSystemArray();
58         }
59
60         /**
61          * Generate a specified amount of personell and return the prepared instance
62          *
63          * @param               $amountPersonell                Number of personell we shall
64          *                                                              generate
65          * @return      $personellInstance              An instance of this object with a
66          *                                                              list of personells
67          */
68         public final static function createSimulatorPersonell ($amountPersonell) {
69                 // Make sure only integer can pass
70                 $amountPersonell = (int) $amountPersonell;
71
72                 // Get a new instance
73                 $personellInstance = new SimulatorPersonell();
74
75                 // Debug message
76                 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->debugOutput(sprintf("[%s:%d] Es werden <strong>%d</strong> Personal bereitgestellt.",
77                         __CLASS__,
78                         __LINE__,
79                         $amountPersonell
80                 ));
81
82                 // Initialize the personell list
83                 $personellInstance->createPersonellList();
84
85                 // Create requested amount of personell
86                 for ($idx = 0; $idx < $amountPersonell; $idx++) {
87                         $personellInstance->addRandomPersonell();
88                 }
89
90                 // Debug message
91                 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->debugOutput(sprintf("[%s:%d] <strong>%d</strong> Personal bereitgestellt.",
92                         __CLASS__,
93                         __LINE__,
94                         $amountPersonell
95                 ));
96
97                 // Tidy up a little
98                 $personellInstance->removeGender();
99                 $personellInstance->removeNames();
100                 $personellInstance->removeBirthday();
101                 $personellInstance->removeSalary();
102                 $personellInstance->removeEmployed();
103                 $personellInstance->removeMarried();
104                 $personellInstance->removeNumberFormaters();
105                 //$personellInstance->removeCache();
106                 $personellInstance->removeSystemArray();
107
108                 // Instanz zurueckgeben
109                 return $personellInstance;
110         }
111
112         /**
113          * Create a SimulatorPersonell object by loading the specified personell
114          * list from an existing database backend
115          *
116          * @param       $idNumber                       The ID number (only right part) of the list
117          * @return      $personellInstance      An instance of this class
118          * @throws      InvalidIDFormatException        If the given id number
119          *                                                                              $idNumber is invalid
120          * @throws      MissingSimulatorIdException             If an ID number was not found
121          * @deprecated
122          */
123         public final static function createSimulatorPersonellByID ($idNumber) {
124                 // Get instance
125                 $personellInstance = new SimulatorPersonell(false);
126                 $personellInstance->makeDeprecated();
127         }
128
129         // Create personell list
130         public function createPersonellList () {
131                 // Is the list already created?
132                 if ($this->personelllList instanceof FrameworkArrayObject) {
133                         // Throw an exception
134                         throw new PersonellListAlreadyCreatedException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
135                 } // END - if
136
137                 // Initialize the array
138                 $this->personellList = new FrameworkArrayObject("FakedPersonellList");
139         }
140
141         // Remove the personell list
142         private final function removePersonellList () {
143                 unset($this->personellList);
144         }
145
146         // Add new personell object to our list
147         public function addRandomPersonell () {
148                 // Gender list...
149                 $genders = array("M", "F");
150
151                 // Create new personell members
152                 $personellInstance = new SimulatorPersonell();
153
154                 // Set a randomized gender
155                 $personellInstance->setGender($genders[mt_rand(0, 1)]);
156
157                 // Set a randomized birthday (maximum age required, see const MAX_AGE)
158                 $personellInstance->createBirthday();
159
160                 // Married? Same values means: married
161                 if (mt_rand(0, 5) == mt_rand(0, 5)) $personellInstance->setMarried(true);
162
163                 // Tidy up a little
164                 $personellInstance->removePersonellList();
165                 $personellInstance->removeMinMaxAge();
166                 $personellInstance->removeCache();
167                 $personellInstance->removeSystemArray();
168
169                 // Add new member to the list
170                 $this->personellList->append($personellInstance);
171         }
172
173         /**
174          * Get a specifyable list of our people, null or empty string will be ignored!
175          *
176          * @return      $cacheList      A list of cached personells
177          */
178         function getSpecialPersonellList ($isEmployed = null, $isMarried = null, $hasGender = "") {
179                 // Serialize the conditions for checking if we can take the cache
180                 $serialized = serialize(array($isEmployed, $isMarried, $hasGender));
181
182                 // The same (last) conditions?
183                 if (($serialized == $this->cacheCond) && (!is_null($this->cacheCond))) {
184                         if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Gecachte Liste wird verwendet.",
185                                 __CLASS__,
186                                 __LINE__
187                         ));
188
189                         // Return cached list
190                         return $this->cacheList;
191                 }
192
193                 // Output debug message
194                 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Personalliste wird nach Kriterien durchsucht...",
195                         __CLASS__,
196                         __LINE__
197                 ));
198
199                 // Remember the conditions
200                 $this->setCacheCond($serialized);
201
202                 // Create cached list
203                 $this->setAllCacheList(new FrameworkArrayObject('FakedCacheList'));
204
205                 // Search all unemployed personells
206                 for ($idx = $this->personellList->getIterator(); $idx->valid(); $idx->next()) {
207                         // Element holen
208                         $el = $idx->current();
209
210                         // Check currenylt all single conditions (combined conditions are not yet supported)
211                         if ((!is_null($isEmployed)) && ($el->isEmployed() == $isEmployed)) {
212                                 // Add this one (employed status asked)
213                                 $this->cacheList->append($el);
214                         } elseif ((!is_null($isMarried)) && ($el->isMarried() == $isMarried)) {
215                                 // Add this one (marrital status asked)
216                                 $this->cacheList->append($el);
217                         } elseif ((!empty($hasGender)) && ($el->getGender() == $hasGender)) {
218                                 // Add this one (specified gender)
219                                 $this->cacheList->append($el);
220                         }
221                 }
222
223                 // Return the completed list
224                 return $this->cacheList;
225         }
226
227         /**
228          * Get amount of unemployed personell
229          *
230          * @return      $count  Amount of unemployed personell
231          */
232         public final function getAllUnemployed () {
233                 // Get a temporary list
234                 $list = $this->getSpecialPersonellList(false);
235
236                 // Anzahl zurueckliefern
237                 return $list->count();
238         }
239
240         /**
241          * Remove cache things
242          *
243          * @return      void
244          */
245         private function removeCache () {
246                 // Remove cache data
247                 unset($this->cacheList);
248                 unset($this->cacheCond);
249         }
250
251         /**
252          * Setter for cache list
253          *
254          * @param               $cacheList      The new cache list to set or null for initialization/reset
255          * @return      void
256          */
257         private final function setAllCacheList (FrameworkArrayObject $cacheList = null) {
258                 $this->cacheList = $cacheList;
259         }
260
261         /**
262          * Setter for cache conditions
263          *
264          * @param               $cacheCond      The new cache conditions to set
265          * @return      void
266          */
267         private final function setCacheCond ($cacheCond) {
268                 $this->cacheCond = (string) $cacheCond;
269         }
270
271         /**
272          * Reset cache list
273          *
274          * @return      void
275          */
276         public function resetCache () {
277                 $this->setAllCacheList(null);
278                 $this->setCacheCond("");
279         }
280
281         /**
282          * Getter for surname. If no surname is set then default surnames are set
283          * for male and female personells.
284          *
285          * @return      $surname        The personell' surname
286          */
287         public final function getSurname () {
288                 $surname = parent::getSurname();
289
290                 // Make sure every one has a surname...
291                 if (empty($surname)) {
292                         if ($this->isMale()) {
293                                 // Typical male name
294                                 $surname = "John";
295                         } else {
296                                 // Typical female name
297                                 $surname = "Jennifer";
298                         }
299
300                         // Set typical family name
301                         parent::setFamily("Smith");
302                 } // END - if
303
304                 // Return surname
305                 return $surname;
306         }
307
308         /**
309          * Getter for personell list
310          *
311          * @return      $personellList          The list of all personells
312          */
313         public final function getPersonellList () {
314                 return $this->personellList;
315         }
316
317         /**
318          * Loads the mostly pre-cached personell list
319          *
320          * @param       $idNumber       The ID number we shall use for looking up
321          *                                              the right data.
322          * @return      void
323          * @deprecated
324          */
325         public function loadPersonellList ($idNumber) {
326                 $this->makeDeprecated();
327         }
328 }
329
330 // [EOF]
331 ?>