Total Complexity | 65 |
Total Lines | 428 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like FamilyTreeStatisticsModule 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 FamilyTreeStatisticsModule, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface |
||
32 | { |
||
33 | /** Show this number of surnames by default */ |
||
34 | const DEFAULT_NUMBER_OF_SURNAMES = 10; |
||
35 | |||
36 | /** {@inheritdoc} */ |
||
37 | public function getTitle() |
||
38 | { |
||
39 | return /* I18N: Name of a module */ I18N::translate('Statistics'); |
||
40 | } |
||
41 | |||
42 | /** {@inheritdoc} */ |
||
43 | public function getDescription() |
||
44 | { |
||
45 | return /* I18N: Description of “Statistics” module */ I18N::translate('The size of the family tree, earliest and latest events, common names, etc.'); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Generate the HTML content of this block. |
||
50 | * |
||
51 | * @param int $block_id |
||
52 | * @param bool $template |
||
53 | * @param string[] $cfg |
||
54 | * |
||
55 | * @return string |
||
56 | */ |
||
57 | public function getBlock($block_id, $template = true, $cfg = array()) |
||
58 | { |
||
59 | global $WT_TREE, $ctype; |
||
60 | |||
61 | $show_last_update = $this->getBlockSetting($block_id, 'show_last_update', '1'); |
||
62 | $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1'); |
||
63 | $number_of_surnames = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES); |
||
64 | $stat_indi = $this->getBlockSetting($block_id, 'stat_indi', '1'); |
||
65 | $stat_fam = $this->getBlockSetting($block_id, 'stat_fam', '1'); |
||
66 | $stat_sour = $this->getBlockSetting($block_id, 'stat_sour', '1'); |
||
67 | $stat_media = $this->getBlockSetting($block_id, 'stat_media', '1'); |
||
68 | $stat_repo = $this->getBlockSetting($block_id, 'stat_repo', '1'); |
||
69 | $stat_surname = $this->getBlockSetting($block_id, 'stat_surname', '1'); |
||
70 | $stat_events = $this->getBlockSetting($block_id, 'stat_events', '1'); |
||
71 | $stat_users = $this->getBlockSetting($block_id, 'stat_users', '1'); |
||
72 | $stat_first_birth = $this->getBlockSetting($block_id, 'stat_first_birth', '1'); |
||
73 | $stat_last_birth = $this->getBlockSetting($block_id, 'stat_last_birth', '1'); |
||
74 | $stat_first_death = $this->getBlockSetting($block_id, 'stat_first_death', '1'); |
||
75 | $stat_last_death = $this->getBlockSetting($block_id, 'stat_last_death', '1'); |
||
76 | $stat_long_life = $this->getBlockSetting($block_id, 'stat_long_life', '1'); |
||
77 | $stat_avg_life = $this->getBlockSetting($block_id, 'stat_avg_life', '1'); |
||
78 | $stat_most_chil = $this->getBlockSetting($block_id, 'stat_most_chil', '1'); |
||
79 | $stat_avg_chil = $this->getBlockSetting($block_id, 'stat_avg_chil', '1'); |
||
80 | |||
81 | // This can be overriden when embedding in an HTML block |
||
82 | $block = '0'; |
||
83 | |||
84 | foreach (array('show_common_surnames', 'number_common_surnames', 'stat_indi', 'stat_fam', 'stat_sour', 'stat_media', 'stat_surname', 'stat_events', 'stat_users', 'stat_first_birth', 'stat_last_birth', 'stat_first_death', 'stat_last_death', 'stat_long_life', 'stat_avg_life', 'stat_most_chil', 'stat_avg_chil', 'block') as $name) { |
||
85 | if (array_key_exists($name, $cfg)) { |
||
86 | $$name = $cfg[$name]; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | $id = $this->getName() . $block_id; |
||
91 | $class = $this->getName() . '_block'; |
||
92 | if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { |
||
|
|||
93 | $title = '<a class="icon-admin" title="' . I18N::translate('Preferences') . '" href="block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype . '"></a>'; |
||
94 | } else { |
||
95 | $title = ''; |
||
96 | } |
||
97 | $title .= $this->getTitle() . ' — ' . $WT_TREE->getTitleHtml(); |
||
98 | |||
99 | $stats = new Stats($WT_TREE); |
||
100 | |||
101 | $content = ''; |
||
102 | |||
103 | if ($show_last_update) { |
||
104 | $content .= '<p>' . /* I18N: %s is a date */ |
||
105 | I18N::translate('This family tree was last updated on %s.', strip_tags($stats->gedcomUpdated())) . '</p>'; |
||
106 | } |
||
107 | |||
108 | /** Responsive Design */ |
||
109 | $content .= '<div class="stat-table1">'; |
||
110 | if ($stat_indi) { |
||
111 | $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Individuals') . '</div><div class="facts_value stats_value stat-cell"><a href="' . "indilist.php?surname_sublist=no&ged=" . $WT_TREE->getNameUrl() . '">' . $stats->totalIndividuals() . '</a></div></div>'; |
||
112 | $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Males') . '</div><div class="facts_value stats_value stat-cell">' . $stats->totalSexMales() . '<br>' . $stats->totalSexMalesPercentage() . '</div></div>'; |
||
113 | $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Females') . '</div><div class="facts_value stats_value stat-cell">' . $stats->totalSexFemales() . '<br>' . $stats->totalSexFemalesPercentage() . '</div></div>'; |
||
114 | } |
||
115 | if ($stat_surname) { |
||
116 | $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Total surnames') . '</div><div class="facts_value stats_value stat-cell"><a href="indilist.php?show_all=yes&surname_sublist=yes&ged=' . $WT_TREE->getNameUrl() . '">' . $stats->totalSurnames() . '</a></div></div>'; |
||
117 | } |
||
118 | if ($stat_fam) { |
||
119 | $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Families') . '</div><div class="facts_value stats_value stat-cell"><a href="famlist.php?ged=' . $WT_TREE->getNameUrl() . '">' . $stats->totalFamilies() . '</a></div></div>'; |
||
120 | } |
||
121 | if ($stat_sour) { |
||
122 | $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Sources') . '</div><div class="facts_value stats_value stat-cell"><a href="sourcelist.php?ged=' . $WT_TREE->getNameUrl() . '">' . $stats->totalSources() . '</a></div></div>'; |
||
123 | } |
||
124 | if ($stat_media) { |
||
125 | $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Media objects') . '</div><div class="facts_value stats_value stat-cell"><a href="medialist.php?ged=' . $WT_TREE->getNameUrl() . '">' . $stats->totalMedia() . '</a></div></div>'; |
||
126 | } |
||
127 | if ($stat_repo) { |
||
128 | $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Repositories') . '</div><div class="facts_value stats_value stat-cell"><a href="repolist.php?ged=' . $WT_TREE->getNameUrl() . '">' . $stats->totalRepositories() . '</a></div></div>'; |
||
129 | } |
||
130 | if ($stat_events) { |
||
131 | $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Total events') . '</div><div class="facts_value stats_value stat-cell">' . $stats->totalEvents() . '</div></div>'; |
||
132 | } |
||
133 | if ($stat_users) { |
||
134 | $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Total users') . '</div><div class="facts_value stats_value stat-cell">'; |
||
135 | if (Auth::isManager($WT_TREE)) { |
||
136 | $content .= '<a href="admin_users.php">' . $stats->totalUsers() . '</a>'; |
||
137 | } else { |
||
138 | $content .= $stats->totalUsers(); |
||
139 | } |
||
140 | $content .= '</div></div>'; |
||
141 | } |
||
142 | if (!$block) { |
||
143 | $content .= '</div><div class="facts_table stat-table2">'; |
||
144 | } |
||
145 | if ($stat_first_birth) { |
||
146 | $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Earliest birth year') . '</div><div class="facts_value stats_value stat-cell">' . $stats->firstBirthYear() . '</div>'; |
||
147 | if (!$block) { |
||
148 | $content .= '<div class="facts_value stat-cell left">' . $stats->firstBirth() . '</div>'; |
||
149 | } |
||
150 | $content .= '</div>'; |
||
151 | } |
||
152 | if ($stat_last_birth) { |
||
153 | $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Latest birth year') . '</div><div class="facts_value stats_value stat-cell">' . $stats->lastBirthYear() . '</div>'; |
||
154 | if (!$block) { |
||
155 | $content .= '<div class="facts_value stat-cell left">' . $stats->lastBirth() . '</div>'; |
||
156 | } |
||
157 | $content .= '</div>'; |
||
158 | } |
||
159 | if ($stat_first_death) { |
||
160 | $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Earliest death year') . '</div><div class="facts_value stats_value stat-cell">' . $stats->firstDeathYear() . '</div>'; |
||
161 | if (!$block) { |
||
162 | $content .= '<div class="facts_value stat-cell left">' . $stats->firstDeath() . '</div>'; |
||
163 | } |
||
164 | $content .= '</div>'; |
||
165 | } |
||
166 | if ($stat_last_death) { |
||
167 | $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Latest death year') . '</div><div class="facts_value stats_value stat-cell">' . $stats->lastDeathYear() . '</div>'; |
||
168 | if (!$block) { |
||
169 | $content .= '<div class="facts_value stat-cell left">' . $stats->lastDeath() . '</div>'; |
||
170 | } |
||
171 | $content .= '</div>'; |
||
172 | } |
||
173 | if ($stat_long_life) { |
||
174 | $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Individual who lived the longest') . '</div><div class="facts_value stats_value stat-cell">' . $stats->longestLifeAge() . '</div>'; |
||
175 | if (!$block) { |
||
176 | $content .= '<div class="facts_value stat-cell left">' . $stats->longestLife() . '</div>'; |
||
177 | } |
||
178 | $content .= '</div>'; |
||
179 | } |
||
180 | if ($stat_avg_life) { |
||
181 | $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Average age at death') . '</div><div class="facts_value stats_value stat-cell">' . $stats->averageLifespan() . '</div>'; |
||
182 | if (!$block) { |
||
183 | $content .= '<div class="facts_value stat-cell left">' . I18N::translate('Males') . ': ' . $stats->averageLifespanMale(); |
||
184 | $content .= ' ' . I18N::translate('Females') . ': ' . $stats->averageLifespanFemale() . '</div>'; |
||
185 | } |
||
186 | $content .= '</div>'; |
||
187 | } |
||
188 | |||
189 | if ($stat_most_chil && !$block) { |
||
190 | $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Family with the most children') . '</div><div class="facts_value stats_value stat-cell">' . $stats->largestFamilySize() . '</div>'; |
||
191 | if (!$block) { |
||
192 | $content .= '<div class="facts_value stat-cell left">' . $stats->largestFamily() . '</div>'; |
||
193 | } |
||
194 | $content .= '</div>'; |
||
195 | } |
||
196 | if ($stat_avg_chil) { |
||
197 | $content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Average number of children per family') . '</div><div class="facts_value stats_value stat-cell">' . $stats->averageChildren() . '</div>'; |
||
198 | if (!$block) { |
||
199 | $content .= '<div class="facts_value stat-cell left"></div>'; |
||
200 | } |
||
201 | $content .= '</div>'; |
||
202 | } |
||
203 | $content .= '</div>'; |
||
204 | |||
205 | if ($show_common_surnames) { |
||
206 | $surnames = FunctionsDb::getTopSurnames($WT_TREE->getTreeId(), 0, (int) $number_of_surnames); |
||
207 | |||
208 | $all_surnames = array(); |
||
209 | foreach (array_keys($surnames) as $surname) { |
||
210 | $all_surnames = array_merge($all_surnames, QueryName::surnames($WT_TREE, $surname, '', false, false)); |
||
211 | } |
||
212 | |||
213 | if (!empty($surnames)) { |
||
214 | ksort($all_surnames); |
||
215 | $content .= '<div class="clearfloat">'; |
||
216 | $content .= '<p>'; |
||
217 | $content .= '<strong>' . I18N::translate('Most common surnames') . '</strong>'; |
||
218 | $content .= '<br>'; |
||
219 | $content .= '<span class="common_surnames">' . FunctionsPrintLists::surnameList($all_surnames, 2, false, 'indilist.php', $WT_TREE) . '</span>'; |
||
220 | $content .= '</p>'; |
||
221 | $content .= '</div>'; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | if ($template) { |
||
226 | return Theme::theme()->formatBlock($id, $title, $class, $content); |
||
227 | } else { |
||
228 | return $content; |
||
229 | } |
||
230 | } |
||
231 | |||
232 | /** {@inheritdoc} */ |
||
233 | public function loadAjax() |
||
234 | { |
||
235 | return true; |
||
236 | } |
||
237 | |||
238 | /** {@inheritdoc} */ |
||
239 | public function isUserBlock() |
||
242 | } |
||
243 | |||
244 | /** {@inheritdoc} */ |
||
245 | public function isGedcomBlock() |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * An HTML form to edit block settings |
||
252 | * |
||
253 | * @param int $block_id |
||
254 | */ |
||
255 | public function configureBlock($block_id) |
||
465 |