Passed
Push — master ( 005d4f...8b5e26 )
by Michael
06:49
created

save()   B

Complexity

Conditions 9
Paths 32

Size

Total Lines 40
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 29
c 2
b 0
f 0
nc 32
nop 0
dl 0
loc 40
rs 8.0555
1
<?php
2
// -------------------------------------------------------------------------
3
4
use Xmf\Request;
5
use XoopsModules\Pedigree;
6
7
//require_once  dirname(dirname(__DIR__)) . '/mainfile.php';
8
require_once __DIR__ . '/header.php';
9
$moduleDirName = basename(__DIR__);
10
xoops_loadLanguage('main', $moduleDirName);
11
12
//needed for generation of pie charts
13
ob_start();
14
include XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->dirname() . '/include/class_eq_pie.php';
15
16
$GLOBALS['xoopsOption']['template_main'] = 'pedigree_edit.tpl';
17
18
include XOOPS_ROOT_PATH . '/header.php';
19
// Include any common code for this module.
20
require_once XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/include/common.php';
21
22
global $xoopsTpl, $xoopsDB;
23
24
//get module configuration
25
/** @var XoopsModuleHandler $moduleHandler */
26
$moduleHandler = xoops_getHandler('module');
27
$module = $moduleHandler->getByDirname($moduleDirName);
28
$configHandler = xoops_getHandler('config');
29
$moduleConfig = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
0 ignored issues
show
Bug introduced by
The method getConfigsByCat() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
$moduleConfig = $configHandler->/** @scrutinizer ignore-call */ getConfigsByCat(0, $module->getVar('mid'));
Loading history...
30
31
if (isset($_GET['f'])) {
32
    if ('save' === $_GET['f']) {
33
        save();
34
    }
35
} else {
36
    edit();
37
}
38
39
function save()
40
{
41
    global $xoopsDB, $moduleConfig;
42
    $a = (!isset($_POST['id']) ? $a = '' : $a = $_POST['id']);
0 ignored issues
show
Unused Code introduced by
The assignment to $a is dead and can be removed.
Loading history...
43
    $animal = new Pedigree\Animal($a);
0 ignored issues
show
Bug introduced by
It seems like $a can also be of type string; however, parameter $id of XoopsModules\Pedigree\Animal::__construct() does only seem to accept integer|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
    $animal = new Pedigree\Animal(/** @scrutinizer ignore-type */ $a);
Loading history...
44
    $fields = $animal->getNumOfFields();
0 ignored issues
show
Bug introduced by
The method getNumOfFields() does not exist on XoopsModules\Pedigree\Animal. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
    /** @scrutinizer ignore-call */ 
45
    $fields = $animal->getNumOfFields();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
    foreach ($fields as $i => $iValue) {
46
        $userField = new Pedigree\Field($fields[$i], $animal->getConfig());
47
        if ($userField->isActive()) {
48
            $currentfield = 'user' . $iValue;
49
            $pictureField = $_FILES[$currentfield]['name'];
50
            if (empty($pictureField) || '' == $pictureField) {
51
                $newvalue = $_POST['user' . $iValue];
52
            } else {
53
                $newvalue = Pedigree\Utility::uploadPicture(0);
54
            }
55
            $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' SET user' . $iValue . "='" . $GLOBALS['xoopsDB']->escape($newvalue) . "' WHERE id='" . $a . "'";
56
            $GLOBALS['xoopsDB']->query($sql);
57
        }
58
    }
59
    //    $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " SET naam = '" . $_POST['naam'] . "', roft = '" . $_POST['roft'] . "' WHERE id='" . $a . "'";
60
    $NAAM = Request::getString('naam', '', 'post');
61
    $roft = Request::getString('roft', '', 'post');
62
    $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " SET naam = '" . $GLOBALS['xoopsDB']->escape($NAAM) . "', roft = '" . $GLOBALS['xoopsDB']->escape($roft) . "' WHERE id='" . $a . "'";
63
    $GLOBALS['xoopsDB']->query($sql);
64
    $pictureField = $_FILES['photo']['name'];
65
    if (empty($pictureField) || '' == $pictureField) {
66
        //llalalala
67
    } else {
68
        $foto = Pedigree\Utility::uploadPicture(0);
69
        //      $sql  = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " SET foto='" . $foto . "' WHERE id='" . $a . "'";
70
        $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " SET foto='" . $GLOBALS['xoopsDB']->escape($foto) . "' WHERE id='" . $a . "'";
71
    }
72
    $GLOBALS['xoopsDB']->query($sql);
73
    if ('1' == $moduleConfig['ownerbreeder']) {
74
        //      $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " SET id_owner = '" . $_POST['id_owner'] . "', id_breeder = '" . $_POST['id_breeder'] . "' WHERE id='" . $a . "'";
75
        $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " SET id_owner = '" . Request::getInt('id_owner', 0, 'post') . "', id_breeder = '" . Request::getInt('id_breeder', 0, 'post') . "' WHERE id='" . $a . "'";
76
        $GLOBALS['xoopsDB']->query($sql);
77
    }
78
    redirect_header('dog.php?id=' . $a, 2, 'Your changes have been saved');
79
}
80
81
/**
82
 * @param int $id
83
 */
