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

RegistryForm   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 65
dl 0
loc 105
rs 10
c 1
b 0
f 0
wmc 11

1 Method

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 96 11
1
<?php
2
3
namespace XoopsModules\Pedigree\Form;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * Module: Pedigree
17
 *
18
 * @category        Module
19
 * @package         pedigree
20
 * @author          XOOPS Development Team <https://xoops.org>
21
 * @copyright       {@link https://xoops.org/ XOOPS Project}
22
 * @license         GPL 2.0 or later
23
 * @link            https://xoops.org/
24
 * @since           1.0.0
25
 */
26
27
use CriteriaCompo;
28
use RuntimeException;
29
use Xmf\Module\Helper\Permission;
30
use XoopsDatabaseFactory;
31
use XoopsFormButton;
32
use XoopsFormElementTray;
33
use XoopsFormFile;
34
use XoopsFormHidden;
35
use XoopsFormLabel;
36
use XoopsFormSelect;
37
use XoopsFormSelectUser;
38
use XoopsFormText;
39
use XoopsFormTextArea;
40
use XoopsLists;
41
use XoopsObjectTree;
42
use XoopsThemeForm;
43
use XoopsModules\Pedigree\{
44
    Helper,
45
    Utility
46
};
47
48
require_once \dirname(__DIR__, 2) . '/include/common.php';
49
50
$moduleDirName = \basename(\dirname(__DIR__, 2));
51
//$helper = Helper::getInstance();
52
$permHelper = new Permission();
53
54
\xoops_load('XoopsFormLoader');
55
56
/**
57
 * Class RegistryForm
58
 */
59
class RegistryForm extends XoopsThemeForm
60
{
61
    public $targetObject;
62
63
    /**
64
     * Constructor
65
     *
66
     * @param $target
67
     */
68
    public function __construct($target)
69
    {
70
        //  global $helper;
71
        $this->helper       = $target->helper;
0 ignored issues
show
Bug Best Practice introduced by
The property helper does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
72
        $this->targetObject = $target;
73
74
        $title = $this->targetObject->isNew() ? \sprintf(\AM_PEDIGREE_REGISTRY_ADD) : \sprintf(AM_PEDIGREE_REGISTRY_EDIT);
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Pedigree\Fo..._PEDIGREE_REGISTRY_EDIT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
75
        parent::__construct($title, 'form', \xoops_getenv('SCRIPT_NAME'), 'post', true);
76
        $this->setExtra('enctype="multipart/form-data"');
77
78
        //include ID field, it's needed so the module knows if it is a new form or an edited form
79
80
        $hidden = new \XoopsFormHidden('id', $this->targetObject->getVar('id'));
81
        $this->addElement($hidden);
82
        unset($hidden);
83
84
        // Id
85
        $this->addElement(new \XoopsFormLabel(\AM_PEDIGREE_REGISTRY_ID, $this->targetObject->getVar('id'), 'id'));
86
        // Pname
87
        $this->addElement(new \XoopsFormTextArea(\AM_PEDIGREE_REGISTRY_PNAME, 'pname', $this->targetObject->getVar('pname'), 4, 47), false);
88
        // Id_owner
89
        $this->addElement(new \XoopsFormText(\AM_PEDIGREE_REGISTRY_ID_OWNER, 'id_owner', 50, 255, $this->targetObject->getVar('id_owner')), false);
90
        // Id_breeder
91
        $this->addElement(new \XoopsFormText(\AM_PEDIGREE_REGISTRY_ID_BREEDER, 'id_breeder', 50, 255, $this->targetObject->getVar('id_breeder')), false);
92
        // User
93
        $this->addElement(new \XoopsFormSelectUser(\AM_PEDIGREE_REGISTRY_USER, 'user', false, $this->targetObject->getVar('user'), 1, false), false);
94
        // Roft
95
        $roft         = new \XoopsFormSelect(\AM_PEDIGREE_REGISTRY_ROFT, 'roft', $this->targetObject->getVar('roft'));
96
        $optionsArray = Utility::enumerate('pedigree_registry', 'roft');
97
        if (!\is_array($optionsArray)) {
98
            throw new RuntimeException($optionsArray . ' must be an array.');
99
        }
100
        foreach ($optionsArray as $enum) {
101
            $roft->addOption($enum, (\defined($enum) ? \constant($enum) : $enum));
102
        }
103
        $this->addElement($roft, false);
104
        // Mother
105
        require_once XOOPS_ROOT_PATH . '/class/tree.php';
106
        /** @var \XoopsModules\Pedigree\RegistryHandler $Handler */
107
        $registryHandler = $this->helper->getHandler('Registry');
108
109
        $criteria      = new CriteriaCompo();
110
        $categoryArray = $registryHandler->getObjects($criteria);
111
        if ($categoryArray) {
112
            $categoryTree = new \XoopsObjectTree($categoryArray, 'id', 'pid');
113
114
            if (Utility::checkVerXoops($GLOBALS['xoopsModule'], '2.5.9')) {
115
                $registryPid = $categoryTree->makeSelectElement('pid', 'title', '--', $this->targetObject->getVar('pname'), true, 0, '', \AM_PEDIGREE_REGISTRY_MOTHER);
116
                $this->addElement($registryPid);
117
            } else {
118
                $registryPid = $categoryTree->makeSelBox('pname', 'title', '--', $this->targetObject->getVar('pname', 'e'), true);
119
                $this->addElement(new \XoopsFormLabel(\AM_PEDIGREE_REGISTRY_MOTHER, $registryPid));
120
            }
121
        }
122
        // Father
123
        require_once XOOPS_ROOT_PATH . '/class/tree.php';
124
        /** @var \XoopsModules\Pedigree\RegistryHandler $Handler */
125
        $registryHandler = $this->helper->getHandler('Registry');
126
127
        $criteria      = new CriteriaCompo();
128
        $categoryArray = $registryHandler->getObjects($criteria);
129
        if ($categoryArray) {
130
            $categoryTree = new \XoopsObjectTree($categoryArray, 'id', 'pid');
131
132
            if (Utility::checkVerXoops($GLOBALS['xoopsModule'], '2.5.9')) {
133
                $registryPid = $categoryTree->makeSelectElement('pid', 'title', '--', $this->targetObject->getVar('pname'), true, 0, '', \AM_PEDIGREE_REGISTRY_FATHER);
134
                $this->addElement($registryPid);
135
            } else {
136
                $registryPid = $categoryTree->makeSelBox('pname', 'title', '--', $this->targetObject->getVar('pname', 'e'), true);
137
                $this->addElement(new \XoopsFormLabel(\AM_PEDIGREE_REGISTRY_FATHER, $registryPid));
138
            }
139
        }
140
        // Foto
141
        $foto = $this->targetObject->getVar('foto') ?: 'blank.png';
142
143
        $uploadDir   = '/uploads/pedigree/images/';
144
        $imgtray     = new \XoopsFormElementTray(\AM_PEDIGREE_REGISTRY_FOTO, '<br>');
145
        $imgpath     = \sprintf(\AM_PEDIGREE_FORMIMAGE_PATH, $uploadDir);
146
        $imageselect = new \XoopsFormSelect($imgpath, 'foto', $foto);
147
        $imageArray  = XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH . $uploadDir);
148
        foreach ($imageArray as $image) {
149
            $imageselect->addOption((string)$image, $image);
150
        }
151
        $imageselect->setExtra("onchange='showImgSelected(\"image_foto\", \"foto\", \"" . $uploadDir . '", "", "' . XOOPS_URL . "\")'");
152
        $imgtray->addElement($imageselect);
153
        $imgtray->addElement(new \XoopsFormLabel('', "<br><img src='" . XOOPS_URL . '/' . $uploadDir . '/' . $foto . "' name='image_foto' id='image_foto' alt='' >"));
154
        $fileseltray = new \XoopsFormElementTray('', '<br>');
155
        $fileseltray->addElement(new \XoopsFormFile(\AM_PEDIGREE_FORMUPLOAD, 'foto', \xoops_getModuleOption('maxsize')));
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getModuleOption() has been deprecated. ( Ignorable by Annotation )

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

155
        $fileseltray->addElement(new \XoopsFormFile(\AM_PEDIGREE_FORMUPLOAD, 'foto', /** @scrutinizer ignore-deprecated */ \xoops_getModuleOption('maxsize')));
Loading history...
156
        $fileseltray->addElement(new \XoopsFormLabel(''));
157
        $imgtray->addElement($fileseltray);
158
        $this->addElement($imgtray);
159
        // Coi
160
        $this->addElement(new \XoopsFormText(\AM_PEDIGREE_REGISTRY_COI, 'coi', 50, 255, $this->targetObject->getVar('coi')), false);
161
162
        $this->addElement(new \XoopsFormHidden('op', 'save'));
163
        $this->addElement(new \XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
164
    }
165
}
166