Passed
Push — master ( 8b5e26...f79285 )
by Michael
04:32
created

edit()   C

Complexity

Conditions 13
Paths 98

Size

Total Lines 95
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 13
eloc 69
c 3
b 0
f 0
nc 98
nop 1
dl 0
loc 95
rs 5.9696

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
/**
14
 * Module: Pedigree
15
 *
16
 * @package   XoopsModules\Pedigree
17
 * @author    XOOPS Module Development Team
18
 * @copyright Copyright (c) 2001-2019 {@link https://xoops.org XOOPS Project}
19
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GNU Public License
20
 */
21
22
use Xmf\Request;
23
use XoopsModules\Pedigree\{
24
    Utility
25
};
26
27
//require_once  \dirname(__DIR__, 2) . '/mainfile.php';
28
require_once __DIR__ . '/header.php';
29
$moduleDirName = basename(__DIR__);
30
xoops_loadLanguage('main', $moduleDirName);
31
32
//needed for generation of pie charts
33
ob_start();
34
require XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->dirname() . '/include/class_eq_pie.php';
35
36
$GLOBALS['xoopsOption']['template_main'] = 'pedigree_edit.tpl';
37
38
require XOOPS_ROOT_PATH . '/header.php';
39
// Include any common code for this module.
40
require_once XOOPS_ROOT_PATH . '/modules/' . $moduleDirName . '/include/common.php';
41
42
global $xoopsTpl, $xoopsDB;
43
44
//get module configuration
45
/** @var \XoopsModuleHandler $moduleHandler */
46
$moduleHandler = xoops_getHandler('module');
47
$module        = $moduleHandler->getByDirname($moduleDirName);
48
/** @var \XoopsConfigHandler $configHandler */
49
$configHandler = xoops_getHandler('config');
50
$moduleConfig  = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
51
52
if (isset($_GET['f'])) {
53
    if ('save' === Request::getCmd('f', '', 'GET')) {
54
        save();
55
    }
56
} else {
57
    edit();
58
}
59
60
function save()
61
{
62
    global $xoopsDB, $moduleConfig;
63
    $a      = (!isset($_POST['id']) ? $a = '' : $a = $_POST['id']);
64
    $animal = new Pedigree\Animal($a);
65
    $fields = $animal->getNumOfFields();
66
    foreach ($fields as $i => $iValue) {
67
        $userField = new Pedigree\Field($fields[$i], $animal->getConfig());
68
        if ($userField->isActive()) {
69
            $currentfield = 'user' . $iValue;
70
            $pictureField = $_FILES[$currentfield]['name'];
71
            if (empty($pictureField) || '' == $pictureField) {
72
                $newvalue = $_POST['user' . $iValue];
73
            } else {
74
                $newvalue = Utility::uploadPicture(0);
75
            }
76
            $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_registry') . ' SET user' . $iValue . "='" . $GLOBALS['xoopsDB']->escape($newvalue) . "' WHERE id='" . $a . "'";
77
            $GLOBALS['xoopsDB']->query($sql);
78
        }
79
    }
80
    //    $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_registry') . " SET pname = '" . $_POST['pname'] . "', roft = '" . $_POST['roft'] . "' WHERE id='" . $a . "'";
81
    $pname = Request::getString('pname', '', 'post');
82
    $roft = Request::getString('roft', '', 'post');
83
    $sql  = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_registry') . " SET pname = '" . $GLOBALS['xoopsDB']->escape($pname) . "', roft = '" . $GLOBALS['xoopsDB']->escape($roft) . "' WHERE id='" . $a . "'";
84
    $GLOBALS['xoopsDB']->query($sql);
85
    $pictureField = $_FILES['photo']['name'];
86
    if (empty($pictureField) || '' == $pictureField) {
87
        //llalalala
88
    } else {
89
        $foto = Utility::uploadPicture(0);
90
        //      $sql  = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_registry') . " SET foto='" . $foto . "' WHERE id='" . $a . "'";
91
        $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_registry') . " SET foto='" . $GLOBALS['xoopsDB']->escape($foto) . "' WHERE id='" . $a . "'";
92
    }
93
    $GLOBALS['xoopsDB']->query($sql);
94
    if ('1' == $helper->getConfig('ownerbreeder')) {
95
        //      $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_registry') . " SET id_owner = '" . $_POST['id_owner'] . "', id_breeder = '" . $_POST['id_breeder'] . "' WHERE id='" . $a . "'";
96
        $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('pedigree_registry') . " SET id_owner = '" . Request::getInt('id_owner', 0, 'post') . "', id_breeder = '" . Request::getInt('id_breeder', 0, 'post') . "' WHERE id='" . $a . "'";
97
        $GLOBALS['xoopsDB']->query($sql);
98
    }
99
    redirect_header('dog.php?id=' . $a, 2, 'Your changes have been saved');
100
}
101
102
/**
103
 * @param int $id
104
 */
