Total Complexity | 76 |
Total Lines | 396 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like SlideShowModule 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 SlideShowModule, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class SlideShowModule extends AbstractModule implements ModuleBlockInterface |
||
32 | { |
||
33 | /** {@inheritdoc} */ |
||
34 | public function getTitle() |
||
35 | { |
||
36 | return /* I18N: Name of a module */ I18N::translate('Slide show'); |
||
37 | } |
||
38 | |||
39 | /** {@inheritdoc} */ |
||
40 | public function getDescription() |
||
41 | { |
||
42 | return /* I18N: Description of the “Slide show” module */ I18N::translate('Random images from the current family tree.'); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Generate the HTML content of this block. |
||
47 | * |
||
48 | * @param int $block_id |
||
49 | * @param bool $template |
||
50 | * @param string[] $cfg |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | public function getBlock($block_id, $template = true, $cfg = array()) |
||
55 | { |
||
56 | global $ctype, $WT_TREE; |
||
57 | |||
58 | $filter = $this->getBlockSetting($block_id, 'filter', 'all'); |
||
59 | $controls = $this->getBlockSetting($block_id, 'controls', '1'); |
||
60 | $start = $this->getBlockSetting($block_id, 'start', '0') || Filter::getBool('start'); |
||
61 | |||
62 | // We can apply the filters using SQL |
||
63 | // Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead. |
||
64 | $all_media = Database::prepare( |
||
65 | "SELECT m_id FROM `##media`" . |
||
66 | " WHERE m_file = ?" . |
||
67 | " AND m_ext IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '')" . |
||
68 | " AND m_type IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '')" |
||
69 | )->execute(array( |
||
70 | $WT_TREE->getTreeId(), |
||
71 | $this->getBlockSetting($block_id, 'filter_avi', '0') ? 'avi' : null, |
||
72 | $this->getBlockSetting($block_id, 'filter_bmp', '1') ? 'bmp' : null, |
||
73 | $this->getBlockSetting($block_id, 'filter_gif', '1') ? 'gif' : null, |
||
74 | $this->getBlockSetting($block_id, 'filter_jpeg', '1') ? 'jpg' : null, |
||
75 | $this->getBlockSetting($block_id, 'filter_jpeg', '1') ? 'jpeg' : null, |
||
76 | $this->getBlockSetting($block_id, 'filter_mp3', '0') ? 'mp3' : null, |
||
77 | $this->getBlockSetting($block_id, 'filter_ole', '1') ? 'ole' : null, |
||
78 | $this->getBlockSetting($block_id, 'filter_pcx', '1') ? 'pcx' : null, |
||
79 | $this->getBlockSetting($block_id, 'filter_pdf', '0') ? 'pdf' : null, |
||
80 | $this->getBlockSetting($block_id, 'filter_png', '1') ? 'png' : null, |
||
81 | $this->getBlockSetting($block_id, 'filter_tiff', '1') ? 'tiff' : null, |
||
82 | $this->getBlockSetting($block_id, 'filter_wav', '0') ? 'wav' : null, |
||
83 | $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null, |
||
84 | $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null, |
||
85 | $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null, |
||
86 | $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null, |
||
87 | $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null, |
||
88 | $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null, |
||
89 | $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null, |
||
90 | $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null, |
||
91 | $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null, |
||
92 | $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null, |
||
93 | $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null, |
||
94 | $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null, |
||
95 | $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null, |
||
96 | $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null, |
||
97 | $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null, |
||
98 | $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null, |
||
99 | $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null, |
||
100 | $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null, |
||
101 | ))->fetchOneColumn(); |
||
102 | |||
103 | // Keep looking through the media until a suitable one is found. |
||
104 | $random_media = null; |
||
105 | while ($all_media) { |
||
|
|||
106 | $n = array_rand($all_media); |
||
107 | $media = Media::getInstance($all_media[$n], $WT_TREE); |
||
108 | if ($media->canShow() && !$media->isExternal()) { |
||
109 | // Check if it is linked to a suitable individual |
||
110 | foreach ($media->linkedIndividuals('OBJE') as $indi) { |
||
111 | if ( |
||
112 | $filter === 'all' || |
||
113 | $filter === 'indi' && strpos($indi->getGedcom(), "\n1 OBJE @" . $media->getXref() . '@') !== false || |
||
114 | $filter === 'event' && strpos($indi->getGedcom(), "\n2 OBJE @" . $media->getXref() . '@') !== false |
||
115 | ) { |
||
116 | // Found one :-) |
||
117 | $random_media = $media; |
||
118 | break 2; |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | unset($all_media[$n]); |
||
123 | }; |
||
124 | |||
125 | $id = $this->getName() . $block_id; |
||
126 | $class = $this->getName() . '_block'; |
||
127 | if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { |
||
128 | $title = '<a class="icon-admin" title="' . I18N::translate('Preferences') . '" href="block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype . '"></a>'; |
||
129 | } else { |
||
130 | $title = ''; |
||
131 | } |
||
132 | $title .= $this->getTitle(); |
||
133 | |||
134 | if ($random_media) { |
||
135 | $content = "<div id=\"random_picture_container$block_id\">"; |
||
136 | if ($controls) { |
||
137 | if ($start) { |
||
138 | $icon_class = 'icon-media-stop'; |
||
139 | } else { |
||
140 | $icon_class = 'icon-media-play'; |
||
141 | } |
||
142 | $content .= '<div dir="ltr" class="center" id="random_picture_controls' . $block_id . '"><br>'; |
||
143 | $content .= "<a href=\"#\" onclick=\"togglePlay(); return false;\" id=\"play_stop\" class=\"" . $icon_class . "\" title=\"" . I18N::translate('Play') . "/" . I18N::translate('Stop') . '"></a>'; |
||
144 | $content .= '<a href="#" onclick="jQuery(\'#block_' . $block_id . '\').load(\'index.php?ctype=' . $ctype . '&action=ajax&block_id=' . $block_id . '\');return false;" title="' . I18N::translate('Next image') . '" class="icon-media-next"></a>'; |
||
145 | $content .= '</div><script> |
||
146 | var play = false; |
||
147 | function togglePlay() { |
||
148 | if (play) { |
||
149 | play = false; |
||
150 | jQuery("#play_stop").removeClass("icon-media-stop").addClass("icon-media-play"); |
||
151 | } |
||
152 | else { |
||
153 | play = true; |
||
154 | playSlideShow(); |
||
155 | jQuery("#play_stop").removeClass("icon-media-play").addClass("icon-media-stop"); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | function playSlideShow() { |
||
160 | if (play) { |
||
161 | window.setTimeout("reload_image()", 6000); |
||
162 | } |
||
163 | } |
||
164 | function reload_image() { |
||
165 | if (play) { |
||
166 | jQuery("#block_' . $block_id . '").load("index.php?ctype=' . $ctype . '&action=ajax&block_id=' . $block_id . '&start=1"); |
||
167 | } |
||
168 | } |
||
169 | </script>'; |
||
170 | } |
||
171 | if ($start) { |
||
172 | $content .= '<script>togglePlay();</script>'; |
||
173 | } |
||
174 | $content .= '<div class="center" id="random_picture_content' . $block_id . '">'; |
||
175 | $content .= '<table id="random_picture_box"><tr><td class="details1">'; |
||
176 | $content .= $random_media->displayImage(); |
||
177 | |||
178 | $content .= '<br>'; |
||
179 | $content .= '<a href="' . $random_media->getHtmlUrl() . '"><b>' . $random_media->getFullName() . '</b></a><br>'; |
||
180 | foreach ($random_media->linkedIndividuals('OBJE') as $individual) { |
||
181 | $content .= '<a href="' . $individual->getHtmlUrl() . '">' . I18N::translate('View this individual') . ' — ' . $individual->getFullName() . '</a><br>'; |
||
182 | } |
||
183 | foreach ($random_media->linkedFamilies('OBJE') as $family) { |
||
184 | $content .= '<a href="' . $family->getHtmlUrl() . '">' . I18N::translate('View this family') . ' — ' . $family->getFullName() . '</a><br>'; |
||
185 | } |
||
186 | foreach ($random_media->linkedSources('OBJE') as $source) { |
||
187 | $content .= '<a href="' . $source->getHtmlUrl() . '">' . I18N::translate('View this source') . ' — ' . $source->getFullName() . '</a><br>'; |
||
188 | } |
||
189 | $content .= '<br><div class="indent">'; |
||
190 | $content .= FunctionsPrint::printFactNotes($random_media->getGedcom(), "1", false); |
||
191 | $content .= '</div>'; |
||
192 | $content .= '</td></tr></table>'; |
||
193 | $content .= '</div>'; // random_picture_content |
||
194 | $content .= '</div>'; // random_picture_container |
||
195 | } else { |
||
196 | $content = I18N::translate('This family tree has no images to display.'); |
||
197 | } |
||
198 | if ($template) { |
||
199 | return Theme::theme()->formatBlock($id, $title, $class, $content); |
||
200 | } else { |
||
201 | return $content; |
||
202 | } |
||
203 | } |
||
204 | |||
205 | /** {@inheritdoc} */ |
||
206 | public function loadAjax() |
||
209 | } |
||
210 | |||
211 | /** {@inheritdoc} */ |
||
212 | public function isUserBlock() |
||
213 | { |
||
214 | return true; |
||
215 | } |
||
216 | |||
217 | /** {@inheritdoc} */ |
||
218 | public function isGedcomBlock() |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * An HTML form to edit block settings |
||
225 | * |
||
226 | * @param int $block_id |
||
227 | */ |
||
228 | public function configureBlock($block_id) |
||
429 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.