Passed
Pull Request — master (#6)
by
unknown
04:13
created

checkName()   C

Complexity

Conditions 10
Paths 8

Size

Total Lines 109
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 69
nc 8
nop 0
dl 0
loc 109
rs 6.8096
c 1
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
 You may not change or alter any portion of this comment or credits of
4
 supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit
6
 authors.
7
8
 This program is distributed in the hope that it will be useful, but
9
 WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
/**
13
 * Module: Pedigree
14
 *
15
 * @package   XoopsModules\Pedigree
16
 * @author    XOOPS Module Development Team
17
 * @copyright Copyright (c) 2001-2019 {@link https://xoops.org XOOPS Project}
18
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GNU Public License
19
 */
20
21
use Xmf\Request;
22
use XoopsModules\Pedigree;
23
24
//require_once  dirname(dirname(__DIR__)) . '/mainfile.php';
25
require_once __DIR__ . '/header.php';
26
/** @var \XoopsModules\Pedigree\Helper $helper */
27
$helper->loadLanguage('main');
28
29
// Include any common code for this module.
30
require_once $helper->path('include/common.php');
31
32
$GLOBALS['xoopsOption']['template_main'] = 'pedigree_adddog.tpl';
33
include XOOPS_ROOT_PATH . '/header.php';
34
$GLOBALS['xoopsTpl']->assign('page_title', _MA_PEDIGREE_UPDATE);
35
36
//check for access
37
$xoopsModule = XoopsModule::getByDirname($moduleDirName);
38
if (empty($GLOBALS['xoopsUser']) || !$GLOBALS['xoopsUser'] instanceof \XoopsUser || $GLOBALS['xoopsUser']->isGuest()) {
39
    $helper->redirect('', 3, _NOPERM . '<br>' . _MA_PEDIGREE_REGIST);
40
}
41
42
//create function variable from url
43
//if (isset($_GET['f'])) {
44
//    $f = $_GET['f'];
45
//} else {
46
//    $f = '';
47
//    addDog();
48
//}
49
50
$f      = Request::getString('f', '', 'GET');
51
$random = '';
52
53
switch ($f) {
54
    case 'checkName':
55
        $name        = Request::getString('naam', '', 'POST');
56
        $queryString = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE naam LIKE'%" . $GLOBALS['xoopsDB']->escape($name) . "%' ORDER BY naam";
57
        $result      = $GLOBALS['xoopsDB']->query($queryString);
58
        $numResults  = $GLOBALS['xoopsDB']->getRowsNum($result);
59
        if ($numResults >= 1 && !isset($_GET['r'])) {
60
            //create form
61
            include XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
62
            $form = new \XoopsThemeForm(strtr(_MA_PEDIGREE_ADD_DOG, ['[animalType]' => $helper->getConfig['animalType']]), 'dogname', 'add_dog.php?f=checkName&r=1', 'post');
0 ignored issues
show
Bug introduced by
The property getConfig does not seem to exist on XoopsModules\Pedigree\Helper.
Loading history...
63
            $form->addElement(new \XoopsFormHiddenToken($name = 'XOOPS_TOKEN_REQUEST', $timeout = 360));
64
            $form->addElement(new \XoopsFormHidden('naam', $name));
65
            $form->addElement(new \XoopsFormHidden('user', $GLOBALS['xoopsUser']->getVar('uid')));
66
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchBoth($result))) {
67
                $form->addElement(new \XoopsFormLabel('<b>' . _MA_PEDIGREE_FLD_NAME . '</b>', '<a href="' . $helper->url('dog.php?id=' . $row['id']) . '">' . stripslashes($row['naam']) . '</a>'));
68
            }
69
            $form->addElement(new \XoopsFormLabel(_MA_PEDIGREE_EXPLAIN, strtr(_MA_PEDIGREE_ADD_KNOWN, ['[animalTypes]' => $helper->getConfig['animalTypes']])));
70
            //submit button
71
            $form->addElement(new \XoopsFormButton('', 'button_id', strtr(_MA_PEDIGREE_ADD_KNOWNOK, ['[animalType]' => $helper->getConfig['animalType']]), 'submit'));
72
            //add data (form) to smarty template
73
            $GLOBALS['xoopsTpl']->assign('form', $form->render());
74
        } else {
75
            //create form
76
            include XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
77
            $form = new \XoopsThemeForm(strtr(_MA_PEDIGREE_ADD_DOG, ['[animalType]' => $helper->getConfig['animalType']]), 'dogname', 'add_dog.php?f=sire', 'post');
78
            //added to handle upload
79
            $form->setExtra("enctype='multipart/form-data'");
80
            $form->addElement(new \XoopsFormHiddenToken($name = 'XOOPS_TOKEN_REQUEST', $timeout = 360));
81
            //create random value
82
            $random = (mt_rand() % 10000);
83
            $form->addElement(new \XoopsFormHidden('random', $random));
84
            $form->addElement(new \XoopsFormHidden('naam', htmlspecialchars($name, ENT_QUOTES)));
85
            //find userid from previous form
86
            $form->addElement(new \XoopsFormHidden('user', $GLOBALS['xoopsUser']->getVar('uid')));
87
88
            //name
89
            $form->addElement(new \XoopsFormLabel('<b>' . _MA_PEDIGREE_FLD_NAME . '</b>', stripslashes($name)));
90
            //gender
91
            $gender_radio = new \XoopsFormRadio('<b>' . _MA_PEDIGREE_FLD_GEND . '</b>', 'roft', $value = '0');
92
            $gender_radio->addOptionArray([
93
                '0' => strtr(_MA_PEDIGREE_FLD_MALE, ['[male]' => $helper->getConfig['male']]),
94
                '1' => strtr(_MA_PEDIGREE_FLD_FEMA, ['[female]' => $helper->getConfig['female']])
95
            ]);
96
            $form->addElement($gender_radio);
97
            if ('1' == $helper->getConfig['ownerbreeder']) {
98
                //breeder
99
                $breeder_select = new \XoopsFormSelect('<b>' . _MA_PEDIGREE_FLD_BREE . '</b>', $name = 'id_breeder', $value = '0', $size = 1, $multiple = false);
100
                $queryfok       = 'SELECT id, lastname, firstname FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_owner') . ' ORDER BY lastname';
101
                $resfok         = $GLOBALS['xoopsDB']->query($queryfok);
102
                $breeder_select->addOption('0', $name = _MA_PEDIGREE_UNKNOWN);
103
                while (false !== ($rowfok = $GLOBALS['xoopsDB']->fetchArray($resfok))) {
104
                    $breeder_select->addOption($rowfok['id'], $name = $rowfok['lastname'] . ', ' . $rowfok['firstname']);
105
                }
106
                $form->addElement($breeder_select);
107
                $form->addElement(new \XoopsFormLabel(_MA_PEDIGREE_EXPLAIN, strtr(_MA_PEDIGREE_FLD_BREE_EX, ['[animalType]' => $helper->getConfig['animalType']])));
108
109
                //owner
110
                $owner_select = new \XoopsFormSelect('<b>' . _MA_PEDIGREE_FLD_OWNE . '</b>', $name = 'id_owner', $value = '0', $size = 1, $multiple = false);
111
                $queryfok     = 'SELECT id, lastname, firstname FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_owner') . ' ORDER BY lastname';
