05559cc836e3643ca6c0a153fc35088502b6ba2a
[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                 // Set description
44                 $this->setObjectDescription("Simulationspersonal");
45
46                 // Create unique ID
47                 $this->generateUniqueId();
48
49                 // Clean-up a little
50                 $this->removeSystemArray();
51         }
52
53         /**
54          * Magic wake-up method called when unserialize() is called. This is
55          * neccessary because in this case a personell does not need to know the
56          * min/max ages range and system classes. This would anyway use more RAM
57          * what is not required.
58          *
59          * @return      void
60          */
61         public function __wakeup () {
62                 // Tidy up a little
63                 $this->removePersonellList();
64                 $this->removeMinMaxAge();
65                 $this->removeCache();
66                 $this->removeSystemArray();
67         }
68
69         /**
70          * Generate a specified amount of personell and return the prepared instance
71          *
72          * @param               $amountPersonell                Number of personell we shall
73          *                                                              generate
74          * @return      $personellInstance              An instance of this object with a
75          *                                                              list of personells
76          */
77         public final static function createSimulatorPersonell ($amountPersonell) {
78                 // Make sure only integer can pass
79                 $amountPersonell = (int) $amountPersonell;
80
81                 // Get a new instance
82                 $personellInstance = new SimulatorPersonell();
83
84                 // Debug message
85                 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->getDebugInstance()->output(sprintf("[%s:%d] Es werden <strong>%d</strong> Personal bereitgestellt.",
86                         __CLASS__,
87                         __LINE__,
88                         $amountPersonell
89                 ));
90
91                 // Initialize the personell list
92                 $personellInstance->createPersonellList();
93
94                 // Create requested amount of personell
95                 for ($idx = 0; $idx < $amountPersonell; $idx++) {
96                         $personellInstance->addRandomPersonell();
97                 }
98
99                 // Debug message
100                 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->getDebugInstance()->output(sprintf("[%s:%d] <strong>%d</strong> Personal bereitgestellt.",
101                         __CLASS__,
102                         __LINE__,
103                         $amountPersonell
104                 ));
105
106                 // Tidy up a little
107                 $personellInstance->removeGender();
108                 $personellInstance->removeNames();
109                 $personellInstance->removeBirthday();
110                 $personellInstance->removeSalary();
111                 $personellInstance->removeEmployed();
112                 $personellInstance->removeMarried();
113                 $personellInstance->removeNumberFormaters();
114                 //$personellInstance->removeCache();
115                 $personellInstance->removeSystemArray();
116
117                 // Instanz zurueckgeben
118                 return $personellInstance;
119         }
120
121         /**
122          * Create a SimulatorPersonell object by loading the specified personell
123          * list from an existing database backend
124          *
125          * @param       $idNumber                       The ID number (only right part) of the list
126          * @return      $personellInstance      An instance of this class
127          * @throws      InvalidIDFormatException        If the given id number
128          *                                                                              $idNumber is invalid
129          * @throws      MissingSimulatorIdException             If an ID number was not found
130          */
131         public final static function createSimulatorPersonellByID ($idNumber) {
132                 // Add the class name if it was not found
133                 if (count(explode("@", $idNumber)) < 2) {
134                         // Add class name in front of the incomplete ID number
135                         $tempID = sprintf("%s@%s", __CLASS__, $idNumber);
136                 } else {
137                         // Use the direct ID number
138                         $tempID = $idNumber;
139                 }
140
141                 // Validate the ID number
142                 if (!preg_match(sprintf("/%s\@([a-f0-9]){32}/i", __CLASS__), $tempID)) {
143                         // Invalid format
144                         throw new InvalidIDFormatException(new SimulatorPersonell(), self::EXCEPTION_ID_IS_INVALID_FORMAT);
145                 }
146
147                 // Get instance
148                 $personellInstance = new SimulatorPersonell(false);
149
150                 // Get database instance
151                 $dbInstance = $personellInstance->getDatabaseInstance();
152
153                 // Is the unique ID already used? Then it must be there!
154                 if (!$dbInstance->isUniqueIdUsed($tempID))  {
155                         // Entry not found!
156                         throw new MissingSimulatorIdException(array($personellInstance, $idNumber), self::EXCEPTION_SIMULATOR_ID_INVALID);
157                 }
158
159                 // Load the personell list and add it to this object
160                 $personellInstance->loadPersonellList($tempID);
161
162                 // Clean-up a little
163                 $personellInstance->removeGender();
164                 $personellInstance->removeNames();
165                 $personellInstance->removeBirthday();
166                 $personellInstance->removeSalary();
167                 $personellInstance->removeEmployed();
168                 $personellInstance->removeMarried();
169                 $personellInstance->removeNumberFormaters();
170                 //$personellInstance->removeCache();
171                 $personellInstance->removeSystemArray();
172
173                 // Return instance
174                 return $personellInstance;
175         }
176
177         // Create personell list
178         public function createPersonellList () {
179                 if (is_null($this->personellList)) {
180                         $this->personellList = new FrameworkArrayObject("FakedPersonellList");
181                 } else {
182                         throw new PersonellListAlreadyCreatedException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
183                 }
184         }
185
186         // Remove the personell list
187         private function removePersonellList () {
188                 unset($this->personellList);
189         }
190
191         // Add new personell object to our list
192         public function addRandomPersonell () {
193                 // Gender list...
194                 $genders = array("M", "F");
195
196                 // Create new personell members
197                 $personellInstance = new SimulatorPersonell();
198
199                 // Set a randomized gender
200                 $personellInstance->setGender($genders[mt_rand(0, 1)]);
201
202                 // Set a randomized birthday (maximum age required, see const MAX_AGE)
203                 $personellInstance->createBirthday();
204
205                 // Married? Same values means: married
206                 if (mt_rand(0, 5) == mt_rand(0, 5)) $personellInstance->setMarried(true);
207
208                 // Tidy up a little
209                 $personellInstance->removePersonellList();
210                 $personellInstance->removeMinMaxAge();
211                 $personellInstance->removeCache();
212                 $personellInstance->removeSystemArray();
213
214                 // Add new member to the list
215                 $this->personellList->append($personellInstance);
216         }
217
218         /**
219          * Get a specifyable list of our people, null or empty string will be ignored!
220          *
221          * @return      $cacheList      A list of cached personells
222          */
223         function getSpecialPersonellList ($isEmployed = null, $isMarried = null, $hasGender = "") {
224                 // Serialize the conditions for checking if we can take the cache
225                 $serialized = serialize(array($isEmployed, $isMarried, $hasGender));
226
227                 // The same (last) conditions?
228                 if (($serialized == $this->cacheCond) && (!is_null($this->cacheCond))) {
229                         if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Gecachte Liste wird verwendet.",
230                                 __CLASS__,
231                                 __LINE__
232                         ));
233
234                         // Return cached list
235                         return $this->cacheList;
236                 }
237
238                 // Output debug message
239                 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Personalliste wird nach Kriterien durchsucht...",
240                         __CLASS__,
241                         __LINE__
242                 ));
243
244                 // Remember the conditions
245                 $this->setCacheCond($serialized);
246
247                 // Create cached list
248                 $this->setAllCacheList(new FrameworkArrayObject('FakedCacheList'));
249
250                 // Search all unemployed personells
251                 for ($idx = $this->personellList->getIterator(); $idx->valid(); $idx->next()) {
252                         // Element holen
253                         $el = $idx->current();
254
255                         // Check currenylt all single conditions (combined conditions are not yet supported)
256                         if ((!is_null($isEmployed)) && ($el->isEmployed() == $isEmployed)) {
257                                 // Add this one (employed status asked)
258                                 $this->cacheList->append($el);
259                         } elseif ((!is_null($isMarried)) && ($el->isMarried() == $isMarried)) {
260                                 // Add this one (marrital status asked)
261                                 $this->cacheList->append($el);
262                         } elseif ((!empty($hasGender)) && ($el->getGender() == $hasGender)) {
263                                 // Add this one (specified gender)
264                                 $this->cacheList->append($el);
265                         }
266                 }
267
268                 // Return the completed list
269                 return $this->cacheList;
270         }
271
272         /**
273          * Get amount of unemployed personell
274          *
275          * @return      $count  Amount of unemployed personell
276          */
277         public final function getAllUnemployed () {
278                 // Get a temporary list
279                 $list = $this->getSpecialPersonellList(false);
280
281                 // Anzahl zurueckliefern
282                 return $list->count();
283         }
284
285         /**
286          * Remove cache things
287          *
288          * @return      void
289          */
290         private function removeCache () {
291                 // Remove cache data
292                 unset($this->cacheList);
293                 unset($this->cacheCond);
294         }
295
296         /**
297          * Setter for cache list
298          *
299          * @param               $cacheList      The new cache list to set or null for initialization/reset
300          * @return      void
301          */
302         private final function setAllCacheList (FrameworkArrayObject $cacheList = null) {
303                 $this->cacheList = $cacheList;
304         }
305
306         /**
307          * Setter for cache conditions
308          *
309          * @param               $cacheCond      The new cache conditions to set
310          * @return      void
311          */
312         private final function setCacheCond ($cacheCond) {
313                 $this->cacheCond = (string) $cacheCond;
314         }
315
316         /**
317          * Reset cache list
318          *
319          * @return      void
320          */
321         public function resetCache () {
322                 $this->setAllCacheList(null);
323                 $this->setCacheCond("");
324         }
325
326         /**
327          * Getter for surname. If no surname is set then default surnames are set
328          * for male and female personells.
329          *
330          * @return      $surname        The personell' surname
331          */
332         public final function getSurname () {
333                 $surname = parent::getSurname();
334
335                 // Make sure every one has a surname...
336                 if (empty($surname)) {
337                         if ($this->isMale()) {
338                                 // Typical male name
339                                 $surname = "John";
340                         } else {
341                                 // Typical female name
342                                 $surname = "Jennifer";
343                         }
344
345                         // Set typical family name
346                         parent::setFamily("Smith");
347                 } // END - if
348
349                 // Return surname
350                 return $surname;
351         }
352
353         /**
354          * Getter for personell list
355          *
356          * @return      $personellList          The list of all personells
357          */
358         public final function getPersonellList () {
359                 return $this->personellList;
360         }
361
362         /**
363          * Loads the mostly pre-cached personell list
364          *
365          * @param       $idNumber       The ID number we shall use for looking up
366          *                                              the right data.
367          * @return      void
368          * @throws      ContainerItemIsNullException    If a container item is null
369          * @throws      ContainerItemIsNoArrayException If a container item is
370          *                                                                                      not an array
371          * @throws      ContainerMaybeDamagedException  If the container item
372          *                                                                                      is missing the indexes
373          *                                                                                      'name' and/or 'value'
374          * @see         SerializationContainer  A special container class which
375          *                                                                      helps storing only some attributes
376          *                                                                      of a class.
377          */
378         public function loadPersonellList ($idNumber) {
379                 // Cleared because old code
380                 $this->partialStub("Cleared because old lost code was used.");
381         }
382 }
383
384 // [EOF]
385 ?>