Completed
Pull Request — master (#1)
by
unknown
03:00
created

edit.php ➔ save()   D

Complexity

Conditions 9
Paths 32

Size

Total Lines 41
Code Lines 30

Duplication

Lines 5
Ratio 12.2 %

Importance

Changes 0
Metric Value
cc 9
eloc 30
nc 32
nop 0
dl 5
loc 41
rs 4.909
c 0
b 0
f 0
1
<?php
2
// -------------------------------------------------------------------------
3
4
require_once dirname(dirname(__DIR__)) . '/mainfile.php';
5
$moduleDirName = basename(__DIR__);
6
xoops_loadLanguage('main', $moduleDirName);
7
8
//needed for generation of pie charts
9
ob_start();
10
include XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/include/class_eq_pie.php';
11
require_once XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/class/field.php';
12
13
$xoopsOption['template_main'] = 'pedigree_edit.tpl';
14
15
include XOOPS_ROOT_PATH . '/header.php';
16
// Include any common code for this module.
17
require_once(XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/include/common.php');
18
19
global $xoopsTpl, $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
20
21
//get module configuration
22
$moduleHandler = xoops_getHandler('module');
23
$module        = $moduleHandler->getByDirname('pedigree');
24
$configHandler = xoops_getHandler('config');
25
$moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
26
27
if ('save' === $_GET['f']) {
28
    global $xoopsDB, $pedigree;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
29
    $a      = XoopsRequest::getInt('Id', 0, 'POST');
30
//    $a      = (!isset($_POST['id']) ? $a = '' : $a = $_POST['id']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
31
    $animal = new PedigreeAnimal($a);
32
    $fields = $animal->getNumOfFields();
33
    $fieldsCount = count($fields);
34
    for ($i = 0; $i < $fieldsCount; ++$i) {
35
        $userField = new Field($fields[$i], $animal->getConfig());
36
        if ($userField->isActive()) {
37
            $currentfield = 'user' . $fields[$i];
38
            $picturefield = $_FILES[$currentfield]['name'];
39 View Code Duplication
            if (empty($picturefield)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
                $newvalue = $_POST['user' . $fields[$i]];
41
            } else {
42
                $newvalue = PedigreeUtilities::uploadPicture(0);
43
            }
44
            $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' SET user' . $fields[$i] . "='" . $GLOBALS['xoopsDB']->escape($newvalue) . "' WHERE Id='" . $a . "'";
45
            $GLOBALS['xoopsDB']->query($sql);
46
        }
47
    }
48
    //    $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " SET NAAM = '" . $_POST['NAAM'] . "', roft = '" . $_POST['roft'] . "' WHERE ID='" . $a . "'";
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
49
    $NAAM = XoopsRequest::getString('NAAM', '', 'post');
50
    $roft = XoopsRequest::getString('roft', '', 'post');
51
    $sql  = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " SET NAAM = '" . $GLOBALS['xoopsDB']->escape($NAAM) . "', roft = '" . $GLOBALS['xoopsDB']->escape($roft) . "' WHERE Id='" . $a . "'";
52
    $GLOBALS['xoopsDB']->query($sql);
53
    $picturefield = $_FILES['photo']['name'];
54
    if (empty($picturefield) || $picturefield == '') {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
55
        //llalalala
56
    } else {
57
        $foto = PedigreeUtilities::uploadPicture(0);
58
        //      $sql  = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " SET foto='" . $foto . "' WHERE ID='" . $a . "'";
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59
        $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " SET foto='" . $GLOBALS['xoopsDB']->escape($foto) . "' WHERE Id='" . $a . "'";
60
    }
61
    $GLOBALS['xoopsDB']->query($sql);
62
    if ('1' == $pedigree->getConfig('ownerbreeder')) {
63
        //      $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " SET id_owner = '" . $_POST['id_owner'] . "', id_breeder = '" . $_POST['id_breeder'] . "' WHERE ID='" . $a . "'";
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
64
        $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " SET id_owner = '" . XoopsRequest::getInt('id_owner', 0, 'POST') . "', id_breeder = '" . XoopsRequest::getInt('id_breeder', 0, 'POST') . "' WHERE Id='" . $a . "'";
65
        $GLOBALS['xoopsDB']->query($sql);
66
    }
67
    redirect_header('dog.php?Id=' . $a, 2, 'Your changes have been saved');
68
69
} else {
70
71
    $id = XoopsRequest::getInt('Id', 0, 'GET');
72
    include XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
73
74
    $sql    = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' WHERE Id=' . $id;
75
    $result = $GLOBALS['xoopsDB']->query($sql);
76
    while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
77
        $form = new XoopsThemeForm('Edit ' . $row['NAAM'], 'dogname', 'edit.php?f=save', 'POST');
78
        $form->addElement(new XoopsFormHiddenToken($name = 'XOOPS_TOKEN_REQUEST', $timeout = 360));
79
        $form->addElement(new XoopsFormHidden('id', $id));
80
        //name
81
        $naam = htmlentities(stripslashes($row['NAAM']), ENT_QUOTES);
82
        $form->addElement(new XoopsFormText('<b>' . _MA_PEDIGREE_FLD_NAME . '</b>', 'NAAM', $size = 50, $maxsize = 255, $value = $naam));
83
        //gender
84
        $roft         = $row['roft'];
85
        $gender_radio = new XoopsFormRadio('<b>' . _MA_PEDIGREE_FLD_GEND . '</b>', 'roft', $value = $roft);
86
        $gender_radio->addOptionArray(array('0' => strtr(_MA_PEDIGREE_FLD_MALE, array('[male]' => $pedigree->getConfig('male'))), '1' => strtr(_MA_PEDIGREE_FLD_FEMA, array('[female]' => $pedigree->getConfig('female')))));
87
        $form->addElement($gender_radio);
88
        //father
89
        $sql       = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE Id='" . $row['father'] . "'";
90
        $resfather = $GLOBALS['xoopsDB']->query($sql);
91
        $numfields = mysqli_num_rows($resfather);
92 View Code Duplication
        if (!$numfields == '0') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
            while (false !== ($rowfetch = $GLOBALS['xoopsDB']->fetchArray($resfather))) {
94
                $form->addElement(new XoopsFormLabel('<b>' . strtr(_MA_PEDIGREE_FLD_FATH, array('[father]' => $pedigree->getConfig('father'))) . '</b>', "<img src=\"assets/images/male.gif\"><a href=\"seldog.php?curval=" . $row['Id'] . "&gend=1&letter=a\">" . $rowfetch['NAAM'] . '</a>'));
95
            }
96
        } else {
97
            $form->addElement(new XoopsFormLabel('<b>' . strtr(_MA_PEDIGREE_FLD_FATH, array('[father]' => $pedigree->getConfig('father'))) . '</b>', "<img src=\"assets/images/male.gif\"><a href=\"seldog.php?curval=" . $row['Id'] . "&gend=1&letter=a\">Unknown</a>"));
98
        }
99
        //mother
100
        $sql       = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . " WHERE Id='" . $row['mother'] . "'";
101
        $resmother = $GLOBALS['xoopsDB']->query($sql);
102
        $numfields = mysqli_num_rows($resmother);
103 View Code Duplication
        if (!$numfields == '0') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
            while (false !== ($rowfetch = $GLOBALS['xoopsDB']->fetchArray($resmother))) {
105
                $form->addElement(new XoopsFormLabel('<b>' . strtr(_MA_PEDIGREE_FLD_MOTH, array('[mother]' => $pedigree->getConfig('mother'))) . '</b>', "<img src=\"assets/images/female.gif\"><a href=\"seldog.php?curval=" . $row['Id'] . "&gend=0&letter=a\">" . $rowfetch['NAAM'] . '</a>'));
106
            }
107
        } else {
108
            $form->addElement(new XoopsFormLabel('<b>' . strtr(_MA_PEDIGREE_FLD_MOTH, array('[mother]' => $pedigree->getConfig('mother'))) . '</b>', "<img src=\"assets/images/female.gif\"><a href=\"seldog.php?curval=" . $row['Id'] . "&gend=0&letter=a\">Unknown</a>"));
109
        }
110
        //owner/breeder
111
        if ('1' == $pedigree->getConfig('ownerbreeder')) {
112
            $owner_select = new XoopsFormSelect('<b>' . _MA_PEDIGREE_FLD_OWNE . '</b>', $name = 'id_owner', $value = $row['id_owner'], $size = 1, $multiple = false);
113
            $queryeig     = 'SELECT Id, lastname, firstname FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_owner') . " ORDER BY \"lastname\"";
114
            $reseig       = $GLOBALS['xoopsDB']->query($queryeig);
115
            $owner_select->addOption(0, $name = _MA_PEDIGREE_UNKNOWN);
116
            while (false !== ($roweig = $GLOBALS['xoopsDB']->fetchArray($reseig))) {
117
                $owner_select->addOption($roweig['Id'], $name = $roweig['lastname'] . ', ' . $roweig['firstname']);
118
            }
119
            $form->addElement($owner_select);
120
            //breeder
121
            $breeder_select = new XoopsFormSelect('<b>' . _MA_PEDIGREE_FLD_BREE . '</b>', $name = 'id_breeder', $value = $row['id_breeder'], $size = 1, $multiple = false);
122
            $queryfok       = 'SELECT Id, lastname, firstname FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_owner') . " ORDER BY \"lastname\"";
