Passed
Branch master (48d769)
by Michael
02:30
created

mpedigree.php ➔ pedigree_main()   F

Complexity

Conditions 35
Paths > 20000

Size

Total Lines 284
Code Lines 126

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 35
eloc 126
nc 4294967295
nop 1
dl 0
loc 284
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
// -------------------------------------------------------------------------
3
4
use Xmf\Request;
5
use XoopsModules\Pedigree;
6
7
8
//require_once  dirname(dirname(__DIR__)) . '/mainfile.php';
9
require_once __DIR__ . '/header.php';
10
11
$moduleDirName = basename(__DIR__);
12
xoops_loadLanguage('main', $moduleDirName);
13
14
// Include any common code for this module.
15
require_once XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/include/common.php';
16
17
$GLOBALS['xoopsOption']['template_main'] = 'pedigree_mpedigree.tpl';
18
19
include $GLOBALS['xoops']->path('/header.php');
20
21
//get module configuration
22
/** @var XoopsModuleHandler $moduleHandler */
23
$moduleHandler = xoops_getHandler('module');
24
$module        = $moduleHandler->getByDirname($moduleDirName);
25
$configHandler = xoops_getHandler('config');
26
$moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
27
28
//always start with Anika
29
$pedId = Request::getInt('pedid', 0, 'GET');
30
//$pedId = $_GET['pedid'];
31
//draw pedigree
32
33
    $queryString = '
34
SELECT d.Id as d_id,
35
d.NAAM as d_naam,
36
    d.roft as d_roft,
37
f.Id as f_id,
38
f.NAAM as f_naam,
39
m.Id as m_id,
40
m.NAAM as m_naam,
41
ff.Id as ff_id,
42
ff.NAAM as ff_naam,
43
mf.Id as mf_id,
44
mf.NAAM as mf_naam,
45
fm.Id as fm_id,
46
fm.NAAM as fm_naam,
47
mm.Id as mm_id,
48
mm.NAAM as mm_naam,
49
fff.Id as fff_id,
50
fff.NAAM as fff_naam,
51
ffm.Id as ffm_id,
52
ffm.NAAM as ffm_naam,
53
fmf.Id as fmf_id,
54
fmf.NAAM as fmf_naam,
55
fmm.Id as fmm_id,
56
fmm.NAAM as fmm_naam,
57
mmf.Id as mmf_id,
58
mmf.NAAM as mmf_naam,
59
mff.Id as mff_id,
60
mff.NAAM as mff_naam,
61
mfm.Id as mfm_id,
62
mfm.NAAM as mfm_naam,
63
mmm.Id as mmm_id,
64
mmm.NAAM as mmm_naam,
65
ffff.Id as ffff_id,
66
ffff.NAAM as ffff_naam,
67
ffmf.Id as ffmf_id,
68
ffmf.NAAM as ffmf_naam,
69
fmff.Id as fmff_id,
70
fmff.NAAM as fmff_naam,
71
fmmf.Id as fmmf_id,
72
fmmf.NAAM as fmmf_naam,
73
mmff.Id as mmff_id,
74
mmff.NAAM as mmff_naam,
75
mfff.Id as mfff_id,
76
mfff.NAAM as mfff_naam,
77
mfmf.Id as mfmf_id,
78
mfmf.NAAM as mfmf_naam,
79
mmmf.Id as mmmf_id,
80
mmmf.NAAM as mmmf_naam,
81
fffm.Id as fffm_id,
82
fffm.NAAM as fffm_naam,
83
ffmm.Id as ffmm_id,
84
ffmm.NAAM as ffmm_naam,
85
fmfm.Id as fmfm_id,
86
fmfm.NAAM as fmfm_naam,
87
fmmm.Id as fmmm_id,
88
fmmm.NAAM as fmmm_naam,
89
mmfm.Id as mmfm_id,
90
mmfm.NAAM as mmfm_naam,
91
mffm.Id as mffm_id,
92
mffm.NAAM as mffm_naam,
93
mfmm.Id as mfmm_id,
94
mfmm.NAAM as mfmm_naam,
95
mmmm.Id as mmmm_id,
96
mmmm.NAAM as mmmm_naam
97
    FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' d
