UserPdf   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 127
dl 0
loc 220
rs 10
c 0
b 0
f 0
wmc 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 5 1
A getUserPdfHeader() 0 19 1
A __construct() 0 6 1
A getInstance() 0 8 2
A render() 0 19 1
C getUserPdfTcpdf() 0 78 10
A getUserPdfFooter() 0 13 1
1
<?php
2
3
namespace XoopsModules\Tdmcreate\Files\User;
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 UserPdf.
31
 */
32
class UserPdf extends Files\CreateFile
33
{
34
    /**
35
     * @var mixed
36
     */
37
    private $uxc = null;
38
39
    /**
40
     * @var string
41
     */
42
    private $xc = null;
43
44
    /**
45
     * @var string
46
     */
47
    private $pc = null;
48
49
    /**
50
     * @public function constructor
51
     * @param null
52
     */
53
    public function __construct()
54
    {
55
        parent::__construct();
56
        $this->xc  = Tdmcreate\Files\CreateXoopsCode::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like XoopsModules\Tdmcreate\F...oopsCode::getInstance() of type XoopsModules\Tdmcreate\Files\CreateXoopsCode is incompatible with the declared type string of property $xc.

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->pc  = Tdmcreate\Files\CreatePhpCode::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like XoopsModules\Tdmcreate\F...ePhpCode::getInstance() of type XoopsModules\Tdmcreate\Files\CreatePhpCode is incompatible with the declared type string of property $pc.

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...
58
        $this->uxc = UserXoopsCode::getInstance();
59
    }
60
61
    /**
62
     * @static function getInstance
63
     * @param null
64
     * @return UserPdf
65
     */
66
    public static function getInstance()
67
    {
68
        static $instance = false;
69
        if (!$instance) {
70
            $instance = new self();
71
        }
72
73
        return $instance;
74
    }
75
76
    /**
77
     * @public function write
78
     * @param string $module
79
     * @param mixed  $table
80
     * @param string $filename
81
     */
82
    public function write($module, $table, $filename)
83
    {
84
        $this->setModule($module);
85
        $this->setTable($table);
86
        $this->setFileName($filename);
87
    }
88
89
    /**
90
     * @private function getUserPagesHeader
91
     * @param $moduleDirname
92
     * @param $tableName
93
     * @param $fields
94
     * @param $language
95
     * @return string
96
     */
97
    private function getUserPdfHeader($moduleDirname, $tableName, $fields, $language)
98
    {
99
        $fieldId        = $this->xc->getXcSaveFieldId($fields);
100
        $ccFieldId      = $this->getCamelCase($fieldId, false, true);
101
        $ret            = $this->pc->getPhpCodeUseNamespace(['Xmf', 'Request'], '', '');
102
        $ret            .= $this->pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname], '', '');
103
        $ret            .= $this->pc->getPhpCodeUseNamespace(['XoopsModules', $moduleDirname, 'Constants']);
104
        $ret            .= $this->getInclude();
105
        $fileExist      = $this->pc->getPhpCodeFileExists("\$tcpdf = XOOPS_ROOT_PATH.'/Frameworks/tcpdf/tcpdf.php'");
106
        $requireOnce    = $this->pc->getPhpCodeIncludeDir('$tcpdf', '', true, true, 'require', "\t");
107
        $ret            .= $this->xc->getXcXoopsRequest($ccFieldId, $fieldId, '', 'Int');
108
        $redirectHeader = $this->xc->getXcRedirectHeader($tableName, '', $numb = '2', "{$language}NO_PDF_LIBRARY", true, "\t");
109
        $ret            .= $this->pc->getPhpCodeConditions($fileExist, '', '', $requireOnce, $redirectHeader);
110
        $ret            .= $this->pc->getPhpCodeCommentLine('Get Instance of Handler');
111
        $ret            .= $this->xc->getXcHandlerLine($tableName);
112
        //$ret            .= $this->xc->getXcGetVar($tableName, 'this', $fieldId, false, '');
113
        $ret            .= $this->pc->getPhpCodeBlankLine();
114
115
        return $ret;
116
    }
117
118
    /**
119
     * @public function getAdminPagesList
120
     * @param $moduleDirname
121
     * @param $tableName
122
     * @param $fields
123
     * @return string
124
     */
125
    public function getUserPdfTcpdf($moduleDirname, $tableName, $fields)