123
            $resfok         = $GLOBALS['xoopsDB']->query($queryfok);
124
            $breeder_select->addOption(0, $name = _MA_PEDIGREE_UNKNOWN);
125
            while (false !== ($rowfok = $GLOBALS['xoopsDB']->fetchArray($resfok))) {
126
                $breeder_select->addOption($rowfok['Id'], $name = $rowfok['lastname'] . ', ' . $rowfok['firstname']);
127
            }
128
            $form->addElement($breeder_select);
129
        }
130
        //picture
131
        if ($row['foto'] != '') {
132
            $picture = '<img src=assets/images/thumbnails/' . $row['foto'] . '_400.jpeg>';
133
            $form->addElement(new XoopsFormLabel('<b>Picture</b>', $picture));
134
        } else {
135
            $picture = '';
136
        }
137
        $form->setExtra("enctype='multipart/form-data'");
138
        $img_box = new XoopsFormFile('<b>Image</b>', 'photo', 1024000);
139
        $img_box->setExtra("size ='50'");
140
        $form->addElement($img_box);
141
        //userfields
142
        //create animal object
143
        $animal = new PedigreeAnimal($id);
144
        //test to find out how many user fields there are..
145
        $fields = $animal->getNumOfFields();
146 View Code Duplication
        for ($i = 0, $iMax = count($fields); $i < $iMax; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
            $userField = new Field($fields[$i], $animal->getConfig());
148
            if ($userField->isActive()) {
149
                $fieldType     = $userField->getSetting('fieldtype');
150
                $fieldObject   = new $fieldType($userField, $animal);
151
                $edditable[$i] = $fieldObject->editField();
152
                $form->addElement($edditable[$i]);
153
            }
154
        }
155
    }
156
    $form->addElement(new XoopsFormButton('', 'button_id', _MA_PEDIGREE_BUT_SUB, 'submit'));
157
    $xoopsTpl->assign('form', $form->render());
158
}
159
160
//comments and footer
161
include XOOPS_ROOT_PATH . '/footer.php';
162