Building::getForm()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 4
nop 1
dl 0
loc 23
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Tdmcreate;
4
5
use XoopsModules\Tdmcreate;
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
 * Building class.
19
 *
20
 * @copyright       XOOPS Project (https://xoops.org)
21
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
22
 *
23
 * @since           2.5.x
24
 *
25
 * @author          TDM TEAM DEV MODULE
26
 *
27
 */
28
29
/**
30
 * Class Building.
31
 */
32
class Building
33
{
34
    /**
35
     * @static function getInstance
36
     *
37
     * @param null
38
     *
39
     * @return Building
40
     */
41
    public static function getInstance()
42
    {
43
        static $instance = false;
44
        if (!$instance) {
45
            $instance = new self();
46
        }
47
48
        return $instance;
49
    }
50
51
    /**
52
     * @param bool $action
53
     *
54
     * @return \XoopsThemeForm
55
     */
56
    public function getForm($action = false)
57
    {
58
        $helper = Tdmcreate\Helper::getInstance();
59
        if (false === $action) {
60
            $action = \Xmf\Request::getString('REQUEST_URI', '', 'SERVER');
61
        }
62
        xoops_load('XoopsFormLoader');
63
        $form = new \XoopsThemeForm(_AM_TDMCREATE_ADMIN_CONST, 'buildform', $action, 'post', true);
0 ignored issues
show
Bug introduced by
It seems like $action can also be of type true; however, parameter $action of XoopsThemeForm::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

63
        $form = new \XoopsThemeForm(_AM_TDMCREATE_ADMIN_CONST, 'buildform', /** @scrutinizer ignore-type */ $action, 'post', true);
Loading history...
64
        $form->setExtra('enctype="multipart/form-data"');
65
        $moduleObj  = $helper->getHandler('modules')->getObjects(null);
0 ignored issues
show
Bug introduced by
The method getObjects() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsRankHandler or XoUserHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

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

65
        $moduleObj  = $helper->getHandler('modules')->/** @scrutinizer ignore-call */ getObjects(null);
Loading history...
66
        $mod_select = new \XoopsFormSelect(_AM_TDMCREATE_CONST_MODULES, 'mod_id', 'mod_id');
67
        $mod_select->addOption('', _AM_TDMCREATE_BUILD_MODSELOPT);
68
        foreach ($moduleObj as $mod) {
69
            $mod_select->addOption($mod->getVar('mod_id'), $mod->getVar('mod_name'));
70
        }
71
        $form->addElement($mod_select, true);
72
        
73
        $form->addElement(new \XoopsFormRadioYN(_AM_TDMCREATE_MODULE_INROOT_COPY, 'inroot_copy', $helper->getConfig('inroot_copy')));
74
        
75
        $form->addElement(new \XoopsFormHidden('op', 'build'));
76
        $form->addElement(new \XoopsFormButton(_REQUIRED . ' <sup class="red bold">*</sup>', 'submit', _SUBMIT, 'submit'));
77
78
        return $form;
79
    }
80
81
    /**
82
     * @param string $dir
83
     * @param string $pattern
84
     */
85
    public function clearDir($dir, $pattern = '*')
86
    {
87
        // Find all files and folders matching pattern
88
        $files = glob($dir . "/$pattern");
89
        // Interate thorugh the files and folders
90
        foreach ($files as $file) {
91
            // if it's a directory then re-call clearDir function to delete files inside this directory
92
            if (is_dir($file) && !in_array($file, ['..', '.'])) {
93
                // Remove the directory itself
94
                $this->clearDir($file, $pattern);
95
            } elseif ((__FILE__ != $file) && is_file($file)) {
96
                // Make sure you don't delete the current script
97
                unlink($file);
98
            }
99
        }
100
        if (is_dir($dir)) {
101
            rmdir($dir);
102
        }
103
    }
104
105
    /**
106
     * @param string $src
107
     * @param string $dst
108
     */
109
    public function copyDir($src, $dst)
110
    {
111
        $dir = opendir($src);
112
        if (!mkdir($dst) && !is_dir($dst)) {
113
            throw new \RuntimeException(sprintf('Directory "%s" was not created', $dst));
114
        }
115
        while (false !== ($file = readdir($dir))) {
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type false; however, parameter $dir_handle of readdir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

115
        while (false !== ($file = readdir(/** @scrutinizer ignore-type */ $dir))) {
Loading history...
116
            if (('.' !== $file) && ('..' !== $file)) {
117
                if (is_dir($src . '/' . $file)) {
118
                    // Copy the directory itself
119
                    $this->copyDir($src . '/' . $file, $dst . '/' . $file);
120
                } else {
121
                    // Make sure you copy the current script
122
                    copy($src . '/' . $file, $dst . '/' . $file);
123
                }
124
            }
125
        }
126
        closedir($dir);
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type false; however, parameter $dir_handle of closedir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

126
        closedir(/** @scrutinizer ignore-type */ $dir);
Loading history...
127
    }
128
}
129