IncludeNotifications   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 70
dl 0
loc 134
rs 10
c 0
b 0
f 0
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getInstance() 0 8 2
A write() 0 5 1
B getNotificationsFunction() 0 71 10
A render() 0 11 1
1
<?php
2
3
namespace XoopsModules\Tdmcreate\Files\Includes;
4
5
use XoopsModules\Tdmcreate;
6
use XoopsModules\Tdmcreate\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
 * tdmcreate module.
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.0
24
 *
25
 * @author          Txmod Xoops http://www.txmodxoops.org
26
 *
27
 */
28
29
/**
30
 * Class IncludeNotifications.
31
 */
32
class IncludeNotifications extends Files\CreateFile
33
{
34
    /**
35
     * @public function constructor
36
     * @param null
37
     */
38
    public function __construct()
39
    {
40
        parent::__construct();
41
    }
42
43
    /**
44
     * @static function getInstance
45
     * @param null
46
     * @return IncludeNotifications
47
     */
48
    public static function getInstance()
49
    {
50
        static $instance = false;
51
        if (!$instance) {
52
            $instance = new self();
53
        }
54
55
        return $instance;
56
    }
57
58
    /**
59
     * @public function write
60
     * @param string $module
61
     * @param mixed  $tables
62
     * @param string $filename
63
     */
64
    public function write($module, $tables, $filename)
65
    {
66
        $this->setModule($module);
67
        $this->setTables($tables);
68
        $this->setFileName($filename);
69
    }
70
71
    /**
72
     * @static function getNotificationsFunction
73
     * @param string $moduleDirname
74
     *
75
     * @return string
76
     */
77
    public function getNotificationsFunction($moduleDirname)
78
    {
79
        $pc               = Tdmcreate\Files\CreatePhpCode::getInstance();
80
        $xc               = Tdmcreate\Files\CreateXoopsCode::getInstance();
81
        $stuModuleDirname = mb_strtoupper($moduleDirname);
82
        $tables           = $this->getTables();
83
        $t      = "\t";
84
        $ret    = $pc->getPhpCodeCommentMultiLine(['comment' => 'callback functions','' => '', '@param  $category' => '', '@param  $item_id' => '', '@return' => 'array item|null']);
85
        $func   = $xc->getXcGetGlobal(['xoopsDB'], $t);
86
        $func   .= $pc->getPhpCodeBlankLine();
87
        $contIf = $pc->getPhpCodeDefine($stuModuleDirname . '_URL',"XOOPS_URL . '/modules/{$moduleDirname}'", $t . "\t");
88
        $func   .= $pc->getPhpCodeConditions("!defined('{$stuModuleDirname}_URL')", '','',$contIf, false, $t);
89
        $func   .= $pc->getPhpCodeBlankLine();
90
91
        $case[] = $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...
92
        $case[] = $xc->getXcEqualsOperator("\$item['url'] ", "''",'',$t . "\t\t");
93
        $case[] = $this->getSimpleString('return $item;', $t . "\t\t");
94
        $cases  = [
95
            'global' => $case,
96
        ];
97
        $contentSwitch = $pc->getPhpCodeCaseSwitch($cases, false, false, $t . "\t");
98
        unset($case);
99
100
        foreach (array_keys($tables) as $i) {
101
            if (1 === (int)$tables[$i]->getVar('table_notifications')) {
102
                $tableName   = $tables[$i]->getVar('table_name');
103
                $fieldParent = false;
104
                $fields      = $this->getTableFields($tables[$i]->getVar('table_mid'), $tables[$i]->getVar('table_id'));
105
                $fieldId     = '';
106
                $fieldMain   = '';
107
                foreach (array_keys($fields) as $f) {
108
                    $fieldName = $fields[$f]->getVar('field_name');
109
                    if ((0 == $f) && (1 == $tables[$i]->getVar('table_autoincrement'))) {
110
                        $fieldId = $fieldName;
111
                    }
112
                    if (1 == $fields[$f]->getVar('field_parent')) {
113
                        $fieldParent = $fieldName;
114
                    }
115
                    if (1 == $fields[$f]->getVar('field_main')) {
116
                        $fieldMain = $fieldName;
117
                    }
118
                }
119
                if (1 == $tables[$i]->getVar('table_single')) {
120
                    $tableSingle = 'single';
121
                } else {
122
                    $tableSingle = $tableName;
123
                }
124
                $case[] = $xc->getXcEqualsOperator('$sql         ', "'SELECT {$fieldMain} FROM ' . \$xoopsDB->prefix('{$moduleDirname}_{$tableName}') . ' WHERE {$fieldId} = '. \$item_id",'',$t . "\t\t");
125
                $case[] = $xc->getXcEqualsOperator('$result      ', '$xoopsDB->query($sql)','',$t . "\t\t");
126
                $case[] = $xc->getXcEqualsOperator('$result_array', '$xoopsDB->fetchArray($result)','',$t . "\t\t");
127
                $case[] = $xc->getXcEqualsOperator("\$item['name']", "\$result_array['{$fieldMain}']",'',$t . "\t\t");
128
                if ($fieldParent) {
129
                    $case[] = $xc->getXcEqualsOperator("\$item['url'] ", "{$stuModuleDirname}_URL . '/{$tableSingle}.php?{$fieldParent}=' . \$result_array['{$fieldParent}'] . '&amp;{$fieldId}=' . \$item_id",'',$t . "\t\t");
130
                } else {
131
                    $case[] = $xc->getXcEqualsOperator("\$item['url'] ", "{$stuModuleDirname}_URL . '/{$tableName}.php?{$fieldId}=' . \$item_id",'',$t . "\t\t");
132
                }
133
134
                $case[] = $this->getSimpleString('return $item;', $t . "\t\t");
135
                $cases  = [
136
                    $tableName => $case,
137
                ];
138
                $contentSwitch .= $pc->getPhpCodeCaseSwitch($cases, false, false, $t . "\t");
139
                unset($case);
140
            }
141
        }
142
143
        $func .= $pc->getPhpCodeSwitch('category', $contentSwitch, $t);
144
        $func .= $this->getSimpleString('return null;', $t );
145
        $ret  .= $pc->getPhpCodeFunction("{$moduleDirname}_notify_iteminfo", '$category, $item_id', $func);
146
147
        return $ret;
148
    }
149
150
    /**
151
     * @public function render
152
     * @param null
153
     * @return bool|string
154
     */
155
    public function render()
156
    {
157
        $module        = $this->getModule();
158
        $filename      = $this->getFileName();
159
        $moduleDirname = $module->getVar('mod_dirname');
160
        $content       = $this->getHeaderFilesComments($module);
161
        $content       .= $this->getNotificationsFunction($moduleDirname);
162
163
        $this->create($moduleDirname, 'include', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
164
165
        return $this->renderFile();
166
    }
167
}
168