84
function edit($id = 0)
85
{
86
    global $xoopsTpl, $xoopsDB, $moduleConfig;
87
    if (isset($_GET['id'])) {
88
        $id = $_GET['id'];
89
    }
90
    include XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
91
92
    $sql = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' WHERE id=' . $id;
93
    $result = $GLOBALS['xoopsDB']->query($sql);
94
    while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
95
        $form = new \XoopsThemeForm('Edit ' . $row['naam'], 'dogname', 'edit.php?f=save', 'post', true);
96
        $form->addElement(new \XoopsFormHiddenToken($name = 'XOOPS_TOKEN_REQUEST', $timeout = 360));
97
        $form->addElement(new \XoopsFormHidden('id', $id));
98
        //name
99
        $naam = htmlentities(stripslashes($row['naam']), ENT_QUOTES);
100
        $form->addElement(new \XoopsFormText('<b>' . _MA_PEDIGREE_FLD_NAME . '</b>', 'naam', $size = 50, $maxsize = 255, $value = $naam));
101
        //gender
102
        $roft = $row['roft'];
103
        $gender_radio = new \XoopsFormRadio('<b>' . _MA_PEDIGREE_FLD_GEND . '</b>', 'roft', $value = $roft);
104
        $gender_radio->addOptionArray([
105
                                          '0' => strtr(_MA_PEDIGREE_FLD_MALE, ['[male]' => $moduleConfig['male']]),
106
                                          '1' => strtr(_MA_PEDIGREE_FLD_FEMA, ['[female]' => $moduleConfig['female']]),
107
                                      ]);
108
        $form->addElement($gender_radio);
109
        //father
110
        $sql = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE id='" . $row['father'] . "'";
111
        $resfather = $GLOBALS['xoopsDB']->query($sql);
112
        $numfields = $GLOBALS['xoopsDB']->getRowsNum($resfather);
113
        if ('0' == !$numfields) {
114
            while (false !== ($rowfetch = $GLOBALS['xoopsDB']->fetchArray($resfather))) {
115
                $form->addElement(new \XoopsFormLabel('<b>' . strtr(_MA_PEDIGREE_FLD_FATH, ['[father]' => $moduleConfig['father']]) . '</b>', '<img src="assets/images/male.gif"><a href="seldog.php?curval=' . $row['id'] . '&gend=0&letter=a">' . $rowfetch['naam'] . '</a>'));
116
            }
117
        } else {
118
            $form->addElement(new \XoopsFormLabel('<b>' . strtr(_MA_PEDIGREE_FLD_FATH, ['[father]' => $moduleConfig['father']]) . '</b>', '<img src="assets/images/male.gif"><a href="seldog.php?curval=' . $row['id'] . '&gend=0&letter=a">Unknown</a>'));
119
        }
120
        //mother
121
        $sql = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE id='" . $row['mother'] . "'";
122
        $resmother = $GLOBALS['xoopsDB']->query($sql);
123
        $numfields = $GLOBALS['xoopsDB']->getRowsNum($resmother);
124
        if ('0' == !$numfields) {
125
            while (false !== ($rowfetch = $GLOBALS['xoopsDB']->fetchArray($resmother))) {
126
                $form->addElement(new \XoopsFormLabel('<b>' . strtr(_MA_PEDIGREE_FLD_MOTH, ['[mother]' => $moduleConfig['mother']]) . '</b>', '<img src="assets/images/female.gif"><a href="seldog.php?curval=' . $row['id'] . '&gend=1&letter=a">' . $rowfetch['naam'] . '</a>'));
127
            }
128
        } else {
129
            $form->addElement(new \XoopsFormLabel('<b>' . strtr(_MA_PEDIGREE_FLD_MOTH, ['[mother]' => $moduleConfig['mother']]) . '</b>', '<img src="assets/images/female.gif"><a href="seldog.php?curval=' . $row['id'] . '&gend=1&letter=a">Unknown</a>'));
130
        }
131
        //owner/breeder
132
        if ('1' == $moduleConfig['ownerbreeder']) {
133
            $owner_select = new \XoopsFormSelect('<b>' . _MA_PEDIGREE_FLD_OWNE . '</b>', $name = 'id_owner', $value = $row['id_owner'], $size = 1, $multiple = false);
134
            $queryeig = 'SELECT id, lastname, firstname FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_owner') . ' ORDER BY "lastname"';
135
            $reseig = $GLOBALS['xoopsDB']->query($queryeig);
136
            $owner_select->addOption(0, $name = _MA_PEDIGREE_UNKNOWN);
137
            while (false !== ($roweig = $GLOBALS['xoopsDB']->fetchArray($reseig))) {
138
                $owner_select->addOption($roweig['id'], $name = $roweig['lastname'] . ', ' . $roweig['firstname']);
139
            }
140
            $form->addElement($owner_select);
141
            //breeder
142
            $breeder_select = new \XoopsFormSelect('<b>' . _MA_PEDIGREE_FLD_BREE . '</b>', $name = 'id_breeder', $value = $row['id_breeder'], $size = 1, $multiple = false);
143
            $queryfok = 'SELECT id, lastname, firstname FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_owner') . ' ORDER BY "lastname"';
