Completed
Pull Request — master (#29)
by Goffy
01:40
created

letter.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * ****************************************************************************
4
 *  - A Project by Developers TEAM For Xoops - ( https://xoops.org )
5
 * ****************************************************************************
6
 *  XNEWSLETTER - MODULE FOR XOOPS
7
 *  Copyright (c) 2007 - 2012
8
 *  Goffy ( wedega.com )
9
 *
10
 *  You may not change or alter any portion of this comment or credits
11
 *  of supporting developers from this source code or any supporting
12
 *  source code which is considered copyrighted (c) material of the
13
 *  original comment or credit authors.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *  ---------------------------------------------------------------------------
20
 * @copyright  Goffy ( wedega.com )
21
 * @license    GPL 2.0
22
 * @package    xnewsletter
23
 * @author     Goffy ( [email protected] )
24
 *
25
 * ****************************************************************************
26
 */
27
28
use Xmf\Request;
29
use XoopsModules\Xnewsletter;
30
31
$currentFile = basename(__FILE__);
32
require_once __DIR__ . '/header.php';
33
34
$helper = Xnewsletter\Helper::getInstance();
35
36
$uid    = (is_object($xoopsUser) && isset($xoopsUser)) ? $xoopsUser->uid() : 0;
37
$groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : [0 => XOOPS_GROUP_ANONYMOUS];
38
39
$op        = Request::getString('op', 'list_letters');
40
$letter_id = Request::getInt('letter_id', 0);
41
$cat_id    = Request::getInt('cat_id', 0);
42
43
switch ($op) {
44
    case 'list_subscrs':
45
        $GLOBALS['xoopsOption']['template_main'] = "{$helper->getModule()->dirname()}_letter_list_subscrs.tpl";
46
        require_once XOOPS_ROOT_PATH . '/header.php';
47
48
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
49
        $xoTheme->addMeta('meta', 'keywords', $helper->getConfig('keywords')); // keywords only for index page
50
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
51
        // breadcrumb
52
        $breadcrumb = new Xnewsletter\Breadcrumb();
53
        $breadcrumb->addLink($helper->getModule()->getVar('name'), XNEWSLETTER_URL);
54
        $breadcrumb->addLink(_MD_XNEWSLETTER_LIST_SUBSCR, '');
55
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
56
57
        // check right to edit/delete subscription of other persons
58
        $permissionChangeOthersSubscriptions = false;
59
        foreach ($groups as $group) {
60
            if (in_array($group, $helper->getConfig('xn_groups_change_other')) || XOOPS_GROUP_ADMIN == $group) {
61
                $permissionChangeOthersSubscriptions = true;
62
                break;
63
            }
64
        }
65
        $xoopsTpl->assign('permissionChangeOthersSubscriptions', $permissionChangeOthersSubscriptions);
66
        // get search subscriber form
67
        if ($permissionChangeOthersSubscriptions) {
68
            require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
69
            $form = new \XoopsThemeForm(_AM_XNEWSLETTER_FORMSEARCH_SUBSCR_EXIST, 'form_search', 'subscription.php', 'post', true);
70
            $form->setExtra('enctype="multipart/form-data"');
71
            $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_SUBSCR_EMAIL, 'subscr_email', 60, 255, '', true));
72
            $form->addElement(new \XoopsFormButton('', 'submit', _AM_XNEWSLETTER_SEARCH, 'submit'));
73
            $xoopsTpl->assign('searchSubscriberForm', $form->render());
74
        } else {
75
            $xoopsTpl->assign('searchSubscriberForm', '');
76
        }
77
        // get cat objects
78
        $catCriteria = new \CriteriaCompo();
79
        $catCriteria->setSort('cat_id');
80
        $catCriteria->setOrder('ASC');
81
        $catObjs = $helper->getHandler('Cat')->getAll($catCriteria, null, true, true);
82
        // cats table
83
        foreach ($catObjs as $cat_id => $catObj) {
84
            $permissionShowCats[$cat_id] = $grouppermHandler->checkRight('newsletter_list_cat', $cat_id, $groups, $helper->getModule()->mid());
85
            if (true === $permissionShowCats[$cat_id]) {
86
                $cat_array         = $catObj->toArray();
87
                $catsubscrCriteria = new \CriteriaCompo();
88
                $catsubscrCriteria->add(new \Criteria('catsubscr_catid', $cat_id));
89
                $cat_array['catsubscrCount'] = $helper->getHandler('Catsubscr')->getCount($catsubscrCriteria);
90
                $xoopsTpl->append('cats', $cat_array);
91
            }
92
        }
93
        // get cat_id
94
        $cat_id = Request::getInt('cat_id', 0);
95
        $xoopsTpl->assign('cat_id', $cat_id);
96
        if ($cat_id > 0) {
97
            $catObj = $helper->getHandler('Cat')->get($cat_id);
98
            $xoopsTpl->assign('cat_name', $catObj->getVar('cat_name'));
99
            // subscrs table
100
            if (true === $permissionShowCats[$cat_id]) {
101
                $counter = 0;
102
                $sql     = 'SELECT `subscr_sex`, `subscr_lastname`, `subscr_firstname`, `subscr_email`, `subscr_id`';
103
                $sql     .= " FROM {$xoopsDB->prefix('xnewsletter_subscr')} INNER JOIN {$xoopsDB->prefix('xnewsletter_catsubscr')} ON `subscr_id` = `catsubscr_subscrid`";
104
                $sql     .= " WHERE (((`catsubscr_catid`)={$cat_id}) AND ((`catsubscr_quited`)=0)) ORDER BY `subscr_lastname`, `subscr_email`;";
105
                if (!$subscrs = $xoopsDB->query($sql)) {
106
                    die('MySQL-Error: ' . $GLOBALS['xoopsDB']->error());
107
                }
108
                while (null !== ($subscr_array = mysqli_fetch_assoc($subscrs))) {
109
                    $subscr_array['counter'] = ++$counter;
110
                    $xoopsTpl->append('subscrs', $subscr_array);
111
                }
112
            }
113
        }
114
        break;
115
    case 'show_preview':
116
    case 'show_letter_preview':
117
        $GLOBALS['xoopsOption']['template_main'] = "{$helper->getModule()->dirname()}_letter_preview.tpl";
118
        require_once XOOPS_ROOT_PATH . '/header.php';
119
120
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
121
        $xoTheme->addMeta('meta', 'keywords', $helper->getConfig('keywords')); // keywords only for index page
122
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
123
        // breadcrumb
124
        $breadcrumb = new Xnewsletter\Breadcrumb();
125
        $breadcrumb->addLink($helper->getModule()->getVar('name'), XNEWSLETTER_URL);
126
        $breadcrumb->addLink(_MD_XNEWSLETTER_LIST, 'javascript:history.go(-1)');
127
        $breadcrumb->addLink(_MD_XNEWSLETTER_LETTER_PREVIEW, '');
128
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
129
130
        // get letter_id
131
        $letter_id = Request::getInt('letter_id', 0);
132
        // get letter object
133
        $letterObj = $helper->getHandler('Letter')->get($letter_id);
134
        // subscr data
135
        $xoopsTpl->assign('sex', _AM_XNEWSLETTER_SUBSCR_SEX_PREVIEW);
136
        $xoopsTpl->assign('salutation', _AM_XNEWSLETTER_SUBSCR_SEX_PREVIEW); // new from v1.3
137
        $xoopsTpl->assign('firstname', _AM_XNEWSLETTER_SUBSCR_FIRSTNAME_PREVIEW);
138
        $xoopsTpl->assign('lastname', _AM_XNEWSLETTER_SUBSCR_LASTNAME_PREVIEW);
139
        $xoopsTpl->assign('subscr_email', _AM_XNEWSLETTER_SUBSCR_EMAIL_PREVIEW);
140
        $xoopsTpl->assign('email', _AM_XNEWSLETTER_SUBSCR_EMAIL_PREVIEW); // new from v1.3
141
        // letter data
142
        $xoopsTpl->assign('title', $letterObj->getVar('letter_title', 'n')); // new from v1.3
143
        $xoopsTpl->assign('content', $letterObj->getVar('letter_content', 'n'));
144
        // letter attachments as link
145
        $attachmentAslinkCriteria = new \CriteriaCompo();
146
        $attachmentAslinkCriteria->add(new \Criteria('attachment_letter_id', $letter_id));
147
        $attachmentAslinkCriteria->add(new \Criteria('attachment_mode', _XNEWSLETTER_ATTACHMENTS_MODE_ASLINK));
148
        $attachmentAslinkCriteria->setSort('attachment_id');
149
        $attachmentAslinkCriteria->setOrder('ASC');
150
        $attachmentObjs = $helper->getHandler('Attachment')->getObjects($attachmentAslinkCriteria, true);
151 View Code Duplication
        foreach ($attachmentObjs as $attachment_id => $attachmentObj) {
152
            $attachment_array                    = $attachmentObj->toArray();
153
            $attachment_array['attachment_url']  = XNEWSLETTER_URL . "/attachment.php?attachment_id={$attachment_id}";
154
            $attachment_array['attachment_link'] = XNEWSLETTER_URL . "/attachment.php?attachment_id={$attachment_id}";
155
            $xoopsTpl->append('attachments', $attachment_array);
156
        }
157
        // extra data
158
        $xoopsTpl->assign('date', time()); // new from v1.3
159
        $xoopsTpl->assign('unsubscribe_url', XOOPS_URL . '/modules/xnewsletter/');
160
        $xoopsTpl->assign('catsubscr_id', '0');
161
162
        $letter_array = $letterObj->toArray();
163
164
        $templateObj = $helper->getHandler('Template')->get($letterObj->getVar('letter_templateid'));
165 View Code Duplication
        if (is_object($templateObj)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
            if ( (int)$templateObj->getVar('template_type') === _XNEWSLETTER_MAILINGLIST_TPL_CUSTOM_VAL) {
167
                // get template from database
168
                $htmlBody = $xoopsTpl->fetchFromData($templateObj->getVar('template_content', 'n'));
169
            } else {
170
                // get template from filesystem
171
                $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/' . $GLOBALS['xoopsConfig']['language'] . '/templates/';
172
                if (!is_dir($template_path)) {
173
                    $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/english/templates/';
174
                }
175
                $template = $template_path . $templateObj->getVar('template_title') . '.tpl';
176
                $htmlBody = $xoopsTpl->fetch($template);
177
            }
178
            try {
179
                $textBody = xnewsletter_html2text($htmlBody);
180
            }
181
            catch (Html2TextException $e) {
182
                $helper->addLog($e);
183
            }
184
        } else {
185
            $htmlBody = _AM_XNEWSLETTER_TEMPLATE_ERR;
186
        }
187
188
        $letter_array['letter_content_templated']      = $htmlBody;
189
        $letter_array['letter_content_templated_html'] = $htmlBody;
190
        $letter_array['letter_content_templated_text'] = $textBody; // new from v1.3
191
        $letter_array['letter_created_formatted']      = formatTimestamp($letterObj->getVar('letter_created'), $helper->getConfig('dateformat'));
192
        $letter_array['letter_submitter_name']         = \XoopsUserUtility::getUnameFromId($letterObj->getVar('letter_submitter'));
193
        $xoopsTpl->assign('letter', $letter_array);
194
        break;
195
    case 'print_letter':
196
        $GLOBALS['xoopsOption']['template_main'] = "{$helper->getModule()->dirname()}_letter_print.tpl";
197
        require_once XOOPS_ROOT_PATH . '/header.php';
198
199
        //$xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
200
        $xoTheme->addMeta('meta', 'keywords', $helper->getConfig('keywords')); // keywords only for index page
201
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
202
203
        // get letter_id
204
        $letter_id = Request::getInt('letter_id', 0);
205
        // get letter object
206
        $letterObj = $helper->getHandler('Letter')->get($letter_id);
207
        // subscr data
208
        $xoopsTpl->assign('sex', _AM_XNEWSLETTER_SUBSCR_SEX_PREVIEW);
209
        $xoopsTpl->assign('salutation', _AM_XNEWSLETTER_SUBSCR_SEX_PREVIEW); // new from v1.3
210
        $xoopsTpl->assign('firstname', _AM_XNEWSLETTER_SUBSCR_FIRSTNAME_PREVIEW);
211
        $xoopsTpl->assign('lastname', _AM_XNEWSLETTER_SUBSCR_LASTNAME_PREVIEW);
212
        $xoopsTpl->assign('subscr_email', _AM_XNEWSLETTER_SUBSCR_EMAIL_PREVIEW);
213
        $xoopsTpl->assign('email', _AM_XNEWSLETTER_SUBSCR_EMAIL_PREVIEW); // new from v1.3
214
        // letter data
215
        $xoopsTpl->assign('title', $letterObj->getVar('letter_title', 'n')); // new from v1.3
216
        $xoopsTpl->assign('content', $letterObj->getVar('letter_content', 'n'));
217
        // extra data
218
        $xoopsTpl->assign('date', time()); // new from v1.3
219
        $xoopsTpl->assign('unsubscribe_url', XOOPS_URL . '/modules/xnewsletter/');
220
        $xoopsTpl->assign('catsubscr_id', '0');
221
222
        $letter_array = $letterObj->toArray();
223
224
        $templateObj = $helper->getHandler('Template')->get($letterObj->getVar('letter_templateid'));
225 View Code Duplication
        if (is_object($templateObj)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
226
            if ( (int)$templateObj->getVar('template_type') === _XNEWSLETTER_MAILINGLIST_TPL_CUSTOM_VAL) {
227
                // get template from database
228
                $htmlBody = $xoopsTpl->fetchFromData($templateObj->getVar('template_content', 'n'));
229
            } else {
230
                // get template from filesystem
231
                $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/' . $GLOBALS['xoopsConfig']['language'] . '/templates/';
232
                if (!is_dir($template_path)) {
233
                    $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/english/templates/';
234
                }
235
                $template = $template_path . $templateObj->getVar('template_title') . '.tpl';
236
                $htmlBody = $xoopsTpl->fetch($template);
237
            }
238
            try {
239
                $textBody = xnewsletter_html2text($htmlBody);
240
            }
241
            catch (Html2TextException $e) {
242
                $helper->addLog($e);
243
            }
244
        } else {
245
            $htmlBody = _AM_XNEWSLETTER_TEMPLATE_ERR;
246
        }
247
248
        $letter_array['letter_content_templated']      = $htmlBody;
249
        $letter_array['letter_content_templated_html'] = $htmlBody;
250
        $letter_array['letter_content_templated_text'] = $textBody; // new from v1.3
251
        $letter_array['letter_created_formatted']      = formatTimestamp($letterObj->getVar('letter_created'), $helper->getConfig('dateformat'));
252
        $letter_array['letter_submitter_name']         = \XoopsUserUtility::getUnameFromId($letterObj->getVar('letter_submitter'));
253
        $xoopsTpl->assign('letter', $letter_array);
254
        break;
255
    case 'list_letters':
256
    default:
257
        $GLOBALS['xoopsOption']['template_main'] = "{$helper->getModule()->dirname()}_letter_list_letters.tpl";
258
        require_once XOOPS_ROOT_PATH . '/header.php';
259
260
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
261
        $xoTheme->addMeta('meta', 'keywords', $helper->getConfig('keywords')); // keywords only for index page
262
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
263
        // breadcrumb
264
        $breadcrumb = new Xnewsletter\Breadcrumb();
265
        $breadcrumb->addLink($helper->getModule()->getVar('name'), XNEWSLETTER_URL);
266
        $breadcrumb->addLink(_MD_XNEWSLETTER_LIST, '');
267
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
268
269
        // get letters array with right to read
270
        $letterCount = $helper->getHandler('Letter')->getCount();
271
        $letterObjs = $helper->getHandler('Letter')->getAll();
272
        $letter_filter = [];
273
        if ($letterCount > 0) {
274
            foreach ($letterObjs as $letter_id => $letterObj) {
275
                $userPermissions = xnewsletter_getUserPermissionsByLetter($letter_id);
276
                if (($userPermissions['read'] && $letterObj->getVar('letter_sent') > 0)
277
                    || (true === $userPermissions['send'])) {
278
                    $letter_cat_ids = explode('|', $letterObj->getVar('letter_cats'));
279
                    // skip letter
280
                    if ((0 != $cat_id) && !in_array($cat_id, $letter_cat_ids)) {
281
                        continue;
282
                    }
283
                    // get categories
284 View Code Duplication
                    foreach ($letter_cat_ids as $letter_cat_id) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
285
                        $catObj = $helper->getHandler('Cat')->get($letter_cat_id);
286
                        if ($grouppermHandler->checkRight('newsletter_read_cat', $catObj->getVar('cat_id'), $groups, $helper->getModule()->mid())) {
287
                            $letter_filter[] = $letterObj->getVar('letter_id');
288
                        }
289
                        unset($catObj);
290
                    }
291
                }
292
            }
293
        }
