IncludeNotifications::render()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Modulebuilder\Files\Includes;
4
5
use XoopsModules\Modulebuilder;
6
use XoopsModules\Modulebuilder\Files;
7
8
/*
9
 You may not change or alter any portion of this comment or credits
10
 of supporting developers from this source code or any supporting source code
11
 which is considered copyrighted (c) material of the original comment or credit authors.
12
13
 This program is distributed in the hope that it will be useful,
14
 but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 */
17
/**
18
 * modulebuilder module.
19
 *
20
 * @copyright       XOOPS Project (https://xoops.org)
21
 * @license         GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
22
 *
23
 * @since           2.5.0
24
 *
25
 * @author          Txmod Xoops https://xoops.org 
26
 *                  Goffy https://myxoops.org
27
 *
28
 */
29
30
/**
31
 * Class IncludeNotifications.
32
 */
33
class IncludeNotifications extends Files\CreateFile
34
{
35
    /**
36
     * @var mixed
37
     */
38
    private $xc = null;
39
40
    /**
41
     * @var mixed
42
     */
43
    private $pc = null;
44
45
    /**
46
     * @public function constructor
47
     * @param null
48
     */
49
    public function __construct()
50
    {
51
        parent::__construct();
52
        $this->xc = Modulebuilder\Files\CreateXoopsCode::getInstance();
53
        $this->pc = Modulebuilder\Files\CreatePhpCode::getInstance();
54
    }
55
56
    /**
57
     * @static function getInstance
58
     * @param null
59
     * @return IncludeNotifications
60
     */
61
    public static function getInstance()
62
    {
63
        static $instance = false;
64
        if (!$instance) {
65
            $instance = new self();
66
        }
67
68
        return $instance;
69
    }
70
71
    /**
72
     * @public function write
73
     * @param string $module
74
     * @param mixed  $tables
75
     * @param string $filename
76
     */
77
    public function write($module, $tables, $filename)
78
    {
79
        $this->setModule($module);
80
        $this->setTables($tables);
81
        $this->setFileName($filename);
82
    }
83
84
    /**
85
     * @static function getNotificationsFunction
86
     * @param string $moduleDirname
87
     *
88
     * @return string
89
     */
90
    public function getNotificationsFunction($moduleDirname)
91
    {
92
        $stuModuleDirname = \mb_strtoupper($moduleDirname);
93
        $tables           = $this->getTables();
94
        $t      = "\t";
95
        $ret    = $this->pc->getPhpCodeCommentMultiLine(['comment' => 'callback functions','' => '', '@param  $category' => '', '@param  $item_id' => '', '@return' => 'array item|null']);
96
        $func   = $this->xc->getXcGetGlobal(['xoopsDB'], $t);
97
        $func   .= $this->pc->getPhpCodeBlankLine();
98
        $contIf = $this->pc->getPhpCodeDefine($stuModuleDirname . '_URL',"\XOOPS_URL . '/modules/{$moduleDirname}'", $t . "\t");
99
        $func   .= $this->pc->getPhpCodeConditions("!\defined('{$stuModuleDirname}_URL')", '','',$contIf, false, $t);
100
        $func   .= $this->pc->getPhpCodeBlankLine();
101
102
        $case[] = $this->xc->getXcEqualsOperator("\$item['name']", "''",'',$t . "\t\t");
0 ignored issues
show
Comprehensibility Best Practice introduced by
$case was never initialized. Although not strictly required by PHP, it is generally a good practice to add $case = array(); before regardless.
Loading history...
103
        $case[] = $this->xc->getXcEqualsOperator("\$item['url'] ", "''",'',$t . "\t\t");
104
        $case[] = $this->getSimpleString('return $item;', $t . "\t\t");
105
        $cases  = [
106
            'global' => $case,
107
        ];
108
        $contentSwitch = $this->pc->getPhpCodeCaseSwitch($cases, false, false, $t . "\t");
109
        unset($case);
110
111
        foreach (\array_keys($tables) as $i) {
112
            if (1 === (int)$tables[$i]->getVar('table_notifications')) {
113
                $tableName   = $tables[$i]->getVar('table_name');
114
                $fieldParent = false;
115
                $fields      = $this->getTableFields($tables[$i]->getVar('table_mid'), $tables[$i]->getVar('table_id'));
116
                $fieldId     = '';
117
                $fieldMain   = '';
118
                foreach (\array_keys($fields) as $f) {
119
                    $fieldName = $fields[$f]->getVar('field_name');
120
                    if ((0 == $f) && (1 == $tables[$i]->getVar('table_autoincrement'))) {
121
                        $fieldId = $fieldName;
122
                    }
123
                    if (1 == $fields[$f]->getVar('field_parent')) {
124
                        $fieldParent = $fieldName;
125
                    }
126
                    if (1 == $fields[$f]->getVar('field_main')) {
127
                        $fieldMain = $fieldName;
128
                    }
129
                }
130
                if (1 == $tables[$i]->getVar('table_single')) {
131
                    $tableSingle = 'single';
132
                } else {
133
                    $tableSingle = $tableName;
134
                }
135
                $case[] = $this->xc->getXcEqualsOperator('$sql         ', "'SELECT {$fieldMain} FROM ' . \$xoopsDB->prefix('{$moduleDirname}_{$tableName}') . ' WHERE {$fieldId} = '. \$item_id",'',$t . "\t\t");
136
                $case[] = $this->xc->getXcEqualsOperator('$result      ', '$xoopsDB->query($sql)','',$t . "\t\t");
137
                $case[] = $this->xc->getXcEqualsOperator('$result_array', '$xoopsDB->fetchArray($result)','',$t . "\t\t");
138
                $case[] = $this->xc->getXcEqualsOperator("\$item['name']", "\$result_array['{$fieldMain}']",'',$t . "\t\t");
139
                if ($fieldParent) {
140
                    $case[] = $this->xc->getXcEqualsOperator("\$item['url'] ", "\\{$stuModuleDirname}_URL . '/{$tableSingle}.php?{$fieldParent}=' . \$result_array['{$fieldParent}'] . '&amp;{$fieldId}=' . \$item_id",'',$t . "\t\t");
141
                } else {
142
                    $case[] = $this->xc->getXcEqualsOperator("\$item['url'] ", "\\{$stuModuleDirname}_URL . '/{$tableName}.php?{$fieldId}=' . \$item_id",'',$t . "\t\t");
143
                }
144
145
                $case[] = $this->getSimpleString('return $item;', $t . "\t\t");
146
                $cases  = [
147
                    $tableName => $case,
148
                ];
149
                $contentSwitch .= $this->pc->getPhpCodeCaseSwitch($cases, false, false, $t . "\t");
150
                unset($case);
151
            }
152
        }
153
154
        $func .= $this->pc->getPhpCodeSwitch('category', $contentSwitch, $t);
155
        $func .= $this->getSimpleString('return null;', $t );
156
        $ret  .= $this->pc->getPhpCodeFunction("{$moduleDirname}_notify_iteminfo", '$category, $item_id', $func);
157
158
        return $ret;
159
    }
160
161
    /**
162
     * @public function render
163
     * @param null
164
     * @return bool|string
165
     */
166
    public function render()
167
    {
168
        $module        = $this->getModule();
169
        $filename      = $this->getFileName();
170
        $moduleDirname = $module->getVar('mod_dirname');
171
        $content       = $this->getHeaderFilesComments($module);
172
        $content       .= $this->getNotificationsFunction($moduleDirname);
173
174
        $this->create($moduleDirname, 'include', $filename, $content, \_AM_MODULEBUILDER_FILE_CREATED, \_AM_MODULEBUILDER_FILE_NOTCREATED);
175
176
        return $this->renderFile();
177
    }
178
}
179