Passed
Pull Request — master (#81)
by Michael
02:53
created

ImagesForm   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
eloc 55
c 2
b 1
f 0
dl 0
loc 104
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 93 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Yogurt\Form;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
17
/**
18
 * Module: Yogurt
19
 *
20
 * @category        Module
21
 * @package         yogurt
22
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
23
 * @copyright       {@link https://xoops.org/ XOOPS Project}
24
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
25
 */
26
27
use Xmf\Module\Helper\Permission;
28
use XoopsFormButton;
29
use XoopsFormHidden;
30
use XoopsFormLabel;
31
use XoopsFormSelectUser;
32
use XoopsFormText;
33
use XoopsFormTextArea;
34
use XoopsFormTextDateSelect;
35
use XoopsModules\Yogurt;
36
use XoopsThemeForm;
37
38
require_once \dirname(__DIR__, 2) . '/include/common.php';
39
40
$moduleDirName = \basename(\dirname(__DIR__, 2));
41
//$helper = Yogurt\Helper::getInstance();
42
$permHelper = new Permission();
43
44
\xoops_load('XoopsFormLoader');
45
46
/**
47
 * Class ImagesForm
48
 */
49
class ImagesForm extends XoopsThemeForm
50
{
51
    public $targetObject;
52
53
    public $helper;
54
55
    /**
56
     * Constructor
57
     *
58
     * @param $target
59
     */
60
    public function __construct($target)
61
    {
62
        $this->helper       = $target->helper;
63
        $this->targetObject = $target;
64
65
        $title = $this->targetObject->isNew() ? \sprintf(\AM_YOGURT_IMAGES_ADD) : \sprintf(\AM_YOGURT_IMAGES_EDIT);
66
        parent::__construct($title, 'form', \xoops_getenv('SCRIPT_NAME'), 'post', true);
67
        $this->setExtra('enctype="multipart/form-data"');
68
69
        //include ID field, it's needed so the module knows if it is a new form or an edited form
70
71
        $hidden = new XoopsFormHidden(
72
            'cod_img', $this->targetObject->getVar(
73
            'cod_img'
74
        )
75
        );
76
        $this->addElement($hidden);
77
        unset($hidden);
78
79
        // Cod_img
80
        $this->addElement(
81
            new XoopsFormLabel(\AM_YOGURT_IMAGES_COD_IMG, $this->targetObject->getVar('cod_img'), 'cod_img')
82
        );
83
        // Title
84
        $this->addElement(
85
            new XoopsFormText(\AM_YOGURT_IMAGES_TITLE, 'title', 50, 255, $this->targetObject->getVar('title')),
86
            false
87
        );
88
89
        // Caption
90
        $this->addElement(
91
            new XoopsFormText(\AM_YOGURT_IMAGES_CAPTION, 'caption', 50, 255, $this->targetObject->getVar('caption')),
92
            false
93
        );
94
95
        // Data_creation
96
        $this->addElement(
97
            new XoopsFormTextDateSelect(
98
                \AM_YOGURT_IMAGES_DATE_CREATED, 'date_created', 0, formatTimestamp($this->targetObject->getVar('date_created'), 's')
0 ignored issues
show
Bug introduced by
formatTimestamp($this->t...r('date_created'), 's') of type string is incompatible with the type integer expected by parameter $value of XoopsFormTextDateSelect::__construct(). ( Ignorable by Annotation )

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

98
                \AM_YOGURT_IMAGES_DATE_CREATED, 'date_created', 0, /** @scrutinizer ignore-type */ formatTimestamp($this->targetObject->getVar('date_created'), 's')
Loading history...
99
            )
100
        );
101
        // Data_update
102
        $this->addElement(
103
            new XoopsFormTextDateSelect(
104
                \AM_YOGURT_IMAGES_DATE_UPDATED, 'date_updated', 0, formatTimestamp($this->targetObject->getVar('date_updated'), 's')
105
            )
106
        );
107
        // Uid_owner
108
        $this->addElement(
109
            new XoopsFormSelectUser(
110
                \AM_YOGURT_IMAGES_UID_OWNER, 'uid_owner', false, $this->targetObject->getVar(
111
                'uid_owner'
112
            ), 1, false
113
            ),
114
            false
115
        );
116
        // Url
117
        //        $this->addElement(
118
        //            new XoopsFormTextArea(AM_YOGURT_IMAGES_URL, 'filename', $this->targetObject->getVar('filename'), 4, 47),
119
        //            false
120
        //        );
121
122
        $url = $this->targetObject->getVar('filename') ?: 'blank.png';
123
124
        $uploadDir   = '/uploads/yogurt/images/';
125
        $imgtray     = new \XoopsFormElementTray(\AM_YOGURT_IMAGES_URL, '<br>');
126
        $imgpath     = \sprintf(\AM_YOGURT_FORMIMAGE_PATH, $uploadDir);
127
        $imageselect = new \XoopsFormSelect($imgpath, 'filename', $url);
128
        $imageArray  = \XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH . $uploadDir);
129
        foreach ($imageArray as $image) {
130
            $imageselect->addOption($image, $image);
131
        }
132
        $imageselect->setExtra("onchange='showImgSelected(\"image_url\", \"filename\", \"" . $uploadDir . '", "", "' . XOOPS_URL . "\")'");
133
        $imgtray->addElement($imageselect);
134
        $imgtray->addElement(new \XoopsFormLabel('', "<br><img src='" . XOOPS_URL . '/' . $uploadDir . '/' . $url . "' name='image_url' id='image_url' alt='' style='max-width:300px'>"));
135
        $fileseltray = new \XoopsFormElementTray('', '<br>');
136
        $fileseltray->addElement(new \XoopsFormFile(\AM_YOGURT_FORMUPLOAD, 'filename', $this->helper->getConfig('maxsize')));
137
        $fileseltray->addElement(new \XoopsFormLabel(''));
138
        $imgtray->addElement($fileseltray);
139
        $this->addElement($imgtray);
140
        // Private
141
        //        $this->addElement(
142
        //            new XoopsFormText(AM_YOGURT_IMAGES_PRIVATE, 'private', 50, 255, $this->targetObject->getVar('private')),
143
        //            false
144
        //        );
145
146
        $private       = $this->targetObject->isNew() ? 0 : $this->targetObject->getVar('private');
147
        $check_private = new \XoopsFormCheckBox(\AM_YOGURT_IMAGES_PRIVATE, 'private', $private);
148
        $check_private->addOption(1, ' ');
149
        $this->addElement($check_private);
150
151
        $this->addElement(new XoopsFormHidden('op', 'save'));
152
        $this->addElement(new XoopsFormButton('', 'submit', \_SUBMIT, 'submit'));
153
    }
154
}
155