294
295
        // get letters array
296
        $letterCriteria = new \CriteriaCompo();
297
        $letterCriteria->add(new \Criteria('letter_id', '(' . implode(',', $letter_filter) . ')', 'IN'));
298
        $letterCriteria->setSort('letter_id');
299
        $letterCriteria->setOrder('DESC');
300
        $letterCount = $helper->getHandler('Letter')->getCount($letterCriteria);
301
        $start       = Request::getInt('start', 0);
302
        $limit       = $helper->getConfig('adminperpage');
303
        $letterCriteria->setStart($start);
304
        $letterCriteria->setLimit($limit);
305
        $letterObjs = $helper->getHandler('Letter')->getAll($letterCriteria, null, true, true);
306
307
        // pagenav
308 View Code Duplication
        if ($letterCount > $limit) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
309
            $pagenav = new \XoopsPageNav($letterCount, $limit, $start, 'start', "op={$op}");
310
            $xoopsTpl->assign('pagenav', $pagenav->renderNav());
311
        }
312
313
314
        // letters table
315
        $showAdminColumns = false;
316
        if ($letterCount > 0) {
317
            foreach ($letterObjs as $letter_id => $letterObj) {
318
                $userPermissions = xnewsletter_getUserPermissionsByLetter($letter_id);
319
                $letter_array                             = $letterObj->toArray();
320
                $letter_array['letter_created_formatted'] = formatTimestamp($letterObj->getVar('letter_created'), $helper->getConfig('dateformat'));
321
                $letter_array['letter_submitter_name']    = \XoopsUserUtility::getUnameFromId($letterObj->getVar('letter_submitter'));
322
                $letter_array['letter_sent_formatted']    = 0 != $letterObj->getVar('letter_sent') ? formatTimestamp($letterObj->getVar('letter_sent'), $helper->getConfig('dateformat')) : '';
323
                $letter_array['letter_sender_name']       = \XoopsUserUtility::getUnameFromId($letterObj->getVar('letter_sender'));
324
                
325
                $letter['template_title'] = $helper->getHandler('Template')->get($letterObj->getVar('letter_templateid'))->getVar('template_title');
326
327
                $letter_cat_ids = explode('|', $letterObj->getVar('letter_cats'));
328
                // skip letter
329
                if ((0 != $cat_id) && !in_array($cat_id, $letter_cat_ids)) {
330
                    continue;
331
                }
332
                // get categories
333
                unset($letter_array['letter_cats']); // IN PROGRESS
334 View Code Duplication
                foreach ($letter_cat_ids as $letter_cat_id) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
335
                    $catObj = $helper->getHandler('Cat')->get($letter_cat_id);
336
                    if ($grouppermHandler->checkRight('newsletter_read_cat', $catObj->getVar('cat_id'), $groups, $helper->getModule()->mid())) {
337
                        $letter_array['letter_cats'][] = $catObj->toArray();
338
                    }
339
                    unset($catObj);
340
                }
341
                // count letter attachements
342
                $attachmentCriteria = new \CriteriaCompo();
343
                $attachmentCriteria->add(new \Criteria('attachment_letter_id', $letterObj->getVar('letter_id')));
344
                $letter_array['attachmentCount'] = $helper->getHandler('Attachment')->getCount($attachmentCriteria);
345
                // get protocols
346
                if ($userPermissions['edit']) {
347
                    // take last item protocol_subscriber_id=0 from table protocol as actual status
348
                    $protocolCriteria = new \CriteriaCompo();
349
                    $protocolCriteria->add(new \Criteria('protocol_letter_id', $letterObj->getVar('letter_id')));
350
                    //$criteria->add(new \Criteria('protocol_subscriber_id', '0'));
351
                    $protocolCriteria->setSort('protocol_id');
352
                    $protocolCriteria->setOrder('DESC');
353
                    $protocolCriteria->setLimit(1);
354
                    $protocolObjs       = $helper->getHandler('Protocol')->getAll($protocolCriteria);
355
                    $protocol_status    = '';
356
                    $protocol_letter_id = 0;
357
                    foreach ($protocolObjs as $protocolObj) {
358
                        $letter_array['protocols'][] = [
359
                            'protocol_status'    => $protocolObj->getVar('protocol_status'),
360
                            'protocol_letter_id' => $protocolObj->getVar('protocol_letter_id'),
361
                        ];
362
                    }
363
                }
364
                // check if table show admin columns
365
                if ((true === $userPermissions['edit']) || (true === $userPermissions['delete'])
366
                    || (true === $userPermissions['create'])
367
                    || (true === $userPermissions['send'])) {
368
                    $showAdminColumns = true;
369
                }
370
                $letter_array['userPermissions'] = $userPermissions;
371
                $xoopsTpl->append('letters', $letter_array);
372
            }