112
                $resfok       = $GLOBALS['xoopsDB']->query($queryfok);
113
                $owner_select->addOption('0', $name = _MA_PEDIGREE_UNKNOWN);
114
                while (false !== ($rowfok = $GLOBALS['xoopsDB']->fetchArray($resfok))) {
115
                    $owner_select->addOption($rowfok['id'], $name = $rowfok['lastname'] . ', ' . $rowfok['firstname']);
116
                }
117
                $form->addElement($owner_select);
118
                $form->addElement(new \XoopsFormLabel(_MA_PEDIGREE_EXPLAIN, strtr(_MA_PEDIGREE_FLD_OWNE_EX, ['[animalType]' => $helper->getConfig['animalType']])));
119
            }
120
            //picture
121
            $max_imgsize = 1024000;
122
            $img_box     = new \XoopsFormFile('Image', 'photo', $max_imgsize);
123
            $img_box->setExtra("size ='50'");
124
            $form->addElement($img_box);
125
126
            //create animal object
127
            $animal = new Pedigree\Animal();
128
            //test to find out how many user fields there are..
129
            $fields = $animal->getNumOfFields();
130
131
            foreach ($fields as $i => $iValue) {
132
                $userField   = new Pedigree\Field($fields[$i], $animal->getConfig());
133
                $fieldType   = $userField->getSetting('FieldType');
134
                $fieldObject = new $fieldType($userField, $animal);
135
                if ($userField->isActive() && !$userField->isLocked()) {
136
                    $newEntry = $fieldObject->newField();
137
                    $form->addElement($newEntry);
138
                }
139
                unset($newEntry);
140
            }
141
142
            //submit button
143
            $form->addElement(new \XoopsFormButton('', 'button_id', strtr(_MA_PEDIGREE_ADD_SIRE, ['[father]' => $helper->getConfig['father']]), 'submit'));
