Passed
Branch timgno (61fada)
by Gino
05:14
created

UserPdf::getUserPdfTcpdf()   C

Complexity

Conditions 10
Paths 8

Size

Total Lines 78
Code Lines 72

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 78
rs 5.6321
cc 10
eloc 72
nc 8
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 24.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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: UserPdf.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
defined('XOOPS_ROOT_PATH') || die('Restricted access');
25
26
/**
27
 * Class UserPdf.
28
 */
29
class UserPdf 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...
30
{
31
    /*
32
    * @var mixed
33
    */
34
    private $usercode = null;
35
36
    /*
37
    * @var mixed
38
    */
39
    private $phpcode = null;
40
41
    /*
42
    * @var string
43
    */
44
    private $xoopscode;
45
46
    /*
47
    *  @public function constructor
48
    *  @param null
49
    */
50
    /**
51
     *
52
     */
53
    public function __construct()
54
    {
55
        parent::__construct();
56
        $this->xoopscode = TDMCreateXoopsCode::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like \TDMCreateXoopsCode::getInstance() of type object<TDMCreateXoopsCode> is incompatible with the declared type string of property $xoopscode.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
        $this->phpcode = TDMCreatePhpCode::getInstance();
58
        $this->usercode = UserXoopsCode::getInstance();
59
    }
60
61
    /*
62
    *  @static function &getInstance
63
    *  @param null
64
    */
65
    /**
66
     * @return UserPdf
67
     */
68
    public static function &getInstance()
69
    {
70
        static $instance = false;
71
        if (!$instance) {
72
            $instance = new self();
73
        }
74
75
        return $instance;
76
    }
77
78
    /*
79
    *  @public function write
80
    *  @param string $module
81
    *  @param mixed $table
82
    *  @param string $filename
83
    */
84
    /**
85
     * @param $module
86
     * @param $table
87
     * @param $filename
88
     */
89
    public function write($module, $table, $filename)
90
    {
91
        $this->setModule($module);
92
        $this->setTable($table);
93
        $this->setFileName($filename);
94
    }
95
96
    /**
97
     * @private function getUserPagesHeader
98
     *
99
     * @param $moduleDirname
100
     * @param $tableName
101
     *
102
     * @return string
103
     */
104
    private function getUserPdfHeader($moduleDirname, $tableName, $fields, $language)
105
    {
106
        $fieldId = $this->xoopscode->getXoopsCodeSaveFieldId($fields);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeSaveFieldId cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
107
        $ret = $this->getInclude();
108
        $fileExist = $this->phpcode->getPhpCodeFileExists("\$tcpdf = XOOPS_ROOT_PATH.'/Frameworks/tcpdf/tcpdf.php'");
109
        $requireOnce = $this->phpcode->getPhpCodeIncludeDir('$tcpdf', '', true, true, 'require');
110
        $redirectHeader = $this->xoopscode->getXoopsCodeRedirectHeader($tableName, "?{$fieldId}=\${$fieldId}", $numb = '2', "{$language}NO_PDF_LIBRARY");
0 ignored issues
show
Bug introduced by
The method getXoopsCodeRedirectHeader cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
111
        $ret .= $this->phpcode->getPhpCodeConditions($fileExist, '', '', $requireOnce, $redirectHeader, $t = '');
112
        $ret .= $this->getCommentLine('Get Instance of Handler');
113
        $ret .= $this->xoopscode->getXoopsHandlerLine($moduleDirname, $tableName);
0 ignored issues
show
Bug introduced by
The method getXoopsHandlerLine cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
114
        $ret .= $this->xoopscode->getXoopsCodeGet($tableName, "\$this->getVar('{$fieldId}')", '', true);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeGet cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
115
116
        return $ret;
117
    }
118
119
    /*
120
    *  @public function getAdminPagesList
121
    *  @param string $tableName
122
    *  @param string $language
123
    */
124
    /**
125
     * @param $module
126
     * @param $tableName
127
     * @param $language
128
     *
129
     * @return string
130
     */
131
    public function getUserPdfTcpdf($moduleDirname, $fields)
132
    {
133
        $fieldId = $this->xoopscode->getXoopsCodeSaveFieldId($fields);
0 ignored issues
show
Unused Code introduced by
$fieldId is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Bug introduced by
The method getXoopsCodeSaveFieldId cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
134
        $stuModuleDirname = strtoupper($moduleDirname);
135
        $ret = '';
136
        foreach (array_keys($fields) as $f) {
137
            $fieldName = $fields[$f]->getVar('field_name');
138
            $fieldDefault = $fields[$f]->getVar('field_default');
139
            $fieldElement = $fields[$f]->getVar('field_element');
140
            $getVar = $this->xoopscode->getXoopsCodeGetVar('', 'pdfContent', $fieldName, true);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeGetVar cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
141
            switch ($fieldElement) {
142
                case 2:
143
                    if (strstr($fieldName, 'title') || strstr($fieldName, 'name') && $fieldDefault == '') {
144
                        $ret .= $this->phpcode->getPhpCodeStripTags("pdfData['title']", $getVar);
145
                    }
146
                break;
147
                case 3:
148
                case 4:
149
                    $ret .= $this->phpcode->getPhpCodeStripTags("pdfData['content']", $getVar);
150
                break;
151
                case 8:
152
                    $ret .= $this->xoopscode->getXoopsCodeUnameFromId("pdfData['author']", $getVar);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeUnameFromId cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
153
                break;
154
                case 15:
155
                    $ret .= $this->xoopscode->getXoopsCodeFormatTimeStamp("pdfData['date']", $getVar);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeFormatTimeStamp cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
156
                break;
157
            }
158
        }
159
        $ret .= $this->getCommentLine('Get Config');
160
        $ret .= $this->xoopscode->getXoopsCodeEqualsOperator("\$pdfData['creator'] ", "\$GLOBALS['xoopsConfig']['xoops_sitename']");
0 ignored issues
show
Bug introduced by
The method getXoopsCodeEqualsOperator cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
161
        $ret .= $this->xoopscode->getXoopsCodeEqualsOperator("\$pdfData['subject'] ", "\$GLOBALS['xoopsConfig']['slogan']");
0 ignored issues
show
Bug introduced by
The method getXoopsCodeEqualsOperator cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
162
        $ret .= $this->xoopscode->getXoopsCodeEqualsOperator("\$pdfData['keywords'] ", "\$GLOBALS['xoopsConfig']['keywords']");
0 ignored issues
show
Bug introduced by
The method getXoopsCodeEqualsOperator cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
163
        $ret .= $this->getCommentLine('Defines');
164
        $ret .= $this->phpcode->getPhpCodeDefine("{$stuModuleDirname}_CREATOR", "\$pdfData['creator']");
165
        $ret .= $this->phpcode->getPhpCodeDefine("{$stuModuleDirname}_AUTHOR", "\$pdfData['author']");
166
        $ret .= $this->phpcode->getPhpCodeDefine("{$stuModuleDirname}_HEADER_TITLE", "\$pdfData['title']");
167
        $ret .= $this->phpcode->getPhpCodeDefine("{$stuModuleDirname}_HEADER_STRING", "\$pdfData['subject']");
168
        $ret .= $this->phpcode->getPhpCodeDefine("{$stuModuleDirname}_HEADER_LOGO", "'logo.gif'");
169
        $ret .= $this->phpcode->getPhpCodeDefine("{$stuModuleDirname}_IMAGES_PATH", "XOOPS_ROOT_PATH.'/images/'");
170
        $ret .= $this->xoopscode->getXoopsCodeEqualsOperator('$myts ', 'MyTextSanitizer::getInstance()', true);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeEqualsOperator cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
171
        $ret .= $this->xoopscode->getXoopsCodeEqualsOperator('$content ', "''");
0 ignored issues
show
Bug introduced by
The method getXoopsCodeEqualsOperator cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
172
        $ret .= $this->xoopscode->getXoopsCodeEqualsOperator('$content .', "\$myts->undoHtmlSpecialChars(\$pdfData['content'])");
0 ignored issues
show
Bug introduced by
The method getXoopsCodeEqualsOperator cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
173
        $ret .= $this->xoopscode->getXoopsCodeEqualsOperator('$content ', '$myts->displayTarea($content)');
0 ignored issues
show
Bug introduced by
The method getXoopsCodeEqualsOperator cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
174
        $ret .= $this->xoopscode->getXoopsCodeEqualsOperator('$pdf ', 'new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, _CHARSET, false)');
0 ignored issues
show
Bug introduced by
The method getXoopsCodeEqualsOperator cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
175
        $ret .= $this->xoopscode->getXoopsCodeEqualsOperator('$title ', "\$myts->undoHtmlSpecialChars(\$pdfData['title'])");
0 ignored issues
show
Bug introduced by
The method getXoopsCodeEqualsOperator cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
176
        $ret .= $this->xoopscode->getXoopsCodeEqualsOperator('$keywords ', "\$myts->undoHtmlSpecialChars(\$pdfData['keywords'])");
0 ignored issues
show
Bug introduced by
The method getXoopsCodeEqualsOperator cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
177
        $ret .= $this->xoopscode->getXoopsCodeEqualsOperator("\$pdfData['fontsize'] ", '12');
0 ignored issues
show
Bug introduced by
The method getXoopsCodeEqualsOperator cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
178
        $ret .= $this->getCommentLine('For schinese');
179
        $ifLang = $this->getSimpleString("\$pdf->SetFont('gbsn00lp', '', \$pdfData['fontsize']);");
180
        $elseLang = $this->getSimpleString("\$pdf->SetFont(\$pdfData['fontname'], '', \$pdfData['fontsize']);");
181
        $ret .= $this->phpcode->getPhpCodeConditions('_LANGCODE', ' == ', "'cn'", $ifLang, $elseLang);
182
        $ret .= $this->getCommentLine('Set document information');
183
        $ret .= $this->getSimpleString("\$pdf->SetCreator(\$pdfData['creator']);");
184
        $ret .= $this->getSimpleString("\$pdf->SetAuthor(\$pdfData['author']);");
185
        $ret .= $this->getSimpleString('$pdf->SetTitle($title);');
186
        $ret .= $this->getSimpleString('$pdf->SetKeywords($keywords);');
187
        $ret .= $this->getCommentLine('Set default header data');
188
        $ret .= $this->getSimpleString("\$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, {$stuModuleDirname}_HEADER_TITLE, {$stuModuleDirname}_HEADER_STRING);");
189
        $ret .= $this->getCommentLine('Set margins');
190
        $ret .= $this->getSimpleString('$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP + 10, PDF_MARGIN_RIGHT);');
191
        $ret .= $this->getCommentLine('Set auto page breaks');
192
        $ret .= $this->getSimpleString('$pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM);');
193
        $ret .= $this->getSimpleString('$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);');
194
        $ret .= $this->getSimpleString('$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);');
195
        $ret .= $this->getSimpleString('$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); //set image scale factor');
196
        $ifLang = $this->getSimpleString("\$pdf->setHeaderFont(array('gbsn00lp', '', \$pdfData['fontsize']));");
197
        $ifLang .= $this->getSimpleString("\$pdf->setFooterFont(array('gbsn00lp', '', \$pdfData['fontsize']));");
198
        $elseLang = $this->getSimpleString("\$pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));");
199
        $elseLang .= $this->getSimpleString("\$pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));");
200
        $ret .= $this->phpcode->getPhpCodeConditions('_LANGCODE', ' == ', "'cn'", $ifLang, $elseLang);
201
        $ret .= $this->getCommentLine('Set some language-dependent strings (optional)');
202
        $fileExist = $this->phpcode->getPhpCodeFileExists("\$lang = XOOPS_ROOT_PATH.'/Frameworks/tcpdf/lang/eng.php')");
203
        $contIf = $this->phpcode->getPhpCodeIncludeDir('$lang', '', true, false, 'require');
204
        $contIf .= $this->getSimpleString('$pdf->setLanguageArray($l);');
205
        $ret .= $this->phpcode->getPhpCodeConditions("@{$fileExist}", '', '', $contIf);
206
207
        return $ret;
208
    }
209
210
    /**
211
     * @private function getUserPdfFooter
212
     *
213
     * @param $moduleDirname
214
     * @param $tableName
215
     *
216
     * @return string
217
     */
218
    private function getUserPdfFooter($moduleDirname, $tableName)
219
    {
220
        $ret = $this->getCommentLine('Initialize document');
221
        $ret .= $this->getSimpleString('$pdf->AliasNbPages();');
222
        $ret .= $this->getCommentLine('Add Page document');
223
        $ret .= $this->getSimpleString('$pdf->AddPage();');
224
        $ret .= $this->getSimpleString("\$pdf->writeHTMLCell(\$w=0, \$h=0, \$x='', \$y='', \$content, \$border=0, \$ln=1, \$fill=0, \$reseth=true, \$align='', \$autopadding=true);");
225
        $ret .= $this->getCommentLine('Pdf Filename');
226
        $ret .= $this->getCommentLine('Output');
227
        $ret .= $this->xoopscode->getXoopsCodeTplAssign('pdfoutput', "\$pdf->Output('{$tableName}.pdf', 'I')");
0 ignored issues
show
Bug introduced by
The method getXoopsCodeTplAssign cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
228
        $ret .= $this->xoopscode->getXoopsCodeTplDisplay("{$moduleDirname}_pdf.tpl");
0 ignored issues
show
Bug introduced by
The method getXoopsCodeTplDisplay cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
229
230
        return $ret;
231
    }
232
233
    /*
234
    *  @public function render
235
    *  @param null
236
    */
237
    /**
238
     * @return bool|string
239
     */
240
    public function render()
241
    {
242
        $module = $this->getModule();
243
        $table = $this->getTable();
244
        $filename = $this->getFileName();
245
        $moduleDirname = $module->getVar('mod_dirname');
246
        $tableId = $table->getVar('table_id');
247
        $tableMid = $table->getVar('table_mid');
248
        $tableName = $table->getVar('table_name');
249
        $fields = $this->getTableFields($tableMid, $tableId);
250
        $language = $this->getLanguage($moduleDirname, 'MA');
251
        $content = $this->getHeaderFilesComments($module, $filename);
252
        $content .= $this->getUserPdfHeader($moduleDirname, $tableName, $fields, $language);
253
        $content .= $this->getUserPdfTcpdf($moduleDirname, $fields);
254
        $content .= $this->getUserPdfFooter($moduleDirname, $tableName);
255
256
        $this->create($moduleDirname, '/', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
257
258
        return $this->renderFile();
259
    }
260
}
261