373
        }
374
        $xoopsTpl->assign('showAdminColumns', $showAdminColumns);
375
        break;
376 View Code Duplication
    case 'new_letter':
377
        $GLOBALS['xoopsOption']['template_main'] = "{$helper->getModule()->dirname()}_letter.tpl"; // IN PROGRESS
378
        require_once XOOPS_ROOT_PATH . '/header.php';
379
380
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
381
        $xoTheme->addMeta('meta', 'keywords', $helper->getConfig('keywords')); // keywords only for index page
382
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
383
        // breadcrumb
384
        $breadcrumb = new Xnewsletter\Breadcrumb();
385
        $breadcrumb->addLink($helper->getModule()->getVar('name'), XNEWSLETTER_URL);
386
        $breadcrumb->addLink(_MD_XNEWSLETTER_LETTER_CREATE, '');
387
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
388
389
        /** @var Xnewsletter\Letter $letterObj */
390
        $letterObj = $helper->getHandler('Letter')->create();
391
        /** @var \XoopsThemeForm $form */
392
        $form    = $letterObj->getForm();
393
        $content = $form->render();
394
        $xoopsTpl->assign('content', $content);
395
        break;
396 View Code Duplication
    case 'edit_letter':
397
        $GLOBALS['xoopsOption']['template_main'] = "{$helper->getModule()->dirname()}_letter.tpl";