98
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' f ON d.father = f.Id
99
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' m ON d.mother = m.Id
100
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' ff ON f.father = ff.Id
101
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' fff ON ff.father = fff.Id
102
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' ffm ON ff.mother = ffm.Id
103
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' mf ON m.father = mf.Id
104
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' mff ON mf.father = mff.Id
105
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' mfm ON mf.mother = mfm.Id
106
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' fm ON f.mother = fm.Id
107
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' fmf ON fm.father = fmf.Id
108
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' fmm ON fm.mother = fmm.Id
109
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' mm ON m.mother = mm.Id
110
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' mmf ON mm.father = mmf.Id
111
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' mmm ON mm.mother = mmm.Id
112
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' ffff ON fff.father = ffff.Id
113
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' ffmf ON ffm.father = ffmf.Id
114
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' fmff ON fmf.father = fmff.Id
115
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' fmmf ON fmm.father = fmmf.Id
116
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' mmff ON mmf.father = mmff.Id
117
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' mfff ON mff.father = mfff.Id
118
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' mfmf ON mfm.father = mfmf.Id
119
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' mmmf ON mmm.father = mmmf.Id
120
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' fffm ON fff.mother = fffm.Id
121
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' ffmm ON ffm.mother = ffmm.Id
122
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' fmfm ON fmf.mother = fmfm.Id
123
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' fmmm ON fmm.mother = fmmm.Id
124
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' mmfm ON mmf.mother = mmfm.Id
125
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' mffm ON mff.mother = mffm.Id
126
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' mfmm ON mfm.mother = mfmm.Id
127
LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' mmmm ON mmm.mother = mmmm.Id
128
WHERE d.Id=' . $pedId;
129
130
    $result = $GLOBALS['xoopsDB']->query($queryString);
131
132
    while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
133
        //crete array to count frequency (to select colour)
134
        count_item($freq, $row['d_id']);
135
        count_item($freq, $row['f_id']);
136
        count_item($freq, $row['m_id']);
137
        count_item($freq, $row['ff_id']);
138
        count_item($freq, $row['fm_id']);
139
        count_item($freq, $row['mf_id']);
140
        count_item($freq, $row['mm_id']);
141
        count_item($freq, $row['fff_id']);
142
        count_item($freq, $row['ffm_id']);
143
        count_item($freq, $row['fmf_id']);
144
        count_item($freq, $row['fmm_id']);
145
        count_item($freq, $row['mff_id']);
146
        count_item($freq, $row['mfm_id']);
147
        count_item($freq, $row['mmf_id']);
148
        count_item($freq, $row['mmm_id']);
149
        count_item($freq, $row['ffff_id']);
150
        count_item($freq, $row['ffmf_id']);
151
        count_item($freq, $row['fmff_id']);
152
        count_item($freq, $row['fmmf_id']);
153
        count_item($freq, $row['mfff_id']);
154
        count_item($freq, $row['mfmf_id']);
155
        count_item($freq, $row['mmff_id']);
156
        count_item($freq, $row['mmmf_id']);
157
        count_item($freq, $row['fffm_id']);
158
        count_item($freq, $row['ffmm_id']);
159
        count_item($freq, $row['fmfm_id']);
160
        count_item($freq, $row['fmmm_id']);
161
        count_item($freq, $row['mffm_id']);
162
        count_item($freq, $row['mfmm_id']);
163
        count_item($freq, $row['mmfm_id']);
164
        count_item($freq, $row['mmmm_id']);
165
166
        //create array for dog (and all parents)
167
        //selected dog
168
        $d['d']['name'] = stripslashes($row['d_naam']);
169
        $d['d']['id']   = $row['d_id'];
170
        $d['d']['roft'] = $row['d_roft'];
171
        $d['d']['col']  = 'transparant';
172
        //father
173
        $d['f']['name'] = stripslashes($row['f_naam']);
174
        $d['f']['id']   = $row['f_id'];
175
        $d['f']['col']  = crcolour('f', $freq[$row['f_id']]);
176
        //mother
177
        $d['m']['name'] = stripslashes($row['m_naam']);
178
        $d['m']['id']   = $row['m_id'];
179
        $d['m']['col']  = crcolour('m', $freq[$row['m_id']]);
180
        //grandparents
181
        //father father
182
        $d['ff']['name'] = stripslashes($row['ff_naam']);
183
        $d['ff']['id']   = $row['ff_id'];
184
        $d['ff']['col']  = crcolour('f', $freq[$row['ff_id']]);
185
        //father mother
186
        $d['fm']['name'] = stripslashes($row['fm_naam']);
187
        $d['fm']['id']   = $row['fm_id'];
188
        $d['fm']['col']  = crcolour('m', $freq[$row['fm_id']]);
189
        //mother father
190
        $d['mf']['name'] = stripslashes($row['mf_naam']);
191
        $d['mf']['id']   = $row['mf_id'];
192
        $d['mf']['col']  = crcolour('f', $freq[$row['mf_id']]);
193
        //mother mother
194
        $d['mm']['name'] = stripslashes($row['mm_naam']);
195
        $d['mm']['id']   = $row['mm_id'];
196
        $d['mm']['col']  = crcolour('m', $freq[$row['mm_id']]);
197
        //great-grandparents
198
        //father father father
199
        $d['fff']['name'] = stripslashes($row['fff_naam']);
200
        $d['fff']['id']   = $row['fff_id'];