144
145
            //add data (form) to smarty template
146
            $GLOBALS['xoopsTpl']->assign('form', $form->render());
147
        }
148
        break;
149
    case 'sire':
150
        $empty  = []; // an empty array
151
        $user            = Request::getInt('user', null, 'POST');
152
        $random          = Request::getString('random', $random, 'GET');
153
        //@todo is $st suppose to be POST?
154
        $st              = Request::getInt('st', 0, 'GET');
155
        $name            = Request::getString('naam', null, 'POST');
156
        $roft            = Request::getString('roft', null, 'POST');
157
        $id_owner        = Request::getInt('id_owner', null, 'POST');
158
        $id_breeder      = Request::getInt('id_breeder', null, 'POST');
159
        $pictureField    = isset($_FILES['photo']) ? $_FILES['photo']['name'] : null; // $_FILES['photo']['name'];
160
        $foto            = (empty($pictureField)) ? '' : Pedigree\Utility::uploadPicture(0);
161
        $numPictureField = 1;
162
163
        //make the redirect
164
        if (!isset($_GET['r'])) {
165
            if (empty($name)) {
166
                $helper->redirect('add_dog.php', 1, _MA_PEDIGREE_ADD_NAMEPLZ);
167
            }
168
            //create animal object
169
            $animal = new Pedigree\Animal();
170
            $fields = $animal->getNumOfFields();//test to find out how many user fields there are..
171
            sort($fields); //sort by ID not by order
172
            $usersql = '';
173
            foreach ($fields as $i => $iValue) {
174
                $userField   = new Pedigree\Field($fields[$i], $animal->getConfig());
175
                $fieldType   = $userField->getSetting('FieldType');
176
                $fieldObject = new $fieldType($userField, $animal);
177
                if ($userField->isActive()) {
178
                    //check if _FILES variable exists for user picturefield
179
                    $currentfield = 'user' . $iValue;
180
                    $pictureField = $_FILES[$currentfield]['name'];
181
                    if ('Picture' === $fieldType && (!empty($pictureField) || '' != $pictureField)) {
182
                        $userpicture = Pedigree\Utility::uploadPicture($numPictureField);
183
                        $usersql .= ",'" . $userpicture . "'";
184
                        ++$numPictureField;
185
                    } elseif ($userField->isLocked()) {
186
                        //userfield is locked, substitute default value
187
                        $usersql .= ",'" . $userField->defaultvalue . "'";
0 ignored issues
show
Bug introduced by
The property defaultvalue does not seem to exist on XoopsModules\Pedigree\Field.
Loading history...
188
                    } else {
189
                        //echo $fieldType.":".$i.":".$fields[$i]."<br>";
190
                        $usersql .= ",'" . Pedigree\Utility::unHtmlEntities($_POST['user' . $iValue]) . "'";
191
                    }
192
                } else {
193
                    $usersql .= ",''";
194
                }
195
                //echo $fields[$i]."<br>";
196
            }
197
198
            //insert into pedigree_temp
199
            //        $query = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . " VALUES ('" . $random . "','" . Pedigree\Utility::unHtmlEntities($name) . "','" . $id_owner . "','" . $id_breeder . "','" . $user . "','" . $roft . "','','','" . $foto . "', ''" . $usersql . ')';
200
            $query = 'INSERT INTO '
201
                . $GLOBALS['xoopsDB']->prefix('pedigree_temp')
202
                . " VALUES ('"
203
                . $GLOBALS['xoopsDB']->escape($random)
204
                . "','"
205
                . $GLOBALS['xoopsDB']->escape(Pedigree\Utility::unHtmlEntities($name))
206
                . "','"
207
                . $GLOBALS['xoopsDB']->escape($id_owner)
208
                . "','"
209
                . $GLOBALS['xoopsDB']->escape($id_breeder)
210
                . "','"
211
                . $GLOBALS['xoopsDB']->escape($user)
212
                . "','"
213
                . $GLOBALS['xoopsDB']->escape($roft)
214
                . "','0','0','"
215
                . $GLOBALS['xoopsDB']->escape($foto)
216
                . "', ''"
217
                . $usersql
218
                . ')';
219
            //echo $query; die();
220
            $GLOBALS['xoopsDB']->queryF($query);
221
            $helper->redirect('add_dog.php?f=sire&random=' . $random . '&st=' . $st . '&r=1&l=a', 1, strtr(_MA_PEDIGREE_ADD_SIREPLZ, ['[father]' => $helper->getConfig['father']]));
222
        }
223
        //find letter on which to start else set to 'a'
224
        $l = Request::getString('l', 'a', 'GET');