398
        require_once XOOPS_ROOT_PATH . '/header.php';
399
400
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
401
        $xoTheme->addMeta('meta', 'keywords', $helper->getConfig('keywords')); // keywords only for index page
402
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
403
        // breadcrumb
404
        $breadcrumb = new Xnewsletter\Breadcrumb();
405
        $breadcrumb->addLink($helper->getModule()->getVar('name'), XNEWSLETTER_URL);
406
        $breadcrumb->addLink(_MD_XNEWSLETTER_LIST, 'javascript:history.go(-1)');
407
        $breadcrumb->addLink(_MD_XNEWSLETTER_LETTER_EDIT, '');
408
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
409
410
        $letterObj = $helper->getHandler('Letter')->get($letter_id);
411
        $form      = $letterObj->getForm();
412
        $content   = $form->render();
413
        $xoopsTpl->assign('content', $content);
414
        break;
415
    case 'delete_attachment':
416
        $GLOBALS['xoopsOption']['template_main'] = "{$helper->getModule()->dirname()}_letter.tpl";
417
        require_once XOOPS_ROOT_PATH . '/header.php';
418
419
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
420
        $xoTheme->addMeta('meta', 'keywords', $helper->getConfig('keywords')); // keywords only for index page
421
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
422
        // breadcrumb