201
        $d['fff']['col']  = crcolour('f', $freq[$row['fff_id']]);
202
        //father father mother
203
        $d['ffm']['name'] = stripslashes($row['ffm_naam']);
204
        $d['ffm']['id']   = $row['ffm_id'];
205
        $d['ffm']['col']  = crcolour('m', $freq[$row['ffm_id']]);
206
        //father mother father
207
        $d['fmf']['name'] = stripslashes($row['fmf_naam']);
208
        $d['fmf']['id']   = $row['fmf_id'];
209
        $d['fmf']['col']  = crcolour('f', $freq[$row['fmf_id']]);
210
        //father mother mother
211
        $d['fmm']['name'] = stripslashes($row['fmm_naam']);
212
        $d['fmm']['id']   = $row['fmm_id'];
213
        $d['fmm']['col']  = crcolour('m', $freq[$row['fmm_id']]);
214
        //mother father father
215
        $d['mff']['name'] = stripslashes($row['mff_naam']);
216
        $d['mff']['id']   = $row['mff_id'];
217
        $d['mff']['col']  = crcolour('f', $freq[$row['mff_id']]);
218
        //mother father mother
219
        $d['mfm']['name'] = stripslashes($row['mfm_naam']);
220
        $d['mfm']['id']   = $row['mfm_id'];
221
        $d['mfm']['col']  = crcolour('m', $freq[$row['mfm_id']]);
222
        //mother mother father
223
        $d['mmf']['name'] = stripslashes($row['mmf_naam']);
224
        $d['mmf']['id']   = $row['mmf_id'];
225
        $d['mmf']['col']  = crcolour('f', $freq[$row['mmf_id']]);
226
        //mother mother mother
227
        $d['mmm']['name'] = stripslashes($row['mmm_naam']);
228
        $d['mmm']['id']   = $row['mmm_id'];
229
        $d['mmm']['col']  = crcolour('m', $freq[$row['mmm_id']]);
230
        //great-great-grandparents (fathers)
231
        //father father father
232
        $d['ffff']['name'] = stripslashes($row['ffff_naam']);
233
        $d['ffff']['id']   = $row['ffff_id'];
234
        $d['ffff']['col']  = crcolour('f', $freq[$row['ffff_id']]);
235
        //father father mother
236
        $d['ffmf']['name'] = stripslashes($row['ffmf_naam']);
237
        $d['ffmf']['id']   = $row['ffmf_id'];
238
        $d['ffmf']['col']  = crcolour('f', $freq[$row['ffmf_id']]);
239
        //father mother father
240
        $d['fmff']['name'] = stripslashes($row['fmff_naam']);
241
        $d['fmff']['id']   = $row['fmff_id'];
242
        $d['fmff']['col']  = crcolour('f', $freq[$row['fmff_id']]);
243
        //father mother mother
244
        $d['fmmf']['name'] = stripslashes($row['fmmf_naam']);
245
        $d['fmmf']['id']   = $row['fmmf_id'];
246
        $d['fmmf']['col']  = crcolour('f', $freq[$row['fmmf_id']]);
247
        //mother father father
248
        $d['mfff']['name'] = stripslashes($row['mfff_naam']);
249
        $d['mfff']['id']   = $row['mfff_id'];
250
        $d['mfff']['col']  = crcolour('f', $freq[$row['mfff_id']]);
251
        //mother father mother
252
        $d['mfmf']['name'] = stripslashes($row['mfmf_naam']);
253
        $d['mfmf']['id']   = $row['mfmf_id'];
254
        $d['mfmf']['col']  = crcolour('f', $freq[$row['mfmf_id']]);
255
        //mother mother father
256
        $d['mmff']['name'] = stripslashes($row['mmff_naam']);
257
        $d['mmff']['id']   = $row['mmff_id'];
258
        $d['mmff']['col']  = crcolour('f', $freq[$row['mmff_id']]);
259
        //mother mother mother
260
        $d['mmmf']['name'] = stripslashes($row['mmmf_naam']);
261
        $d['mmmf']['id']   = $row['mmmf_id'];
262
        $d['mmmf']['col']  = crcolour('f', $freq[$row['mmmf_id']]);
263
        //great-great-grandparents (mothers)
264
        //father father father
265
        $d['fffm']['name'] = stripslashes($row['fffm_naam']);
266
        $d['fffm']['id']   = $row['fffm_id'];
267
        $d['fffm']['col']  = crcolour('m', $freq[$row['fffm_id']]);
268
        //father father mother
269
        $d['ffmm']['name'] = stripslashes($row['ffmm_naam']);
270
        $d['ffmm']['id']   = $row['ffmm_id'];
271
        $d['ffmm']['col']  = crcolour('m', $freq[$row['ffmm_id']]);
272
        //father mother father
