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