423
        $breadcrumb = new Xnewsletter\Breadcrumb();
424
        $breadcrumb->addLink($helper->getModule()->getVar('name'), XNEWSLETTER_URL);
425
        $breadcrumb->addLink(_MD_XNEWSLETTER_LIST, 'javascript:history.go(-1)');
426
        $breadcrumb->addLink(_MD_XNEWSLETTER_LETTER_EDIT, '');
427
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
428
429
        // update existing_attachments
430
        $existing_attachments_mode = Request::getArray('existing_attachments_mode', []);
431
        foreach ($existing_attachments_mode as $attachment_id => $attachment_mode) {
432
            $attachmentObj = $helper->getHandler('Attachment')->get($attachment_id);
433
            $attachmentObj->setVar('attachment_mode', $attachment_mode);
434
            $helper->getHandler('Attachment')->insert($attachmentObj);
435
        }
436
437
        $attachment_id = Request::getInt('deleted_attachment_id', 0, 'POST');
438
        if (0 == $attachment_id) {
439
            redirect_header($currentFile, 3, _AM_XNEWSLETTER_LETTER_ERROR_INVALID_ATT_ID);
440
        }
441
        $attachmentObj   = $helper->getHandler('Attachment')->get($attachment_id);
442
        $attachment_name = $attachmentObj->getVar('attachment_name');