144
            $resfok = $GLOBALS['xoopsDB']->query($queryfok);
145
            $breeder_select->addOption(0, $name = _MA_PEDIGREE_UNKNOWN);
146
            while (false !== ($rowfok = $GLOBALS['xoopsDB']->fetchArray($resfok))) {
147
                $breeder_select->addOption($rowfok['id'], $name = $rowfok['lastname'] . ', ' . $rowfok['firstname']);
148
            }
149
            $form->addElement($breeder_select);
150
        }
151
        //picture
152
        if ('' != $row['foto']) {
153
            $picture = '<img src=' . PEDIGREE_UPLOAD_URL . '/images/thumbnails/' . $row['foto'] . '_400.jpeg>';
0 ignored issues
show
Bug introduced by
The constant PEDIGREE_UPLOAD_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
154
            $form->addElement(new \XoopsFormLabel('<b>Picture</b>', $picture));
155
        } else {
156
            $picture = '';
157
        }
158
        $form->setExtra("enctype='multipart/form-data'");
159
        $img_box = new \XoopsFormFile('<b>Image</b>', 'photo', 1024000);
160
        $img_box->setExtra("size ='50'");
161
        $form->addElement($img_box);
162
        //userfields
163
        //create animal object
164
        $animal = new Pedigree\Animal($id);
165
        //test to find out how many user fields there are..
166
        $fields = $animal->getNumOfFields();
167
        for ($i = 0, $iMax = count($fields); $i < $iMax; ++$i) {
168
            $userField = new Pedigree\Field($fields[$i], $animal->getConfig());
169
            if ($userField->isActive()) {
170
                $fieldType = $userField->getSetting('FieldType');
171
                $fieldObject = new $fieldType($userField, $animal);
172
                $edditable[$i] = $fieldObject->editField();
173
                $form->addElement($edditable[$i]);
174
            }
175
        }
176
    }
177
    $form->addElement(new \XoopsFormButton('', 'button_id', _MA_PEDIGREE_BUT_SUB, 'submit'));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $form does not seem to be defined for all execution paths leading up to this point.
Loading history...
178
    $xoopsTpl->assign('form', $form->render());
179
}
180
181
//comments and footer
182
include XOOPS_ROOT_PATH . '/footer.php';
183