Passed
Branch master (e5d775)
by Michael
25:14 queued 11:39
created

Confirm::getFormConfirm()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 37
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 25
c 1
b 0
f 1
nc 32
nop 0
dl 0
loc 37
rs 8.8977
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Publisher\Common;
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
 * Custom form confirm for XOOPS modules
19
 *
20
 * @copyright       2020 XOOPS Project (https://xoops.org)
21
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
22
 * @package         general
23
 * @since           1.0
24
 * @min_xoops       2.5.9
25
 * @author          Goffy - Email:<[email protected]> - Website:<https://xoops.org>
26
 *
27
 *
28
 * Example:
29
 * $customConfirm = new Common\Confirm(
30
 * ['ok' => 1, 'item_id' => $itemId, 'op' => 'delete'],
31
 * $_SERVER['REQUEST_URI'],
32
 * \sprintf(\_MA_MYMODULE_FORM_SURE_DELETE,
33
 * $itemsObj->getCaption()));
34
 * $form = $customConfirm->getFormConfirm();
35
 * $GLOBALS['xoopsTpl']->assign('form', $form->render());
36
 */
37
38
\defined('XOOPS_ROOT_PATH') || die('Restricted access');
39
40
/**
41
 * Class Object Confirm
42
 */
43
class Confirm
44
{
45
    private $hiddens = [];
46
    private $action  = '';
47
    private $title   = '';
48
    private $label   = '';
49
    private $object  = '';
50
51
    /**
52
     * @public function constructor class
53
     * @param array  $hiddens
54
     * @param string $action
55
     * @param string $object
56
     * @param string $title
57
     * @param string $label
58
     */
59
    public function __construct(array $hiddens, string $action, string $object, string $title = '', string $label = '')
60
    {
61
        $this->hiddens = $hiddens;
62
        $this->action  = $action;
63
        $this->object  = $object;
64
        $this->title   = $title;
65
        $this->label   = $label;
66
    }
67
68
    /**
69
     * @public function getFormConfirm
70
     * @return \XoopsThemeForm
71
     */
72
    public function getFormConfirm(): \XoopsThemeForm
73
    {
74
        $moduleDirName      = \basename(__DIR__);
75
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
76
        //in order to be accessable from user and admin area this should be place in language common.php
77
        if (!\defined('CO_' . $moduleDirNameUpper . '_DELETE_CONFIRM')) {
78
            \define('CO_' . $moduleDirNameUpper . '_DELETE_CONFIRM', 'Confirm delete');
79
            \define('CO_' . $moduleDirNameUpper . '_DELETE_LABEL', 'Do you really want to delete:');
80
        }
81
82
        // Get Theme Form
83
        if ('' === $this->action) {
84
            $this->action = \Xmf\Request::getString('REQUEST_URI', '', 'SERVER');
85
        }
86
        if ('' === $this->title) {
87
            $this->title = \constant('CO_' . $moduleDirNameUpper . '_DELETE_CONFIRM');
88
        }
89
        if ('' === $this->label) {
90
            $this->label = \constant('CO_' . $moduleDirNameUpper . '_DELETE_LABEL');
91
        }
92
93
        \xoops_load('XoopsFormLoader');
94
        $form = new \XoopsThemeForm($this->title, 'formConfirm', $this->action, 'post', true);
95
        $form->setExtra('enctype="multipart/form-data"');
96
        $form->addElement(new \XoopsFormLabel($this->label, $this->object));
97
        //hiddens
98
        foreach ($this->hiddens as $key => $value) {
99
            $form->addElement(new \XoopsFormHidden($key, $value));
100
        }
101
        $form->addElement(new \XoopsFormHidden('ok', 1));
102
        $buttonTray = new \XoopsFormElementTray('');
103
        $buttonTray->addElement(new \XoopsFormButton('', 'confirm_submit', \_YES, 'submit'));
104
        $buttonBack = new \XoopsFormButton('', 'confirm_back', \_NO, 'button');
105
        $buttonBack->setExtra('onclick="history.go(-1);return true;"');
106
        $buttonTray->addElement($buttonBack);
107
        $form->addElement($buttonTray);
108
        return $form;
109
    }
110
}
111