443
444
        if ($helper->getHandler('Attachment')->delete($attachmentObj, true)) {
445
            $letterObj = $helper->getHandler('Letter')->get($letter_id);           
446
            $letterObj->setVar('letter_title',      Request::getString('letter_title', ''));
447
            $letterObj->setVar('letter_content',    Request::getText('letter_content', ''));
448
            $letterObj->setVar('letter_templateid', Request::getInt('letter_templateid', 0));
449
            $letterObj->setVar('letter_cats',       implode('|', Request::getArray('letter_cats', [])));
450
            $letterObj->setVar('letter_account',    Request::getInt('letter_account', 0));
451
            $letterObj->setVar('letter_email_test', Request::getString('letter_email_test', ''));
452
453
            $form    = $letterObj->getForm(false, true);
454
            $content = $form->render();
455
            $xoopsTpl->assign('content', $content);
456
        } else {
457
            $content = $attachmentObj->getHtmlErrors();
458
            $xoopsTpl->assign('content', $content);
459
        }
460
        break;
461
    case 'save_letter':
462
        $GLOBALS['xoopsOption']['template_main'] = "{$helper->getModule()->dirname()}_empty.tpl";
463
        require_once XOOPS_ROOT_PATH . '/header.php';
464
465
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
466
        $xoTheme->addMeta('meta', 'keywords', $helper->getConfig('keywords')); // keywords only for index page
467
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
468
        // breadcrumb
469
        $breadcrumb = new Xnewsletter\Breadcrumb();
470
        $breadcrumb->addLink($helper->getModule()->getVar('name'), XNEWSLETTER_URL);
471
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
472
473
        if (!$GLOBALS['xoopsSecurity']->check()) {
474
            redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
475
        }
476
        $letterObj = $helper->getHandler('Letter')->get($letter_id); // create if doesn't exist
477
        $letterObj->setVar('letter_title',      Request::getString('letter_title', ''));
478
        $letterObj->setVar('letter_content',    Request::getText('letter_content', ''));
479
        $letterObj->setVar('letter_templateid', Request::getInt('letter_templateid', 0));
480
        $letterObj->setVar('letter_cats',       implode('|', Request::getArray('letter_cats', [])));
481
        $letterObj->setVar('letter_account',    Request::getInt('letter_account', 0));
482
        $letterObj->setVar('letter_email_test', Request::getString('letter_email_test', ''));
483
        $letterObj->setVar('letter_submitter',  Request::getInt('letter_submitter', 0));
484
        $letterObj->setVar('letter_created',    Request::getInt('letter_created', time()));
