d77fbaa05d9ff95e78bd3ac385beef1e6e05cd14
[shipsimu.git] / application / ship-simu / main / class_BasePersonell.php
1 <?php
2 /**
3  * A general class for personell
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 BasePersonell extends BaseFrameworkSystem implements Personellizer {
25         // Maximum/minimum age
26         private $MIN_AGE = 21;
27         private $MAX_AGE = 40;
28
29         // Male/female
30         private $gender     = ""; // M=Male, F=Female, empty=uninitialized
31
32         // Year/month/day of birth
33         private $yearBirth  = 0;
34         private $monthBirth = 0;
35         private $dayBirth   = 0;
36
37         // Surname/family name
38         private $surname    = "";
39         private $family     = "";
40
41         // Employed?
42         private $employed   = false;
43
44         // Married?
45         private $married    = false;
46
47         // Her/his salary
48         private $salary     = 0.00;
49
50         // Constructor
51         private function __construct ($class) {
52                 // Call parent constructor
53                 parent::constructor($class);
54
55                 // Debug message
56                 if ((defined('DEBUG_PERSONELL')) && (defined('DEBUG_CONSTRUCT'))) $this->getDebugInstance()->output("[PersonellBase:] Konstruktor erreicht.<br />\n");
57
58                 // Set description
59                 $this->setPartDescr("Personal");
60         }
61
62         // Calls the constructor
63         public function constructor ($class) {
64                 $this->__construct($class);
65         }
66
67         // Remove min/max ages
68         public final function removeMinMaxAge () {
69                 if (defined('DEBUG_PERSONELL')) $this->getDebugInstance()->output(sprintf("[%s:%d] Minimum-/Maximum-Alter entfernt.<br />\n",
70                         __CLASS__,
71                         __LINE__
72                 ));
73                 unset($this->MIN_AGE);
74                 unset($this->MAX_AGE);
75         }
76
77         // Generates a birthday based on MAX_AGE/MIN_AGE and the current date
78         public final function createBirthday () {
79                 // Is the birthday already set?
80                 if ($this->isDateValid($this->yearBirth, $this->monthBirth, $this->dayBirth)) return false;
81
82                 // Get current year
83                 $currYear = date("Y", time());
84
85                 // Generate random year/month/day
86                 $year = mt_rand(($currYear - $this->MIN_AGE), ($currYear - $this->MAX_AGE));
87                 $month = 0;
88                 $day = 0;
89                 while (!$this->isDateValid($year, $month, $day)) {
90                         $month = mt_rand(1, 12);
91                         switch ($month) {
92                         case 1:
93                         case 3:
94                         case 5:
95                         case 7:
96                         case 8:
97                         case 10:
98                         case 12:
99                                 $day   = mt_rand(1, 31);
100                                 break;
101
102                         case 4:
103                         case 6:
104                         case 9:
105                         case 11:
106                                 $day   = mt_rand(1, 30);
107                                 break;
108
109                         case 2: // February
110                                 if ($year % 4 == 0) {
111                                         // Is a "Schaltjahr"
112                                         $day = mt_rand(1, 29);
113                                 } else {
114                                         // Regular year
115                                         $day = mt_rand(1, 28);
116                                 }
117                                 break;
118                         } // switch - END
119                 } // while - END
120
121                 // Set the new birthday
122                 $this->setBirthday($year, $month, $day);
123         }
124
125         // Is the current day valid?
126         public final function isDateValid ($year, $month, $day) {
127                 // Create timestamp
128                 $stamp = mktime(0, 0, 0, $month, $day, $year);
129
130                 // Get year/month/day back
131                 $y = date("Y", $stamp);
132                 $m = date("m", $stamp);
133                 $d = date("d", $stamp);
134
135                 // Compare all
136                 return (($y == $year) && ($m == $month) && ($d == $day));
137         }
138
139         // Employed?
140         public final function isEmployed () {
141                 return $this->employed;
142         }
143
144         // Married?
145         public final function isMarried () {
146                 return $this->married;
147         }
148
149         // Male?
150         public final function isMale () {
151                 return ($this->gender == "M");
152         }
153
154         // Female
155         public final function isFemale () {
156                 return ($this->gender == "F");
157         }
158
159         // Setter for surname
160         public final function setSurname ($surname) {
161                 $this->surname = (string) $surname;
162         }
163
164         // Getter for surname
165         public function getSurname () {
166                 return $this->surname;
167         }
168
169         // Setter for family name
170         public final function setFamily ($family) {
171                 $this->family = (string) $family;
172         }
173
174         // Getter for family name
175         public final function getFamily () {
176                 return $this->family;
177         }
178
179         // Setter for gender
180         public final function setGender ($gender) {
181                 // Set random gender here
182                 if (($gender == "M") || ($gender == "F") || ((empty($gender)) && ($this->getSurname() == ""))) {
183                         $this->gender = $gender;
184                 } else {
185                         throw new WrongGenderSpecifiedException($gender, self::EXCEPTION_GENDER_IS_WRONG);
186                 }
187         }
188
189         // Getter for gender
190         public final function getGender () {
191                 return $this->gender;
192         }
193
194         // Setter for employment status
195         public final function setEmployed ($employed) {
196                 $this->employed = (boolean) $employed;
197         }
198
199         // Setter for marriage status
200         public final function setMarried ($married) {
201                 $this->married = (boolean) $married;
202         }
203
204         // Getter for salary
205         public final function getSalary () {
206                 return $this->salary;
207         }
208
209         // Increase salary
210         public final function increaseSalary ($add) {
211                 $this->salary += (float) abs($add);
212         }
213
214         // Decrease salary
215         public final function decreaseSalary ($sub) {
216                 $this->salary -= (float) abs($sub);
217         }
218
219         // Setter for birthday
220         public final function setBirthday ($year, $month, $day) {
221                 $this->yearBirth  = (int) abs($year);
222                 $this->monthBirth = (int) abs($month);
223                 $this->dayBirth   = (int) abs($day);
224         }
225
226         // Remove gender
227         public final function removeGender () {
228                 unset($this->gender);
229         }
230
231         // Remove both names
232         public final function removeNames () {
233                 unset($this->surname);
234                 unset($this->family);
235         }
236
237         // Remove complete birthday
238         public final function removeBirthday () {
239                 unset($this->yearBirth);
240                 unset($this->monthBirth);
241                 unset($this->dayBirth);
242         }
243
244         // Remove salary
245         public final function removeSalary () {
246                 unset($this->salary);
247         }
248
249         // Remove employment status
250         public final function removeEmployed () {
251                 unset($this->employed);
252         }
253
254         // Remove marrital status
255         public final function removeMarried () {
256                 unset($this->married);
257         }
258 }
259
260 // [EOF]
261 ?>