Completed
Pull Request — master (#126)
by Gino
03:40
created

IncludeCommon::getCommonCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 50
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 1
Metric Value
cc 2
eloc 44
nc 2
nop 1
dl 0
loc 50
rs 9.3333
c 5
b 2
f 1
1
<?php
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
/**
13
 * tdmcreate module.
14
 *
15
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 *
18
 * @since           2.5.0
19
 *
20
 * @author          Txmod Xoops http://www.txmodxoops.org
21
 *
22
 * @version         $Id: IncludeCommon.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class IncludeCommon.
27
 */
28
class IncludeCommon extends TDMCreateFile
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
29
{
30
    /*
31
    *  @public function constructor
32
    *  @param null
33
    */
34
    /**
35
     *
36
     */
37
    public function __construct()
38
    {
39
        parent::__construct();
40
    }
41
42
    /*
43
    *  @static function getInstance
44
    *  @param null
45
    */
46
    /**
47
     * @return IncludeCommon
48
     */
49
    public static function getInstance()
50
    {
51
        static $instance = false;
52
        if (!$instance) {
53
            $instance = new self();
54
        }
55
56
        return $instance;
57
    }
58
59
    /*
60
    *  @public function write
61
    *  @param string $module
62
    *  @param object $table
63
    *  @param string $filename
64
    */
65
    /**
66
     * @param $module
67
     * @param $table
68
     * @param $filename
69
     */
70
    public function write($module, $table, $filename)
71
    {
72
        $this->setModule($module);
73
        $this->setTable($table);
74
        $this->setFileName($filename);
75
    }
76
77
    /**
78
     * @param $modDirname
79
     * @param $const
80
     * @param $desc
81
     */
82
    private function getCommonDefines($modDirname, $const, $desc)
83
    {
84
        $stuModDirname = strtoupper($modDirname);
85
86
        return "define('{$stuModDirname}_{$const}', {$desc});\n";
87
    }
88
89
    /*
90
    *  @private function getCommonCode
91
    *  @param object $module
92
    */
93
    /**
94
     * @param $module
95
     *
96
     * @return string
97
     */
98
    private function getCommonCode($module)
99
    {
100
        $pc = TDMCreatePhpCode::getInstance();
101
        $xc = TDMCreateXoopsCode::getInstance();
102
        $table = $this->getTable();
103
        $moduleDirname = $module->getVar('mod_dirname');
104
        $stuModuleDirname = strtoupper($moduleDirname);
105
        $moduleAuthor = $module->getVar('mod_author');
106
        $moduleAuthorWebsiteName = $module->getVar('mod_author_website_name');
107
        $moduleAuthorWebsiteUrl = $module->getVar('mod_author_website_url');
108
        $moduleAuthorImage = str_replace(' ', '', strtolower($moduleAuthor));
109
        $ret = <<<EOT
110
if (!defined('XOOPS_ICONS32_PATH')) define('XOOPS_ICONS32_PATH', XOOPS_ROOT_PATH . '/Frameworks/moduleclasses/icons/32');
111
if (!defined('XOOPS_ICONS32_URL')) define('XOOPS_ICONS32_URL', XOOPS_URL . '/Frameworks/moduleclasses/icons/32');
112
EOT;
113
        $ret .= self::getCommonDefines($moduleDirname, 'DIRNAME', "'{$moduleDirname}'");
114
        $ret .= self::getCommonDefines($moduleDirname, 'PATH', "XOOPS_ROOT_PATH.'/modules/'.{$stuModuleDirname}_DIRNAME");
115
        $ret .= self::getCommonDefines($moduleDirname, 'URL', "XOOPS_URL.'/modules/'.{$stuModuleDirname}_DIRNAME");
116
        $ret .= self::getCommonDefines($moduleDirname, 'ICONS_PATH', "{$stuModuleDirname}_PATH.'/assets/icons'");
117
        $ret .= self::getCommonDefines($moduleDirname, 'ICONS_URL', "{$stuModuleDirname}_URL.'/assets/icons'");
118
        $ret .= self::getCommonDefines($moduleDirname, 'IMAGE_PATH', "{$stuModuleDirname}_PATH.'/assets/images'");
119
        $ret .= self::getCommonDefines($moduleDirname, 'IMAGE_URL', "{$stuModuleDirname}_URL.'/assets/images'");
120
        $ret .= self::getCommonDefines($moduleDirname, 'UPLOAD_PATH', "XOOPS_UPLOAD_PATH.'/'.{$stuModuleDirname}_DIRNAME");
121
        $ret .= self::getCommonDefines($moduleDirname, 'UPLOAD_URL', "XOOPS_UPLOAD_URL.'/'.{$stuModuleDirname}_DIRNAME");
122
123
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
124
        $fieldElement = array();
125
        foreach (array_keys($fields) as $f) {
126
            $fieldElement[] = $fields[$f]->getVar('field_element');
127
        }
128
        $ret .= self::getCommonDefines($moduleDirname, 'UPLOAD_FILES_PATH', "{$stuModuleDirname}_UPLOAD_PATH.'/files'");
129
        $ret .= self::getCommonDefines($moduleDirname, 'UPLOAD_FILES_URL', "{$stuModuleDirname}_UPLOAD_URL.'/files'");
130
        $ret .= self::getCommonDefines($moduleDirname, 'UPLOAD_IMAGE_PATH', "{$stuModuleDirname}_UPLOAD_PATH.'/images'");
131
        $ret .= self::getCommonDefines($moduleDirname, 'UPLOAD_IMAGE_URL', "{$stuModuleDirname}_UPLOAD_URL.'/images'");
132
        $ret .= self::getCommonDefines($moduleDirname, 'UPLOAD_SHOTS_PATH', "{$stuModuleDirname}_UPLOAD_PATH.'/images/shots'");
133
        $ret .= self::getCommonDefines($moduleDirname, 'UPLOAD_SHOTS_URL', "{$stuModuleDirname}_UPLOAD_URL.'/images/shots'");
134
        $ret .= self::getCommonDefines($moduleDirname, 'ADMIN', "{$stuModuleDirname}_URL . '/admin/index.php'");
135
        $ret .= $xc->getXcEqualsOperator('$localLogo', "{$stuModuleDirname}_IMAGE_URL . '/{$moduleAuthorImage}_logo.png'");
136
        $ret .= $pc->getPhpCodeCommentLine('Module Information');
137
        $htmlCode = TDMCreateHtmlCode::getInstance();
138
        $img = $htmlCode->getHtmlImage('".$localLogo."', $moduleAuthorWebsiteName);
139
        $anchor = $htmlCode->getHtmlAnchor($moduleAuthorWebsiteUrl, $img, $moduleAuthorWebsiteName, '_blank');
140
        $replace = $xc->getXcEqualsOperator('$copyright', '"'.$anchor.'"');
141
        $ret .= str_replace("\n", '', $replace).PHP_EOL;
142
        $ret .= $pc->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/xoopsrequest', true);
143
        $ret .= $pc->getPhpCodeIncludeDir("{$stuModuleDirname}_PATH", 'class/helper', true);
144
        $ret .= $pc->getPhpCodeIncludeDir("{$stuModuleDirname}_PATH", 'include/functions', true);
145
146
        return $ret;
147
    }
148
149
    /*
150
    *  @public function render
151
    *  @param null
152
    */
153
    /**
154
     * @return bool|string
155
     */
156
    public function render()
157
    {
158
        $module = $this->getModule();
159
        $moduleDirname = $module->getVar('mod_dirname');
160
        $filename = $this->getFileName();
161
        $content = $this->getHeaderFilesComments($module, $filename);
162
        $content .= $this->getCommonCode($module);
163
        $this->create($moduleDirname, 'include', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
164
165
        return $this->renderFile();
166
    }
167
}
168