IncludeNotifications::write()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php namespace XoopsModules\Tdmcreate\Files\Includes;
2
3
use XoopsModules\Tdmcreate;
4
use XoopsModules\Tdmcreate\Files;
5
6
/*
7
 You may not change or alter any portion of this comment or credits
8
 of supporting developers from this source code or any supporting source code
9
 which is considered copyrighted (c) material of the original comment or credit authors.
10
11
 This program is distributed in the hope that it will be useful,
12
 but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 */
15
/**
16
 * tdmcreate module.
17
 *
18
 * @copyright       XOOPS Project (https://xoops.org)
19
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
20
 *
21
 * @since           2.5.0
22
 *
23
 * @author          Txmod Xoops http://www.txmodxoops.org
24
 *
25
 * @version         $Id: IncludeNotifications.php 12258 2014-01-02 09:33:29Z timgno $
26
 */
27
28
/**
29
 * Class IncludeNotifications.
30
 */
31
class IncludeNotifications extends Files\CreateFile
32
{
33
    /**
34
     *  @public function constructor
35
     *  @param null
36
     */
37
    public function __construct()
38
    {
39
        parent::__construct();
40
    }
41
42
    /**
43
     *  @static function getInstance
44
     *  @param null
45
     * @return IncludeNotifications
46
     */
47
    public static function getInstance()
48
    {
49
        static $instance = false;
50
        if (!$instance) {
51
            $instance = new self();
52
        }
53
54
        return $instance;
55
    }
56
57
    /**
58
     *  @public function write
59
     *  @param string $module
60
     *  @param mixed $table
61
     *  @param string $filename
62
     */
63
    public function write($module, $table, $filename)
64
    {
65
        $this->setModule($module);
66
        $this->setTable($table);
67
        $this->setFileName($filename);
68
    }
69
70
    /**
71
     *  @static function getNotificationsFunction
72
     *  @param string $moduleDirname
73
     *
74
     * @return string
75
     */
76
    public function getNotificationsFunction($moduleDirname)
77
    {
78
        $stuModuleDirname = mb_strtoupper($moduleDirname);
79
        $table = $this->getTable();
80
        $tableName = $table->getVar('table_name');
81
        $tableFieldname = $table->getVar('table_fieldname');
0 ignored issues
show
Unused Code introduced by
The assignment to $tableFieldname is dead and can be removed.
Loading history...
82
        $tableSoleName = $table->getVar('table_solename');
83
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
84
        $fieldParent = 'cid';
85
        foreach (array_keys($fields) as $f) {
86
            $fieldName = $fields[$f]->getVar('field_name');
87
            if ((0 == $f) && (1 == $table->getVar('table_autoincrement'))) {
88
                $fieldId = $fieldName;
89
            }
90
            if (1 == $fields[$f]->getVar('field_parent')) {
91
                $fieldParent = $fieldName;
92
            }
93
            if (1 == $fields[$f]->getVar('field_main')) {
94
                $fieldMain = $fieldName;
95
            }
96
        }
97
        if (1 == $table->getVar('table_single')) {
98
            $tableSingle = 'single';
99
        } else {
100
            $tableSingle = $tableName;
101
        }
102
        $ret = <<<EOT
103
\n/**
104
 * comment callback functions
105
 *
106
 * @param \$category
107
 * @param \$item_id
108
 * @return array item|null
109
 */
110
function {$moduleDirname}_notify_iteminfo(\$category, \$item_id)
111
{
112
    global \$xoopsModule, \$xoopsModuleConfig, \$xoopsDB;
113
    //
114
    if (empty(\$xoopsModule) || \$xoopsModule->getVar('dirname') != '{$moduleDirname}')
115
    {
116
        \$moduleHandler = xoops_getHandler('module');
117
        \$module = \$moduleHandler->getByDirname('{$moduleDirname}');
118
        \$configHandler = xoops_getHandler('config');
119
        \$config =& \$configHandler->getConfigsByCat(0, \$module->getVar('mid'));
120
    } else {
121
        \$module =& \$xoopsModule;
122
        \$config =& \$xoopsModuleConfig;
123
    }
124
    //
125
    switch(\$category) {
126
        case 'global':
127
            \$item['name'] = '';
128
            \$item['url'] = '';
129
            return \$item;
130
        break;
131
        case 'category':
132
            // Assume we have a valid category id
133
            \$sql = 'SELECT {$fieldMain} FROM ' . \$xoopsDB->prefix('{$moduleDirname}_{$tableName}') . ' WHERE {$fieldId} = '. \$item_id;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fieldId does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $fieldMain does not seem to be defined for all execution paths leading up to this point.
Loading history...
134
            \$result = \$xoopsDB->query(\$sql); // TODO: error check
135
            \$result_array = \$xoopsDB->fetchArray(\$result);
136
            \$item['name'] = \$result_array['{$fieldMain}'];
137
            \$item['url'] = {$stuModuleDirname}_URL . '/{$tableName}.php?{$fieldId}=' . \$item_id;
138
            return \$item;
139
        break;
140
        case '{$tableSoleName}':
141
            // Assume we have a valid link id
142
            \$sql = 'SELECT {$fieldId}, {$fieldMain} FROM '.\$xoopsDB->prefix('{$moduleDirname}_{$tableName}') . ' WHERE {$fieldId} = ' . \$item_id;
143
            \$result = \$xoopsDB->query(\$sql); // TODO: error check
144
            \$result_array = \$xoopsDB->fetchArray(\$result);
145
            \$item['name'] = \$result_array['{$fieldMain}'];\n
146
EOT;
147
        if ($fieldParent) {
148
            $ret .= <<<EOT
149
			\$item['url'] = {$stuModuleDirname}_URL . '/{$tableSingle}.php?{$fieldParent}=' . \$result_array['{$fieldParent}'] . '&amp;{$fieldId}=' . \$item_id;\n
150
EOT;
151
        } else {
152
            $ret .= <<<EOT
153
			\$item['url'] = {$stuModuleDirname}_URL . '/{$tableSingle}.php?{$fieldId}=' . \$item_id;\n
154
EOT;
155
        }
156
        $ret .= <<<'EOT'
157
			return $item;
158
        break;
159
    }
160
    return null;
161
}
162
EOT;
163
164
        return $ret;
165
    }
166
167
    /**
168
     *  @public function render
169
     *  @param null
170
     * @return bool|string
171
     */
172
    public function render()
173
    {
174
        $module = $this->getModule();
175
        $filename = $this->getFileName();
176
        $moduleDirname = $module->getVar('mod_dirname');
177
        $content = $this->getHeaderFilesComments($module, $filename);
178
        $content .= $this->getNotificationsFunction($moduleDirname);
179
180
        $this->create($moduleDirname, 'include', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
181
182
        return $this->renderFile();
183
    }
184
}
185