105
function edit($id = 0)
106
{
107
    global $xoopsTpl, $xoopsDB, $moduleConfig;
108
    if (isset($_GET['id'])) {
109
        $id = Request::getInt('id', 0, 'GET);
110
    }
111
    require XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_CLASS on line 111 at column 32
Loading history...
112
113
    $sql    = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_registry') . ' WHERE id=' . $id;
114
    $result = $GLOBALS['xoopsDB']->query($sql);
115
    while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
116
        $form = new \XoopsThemeForm('Edit ' . $row['pname'], 'dogname', 'edit.php?f=save', 'post', true);
117
        $form->addElement(new \XoopsFormHiddenToken($name = 'XOOPS_TOKEN_REQUEST', $timeout = 360));
118
        $form->addElement(new \XoopsFormHidden('id', $id));
119
        //name
120
        $pname = htmlentities(stripslashes($row['pname']), ENT_QUOTES);
121
        $form->addElement(new \XoopsFormText('<b>' . _MA_PEDIGREE_FLD_NAME . '</b>', 'pname', $size = 50, $maxsize = 255, $value = $pname));
122
        //gender
123
        $roft         = $row['roft'];
124
        $gender_radio = new \XoopsFormRadio('<b>' . _MA_PEDIGREE_FLD_GEND . '</b>', 'roft', $value = $roft);
125
        $gender_radio->addOptionArray([
126
                                          '0' => strtr(_MA_PEDIGREE_FLD_MALE, ['[male]' => $moduleConfig['male']]),
127
                                          '1' => strtr(_MA_PEDIGREE_FLD_FEMA, ['[female]' => $moduleConfig['female']]),
128
                                      ]);
129
        $form->addElement($gender_radio);
130
        //father
131
        $sql       = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_registry') . " WHERE id='" . $row['father'] . "'";
132
        $resfather = $GLOBALS['xoopsDB']->query($sql);
133
        $numfields = $GLOBALS['xoopsDB']->getRowsNum($resfather);
134
        if ('0' == !$numfields) {
135
            while (false !== ($rowfetch = $GLOBALS['xoopsDB']->fetchArray($resfather))) {
136
                $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['pname'] . '</a>'));
137
            }
138
        } else {
139
            $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>'));
140
        }
141
        //mother
142
        $sql       = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_registry') . " WHERE id='" . $row['mother'] . "'";
143
        $resmother = $GLOBALS['xoopsDB']->query($sql);
144
        $numfields = $GLOBALS['xoopsDB']->getRowsNum($resmother);
145
        if ('0' == !$numfields) {
146
            while (false !== ($rowfetch = $GLOBALS['xoopsDB']->fetchArray($resmother))) {
147
                $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['pname'] . '</a>'));
148
            }
149
        } else {
150
            $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>'));
151
        }
152
        //owner/breeder
153
        if ('1' == $helper->getConfig('ownerbreeder')) {
154
            $owner_select = new \XoopsFormSelect('<b>' . _MA_PEDIGREE_FLD_OWNE . '</b>', $name = 'id_owner', $value = $row['id_owner'], $size = 1, $multiple = false);
155
            $queryeig     = 'SELECT id, lastname, firstname FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_owner') . ' ORDER BY "lastname"';
156
            $reseig       = $GLOBALS['xoopsDB']->query($queryeig);
157
            $owner_select->addOption(0, $name = _MA_PEDIGREE_UNKNOWN);
158
            while (false !== ($roweig = $GLOBALS['xoopsDB']->fetchArray($reseig))) {
159
                $owner_select->addOption($roweig['id'], $name = $roweig['lastname'] . ', ' . $roweig['firstname']);
160
            }
161
            $form->addElement($owner_select);
162
            //breeder
163
            $breeder_select = new \XoopsFormSelect('<b>' . _MA_PEDIGREE_FLD_BREE . '</b>', $name = 'id_breeder', $value = $row['id_breeder'], $size = 1, $multiple = false);
164
            $queryfok       = 'SELECT id, lastname, firstname FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_owner') . ' ORDER BY "lastname"';
165
            $resfok         = $GLOBALS['xoopsDB']->query($queryfok);
166
            $breeder_select->addOption(0, $name = _MA_PEDIGREE_UNKNOWN);
167
            while (false !== ($rowfok = $GLOBALS['xoopsDB']->fetchArray($resfok))) {
168
                $breeder_select->addOption($rowfok['id'], $name = $rowfok['lastname'] . ', ' . $rowfok['firstname']);
169
            }
170
            $form->addElement($breeder_select);
171
        }
172
        //picture
173
        if ('' != $row['foto']) {
174
            $picture = '<img src=' . PEDIGREE_UPLOAD_URL . '/images/thumbnails/' . $row['foto'] . '_400.jpeg>';
175
            $form->addElement(new \XoopsFormLabel('<b>Picture</b>', $picture));
176
        } else {
177
            $picture = '';
178
        }
179
        $form->setExtra("enctype='multipart/form-data'");
180
        $img_box = new \XoopsFormFile('<b>Image</b>', 'photo', 1024000);
181
        $img_box->setExtra("size ='50'");
182
        $form->addElement($img_box);
183
        //userfields
184
        //create animal object
185
        $animal = new Pedigree\Animal($id);
186
        //test to find out how many user fields there are..
187
        $fields = $animal->getNumOfFields();
188
        for ($i = 0, $iMax = count($fields); $i < $iMax; ++$i) {
189
            $userField = new Pedigree\Field($fields[$i], $animal->getConfig());
190
            if ($userField->isActive()) {
191
                $fieldType     = $userField->getSetting('FieldType');
192
                $fieldObject   = new $fieldType($userField, $animal);
193
                $edditable[$i] = $fieldObject->editField();
194
                $form->addElement($edditable[$i]);
195
            }
196
        }
197
    }
198
    $form->addElement(new \XoopsFormButton('', 'button_id', _MA_PEDIGREE_BUT_SUB, 'submit'));
199
    $xoopsTpl->assign('form', $form->render());
200
}
201
202
//comments and footer
203
require XOOPS_ROOT_PATH . '/footer.php';
204