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