generateUniqueId() and more useless/deprecated methods removed, code speed-up, link...
[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, this is free software
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          */
122         public final static function createSimulatorPersonellByID ($idNumber) {
123                 // Use the direct ID number
124                 $tempID = $idNumber;
125
126                 // Add the class name if it was not found
127                 if (count(explode("@", $idNumber)) < 2) {
128                         // Add class name in front of the incomplete ID number
129                         $tempID = sprintf("%s@%s", __CLASS__, $idNumber);
130                 } // END - if
131
132                 // Validate the ID number
133                 if (!preg_match(sprintf("/%s\@([a-f0-9]){32}/i", __CLASS__), $tempID)) {
134                         // Invalid format
135                         throw new InvalidIDFormatException(new SimulatorPersonell(), self::EXCEPTION_ID_IS_INVALID_FORMAT);
136                 } // END - if
137
138                 // Get instance
139                 $personellInstance = new SimulatorPersonell(false);
140
141                 // Get database instance
142                 $dbInstance = $personellInstance->getDatabaseInstance();
143
144                 // Is the unique ID already used? Then it must be there!
145                 if (!$dbInstance->isUniqueIdUsed($tempID))  {
146                         // Entry not found!
147                         throw new MissingSimulatorIdException(array($personellInstance, $idNumber), self::EXCEPTION_SIMULATOR_ID_INVALID);
148                 } // END - if
149
150                 // Load the personell list and add it to this object
151                 $personellInstance->loadPersonellList($tempID);
152
153                 // Clean-up a little
154                 $personellInstance->removeGender();
155                 $personellInstance->removeNames();
156                 $personellInstance->removeBirthday();
157                 $personellInstance->removeSalary();
158                 $personellInstance->removeEmployed();
159                 $personellInstance->removeMarried();
160                 $personellInstance->removeNumberFormaters();
161                 //$personellInstance->removeCache();
162                 $personellInstance->removeSystemArray();
163
164                 // Return instance
165                 return $personellInstance;
166         }
167
168         // Create personell list
169         public function createPersonellList () {
170                 // Is the list already created?
171                 if ($this->personelllList instanceof FrameworkArrayObject) {
172                         // Throw an exception
173                         throw new PersonellListAlreadyCreatedException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
174                 } // END - if
175
176                 // Initialize the array
177                 $this->personellList = new FrameworkArrayObject("FakedPersonellList");
178         }
179
180         // Remove the personell list
181         private final function removePersonellList () {
182                 unset($this->personellList);
183         }
184
185         // Add new personell object to our list
186         public function addRandomPersonell () {
187                 // Gender list...
188                 $genders = array("M", "F");
189
190                 // Create new personell members
191                 $personellInstance = new SimulatorPersonell();
192
193                 // Set a randomized gender
194                 $personellInstance->setGender($genders[mt_rand(0, 1)]);
195
196                 // Set a randomized birthday (maximum age required, see const MAX_AGE)
197                 $personellInstance->createBirthday();
198
199                 // Married? Same values means: married
200                 if (mt_rand(0, 5) == mt_rand(0, 5)) $personellInstance->setMarried(true);
201
202                 // Tidy up a little
203                 $personellInstance->removePersonellList();
204                 $personellInstance->removeMinMaxAge();
205                 $personellInstance->removeCache();
206                 $personellInstance->removeSystemArray();
207
208                 // Add new member to the list
209                 $this->personellList->append($personellInstance);
210         }
211
212         /**
213          * Get a specifyable list of our people, null or empty string will be ignored!
214          *
215          * @return      $cacheList      A list of cached personells
216          */
217         function getSpecialPersonellList ($isEmployed = null, $isMarried = null, $hasGender = "") {
218                 // Serialize the conditions for checking if we can take the cache
219                 $serialized = serialize(array($isEmployed, $isMarried, $hasGender));
220
221                 // The same (last) conditions?
222                 if (($serialized == $this->cacheCond) && (!is_null($this->cacheCond))) {
223                         if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Gecachte Liste wird verwendet.",
224                                 __CLASS__,
225                                 __LINE__
226                         ));
227
228                         // Return cached list
229                         return $this->cacheList;
230                 }
231
232                 // Output debug message
233                 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Personalliste wird nach Kriterien durchsucht...",
234                         __CLASS__,
235                         __LINE__
236                 ));
237
238                 // Remember the conditions
239                 $this->setCacheCond($serialized);
240
241                 // Create cached list
242                 $this->setAllCacheList(new FrameworkArrayObject('FakedCacheList'));
243
244                 // Search all unemployed personells
245                 for ($idx = $this->personellList->getIterator(); $idx->valid(); $idx->next()) {
246                         // Element holen
247                         $el = $idx->current();
248
249                         // Check currenylt all single conditions (combined conditions are not yet supported)
250                         if ((!is_null($isEmployed)) && ($el->isEmployed() == $isEmployed)) {
251                                 // Add this one (employed status asked)
252                                 $this->cacheList->append($el);
253                         } elseif ((!is_null($isMarried)) && ($el->isMarried() == $isMarried)) {
254                                 // Add this one (marrital status asked)
255                                 $this->cacheList->append($el);
256                         } elseif ((!empty($hasGender)) && ($el->getGender() == $hasGender)) {
257                                 // Add this one (specified gender)
258                                 $this->cacheList->append($el);
259                         }
260                 }
261
262                 // Return the completed list
263                 return $this->cacheList;
264         }
265
266         /**
267          * Get amount of unemployed personell
268          *
269          * @return      $count  Amount of unemployed personell
270          */
271         public final function getAllUnemployed () {
272                 // Get a temporary list
273                 $list = $this->getSpecialPersonellList(false);
274
275                 // Anzahl zurueckliefern
276                 return $list->count();
277         }
278
279         /**
280          * Remove cache things
281          *
282          * @return      void
283          */
284         private function removeCache () {
285                 // Remove cache data
286                 unset($this->cacheList);
287                 unset($this->cacheCond);
288         }
289
290         /**
291          * Setter for cache list
292          *
293          * @param               $cacheList      The new cache list to set or null for initialization/reset
294          * @return      void
295          */
296         private final function setAllCacheList (FrameworkArrayObject $cacheList = null) {
297                 $this->cacheList = $cacheList;
298         }
299
300         /**
301          * Setter for cache conditions
302          *
303          * @param               $cacheCond      The new cache conditions to set
304          * @return      void
305          */
306         private final function setCacheCond ($cacheCond) {
307                 $this->cacheCond = (string) $cacheCond;
308         }
309
310         /**
311          * Reset cache list
312          *
313          * @return      void
314          */
315         public function resetCache () {
316                 $this->setAllCacheList(null);
317                 $this->setCacheCond("");
318         }
319
320         /**
321          * Getter for surname. If no surname is set then default surnames are set
322          * for male and female personells.
323          *
324          * @return      $surname        The personell' surname
325          */
326         public final function getSurname () {
327                 $surname = parent::getSurname();
328
329                 // Make sure every one has a surname...
330                 if (empty($surname)) {
331                         if ($this->isMale()) {
332                                 // Typical male name
333                                 $surname = "John";
334                         } else {
335                                 // Typical female name
336                                 $surname = "Jennifer";
337                         }
338
339                         // Set typical family name
340                         parent::setFamily("Smith");
341                 } // END - if
342
343                 // Return surname
344                 return $surname;
345         }
346
347         /**
348          * Getter for personell list
349          *
350          * @return      $personellList          The list of all personells
351          */
352         public final function getPersonellList () {
353                 return $this->personellList;
354         }
355
356         /**
357          * Loads the mostly pre-cached personell list
358          *
359          * @param       $idNumber       The ID number we shall use for looking up
360          *                                              the right data.
361          * @return      void
362          * @throws      ContainerItemIsNullException    If a container item is null
363          * @throws      ContainerItemIsNoArrayException If a container item is
364          *                                                                                      not an array
365          * @throws      ContainerMaybeDamagedException  If the container item
366          *                                                                                      is missing the indexes
367          *                                                                                      'name' and/or 'value'
368          */
369         public function loadPersonellList ($idNumber) {
370                 // Cleared because old code
371                 $this->partialStub("Cleared because old lost code was used.");
372         }
373 }
374
375 // [EOF]
376 ?>