225
        $GLOBALS['xoopsTpl']->assign('sire', '1');
226
227
        //create list of males dog to select from
228
        $perPage = $helper->getConfig['perpage'];
229
        //count total number of dogs
230
        $numDog = 'SELECT count(id) FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE roft='0' AND naam LIKE '" . $l . "%'";
231
        $numRes = $GLOBALS['xoopsDB']->query($numDog);
232
        //total number of dogs the query will find
233
        list($numResults) = $GLOBALS['xoopsDB']->fetchRow($numRes);
234
        //total number of pages
235
        $numPages = floor($numResults / $perPage) + 1;
236
        if (($numPages * $perPage) == ($numResults + $perPage)) {
237
            --$numPages;
238
        }
239
        //find current page
240
        $currentPage = floor($st / $perPage) + 1;
241
        //create alphabet
242
        $pages = '';
243
        for ($i = 65; $i <= 90; ++$i) {
244
            if ($l == chr($i)) {
245
                $pages .= '<b><a href="' . $helper->url('add_dog.php?f=sire&r=1&random=' . $random . '&l=' . chr($i)) . '">' . chr($i) . '</a></b>&nbsp;';
246
            } else {
247
                $pages .= '<a href="' . $helper->url('add_dog.php?f=sire&r=1&random=' . $random . '&l=' . chr($i)) . '">' . chr($i) . '</a>&nbsp;';
248
            }
249
        }
250
        $pages .= '-&nbsp;';
251
        $pages .= '<a href="' . $helper->url('add_dog.php?f=sire&r=1&random=' . $random . '&l=Ã…') . '">Ã…</a>&nbsp;';
252
        $pages .= '<a href="' . $helper->url('add_dog.php?f=sire&r=1&random=' . $random . '&l=Ö') . '">Ö</a>&nbsp;';
253
        //create linebreak
254
        $pages .= '<br>';
255
        //create previous button
256
        if ($numPages > 1) {
257
            if ($currentPage > 1) {
258
                $pages .= '<a href="' . $helper->url('add_dog.php?f=sire&r=1&l=' . $l . '&random=' . $random . '&st=' . ($st - $perPage)) . '">' . _MA_PEDIGREE_PREVIOUS . '</a>&nbsp;&nbsp';
259
            }
260
        }
261
        //create numbers
262
        for ($x = 1; $x < ($numPages + 1); ++$x) {
263
            //create line break after 20 numbers
264
            if (0 == ($x % 20)) {
265
                $pages .= '<br>';
266
            }
267
            if ($x != $currentPage) {
268
                $pages .= '<a href="' . $helper->url('add_dog.php?f=sire&r=1&l=' . $l . '&random=' . $random . '&st=' . ($perPage * ($x - 1))) . '">' . $x . '</a>&nbsp;&nbsp;';
269
            } else {
270
                $pages .= $x . '&nbsp;&nbsp';
271
            }
272
        }
273
        //create next button
274
        if ($numPages > 1) {
275
            if ($currentPage < $numPages) {
276
                $pages .= '<a href="' . $helper->url('add_dog.php?f=sire&r=1&l=' . $l . '&random=' . $random . '&st=' . ($st + $perPage)) . '">' . _MA_PEDIGREE_NEXT . '</a>&nbsp;&nbsp';
277
            }
278
        }
279
280
        //query
281
        $queryString = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE roft = '0' AND naam LIKE '" . $l . "%'ORDER BY naam LIMIT " . $st . ', ' . $perPage;
282
        $result      = $GLOBALS['xoopsDB']->query($queryString);
283
284
        $animal = new Pedigree\Animal();
285
        //test to find out how many user fields there are...
286
        $fields       = $animal->getNumOfFields();
287
        $numofcolumns = 1;
288
        $columns[]    = ['columnname' => 'Name'];
289
        foreach ($fields as $i => $iValue) {
290
            $userField   = new Pedigree\Field($fields[$i], $animal->getConfig());
291
            $fieldType   = $userField->getSetting('FieldType');
292
            $fieldObject = new $fieldType($userField, $animal);
293
            //create empty string
294
            $lookupValues = '';
295
            if ($userField->isActive() && $userField->inList()) {
296
                if ($userField->hasLookup()) {
297
                    $lookupValues = $userField->lookupField($fields[$i]);
298
                    //debug information
299
                    //print_r($lookupValues);
300
                }
301
                $columns[] = [
302
                    'columnname'   => $fieldObject->fieldname,
303
                    'columnnumber' => $userField->getId(),
304
                    'lookupval'    => $lookupValues
305
                ];
306
                ++$numofcolumns;
307
                unset($lookupValues);
308
            }
309
        }