126
    {
127
        $stuModuleDirname = mb_strtoupper($moduleDirname);
128
        $ret              = '';
129
        foreach (array_keys($fields) as $f) {
130
            $fieldName    = $fields[$f]->getVar('field_name');
131
            $fieldDefault = $fields[$f]->getVar('field_default');
132
            $fieldElement = $fields[$f]->getVar('field_element');
133
            $getVar       = $this->xc->getXcGetVar('', $tableName . 'Handler', $fieldName, true);
134
            switch ($fieldElement) {
135
                case 2:
136
                    if (false !== mb_strpos($fieldName, 'title') || false !== mb_strpos($fieldName, 'name') && '' == $fieldDefault) {
137
                        $ret .= $this->pc->getPhpCodeStripTags("pdfData['title']  ", $getVar);
138
                    }
139
                    break;
140
                case 3:
141
                case 4:
142
                    $ret .= $this->pc->getPhpCodeStripTags("pdfData['content']", $getVar);
143
                    break;
144
                case 8:
145
                    $ret .= $this->xc->getXcXoopsUserUnameFromId("pdfData['author'] ", $getVar);
146
                    break;
147
                case 15:
148
                    $ret .= $this->xc->getXcFormatTimeStamp("pdfData['date']   ", $getVar);
149
                    break;
150
            }
151
        }
152
        $ret       .= $this->pc->getPhpCodeBlankLine();
153
        $ret       .= $this->pc->getPhpCodeCommentLine('Get Config');
154
        $ret       .= $this->xc->getXcEqualsOperator("\$pdfData['creator']  ", "\$GLOBALS['xoopsConfig']['xoops_sitename']");
155
        $ret       .= $this->xc->getXcEqualsOperator("\$pdfData['subject']  ", "\$GLOBALS['xoopsConfig']['slogan']");
156
        $ret       .= $this->xc->getXcEqualsOperator("\$pdfData['keywords'] ", "\$GLOBALS['xoopsConfig']['keywords']");
157
        $ret       .= $this->pc->getPhpCodeCommentLine('Defines');
158
        $ret       .= $this->pc->getPhpCodeDefine("{$stuModuleDirname}_CREATOR", "\$pdfData['creator']");
159
        $ret       .= $this->pc->getPhpCodeDefine("{$stuModuleDirname}_AUTHOR", "\$pdfData['author']");
160
        $ret       .= $this->pc->getPhpCodeDefine("{$stuModuleDirname}_HEADER_TITLE", "\$pdfData['title']");
161
        $ret       .= $this->pc->getPhpCodeDefine("{$stuModuleDirname}_HEADER_STRING", "\$pdfData['subject']");
162
        $ret       .= $this->pc->getPhpCodeDefine("{$stuModuleDirname}_HEADER_LOGO", "'logo.gif'");
163
        $ret       .= $this->pc->getPhpCodeDefine("{$stuModuleDirname}_IMAGES_PATH", "XOOPS_ROOT_PATH.'/images/'");
164
        $ret       .= $this->xc->getXcEqualsOperator('$myts', 'MyTextSanitizer::getInstance()');
165
        $ret       .= $this->xc->getXcEqualsOperator('$content', "''");
166
        $ret       .= $this->xc->getXcEqualsOperator('$content', "\$myts->undoHtmlSpecialChars(\$pdfData['content'])", '.');
167
        $ret       .= $this->xc->getXcEqualsOperator('$content', '$myts->displayTarea($content)');
168
        $ret       .= $this->xc->getXcEqualsOperator('$pdf', 'new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, _CHARSET, false)');
169
        $ret       .= $this->xc->getXcEqualsOperator('$title', "\$myts->undoHtmlSpecialChars(\$pdfData['title'])");
170
        $ret       .= $this->xc->getXcEqualsOperator('$keywords', "\$myts->undoHtmlSpecialChars(\$pdfData['keywords'])");
171
        $ret       .= $this->xc->getXcEqualsOperator("\$pdfData['fontsize']", '12');
172
        $ret       .= $this->pc->getPhpCodeCommentLine('For schinese');
173
        $ifLang    = $this->getSimpleString("\$pdf->SetFont('gbsn00lp', '', \$pdfData['fontsize']);", "\t");
174
        $elseLang  = $this->getSimpleString("\$pdf->SetFont(\$pdfData['fontname'], '', \$pdfData['fontsize']);", "\t");
175
        $ret       .= $this->pc->getPhpCodeConditions('_LANGCODE', ' == ', "'cn'", $ifLang, $elseLang);
176
        $ret       .= $this->pc->getPhpCodeCommentLine('Set document information');
177
        $ret       .= $this->getSimpleString("\$pdf->SetCreator(\$pdfData['creator']);");
178
        $ret       .= $this->getSimpleString("\$pdf->SetAuthor(\$pdfData['author']);");
179
        $ret       .= $this->getSimpleString('$pdf->SetTitle($title);');
180
        $ret       .= $this->getSimpleString('$pdf->SetKeywords($keywords);');
181
        $ret       .= $this->pc->getPhpCodeCommentLine('Set default header data');
182
        $ret       .= $this->getSimpleString("\$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, {$stuModuleDirname}_HEADER_TITLE, {$stuModuleDirname}_HEADER_STRING);");
183
        $ret       .= $this->pc->getPhpCodeCommentLine('Set margins');
184
        $ret       .= $this->getSimpleString('$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP + 10, PDF_MARGIN_RIGHT);');
185
        $ret       .= $this->pc->getPhpCodeCommentLine('Set auto page breaks');
186
        $ret       .= $this->getSimpleString('$pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM);');
187
        $ret       .= $this->getSimpleString('$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);');
188
        $ret       .= $this->getSimpleString('$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);');
189
        $ret       .= $this->getSimpleString('$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); //set image scale factor');
190
        $ifLang    = $this->getSimpleString("\$pdf->setHeaderFont(array('gbsn00lp', '', \$pdfData['fontsize']));", "\t");
191
        $ifLang    .= $this->getSimpleString("\$pdf->setFooterFont(array('gbsn00lp', '', \$pdfData['fontsize']));", "\t");
192
        $elseLang  = $this->getSimpleString("\$pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));", "\t");
193
        $elseLang  .= $this->getSimpleString("\$pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));", "\t");
194
        $ret       .= $this->pc->getPhpCodeConditions('_LANGCODE', ' == ', "'cn'", $ifLang, $elseLang);
195
        $ret       .= $this->pc->getPhpCodeCommentLine('Set some language-dependent strings (optional)');
196
        $ret       .= $this->xc->getXcEqualsOperator('$lang', "XOOPS_ROOT_PATH.'/Frameworks/tcpdf/lang/eng.php'");
197
        $fileExist = $this->pc->getPhpCodeFileExists('$lang');
198
        $contIf    = $this->pc->getPhpCodeIncludeDir('$lang', '', true, true, 'require', "\t");
199
        $contIf    .= $this->getSimpleString('$pdf->setLanguageArray($lang);', "\t");
200
        $ret       .= $this->pc->getPhpCodeConditions("@{$fileExist}", '', '', $contIf);
201
202
        return $ret;
203
    }
