| Total Complexity | 55 |
| Total Lines | 330 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like IndividualController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use IndividualController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class IndividualController extends GedcomRecordController { |
||
| 37 | /** @var int Count of names */ |
||
| 38 | public $name_count = 0; |
||
| 39 | |||
| 40 | /** @var int Count of names. */ |
||
| 41 | public $total_names = 0; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Startup activity |
||
| 45 | * |
||
| 46 | * @param Individual|null $record |
||
| 47 | */ |
||
| 48 | public function __construct($record) { |
||
| 49 | parent::__construct($record); |
||
| 50 | |||
| 51 | // If we can display the details, add them to the page header |
||
| 52 | if ($this->record && $this->record->canShow()) { |
||
| 53 | $this->setPageTitle($this->record->getFullName() . ' ' . $this->record->getLifeSpan()); |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Get significant information from this page, to allow other pages such as |
||
| 59 | * charts and reports to initialise with the same records |
||
| 60 | * |
||
| 61 | * @return Individual |
||
| 62 | */ |
||
| 63 | public function getSignificantIndividual() { |
||
| 64 | if ($this->record) { |
||
| 65 | return $this->record; |
||
| 66 | } |
||
| 67 | |||
| 68 | return parent::getSignificantIndividual(); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Get significant information from this page, to allow other pages such as |
||
| 73 | * charts and reports to initialise with the same records |
||
| 74 | * |
||
| 75 | * @return Family |
||
| 76 | */ |
||
| 77 | public function getSignificantFamily() { |
||
| 78 | if ($this->record) { |
||
| 79 | foreach ($this->record->getChildFamilies() as $family) { |
||
| 80 | return $family; |
||
| 81 | } |
||
| 82 | foreach ($this->record->getSpouseFamilies() as $family) { |
||
| 83 | return $family; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | return parent::getSignificantFamily(); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Which tabs should we show on this individual's page. |
||
| 92 | * We don't show empty tabs. |
||
| 93 | * |
||
| 94 | * @param Individual $individual |
||
| 95 | * |
||
| 96 | * @return ModuleTabInterface[] |
||
| 97 | */ |
||
| 98 | public function getTabs(Individual $individual) { |
||
| 99 | $active_tabs = Module::getActiveTabs($individual->getTree()); |
||
| 100 | |||
| 101 | return array_filter($active_tabs, function (ModuleTabInterface $tab) use ($individual) { |
||
| 102 | return $tab->hasTabContent($individual); |
||
| 103 | }); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Handle AJAX requests - to generate the tab content |
||
| 108 | * |
||
| 109 | * @param Individual $individual |
||
| 110 | */ |
||
| 111 | public function ajaxRequest(Individual $individual) { |
||
| 112 | header('Content-Type: text/html; charset=UTF-8'); |
||
| 113 | |||
| 114 | $tab = Filter::get('module'); |
||
| 115 | $tabs = $this->getTabs($individual); |
||
| 116 | |||
| 117 | if (!array_key_exists($tab, $tabs)) { |
||
| 118 | http_response_code(404); |
||
| 119 | } else { |
||
| 120 | echo $tabs[$tab]->getTabContent($individual); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Format a name record |
||
| 126 | * |
||
| 127 | * @param int $n |
||
| 128 | * @param Fact $fact |
||
| 129 | * |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | public function formatNameRecord($n, Fact $fact) { |
||
| 133 | $individual = $fact->getParent(); |
||
| 134 | |||
| 135 | // Create a dummy record, so we can extract the formatted NAME value from it. |
||
| 136 | $dummy = new Individual( |
||
| 137 | 'xref', |
||
| 138 | "0 @xref@ INDI\n1 DEAT Y\n" . $fact->getGedcom(), |
||
| 139 | null, |
||
| 140 | $individual->getTree() |
||
| 141 | ); |
||
| 142 | $dummy->setPrimaryName(0); // Make sure we use the name from "1 NAME" |
||
| 143 | |||
| 144 | $container_class = 'card'; |
||
| 145 | $content_class = 'collapse'; |
||
| 146 | $aria = 'false'; |
||
| 147 | |||
| 148 | if ($n === 0) { |
||
| 149 | $content_class = 'collapse show'; |
||
| 150 | $aria = 'true'; |
||
| 151 | } |
||
| 152 | if ($fact->isPendingDeletion()) { |
||
| 153 | $container_class .= ' old'; |
||
| 154 | } elseif ($fact->isPendingAddition()) { |
||
| 155 | $container_class .= ' new'; |
||
| 156 | } |
||
| 157 | |||
| 158 | ob_start(); |
||
| 159 | echo '<dl><dt class="label">', I18N::translate('Name'), '</dt>'; |
||
| 160 | echo '<dd class="field">', $dummy->getFullName(), '</dd>'; |
||
| 161 | $ct = preg_match_all('/\n2 (\w+) (.*)/', $fact->getGedcom(), $nmatch, PREG_SET_ORDER); |
||
| 162 | for ($i = 0; $i < $ct; $i++) { |
||
| 163 | $tag = $nmatch[$i][1]; |
||
| 164 | if ($tag != 'SOUR' && $tag != 'NOTE' && $tag != 'SPFX') { |
||
| 165 | echo '<dt class="label">', GedcomTag::getLabel($tag, $this->record), '</dt>'; |
||
| 166 | echo '<dd class="field">'; // Before using dir="auto" on this field, note that Gecko treats this as an inline element but WebKit treats it as a block element |
||
| 167 | if (isset($nmatch[$i][2])) { |
||
| 168 | $name = e($nmatch[$i][2]); |
||
| 169 | $name = str_replace('/', '', $name); |
||
| 170 | $name = preg_replace('/(\S*)\*/', '<span class="starredname">\\1</span>', $name); |
||
| 171 | switch ($tag) { |
||
| 172 | case 'TYPE': |
||
| 173 | echo GedcomCodeName::getValue($name, $this->record); |
||
| 174 | break; |
||
| 175 | case 'SURN': |
||
| 176 | // The SURN field is not necessarily the surname. |
||
| 177 | // Where it is not a substring of the real surname, show it after the real surname. |
||
| 178 | $surname = e($dummy->getAllNames()[0]['surname']); |
||
| 179 | $surns = preg_replace('/, */', ' ', $nmatch[$i][2]); |
||
| 180 | if (strpos($dummy->getAllNames()[0]['surname'], $surns) !== false) { |
||
| 181 | echo '<span dir="auto">' . $surname . '</span>'; |
||
| 182 | } else { |
||
| 183 | echo I18N::translate('%1$s (%2$s)', '<span dir="auto">' . $surname . '</span>', '<span dir="auto">' . $name . '</span>'); |
||
| 184 | } |
||
| 185 | break; |
||
| 186 | default: |
||
| 187 | echo '<span dir="auto">' . $name . '</span>'; |
||
| 188 | break; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | echo '</dd>'; |
||
| 192 | echo '</dl>'; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | if (preg_match("/\n2 SOUR/", $fact->getGedcom())) { |
||
| 196 | echo '<div id="indi_sour" class="clearfloat">', FunctionsPrintFacts::printFactSources($fact->getGedcom(), 2), '</div>'; |
||
| 197 | } |
||
| 198 | if (preg_match("/\n2 NOTE/", $fact->getGedcom())) { |
||
| 199 | echo '<div id="indi_note" class="clearfloat">', FunctionsPrint::printFactNotes($fact->getGedcom(), 2), '</div>'; |
||
| 200 | } |
||
| 201 | $content = ob_get_clean(); |
||
| 202 | |||
| 203 | if ($this->record->canEdit() && !$fact->isPendingDeletion()) { |
||
| 204 | $edit_links = |
||
| 205 | FontAwesome::linkIcon('delete', I18N::translate('Delete this name'), ['class' => 'btn btn-link', 'href' => '#', 'onclick' => 'return delete_fact("' . I18N::translate('Are you sure you want to delete this fact?') . '", "' . $this->record->getXref() . '", "' . $fact->getFactId() . '");']) . |
||
| 206 | FontAwesome::linkIcon('edit', I18N::translate('Edit the name'), ['class' => 'btn btn-link', 'href' => 'edit_interface.php?action=editname&xref=' . $this->record->getXref() . '&fact_id=' . $fact->getFactId() . '&ged=' . $this->record->getTree()->getNameHtml()]); |
||
| 207 | } else { |
||
| 208 | $edit_links = ''; |
||
| 209 | } |
||
| 210 | |||
| 211 | return ' |
||
| 212 | <div class="' . $container_class . '"> |
||
| 213 | <div class="card-header" role="tab" id="name-header-' . $n . '"> |
||
| 214 | <a data-toggle="collapse" data-parent="#individual-names" href="#name-content-' . $n . '" aria-expanded="' . $aria . '" aria-controls="name-content-' . $n . '">' . $dummy->getFullName() . '</a> |
||
| 215 | ' . $edit_links . ' |
||
| 216 | </div> |
||
| 217 | <div id="name-content-' . $n . '" class="' . $content_class . '" role="tabpanel" aria-labelledby="name-header-' . $n . '"> |
||
| 218 | <div class="card-body">' . $content . '</div> |
||
| 219 | </div> |
||
| 220 | </div>'; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * print information for a sex record |
||
| 225 | * |
||
| 226 | * @param Fact $fact |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public function formatSexRecord(Fact $fact) { |
||
| 231 | $individual = $fact->getParent(); |
||
| 232 | |||
| 233 | switch ($fact->getValue()) { |
||
| 234 | case 'M': |
||
| 235 | $sex = I18N::translate('Male'); |
||
| 236 | break; |
||
| 237 | case 'F': |
||
| 238 | $sex = I18N::translate('Female'); |
||
| 239 | break; |
||
| 240 | default: |
||
| 241 | $sex = I18N::translateContext('unknown gender', 'Unknown'); |
||
| 242 | break; |
||
| 243 | } |
||
| 244 | |||
| 245 | $container_class = 'card'; |
||
| 246 | if ($fact->isPendingDeletion()) { |
||
| 247 | $container_class .= ' old'; |
||
| 248 | } elseif ($fact->isPendingAddition()) { |
||
| 249 | $container_class .= ' new'; |
||
| 250 | } |
||
| 251 | |||
| 252 | if ($individual->canEdit() && !$fact->isPendingDeletion()) { |
||
| 253 | $edit_links = FontAwesome::linkIcon('edit', I18N::translate('Edit the gender'), ['class' => 'btn btn-link', 'href' => 'edit_interface.php?action=edit&xref=' . $individual->getXref() . '&fact_id=' . $fact->getFactId() . '&ged=' . $individual->getTree()->getNameHtml()]); |
||
| 254 | } else { |
||
| 255 | $edit_links = ''; |
||
| 256 | } |
||
| 257 | |||
| 258 | return ' |
||
| 259 | <div class="' . $container_class . '"> |
||
| 260 | <div class="card-header" role="tab" id="name-header-add"> |
||
| 261 | <div class="card-title mb-0"> |
||
| 262 | <b>' . I18N::translate('Gender') . '</b> ' . $sex . $edit_links . ' |
||
| 263 | </div> |
||
| 264 | </div> |
||
| 265 | </div>'; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * get edit menu |
||
| 270 | */ |
||
| 271 | public function getEditMenu() { |
||
| 272 | if (!$this->record || $this->record->isPendingDeletion()) { |
||
| 273 | return null; |
||
| 274 | } |
||
| 275 | // edit menu |
||
| 276 | $menu = new Menu(I18N::translate('Edit'), '#', 'menu-indi'); |
||
| 277 | |||
| 278 | if (Auth::isEditor($this->record->getTree())) { |
||
| 279 | // delete |
||
| 280 | $menu->addSubmenu(new Menu(I18N::translate('Delete'), '#', 'menu-indi-del', [ |
||
| 281 | 'onclick' => 'return delete_record("' . I18N::translate('Are you sure you want to delete “%s”?', strip_tags($this->record->getFullName())) . '", "' . $this->record->getXref() . '");', |
||
| 282 | ])); |
||
| 283 | } |
||
| 284 | |||
| 285 | // edit raw |
||
| 286 | if (Auth::isAdmin() || Auth::isEditor($this->record->getTree()) && $this->record->getTree()->getPreference('SHOW_GEDCOM_RECORD')) { |
||
| 287 | $menu->addSubmenu(new Menu(I18N::translate('Edit the raw GEDCOM'), 'edit_interface.php?action=editraw&ged=' . $this->record->getTree()->getNameHtml() . '&xref=' . $this->record->getXref(), 'menu-indi-editraw')); |
||
| 288 | } |
||
| 289 | |||
| 290 | return $menu; |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * get the person box stylesheet class for the given person |
||
| 295 | * |
||
| 296 | * @param Individual $person |
||
| 297 | * |
||
| 298 | * @return string returns 'person_box', 'person_boxF', or 'person_boxNN' |
||
| 299 | */ |
||
| 300 | public function getPersonStyle($person) { |
||
| 301 | switch ($person->getSex()) { |
||
| 302 | case 'M': |
||
| 303 | $class = 'person_box'; |
||
| 304 | break; |
||
| 305 | case 'F': |
||
| 306 | $class = 'person_boxF'; |
||
| 307 | break; |
||
| 308 | default: |
||
| 309 | $class = 'person_boxNN'; |
||
| 310 | break; |
||
| 311 | } |
||
| 312 | if ($person->isPendingDeletion()) { |
||
| 313 | $class .= ' old'; |
||
| 314 | } elseif ($person->isPendingAddition()) { |
||
| 315 | $class .= ' new'; |
||
| 316 | } |
||
| 317 | |||
| 318 | return $class; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get significant information from this page, to allow other pages such as |
||
| 323 | * charts and reports to initialise with the same records |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function getSignificantSurname() { |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get the contents of sidebar. |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function getSideBarContent() { |
||
| 366 | } |
||
| 367 | } |
||
| 368 | } |
||
| 369 |