485
486 View Code Duplication
        if ($helper->getHandler('Letter')->insert($letterObj)) {
487
            $letter_id = $letterObj->getVar('letter_id');
488
            // update existing_attachments
489
            $existing_attachments_mode = Request::getArray('existing_attachments_mode', []);
490
            foreach ($existing_attachments_mode as $attachment_id => $attachment_mode) {
491
                $attachmentObj = $helper->getHandler('Attachment')->get($attachment_id);
492
                $attachmentObj->setVar('attachment_mode', $attachment_mode);
493
                $helper->getHandler('Attachment')->insert($attachmentObj);
494
            }
495
            // upload attachments
496
            $uploadedFiles = [];
497
            require_once XOOPS_ROOT_PATH . '/class/uploader.php';
498
            $uploaddir = XOOPS_UPLOAD_PATH . $helper->getConfig('xn_attachment_path') . $letter_id . '/';
499
            // check upload_dir
500
            if (!is_dir($uploaddir)) {
501
                $indexFile = XOOPS_UPLOAD_PATH . '/index.html';
502
                if (!mkdir($uploaddir, 0777) && !is_dir($uploaddir)) {
503
                    throw new \RuntimeException(sprintf('Directory "%s" was not created', $uploaddir));
504
                }
505
                chmod($uploaddir, 0777);
506
                copy($indexFile, $uploaddir . 'index.html');
507
            }
508
            $new_attachments_mode = Request::getArray('new_attachments_mode', []);
509
            for ($upl = 0; $upl < $helper->getConfig('xn_maxattachments'); ++$upl) {
510
                $uploader = new \XoopsMediaUploader($uploaddir, $helper->getConfig('xn_mimetypes'), $helper->getConfig('xn_maxsize'), null, null);
511
                if ($uploader->fetchMedia(@$_POST['xoops_upload_file'][$upl])) {
512
                    //$uploader->setPrefix("xn_") ; keep original name
513
                    $uploader->fetchMedia($_POST['xoops_upload_file'][$upl]);
514
                    if (!$uploader->upload()) {
515
                        $errors = $uploader->getErrors();
516
                        redirect_header('<script>javascript:history.go(-1)</script>', 3, $errors);
517
                    } else {
518
                        preg_match('/ne\w_attachment_index=([0-9]+)/', $_POST['xoops_upload_file'][$upl], $matches);
519
                        $index           = $matches[1];
520
                        $uploadedFiles[] = [
521
                            'name' => $uploader->getSavedFileName(),
522
                            'type' => $uploader->getMediaType(),
523
                            'size' => $uploader->getMediaSize(),
524
                            'mode' => $new_attachments_mode[$index],
525
                        ];
526
                    }
527
                }
528
            }
529
            // create items in attachments
530
            foreach ($uploadedFiles as $file) {
531
                $attachmentObj = $helper->getHandler('Attachment')->create();
532
                $attachmentObj->setVar('attachment_letter_id', $letter_id);
533
                $attachmentObj->setVar('attachment_name', $file['name']);
534
                $attachmentObj->setVar('attachment_type', $file['type']);
535
                $attachmentObj->setVar('attachment_submitter', $xoopsUser->uid());
536
                $attachmentObj->setVar('attachment_created', time());
537
                $attachmentObj->setVar('attachment_size', $file['size']);
538
                $attachmentObj->setVar('attachment_mode', $file['mode']);
539
540
                $helper->getHandler('Attachment')->insert($attachmentObj);
541
            }
542
            // create item in protocol
543
            $protocolObj = $helper->getHandler('Protocol')->create();
544
            $protocolObj->setVar('protocol_letter_id', $letter_id);
545
            $protocolObj->setVar('protocol_subscriber_id', 0);
546
            $protocolObj->setVar('protocol_success', true);
547
            $action = Request::getInt('letter_action', _XNEWSLETTER_LETTER_ACTION_VAL_NO);
548
            switch ($action) {
549
                case _XNEWSLETTER_LETTER_ACTION_VAL_PREVIEW:
550
                    $redirectUrl = "?op=show_preview&letter_id={$letter_id}";
551
                    break;
552
                case _XNEWSLETTER_LETTER_ACTION_VAL_SEND:
553
                    $redirectUrl = "sendletter.php?op=send_letter&letter_id={$letter_id}";
554
                    break;
555
                case _XNEWSLETTER_LETTER_ACTION_VAL_SENDTEST:
556
                    $redirectUrl = "sendletter.php?op=send_test&letter_id={$letter_id}";
557
                    break;
558
                default:
559
                    $redirectUrl = '?op=list_letters';
560
                    break;
561
            }
562
            $protocolObj->setVar('protocol_status', _AM_XNEWSLETTER_LETTER_ACTION_SAVED);
563
            $protocolObj->setVar('protocol_status_str_id', _XNEWSLETTER_PROTOCOL_STATUS_SAVED); // new from v1.3
564
            $protocolObj->setVar('protocol_status_vars', []); // new from v1.3
565
            $protocolObj->setVar('protocol_submitter', $xoopsUser->uid());
566
            $protocolObj->setVar('protocol_created', time());
567
568
            if ($helper->getHandler('Protocol')->insert($protocolObj)) {
569
                // create protocol is ok
570
                redirect_header($redirectUrl, 3, _AM_XNEWSLETTER_FORMOK);
571
            } else {
572
                $GLOBALS['xoopsTpl']->assign('error', 'Error create protocol: ' . $protocolObj->getHtmlErrors());
573
            }
574
        } else {
575
            $GLOBALS['xoopsTpl']->assign('error', 'Error create letter: ' . $protocolObj->getHtmlErrors());
576
        }