204
205
    /**
206
     * @private function getUserPdfFooter
207
     *
208
     * @param $moduleDirname
209
     * @param $tableName
210
     *
211
     * @return string
212
     */
213
    private function getUserPdfFooter($moduleDirname, $tableName)
214
    {
215
        $ret = $this->pc->getPhpCodeCommentLine('Initialize document');
216
        $ret .= $this->getSimpleString('$pdf->AliasNbPages();');
217
        $ret .= $this->pc->getPhpCodeCommentLine('Add Page document');
218
        $ret .= $this->getSimpleString('$pdf->AddPage();');
219
        $ret .= $this->getSimpleString("\$pdf->writeHTMLCell(\$w=0, \$h=0, \$x='', \$y='', \$content, \$border=0, \$ln=1, \$fill=0, \$reseth=true, \$align='', \$autopadding=true);");
220
        $ret .= $this->pc->getPhpCodeCommentLine('Pdf Filename');
221
        $ret .= $this->pc->getPhpCodeCommentLine('Output');
222
        $ret .= $this->xc->getXcXoopsTplAssign('pdfoutput', "\$pdf->Output('{$tableName}.pdf', 'I')");
223
        $ret .= $this->xc->getXcXoopsTplDisplay($moduleDirname . '_pdf.tpl', '', false);
224
225
        return $ret;
226
    }
227
228
    /**
229
     * @public function render
230
     * @param null
231
     * @return bool|string
232
     */
233
    public function render()
234
    {
235
        $module        = $this->getModule();
236
        $table         = $this->getTable();
237
        $filename      = $this->getFileName();
238
        $moduleDirname = $module->getVar('mod_dirname');
239
        $tableId       = $table->getVar('table_id');
240
        $tableMid      = $table->getVar('table_mid');
241
        $tableName     = $table->getVar('table_name');
242
        $fields        = $this->getTableFields($tableMid, $tableId);
243
        $language      = $this->getLanguage($moduleDirname, 'MA');
244
        $content       = $this->getHeaderFilesComments($module);
245
        $content       .= $this->getUserPdfHeader($moduleDirname, $tableName, $fields, $language);
246
        $content       .= $this->getUserPdfTcpdf($moduleDirname, $tableName, $fields);
247
        $content       .= $this->getUserPdfFooter($moduleDirname, $tableName);
248
249
        $this->create($moduleDirname, '/', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
250
251
        return $this->renderFile();
252
    }
253
}
254