310
311
        for ($i = 1; $i < $numofcolumns; ++$i) {
312
            $empty[] = ['value' => ''];
313
        }
314
        $dogs [] = [
315
            'id'          => '0',
316
            'name'        => '',
317
            'gender'      => '',
318
            'link'        => '<a href="' . $helper->url('add_dog.php?f=dam&random=' . $random . '&selsire=0') . '">' . strtr(_MA_PEDIGREE_ADD_SIREUNKNOWN, ['[father]' => $helper->getConfig['father']]) . '</a>',
319
            'colour'      => '',
320
            'number'      => '',
321
            'usercolumns' => $empty
322
        ];
323
324
        while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
325
            //create picture information
326
            if ('' != $row['foto']) {
327
                $camera = ' <img src="assets/images/camera.png">';
328
            } else {
329
                $camera = '';
330
            }
331
            $name = stripslashes($row['naam']) . $camera;
332
            //empty array
333
            unset($columnvalue);
334
            //fill array
335
            for ($i = 1; $i < $numofcolumns; ++$i) {
336
                $x = $columns[$i]['columnnumber'];
337
                if (is_array($columns[$i]['lookupval'])) {
338
                    foreach ($columns[$i]['lookupval'] as $key => $keyValue) {
339
                        if ($key == $row['user' . $x]) {
340
                            $value = $keyValue['value'];
341
                        }
342
                    }
343
                    //debug information
344
                    ///echo $columns[$i]['columnname']."is an array !";
345
                } //format value - cant use object because of query count
346
                elseif (0 === strncmp($row['user' . $x], 'http://', 7)) { //@todo need to update for https
347
                    $value = '<a href="' . $row['user' . $x] . '">' . $row['user' . $x] . '</a>';
348
                } else {
349
                    $value = $row['user' . $x];
350
                }
351
                $columnvalue[] = ['value' => $value];
352
            }
353
            $dogs[] = [
354
                'id'          => $row['id'],
355
                'name'        => $name,
356
                'gender'      => '<img src="assets/images/male.gif">',
357
                'link'        => '<a href="' . $helper->url('add_dog.php?f=dam&random=' . $random . '&selsire=' . $row['id']) . '">' . $name . '</a>',
358
                'colour'      => '',
359
                'number'      => '',
360
                'usercolumns' => $columnvalue
361
            ];
362
        }
363
364
        //add data to smarty template
365
        //assign dog
366
        $GLOBALS['xoopsTpl']->assign('dogs', $dogs);
367
        $GLOBALS['xoopsTpl']->assign('columns', $columns);
368
        $GLOBALS['xoopsTpl']->assign('numofcolumns', $numofcolumns);
369
        $GLOBALS['xoopsTpl']->assign('tsarray', Pedigree\Utility::sortTable($numofcolumns));
370
        //assign links
371
        $GLOBALS['xoopsTpl']->assign('nummatch', strtr(_MA_PEDIGREE_ADD_SELSIRE, ['[father]' => $helper->getConfig['father']]));
372
        $GLOBALS['xoopsTpl']->assign('pages', $pages);
373
374
        //mb =========== FATHER LETTERS =============================
375
        $roft     = 0;
376
        //    $criteria     = $helper->getHandler('Tree')->getActiveCriteria($roft);
377
        $activeObject = 'Tree';
378
        $name         = 'naam';
379
        $number1      = '1';
380
        $number2      = '0';
381
        //    $link         = "virtual.php?r={$number1}&st={$number2}&l=";
382
        $link = "add_dog.php?f=sire&r=1&random={$random}&l=";
383
384
        //    http://localhost/257belgi/modules/pedigree/virtual.php?f=dam&selsire=35277
385
386
        $link2 = '';
387
388
        $criteria = $helper->getHandler('Tree')->getActiveCriteria($roft);
389
        //    $criteria->setGroupby('UPPER(LEFT(' . $name . ',1))');
390
391
        $fatherArray['letters'] = Pedigree\Utility::lettersChoice($helper, $activeObject, $criteria, $name, $link, $link2);
392
        //$catarray['toolbar']    = pedigree_toolbar();
393
        $GLOBALS['xoopsTpl']->assign('fatherArray', $fatherArray);
394
        break;
395
396
    case 'dam':
397
        $empty = []; // an empty array
398
        if (empty($random)) {
0 ignored issues
show
introduced by
The condition empty($random) is always true.
Loading history...
399
            $random = Request::getInt('random', 0);
400
        }
401
        $st = Request::getInt('st', 0, 'GET');
402
        $l  = Request::getString('l', 'a', 'GET');
