| Conditions | 26 |
| Paths | 180 |
| Total Lines | 180 |
| Code Lines | 129 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 117 | public function __construct() |
||
| 118 | { |
||
| 119 | global $WT_TREE; |
||
| 120 | |||
| 121 | parent::__construct(); |
||
| 122 | $this->setPageTitle(I18N::translate('Lifespans')); |
||
| 123 | |||
| 124 | $this->facts = explode('|', WT_EVENTS_BIRT . '|' . WT_EVENTS_DEAT . '|' . WT_EVENTS_MARR . '|' . WT_EVENTS_DIV); |
||
| 125 | $tmp = explode('\\', get_class(I18N::defaultCalendar())); |
||
| 126 | $cal = strtolower(array_pop($tmp)); |
||
| 127 | $this->defaultCalendar = str_replace('calendar', '', $cal); |
||
| 128 | $filterPids = false; |
||
| 129 | |||
| 130 | // Request parameters |
||
| 131 | $clear = Filter::getBool('clear'); |
||
| 132 | $newpid = Filter::get('newpid', WT_REGEX_XREF); |
||
| 133 | $addfam = Filter::getBool('addFamily'); |
||
| 134 | $this->place = Filter::get('place'); |
||
| 135 | $this->beginYear = Filter::getInteger('beginYear', 0, PHP_INT_MAX, null); |
||
| 136 | $this->endYear = Filter::getInteger('endYear', 0, PHP_INT_MAX, null); |
||
| 137 | $this->calendar = Filter::get('calendar', null, $this->defaultCalendar); |
||
| 138 | $this->strictDate = Filter::getBool('strictDate'); |
||
| 139 | |||
| 140 | // Set up base color parameters |
||
| 141 | $this->colors['M'] = new ColorGenerator(240, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE * -1); |
||
|
|
|||
| 142 | $this->colors['F'] = new ColorGenerator(000, self::SATURATION, self::LIGHTNESS, self::ALPHA, self::RANGE); |
||
| 143 | |||
| 144 | // Build a list of people based on the input parameters |
||
| 145 | if ($clear) { |
||
| 146 | // Empty list & reset form |
||
| 147 | $xrefs = array(); |
||
| 148 | $this->place = null; |
||
| 149 | $this->beginYear = null; |
||
| 150 | $this->endYear = null; |
||
| 151 | $this->calendar = $this->defaultCalendar; |
||
| 152 | } elseif ($this->place) { |
||
| 153 | // Get all individual & family records found for a place |
||
| 154 | $this->place_obj = new Place($this->place, $WT_TREE); |
||
| 155 | $xrefs = Database::prepare( |
||
| 156 | "SELECT DISTINCT `i_id` FROM `##placelinks`" . |
||
| 157 | " JOIN `##individuals` ON `pl_gid`=`i_id` AND `pl_file`=`i_file`" . |
||
| 158 | " WHERE `i_file`=:tree_id" . |
||
| 159 | " AND `pl_p_id`=:place_id" . |
||
| 160 | " UNION" . |
||
| 161 | " SELECT DISTINCT `f_id` FROM `##placelinks`" . |
||
| 162 | " JOIN `##families` ON `pl_gid`=`f_id` AND `pl_file`=`f_file`" . |
||
| 163 | " WHERE `f_file`=:tree_id" . |
||
| 164 | " AND `pl_p_id`=:place_id" |
||
| 165 | )->execute(array( |
||
| 166 | 'tree_id' => $WT_TREE->getTreeId(), |
||
| 167 | 'place_id' => $this->place_obj->getPlaceId(), |
||
| 168 | ))->fetchOneColumn(); |
||
| 169 | } else { |
||
| 170 | // Modify an existing list of records |
||
| 171 | $xrefs = Session::get(self::SESSION_DATA, array()); |
||
| 172 | if ($newpid) { |
||
| 173 | $xrefs = array_merge($xrefs, $this->addFamily(Individual::getInstance($newpid, $WT_TREE), $addfam)); |
||
| 174 | $xrefs = array_unique($xrefs); |
||
| 175 | } elseif (!$xrefs) { |
||
| 176 | $xrefs = $this->addFamily($this->getSignificantIndividual(), false); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | $tmp = $this->getCalendarDate(unixtojd()); |
||
| 181 | $this->currentYear = $tmp->today()->y; |
||
| 182 | |||
| 183 | $tmp = strtoupper(strtr($this->calendar, array( |
||
| 184 | 'jewish' => 'hebrew', |
||
| 185 | 'french' => 'french r', |
||
| 186 | ))); |
||
| 187 | $this->calendarEscape = sprintf('@#D%s@', $tmp); |
||
| 188 | |||
| 189 | if ($xrefs) { |
||
| 190 | // ensure date ranges are valid in preparation for filtering list |
||
| 191 | if ($this->beginYear || $this->endYear) { |
||
| 192 | $filterPids = true; |
||
| 193 | if (!$this->beginYear) { |
||
| 194 | $tmp = new Date($this->calendarEscape . ' 1'); |
||
| 195 | $this->beginYear = $tmp->minimumDate()->y; |
||
| 196 | } |
||
| 197 | if (!$this->endYear) { |
||
| 198 | $this->endYear = $this->currentYear; |
||
| 199 | } |
||
| 200 | $this->startDate = new Date($this->calendarEscape . $this->beginYear); |
||
| 201 | $this->endDate = new Date($this->calendarEscape . $this->endYear); |
||
| 202 | } |
||
| 203 | |||
| 204 | // Test each xref to see if the search criteria are met |
||
| 205 | foreach ($xrefs as $key => $xref) { |
||
| 206 | $valid = false; |
||
| 207 | $person = Individual::getInstance($xref, $WT_TREE); |
||
| 208 | if ($person !== null && $person->canShow()) { |
||
| 209 | foreach ($person->getFacts() as $fact) { |
||
| 210 | if ($this->checkFact($fact)) { |
||
| 211 | $this->people[] = $person; |
||
| 212 | $valid = true; |
||
| 213 | break; |
||
| 214 | } |
||
| 215 | } |
||
| 216 | } else { |
||
| 217 | $family = Family::getInstance($xref, $WT_TREE); |
||
| 218 | if ($family !== null && $family->canShow()) { |
||
| 219 | foreach ($family->getFacts() as $fact) { |
||
| 220 | if ($this->checkFact($fact)) { |
||
| 221 | $valid = true; |
||
| 222 | $this->people[] = $family->getHusband(); |
||
| 223 | $this->people[] = $family->getWife(); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 | if (!$valid) { |
||
| 229 | unset($xrefs[$key]); // no point in storing a xref if we can't use it |
||
| 230 | } |
||
| 231 | } |
||
| 232 | Session::put(self::SESSION_DATA, $xrefs); |
||
| 233 | } else { |
||
| 234 | Session::forget(self::SESSION_DATA); |
||
| 235 | } |
||
| 236 | |||
| 237 | $this->people = array_filter(array_unique($this->people)); |
||
| 238 | $count = count($this->people); |
||
| 239 | if ($count) { |
||
| 240 | // Build the subtitle |
||
| 241 | if ($this->place && $filterPids) { |
||
| 242 | $this->subtitle = I18N::plural( |
||
| 243 | '%s individual with events in %s between %s and %s', |
||
| 244 | '%s individuals with events in %s between %s and %s', |
||
| 245 | $count, I18N::number($count), |
||
| 246 | $this->place, $this->startDate->display(false, '%Y'), $this->endDate->display(false, '%Y') |
||
| 247 | ); |
||
| 248 | } elseif ($this->place) { |
||
| 249 | $this->subtitle = I18N::plural( |
||
| 250 | '%s individual with events in %s', |
||
| 251 | '%s individuals with events in %s', |
||
| 252 | $count, I18N::number($count), |
||
| 253 | $this->place |
||
| 254 | ); |
||
| 255 | } elseif ($filterPids) { |
||
| 256 | $this->subtitle = I18N::plural( |
||
| 257 | '%s individual with events between %s and %s', |
||
| 258 | '%s individuals with events between %s and %s', |
||
| 259 | $count, I18N::number($count), |
||
| 260 | $this->startDate->display(false, '%Y'), $this->endDate->display(false, '%Y') |
||
| 261 | ); |
||
| 262 | } else { |
||
| 263 | $this->subtitle = I18N::plural( |
||
| 264 | '%s individual', |
||
| 265 | '%s individuals', |
||
| 266 | $count, I18N::number($count)); |
||
| 267 | } |
||
| 268 | |||
| 269 | // Sort the array in order of birth year |
||
| 270 | usort($this->people, function (Individual $a, Individual $b) { |
||
| 271 | return Date::compare($a->getEstimatedBirthDate(), $b->getEstimatedBirthDate()); |
||
| 272 | }); |
||
| 273 | |||
| 274 | //Find the mimimum birth year and maximum death year from the individuals in the array. |
||
| 275 | $bdate = $this->getCalendarDate($this->people[0]->getEstimatedBirthDate()->minimumJulianDay()); |
||
| 276 | $minyear = $bdate->y; |
||
| 277 | |||
| 278 | $that = $this; // PHP5.3 cannot access $this inside a closure |
||
| 279 | $maxyear = array_reduce($this->people, function ($carry, Individual $item) use ($that) { |
||
| 280 | $date = $that->getCalendarDate($item->getEstimatedDeathDate()->maximumJulianDay()); |
||
| 281 | |||
| 282 | return max($carry, $date->y); |
||
| 283 | }, 0); |
||
| 284 | } elseif ($filterPids) { |
||
| 285 | $minyear = $this->endYear; |
||
| 286 | $maxyear = $this->endYear; |
||
| 287 | } else { |
||
| 288 | $minyear = $this->currentYear; |
||
| 289 | $maxyear = $this->currentYear; |
||
| 290 | } |
||
| 291 | |||
| 292 | $maxyear = min($maxyear, $this->currentYear); // Limit maximum year to current year as we can't forecast the future |
||
| 293 | $minyear = min($minyear, $maxyear - $WT_TREE->getPreference('MAX_ALIVE_AGE')); // Set default minimum chart length |
||
| 294 | |||
| 295 | $this->timelineMinYear = (int) floor($minyear / 10) * 10; // round down to start of the decade |
||
| 296 | $this->timelineMaxYear = (int) ceil($maxyear / 10) * 10; // round up to start of next decade |
||
| 297 | } |
||
| 556 |