273
        $d['fmfm']['name'] = stripslashes($row['fmfm_naam']);
274
        $d['fmfm']['id']   = $row['fmfm_id'];
275
        $d['fmfm']['col']  = crcolour('m', $freq[$row['fmfm_id']]);
276
        //father mother mother
277
        $d['fmmm']['name'] = stripslashes($row['fmmm_naam']);
278
        $d['fmmm']['id']   = $row['fmmm_id'];
279
        $d['fmmm']['col']  = crcolour('m', $freq[$row['fmmm_id']]);
280
        //mother father father
281
        $d['mffm']['name'] = stripslashes($row['mffm_naam']);
282
        $d['mffm']['id']   = $row['mffm_id'];
283
        $d['mffm']['col']  = crcolour('m', $freq[$row['mffm_id']]);
284
        //mother father mother
285
        $d['mfmm']['name'] = stripslashes($row['mfmm_naam']);
286
        $d['mfmm']['id']   = $row['mfmm_id'];
287
        $d['mfmm']['col']  = crcolour('m', $freq[$row['mfmm_id']]);
288
        //mother mother father
289
        $d['mmfm']['name'] = stripslashes($row['mmfm_naam']);
290
        $d['mmfm']['id']   = $row['mmfm_id'];
291
        $d['mmfm']['col']  = crcolour('m', $freq[$row['mmfm_id']]);
292
        //mother mother mother
293
        $d['mmmm']['name'] = stripslashes($row['mmmm_naam']);
294
        $d['mmmm']['id']   = $row['mmmm_id'];
295
        $d['mmmm']['col']  = crcolour('m', $freq[$row['mmmm_id']]);
296
    }
297
298
    //add data to smarty template
299
    $GLOBALS['xoopsTpl']->assign('xoops_pagetitle', $d['d']['name'] . ' -- mega pedigree');
300
    //assign dog(s)
301
    $GLOBALS['xoopsTpl']->assign('d', $d);
302
    $GLOBALS['xoopsTpl']->assign('male', '<img src="assets/images/male.gif">');
303
    $GLOBALS['xoopsTpl']->assign('female', '<img src="assets/images/female.gif">');
304
    //assign extra display options
305
    $GLOBALS['xoopsTpl']->assign('unknown', 'Unknown');
306
    $GLOBALS['xoopsTpl']->assign('f2', strtr(_MA_PEDIGREE_MPED_F2, ['[animalType]' => $moduleConfig['animalType']]));
307
    $GLOBALS['xoopsTpl']->assign('f3', strtr(_MA_PEDIGREE_MPED_F3, ['[animalType]' => $moduleConfig['animalType']]));
308
    $GLOBALS['xoopsTpl']->assign('f4', strtr(_MA_PEDIGREE_MPED_F4, ['[animalType]' => $moduleConfig['animalType']]));
309
    $GLOBALS['xoopsTpl']->assign('m2', strtr(_MA_PEDIGREE_MPED_M2, ['[animalType]' => $moduleConfig['animalType']]));
310
    $GLOBALS['xoopsTpl']->assign('m3', strtr(_MA_PEDIGREE_MPED_M3, ['[animalType]' => $moduleConfig['animalType']]));
311
    $GLOBALS['xoopsTpl']->assign('m4', strtr(_MA_PEDIGREE_MPED_M4, ['[animalType]' => $moduleConfig['animalType']]));
312
}
313
314
/**
315
 * @param $sex
316
 * @param $item
317
 *
318
 * @return string
319
 * @todo move this to ./include directory
320
 */
321
function crcolour($sex, $item)
322
{
323
    if ('1' == $item) {
324
        $col = 'transparant';
325
    } elseif ('2' == $item && 'f' === $sex) {
326
        $col = '#C8C8FF';
327
    } elseif (3 == $item && 'f' === $sex) {
328
        $col = '#6464FF';
329
    } elseif ('4' == $item && 'f' === $sex) {
330
        $col = '#0000FF';
331
    } elseif ('2' == $item && 'm' === $sex) {
332
        $col = '#FFC8C8';
333
    } elseif ('3' == $item && 'm' === $sex) {
334
        $col = '#FF6464';
335
    } elseif ('4' == $item && 'm' === $sex) {
336
        $col = '#FF0000';
337
    } else {
338
        $col = 'transparant';
339
    }
340
341
    return $col;
342
}
343
344
/**
345
 * @param     $freq
346
 * @param     $item
347
 * @param int $inc
348
 *
349
 * @return bool
350
 */
351
function count_item(&$freq, $item, $inc = 1)
352
{
353
    if (!is_array($freq)) {
354
        $freq = [];
355
    }
356
    $freq[$item] = (isset($freq[$item]) ? ($freq[$item] += $inc) : $inc);
357
358
    return true;
359
}
360