403
        //make the redirect
404
        if (!isset($_GET['r'])) {
405
            //insert into pedigree_temp
406
            //        $query = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . ' SET father =' . $_GET['selsire'] . ' WHERE id=' . $random;
407
            //        $GLOBALS['xoopsDB']->queryF($query);
408
            $query = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . ' SET father =' . Request::getInt('selsire', 0, 'GET') . ' WHERE id=' . $random;
409
            $GLOBALS['xoopsDB']->queryF($query);
410
            $helper->redirect('add_dog.php?f=dam&random=' . $random . '&st=' . $st . '&r=1&l=a', 1, strtr(_MA_PEDIGREE_ADD_SIREOK, ['[mother]' => $helper->getConfig['mother']]));
411
        }
412
413
        $GLOBALS['xoopsTpl']->assign('sire', '1');
414
        //create list of males dog to select from
415
        $perPage = $helper->getConfig['perpage'];
416
        //count total number of dogs
417
        $numDog = 'SELECT count(id) FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE roft='1' AND naam LIKE '" . $l . "%'";
418
        $numRes = $GLOBALS['xoopsDB']->query($numDog);
419
        list($numResults) = $GLOBALS['xoopsDB']->fetchRow($numRes);
420
        $numPages = floor($numResults / $perPage) + 1;
421
        if (($numPages * $perPage) == ($numResults + $perPage)) {
422
            --$numPages;
423
        }
424
        $currentPage = floor($st / $perPage) + 1;
425
        //create alphabet
426
        $pages = '';
427
        for ($i = 65; $i <= 90; ++$i) {
428
            if ($l == chr($i)) {
429
                $pages .= '<b><a href="' . $helper->url('add_dog.php?f=dam&r=1&random=' . $random . '&l=' . chr($i)) . '">' . chr($i) . '</a></b>&nbsp;';
430
            } else {
431
                $pages .= '<a href="' . $helper->url('add_dog.php?f=dam&r=1&random=' . $random . '&l=' . chr($i)) . '">' . chr($i) . '</a>&nbsp;';
432
            }
433
        }
434
        $pages .= '-&nbsp;';
435
        $pages .= '<a href="' . $helper->url('add_dog.php?f=dam&r=1&random=' . $random . '&l=Ã…') . '">Ã…</a>&nbsp;';
436
        $pages .= '<a href="' . $helper->url('add_dog.php?f=dam&r=1&random=' . $random . '&l=Ö') . '">Ö</a>&nbsp;';
437
        $pages .= '<br>';
438
        //create previous button
439
        if ($numPages > 1) {
440
            if ($currentPage > 1) {
441
                $pages .= '<a href="' . $helper->url('add_dog.php?f=dam&r=1&l=' . $l . '&random=' . $random . '&st=' . ($st - $perPage)) . '">' . _MA_PEDIGREE_PREVIOUS . '</a>&nbsp;&nbsp';
442
            }
443
        }
444
        //create numbers
445
        for ($x = 1; $x < ($numPages + 1); ++$x) {
446
            //create line break after 20 number
447
            if (0 == ($x % 20)) {
448
                $pages .= '<br>';
449
            }
450
            if ($x != $currentPage) {
451
                $pages .= '<a href="' . $helper->url('add_dog.php?f=dam&r=1&l=' . $l . '&random=' . $random . '&st=' . ($perPage * ($x - 1))) . '">' . $x . '</a>&nbsp;&nbsp;';
452
            } else {
453
                $pages .= $x . '&nbsp;&nbsp';
454
            }
455
        }
456
        //create next button
457
        if ($numPages > 1) {
458
            if ($currentPage < $numPages) {
459
                $pages .= '<a href="' . $helper->url('add_dog.php?f=dam&l=' . $l . '&r=1&random=' . $random . '&st=' . ($st + $perPage)) . '">' . _MA_PEDIGREE_NEXT . '</a>&nbsp;&nbsp;';
460
            }
461
        }
462
463
        //query
464
        $queryString = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE roft = '1' AND naam LIKE '" . $l . "%' ORDER BY naam LIMIT " . $st . ', ' . $perPage;
465
        $result      = $GLOBALS['xoopsDB']->query($queryString);
466
467
        $animal = new Pedigree\Animal();
468
        //test to find out how many user fields there are...
469
        $fields       = $animal->getNumOfFields();
470
        $numofcolumns = 1;
471
        $columns[]    = ['columnname' => 'Name'];