577
        break;
578
    case 'copy_letter':
579
    case 'clone_letter':
580
        $GLOBALS['xoopsOption']['template_main'] = "{$helper->getModule()->dirname()}_letter.tpl";
581
        require_once XOOPS_ROOT_PATH . '/header.php';
582
583
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
584
        $xoTheme->addMeta('meta', 'keywords', $helper->getConfig('keywords')); // keywords only for index page
585
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
586
        // breadcrumb
587
        $breadcrumb = new Xnewsletter\Breadcrumb();
588
        $breadcrumb->addLink($helper->getModule()->getVar('name'), XNEWSLETTER_URL);
589
        $breadcrumb->addLink(_MD_XNEWSLETTER_LIST, 'javascript:history.go(-1)');
590
        $breadcrumb->addLink(_MD_XNEWSLETTER_LETTER_COPY, '');
591
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
592
593
        $oldLetterObj = $helper->getHandler('Letter')->get($letter_id);
594
        $newLetterObj = $helper->getHandler('Letter')->create();
595
        $newLetterObj->setVar('letter_title',      sprintf(_AM_XNEWSLETTER_LETTER_CLONED, $oldLetterObj->getVar('letter_title')));
596
        $newLetterObj->setVar('letter_content',    $oldLetterObj->getVar('letter_content', 'n'));
597
        $newLetterObj->setVar('letter_template',   $oldLetterObj->getVar('letter_template'));
598
        $newLetterObj->setVar('letter_cats',       $oldLetterObj->getVar('letter_cats'));
599
        $newLetterObj->setVar('letter_account',    $oldLetterObj->getVar('letter_account'));
600
        $newLetterObj->setVar('letter_email_test', $oldLetterObj->getVar('letter_email_test'));
601
        unset($oldLetterObj);
602
        $action  = XOOPS_URL . "/modules/xnewsletter/{$currentFile}?op=copy_letter";
603
        $form    = $newLetterObj->getForm($action);
604
        $content = $form->render();
605
        $xoopsTpl->assign('content', $content);
606
        break;
607
    case 'delete_letter':
608
        $GLOBALS['xoopsOption']['template_main'] = "{$helper->getModule()->dirname()}_empty.tpl";
609
        require_once XOOPS_ROOT_PATH . '/header.php';
610
611
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
612
        $xoTheme->addMeta('meta', 'keywords', $helper->getConfig('keywords')); // keywords only for index page
613
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
614
        // breadcrumb
615
        $breadcrumb = new Xnewsletter\Breadcrumb();
616
        $breadcrumb->addLink($helper->getModule()->getVar('name'), XNEWSLETTER_URL);
617
        $breadcrumb->addLink(_MD_XNEWSLETTER_LIST, 'javascript:history.go(-1)');
618
        $breadcrumb->addLink(_MD_XNEWSLETTER_LETTER_DELETE, '');
619
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
620
621
        // IN PROGRESS FROM HERE
622
623
        $letterObj = $helper->getHandler('Letter')->get($letter_id);
624 View Code Duplication
        if (true === Request::getBool('ok', false, 'POST')) {
625
            if (!$GLOBALS['xoopsSecurity']->check()) {
626
                redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
627
            }
628
            if ($helper->getHandler('Letter')->delete($letterObj)) {
629
                //delete protocols
630
                $sql = 'DELETE';
631
                $sql .= " FROM `{$xoopsDB->prefix('xnewsletter_protocol')}`";
632
                $sql .= " WHERE `protocol_letter_id`={$letter_id}";
633
                if (!$result = $xoopsDB->query($sql)) {
634
                    die('MySQL-Error: ' . $GLOBALS['xoopsDB']->error());
635
                }
636
                // delete attachments
637
                $attachmentCriteria = new \Criteria('attachment_letter_id', $letter_id);
638
                $helper->getHandler('Attachment')->deleteAll($attachmentCriteria, true, true);
639
                redirect_header($currentFile, 3, _AM_XNEWSLETTER_FORMDELOK);
640
            } else {
641
                $GLOBALS['xoopsTpl']->assign('error', $letterObj->getHtmlErrors());
642
            }
643
        } else {
644
            xoops_confirm(['ok' => true, 'letter_id' => $letter_id, 'op' => 'delete_letter'], $_SERVER['REQUEST_URI'], sprintf(_AM_XNEWSLETTER_FORMSUREDEL, $letterObj->getVar('letter_title')));
645
        }
646
        break;
647
}
648
649
require_once __DIR__ . '/footer.php';
650