472
        foreach ($fields as $i => $iValue) {
473
            $userField   = new Pedigree\Field($fields[$i], $animal->getConfig());
474
            $fieldType   = $userField->getSetting('FieldType');
475
            $fieldObject = new $fieldType($userField, $animal);
476
            //create empty string
477
            $lookupValues = '';
478
            if ($userField->isActive() && $userField->inList()) {
479
                if ($userField->hasLookup()) {
480
                    $lookupValues = $userField->lookupField($fields[$i]);
481
                    //debug information
482
                    //print_r($lookupValues);
483
                }
484
                $columns[] = [
485
                    'columnname'   => $fieldObject->fieldname,
486
                    'columnnumber' => $userField->getId(),
487
                    'lookupval'    => $lookupValues
488
                ];
489
                ++$numofcolumns;
490
                unset($lookupValues);
491
            }
492
        }
493
494
        for ($i = 1; $i < $numofcolumns; ++$i) {
495
            $empty[] = ['value' => ''];
496
        }
497
        $dogs [] = [
498
            'id'          => '0',
499
            'name'        => '',
500
            'gender'      => '',
501
            'link'        => '<a href="' . $helper->url('add_dog.php?f=check&random=' . $random . '&seldam=0') . '">' . strtr(_MA_PEDIGREE_ADD_DAMUNKNOWN, ['[mother]' => $helper->getConfig['mother']]) . '</a>',
502
            'colour'      => '',
503
            'number'      => '',
504
            'usercolumns' => $empty
505
        ];
506
507
        while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
508
            //create picture information
509
            if ('' != $row['foto']) {
510
                $camera = ' <img src="assets/images/camera.png">';
511
            } else {
512
                $camera = '';
513
            }
514
            $name = stripslashes($row['naam']) . $camera;
515
            //empty array
516
            unset($columnvalue);
517
            //fill array
518
            for ($i = 1; $i < $numofcolumns; ++$i) {
519
                $x = $columns[$i]['columnnumber'];
520
                if (is_array($columns[$i]['lookupval'])) {
521
                    foreach ($columns[$i]['lookupval'] as $key => $keyValue) {
522
                        if ($key == $row['user' . $x]) {
523
                            $value = $keyValue['value'];
524
                        }
525
                    }
526
                    //debug information
527
                    ///echo $columns[$i]['columnname']."is an array !";
528
                } //format value - cant use object because of query count
529
                elseif (0 === strncmp($row['user' . $x], 'http://', 7)) {
530
                    $value = '<a href="' . $row['user' . $x] . '">' . $row['user' . $x] . '</a>';
531
                } else {
532
                    $value = $row['user' . $x];
533
                }
534
                $columnvalue[] = ['value' => $value];
535
            }
536
            $dogs[] = [
537
                'id'          => $row['id'],
538
                'name'        => $name,
539
                'gender'      => '<img src="assets/images/female.gif">',
540
                'link'        => '<a href="' . $helper->url('add_dog.php?f=check&random=' . $random . '&seldam=' . $row['id']) . '">' . $name . '</a>',
541
                'colour'      => '',
542
                'number'      => '',
543
                'usercolumns' => $columnvalue
544
            ];
545
        }
546
547
        //add data to smarty template
548
        //assign dog
549
        $GLOBALS['xoopsTpl']->assign('dogs', $dogs);
550
        $GLOBALS['xoopsTpl']->assign('columns', $columns);
551
        $GLOBALS['xoopsTpl']->assign('numofcolumns', $numofcolumns);
552
        $GLOBALS['xoopsTpl']->assign('tsarray', Pedigree\Utility::sortTable($numofcolumns));
553
        $GLOBALS['xoopsTpl']->assign('nummatch', strtr(_MA_PEDIGREE_ADD_SELDAM, ['[mother]' => $helper->getConfig['mother']]));
554
        $GLOBALS['xoopsTpl']->assign('pages', $pages);
555
556
        //mb ========= MOTHER LETTERS===============================
557
        $roft     = 1;
558
        //    $criteria     = $helper->getHandler('Tree')->getActiveCriteria($roft);
559
        $activeObject = 'Tree';
560
        $name         = 'naam';
561
        $number1      = '1';
562
        $number2      = '0';
563
        $link         = "add_dog.php?f=dam&r=1&random={$random}&l=";
564
        $link2        = '';
565
566
        $criteria = $helper->getHandler('Tree')->getActiveCriteria($roft);
567
        //    $criteria->setGroupby('UPPER(LEFT(' . $name . ',1))');
568
569
        $motherArray['letters'] = Pedigree\Utility::lettersChoice($helper, $activeObject, $criteria, $name, $link, $link2);
570
        //$catarray['toolbar']    = pedigree_toolbar();
571
        $GLOBALS['xoopsTpl']->assign('motherArray', $motherArray);
572
        break;
573
574
    case 'check':
575
        if (empty($random)) {
0 ignored issues
show
introduced by
The condition empty($random) is always true.
Loading history...
576
            $random = Request::getInt('random', null);
577
        }
578
579
        //query
580
        $queryString = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . ' WHERE id = ' . $random;
581
        $result      = $GLOBALS['xoopsDB']->query($queryString);
582
        while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
583
            //create animal object
584
            $animal = new Pedigree\Animal();
585
            $fields = $animal->getNumOfFields(); //test to find out how many user fields there are..
586
            sort($fields);
587
            $usersql = '';
588
            foreach ($fields as $i => $iValue) {
589
                $userField   = new Pedigree\Field($fields[$i], $animal->getConfig());
590
                $fieldType   = $userField->getSetting('FieldType');
591
                $fieldObject = new $fieldType($userField, $animal);
592
                if ($userField->isActive()) {
593
                    $usersql .= ",'" . addslashes($row['user' . $iValue]) . "'";
594
                } else {
595
                    $usersql .= ",'" . $fieldObject->defaultvalue . "'";
596
                }
597
                //echo $fields[$i]."<br>";
598
            }
599
            //insert into pedigree
600
            //$query = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " VALUES ('','" . addslashes($row['naam']) . "','" . $row['id_owner'] . "','" . $row['id_breeder'] . "','" . $row['user'] . "','" . $row['roft'] . "','" . $_GET['seldam'] . "','" . $row['father'] . "','" . addslashes($row['foto']) . "',''" . $usersql . ')';
601
            $sql = 'INSERT INTO '
602
                . $GLOBALS['xoopsDB']->prefix('pedigree_tree')
603
                . " VALUES (0,'"
604
                . $GLOBALS['xoopsDB']->escape($row['naam'])
605
                . "','"
606
                . $GLOBALS['xoopsDB']->escape($row['id_owner'])
607
                . "','"
608
                . $GLOBALS['xoopsDB']->escape($row['id_breeder'])
609
                . "','"
610
                . $GLOBALS['xoopsDB']->escape($row['user'])
611
                . "','"
612
                . $GLOBALS['xoopsDB']->escape($row['roft'])
613
                . "','"
614
                . $GLOBALS['xoopsDB']->escape($_GET['seldam'])
615
                . "','"
616
                . $GLOBALS['xoopsDB']->escape($row['father'])
617
                . "','"
618
                . $GLOBALS['xoopsDB']->escape($row['foto'])
619
                . "',''"
620
                . $usersql
621
                . ')';
622
            $GLOBALS['xoopsDB']->queryF($sql);
623
            //echo $query; die();
624
            break;
625
        }
626
        $sqlQuery = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . " WHERE id='" . $random . "'";
627
        $GLOBALS['xoopsDB']->queryF($sqlQuery);
628
        $helper->redirect('latest.php', 1, strtr(_MA_PEDIGREE_ADD_OK, ['[animalType]' => $helper->getConfig['animalType']]));
629
        break;
630
631
    default: //add a dog
632
        include XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
633
        //create form
634
        $form = new \XoopsThemeForm(strtr(_MA_PEDIGREE_ADD_DOG, ['[animalType]' => $helper->getConfig['animalType']]), 'dogname', 'add_dog.php?f=checkName', 'post');
635
        $form->addElement(new \XoopsFormHiddenToken($name = 'XOOPS_TOKEN_REQUEST', $timeout = 360));
636
        //create random value
637
        $random = (mt_rand() % 10000);
638
        $form->addElement(new \XoopsFormHidden('random', $random));
639
        //find userid
640
        $form->addElement(new \XoopsFormHidden('user', $GLOBALS['xoopsUser']->getVar('uid')));
641
642
        //name
643
        $form->addElement(new \XoopsFormText('<b>' . _MA_PEDIGREE_FLD_NAME . '</b>', 'naam', $size = 50, $maxsize = 255, $value = ''), true); // name required
644
        $string = strtr(_MA_PEDIGREE_FLD_NAME_EX, ['[animalType]' => $helper->getConfig['animalType']]);
645
        $form->addElement(new \XoopsFormLabel(_MA_PEDIGREE_EXPLAIN, $string));
646
647
        //submit button
648
        $form->addElement(new \XoopsFormButton('', 'button_id', strtr(_MA_PEDIGREE_ADD_DATA, ['[animalType]' => $helper->getConfig['animalType']]), 'submit'));
649
650
        //add data (form) to smarty template
651
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
652
        break;
653
}
654
655
//footer
656
include XOOPS_ROOT_PATH . '/footer.php';
657