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

admin/letter.php (3 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__ . '/admin_header.php';
33
xoops_cp_header();
34
35
// set template
36
$templateMain = 'xnewsletter_admin_letters.tpl';
37
38
// We recovered the value of the argument op in the URL$
39
$op       = Request::getString('op', 'list');
40
$letterId = Request::getInt('letter_id', 0);
41
42
$GLOBALS['xoopsTpl']->assign('xnewsletter_url', XNEWSLETTER_URL);
43
$GLOBALS['xoopsTpl']->assign('xnewsletter_icons_url', XNEWSLETTER_ICONS_URL);
44
45
switch ($op) {
46
    case 'show_preview':
47
    case 'show_letter_preview':
48
        global $XoopsTpl;
49
50
        $adminObject->displayNavigation($currentFile);
51
        $adminObject->addItemButton(_AM_XNEWSLETTER_LETTERLIST, '?op=list', 'list');
52
        $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->renderButton('left'));
53
54
        $letterTpl = new \XoopsTpl();
55
56
        $letterObj = $helper->getHandler('Letter')->get($letterId);
57
        // subscr data
58
        $letterTpl->assign('sex', _AM_XNEWSLETTER_SUBSCR_SEX_PREVIEW);
59
        $letterTpl->assign('salutation', _AM_XNEWSLETTER_SUBSCR_SEX_PREVIEW); // new from v1.3
60
        $letterTpl->assign('firstname', _AM_XNEWSLETTER_SUBSCR_FIRSTNAME_PREVIEW);
61
        $letterTpl->assign('lastname', _AM_XNEWSLETTER_SUBSCR_LASTNAME_PREVIEW);
62
        $letterTpl->assign('subscr_email', _AM_XNEWSLETTER_SUBSCR_EMAIL_PREVIEW);
63
        $letterTpl->assign('email', _AM_XNEWSLETTER_SUBSCR_EMAIL_PREVIEW); // new from v1.3
64
        // letter data
65
        $letterTpl->assign('title', $letterObj->getVar('letter_title', 'n')); // new from v1.3
66
        $letterTpl->assign('content', $letterObj->getVar('letter_content', 'n'));
67
        // letter attachments as link
68
        $attachmentAslinkCriteria = new \CriteriaCompo();
69
        $attachmentAslinkCriteria->add(new \Criteria('attachment_letter_id', $letterId));
70
        $attachmentAslinkCriteria->add(new \Criteria('attachment_mode', _XNEWSLETTER_ATTACHMENTS_MODE_ASLINK));
71
        $attachmentAslinkCriteria->setSort('attachment_id');
72
        $attachmentAslinkCriteria->setOrder('ASC');
73
        $attachmentObjs = $helper->getHandler('Attachment')->getObjects($attachmentAslinkCriteria, true);
74 View Code Duplication
        foreach ($attachmentObjs as $attachment_id => $attachmentObj) {
75
            $attachment_array                    = $attachmentObj->toArray();
76
            $attachment_array['attachment_url']  = XNEWSLETTER_URL . "/attachment.php?attachment_id={$attachment_id}";
77
            $attachment_array['attachment_link'] = XNEWSLETTER_URL . "/attachment.php?attachment_id={$attachment_id}";
78
            $letterTpl->append('attachments', $attachment_array);
79
        }
80
        // extra data
81
        $letterTpl->assign('date', time()); // new from v1.3
82
        $letterTpl->assign('unsubscribe_url', '#');
83
        $letterTpl->assign('catsubscr_id', '0');
84
85
        $templateObj = $helper->getHandler('Template')->get($letterObj->getVar('letter_templateid'));
86 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...
87
            if ( (int)$templateObj->getVar('template_type') === _XNEWSLETTER_MAILINGLIST_TPL_CUSTOM_VAL) {
88
                // get template from database
89
                $htmlBody = $letterTpl->fetchFromData($templateObj->getVar('template_content', 'n'));
90
            } else {
91
                // get template from filesystem
92
                $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/' . $GLOBALS['xoopsConfig']['language'] . '/templates/';
93
                if (!is_dir($template_path)) {
94
                    $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/english/templates/';
95
                }
96
                $template = $template_path . $templateObj->getVar('template_title') . '.tpl';
97
                $htmlBody = $letterTpl->fetch($template);
98
            }
99
            try {
100
                $textBody = xnewsletter_html2text($htmlBody);
101
            }
102
            catch (Html2TextException $e) {
103
                $helper->addLog($e);
104
            }
105
        } else {
106
            $htmlBody = _AM_XNEWSLETTER_TEMPLATE_ERR;
107
        }
108
109
        $preview =  "<h2>{$letterObj->getVar('letter_title')}</h2>";
110
        $preview .= "<div style='clear:both'>";
111
        $preview .= "<div style='padding:10px;border:1px solid #000000'>";
112
        $preview .= $htmlBody;
113
//        $preview .= '</div>';
114
//        $preview .= "<div style='padding:10px;border:1px solid black; font-family: monospace;'>";
115
        //$preview .= nl2br(utf8_encode($textBody));
116
        $preview .= '</div>';
117
        $preview .= '</div>';
118
        $GLOBALS['xoopsTpl']->assign('preview', $preview);
119
        break;
120
    case 'list_letters':
121
    default:
122
        $adminObject->displayNavigation($currentFile);
123
        $adminObject->addItemButton(_AM_XNEWSLETTER_NEWLETTER, '?op=new_letter', 'add');
124
        $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->renderButton('left'));
125
126
        $limit          = $helper->getConfig('adminperpage');
127
        $letterCriteria = new \CriteriaCompo();
128
        $letterCriteria->setSort('letter_id');
129
        $letterCriteria->setOrder('DESC');
130
        $letterCount = $helper->getHandler('Letter')->getCount();
131
        $start       = Request::getInt('start', 0);
132
        $letterCriteria->setStart($start);
133
        $letterCriteria->setLimit($limit);
134
        $lettersAll = $helper->getHandler('Letter')->getAll($letterCriteria);
135
136 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...
137
            // pagenav
138
            require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
139
            $pagenav = new \XoopsPageNav($letterCount, $limit, $start, 'start', 'op=list');
140
            $GLOBALS['xoopsTpl']->assign('pagenav', $pagenav->renderNav(4));
141
        }
142
143
        if ($letterCount > 0) {
144
            $GLOBALS['xoopsTpl']->assign('letterCount', $letterCount);
145
146
            $class = 'odd';
147
            foreach ($lettersAll as $letter_id => $letterObj) {
148
                $letter = $letterObj->getValuesLetter();
149
                $letter_cat_ids = explode('|', $letter['letter_cats']);
150
                $cats = '';
151
                foreach ($letter_cat_ids as $letter_cat_id) {
152
                    $catObj = $helper->getHandler('Cat')->get($letter_cat_id);
153
                    if (is_object($catObj)) {
154
                        $cats .= $catObj->getVar('cat_name') . '<br>';
155
                    } else {
156
                        $cats .= 'Invalid cat_name<br>';
157
                    }
158
                }
159
                $letter['cats_text'] = $cats;
160
                // check whether template exist or not
161
                $templateObj = $helper->getHandler('Template')->get($letter['templateid']);
162
                $letter['template_err'] = false;
163
                if (is_object($templateObj)) {
164 View Code Duplication
                    if ($templateObj->getVar('template_type') === _XNEWSLETTER_MAILINGLIST_TPL_FILE_VAL) {
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...
165
                        $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/' . $GLOBALS['xoopsConfig']['language'] . '/templates/';
166
                        if (!is_dir($template_path)) {
167
                            $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/english/templates/';
168
                        }
169
                        $filename = $template_path . $templateObj->getVar('template_title') . '.tpl';
170
                        if (!file_exists ( $filename )) {
171
                            $letter['template_err'] = true;
172
                            $letter['template_err_text'] = str_replace('%s', $template_path, _AM_XNEWSLETTER_TEMPLATE_ERR_FILE);
173
                        }
174
                    }
175
                } else {
176
                    $letter['template_err'] = true;
177
                    $letter['template_err_text'] = _AM_XNEWSLETTER_TEMPLATE_ERR_TABLE;
178
                }
179
180
                $attachments = '';
181
                $attachmentCriteria = new \CriteriaCompo();
182
                $attachmentCriteria->add(new \Criteria('attachment_letter_id', $letter_id));
183
                $attachmentCount = $helper->getHandler('Attachment')->getCount($attachmentCriteria);
184
                $attachmentObjs  = $helper->getHandler('Attachment')->getObjects($attachmentCriteria, true);
185
                $attachmentsSize = 0;
186
                if ($attachmentCount > 0) {
187
                    $attachmentsSize = 0;
188
                    $attachments .= '<br><br>' . _AM_XNEWSLETTER_LETTER_ATTACHMENT . ':<ul>';
189
                    foreach ($attachmentObjs as $attachment_id => $attachmentObj) {
190
                        $attachmentsSize = $attachmentsSize + $attachmentObj->getVar('attachment_size');
191
                        $size            = xnewsletter_bytesToSize1024($attachmentObj->getVar('attachment_size'));
192
                        $attachments .=  "<li><span title='" . $attachmentObj->getVar('attachment_type') . ' ' . $size . "'>{$attachmentObj->getVar('attachment_name')}</span></li>";
193
                    }
194
                    $attachments .=  '</ul>';
195
                    $attachments .=  _AM_XNEWSLETTER_LETTER_ATTACHMENT_TOTALSIZE . ": <span title='" . $attachmentsSize . " Bytes'>" . xnewsletter_bytesToSize1024($attachmentsSize) . '</span>';
196
                }
197
                try {
198
                    $emailSize = xnewsletter_emailSize($letter_id);
199
                }
200
                catch (Html2TextException $e) {
201
                    $helper->addLog($e);
202
                }
203
                $lettersize =  _AM_XNEWSLETTER_LETTER_EMAIL_SIZE . ": <span title='" . $emailSize . ' Bytes (' . _AM_XNEWSLETTER_LETTER_EMAIL_SIZE_DESC . ")'>" . xnewsletter_bytesToSize1024($emailSize) . '</span>';
204
                $letter['size_attachments'] = $lettersize . $attachments;
205
206
                $accountCriteria = new \CriteriaCompo();
207
                $accountCriteria->setSort('accounts_id');
208
                $accountCriteria->setOrder('ASC');
209
                $accountObj     = $helper->getHandler('Accounts')->get($letterObj->getVar('letter_account'));
210
                $letter_account = $accountObj ? $accountObj->getVar('accounts_name') : _NONE;
211
                $letter['letter_account'] = $letter_account;
212
213
                // take last item protocol_subscriber_id=0 from table protocol as actual status
214
                $protocolCriteria = new \CriteriaCompo();
215
                $protocolCriteria->add(new \Criteria('protocol_letter_id', $letter_id));
216
                $protocolCriteria->add(new \Criteria('protocol_subscriber_id', '0'));
217
                $protocolCriteria->setSort('protocol_id');
218
                $protocolCriteria->setOrder('DESC');
219
                $protocolCriteria->setLimit(1);
220
                $protocolObjs       = $helper->getHandler('Protocol')->getAll($protocolCriteria);
221
                $protocol_status    = '';
222
                $protocol_letter_id = 0;
223
                foreach ($protocolObjs as $protocolObj) {
224
                    $protocol_status    .= $protocolObj->getVar('protocol_status');
225
                    $protocol_letter_id = $protocolObj->getVar('protocol_letter_id');
226
                }
227
                $letter['protocol_status'] = $protocol_status;
228
                $letter['protocol_letter_id'] = $protocol_letter_id;
229
230
231
                $GLOBALS['xoopsTpl']->append('letters_list', $letter);
232
                unset($subscr);
233
            }
234
        } else {
235
            $GLOBALS['xoopsTpl']->assign('error', _AM_XNEWSLETTER_THEREARENT_LETTER);
236
        }
237
        break;
238
    case 'new_letter':
239
        $adminObject->displayNavigation($currentFile);
240
        $adminObject->addItemButton(_AM_XNEWSLETTER_LETTERLIST, '?op=list', 'list');
241
        $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->renderButton('left'));
242
243
        $letterObj = $helper->getHandler('Letter')->create();
244
        $form      = $letterObj->getForm(false, true);
245
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
246
        break;
247
    case 'edit_letter':
248
        $adminObject->displayNavigation($currentFile);
249
        $adminObject->addItemButton(_AM_XNEWSLETTER_NEWLETTER, '?op=new_letter', 'add');
250
        $adminObject->addItemButton(_AM_XNEWSLETTER_LETTERLIST, '?op=list', 'list');
251
        $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->renderButton('left'));
252
253
        $letterObj = $helper->getHandler('Letter')->get($letterId);
254
        $form      = $letterObj->getForm(false, true);
255
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
256
        break;
257
    case 'delete_attachment':
258
        $adminObject->displayNavigation($currentFile);
259
        $adminObject->addItemButton(_AM_XNEWSLETTER_NEWLETTER, '?op=new_letter', 'add');
260
        $adminObject->addItemButton(_AM_XNEWSLETTER_LETTERLIST, '?op=list', 'list');
261
        $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->renderButton('left'));
262
        //
263
        // update existing_attachments
264
        $existing_attachments_mode = Request::getArray('existing_attachments_mode', []);
265
        foreach ($existing_attachments_mode as $existing_attachment_id => $existing_attachment_mode) {
266
            $attachmentObj = $helper->getHandler('Attachment')->get($existing_attachment_id);
267
            $attachmentObj->setVar('attachment_mode', $existing_attachment_mode);
268
            $helper->getHandler('Attachment')->insert($attachmentObj);
269
        }
270
271
        $attachment_id = Request::getInt('deleted_attachment_id', 0, 'POST');
272
        if (0 == $attachment_id) {
273
            redirect_header($currentFile, 3, _AM_XNEWSLETTER_LETTER_ERROR_INVALID_ATT_ID);
274
        }
275
        $attachmentObj   = $helper->getHandler('Attachment')->get($attachment_id);
276
        $attachment_name = $attachmentObj->getVar('attachment_name');
277
278
        if ($helper->getHandler('Attachment')->delete($attachmentObj, true)) {
279
            $letterObj = $helper->getHandler('Letter')->get($letterId);
280
            $letterObj->setVar('letter_title',      Request::getString('letter_title', ''));
281
            $letterObj->setVar('letter_content',    Request::getText('letter_content', ''));
282
            $letterObj->setVar('letter_templateid', Request::getInt('letter_templateid', 0));
283
            $letterObj->setVar('letter_cats',       implode('|', Request::getArray('letter_cats', [])));
284
            $letterObj->setVar('letter_account',    Request::getInt('letter_account', 0));
285
            $letterObj->setVar('letter_email_test', Request::getString('letter_email_test', ''));
286
287
            $form = $letterObj->getForm(false, true);
288
            $GLOBALS['xoopsTpl']->assign('form', $form->render());
289
        } else {
290
            $GLOBALS['xoopsTpl']->assign('error', $attachmentObj->getHtmlErrors());
291
        }
292
        break;
293
    case 'save_letter':
294
        if (!$GLOBALS['xoopsSecurity']->check()) {
295
            redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
296
        }
297
        $letterObj = $helper->getHandler('Letter')->get($letterId); // create if doesn't exist
298
        $letterObj->setVar('letter_title',      Request::getString('letter_title', ''));
299
        $letterObj->setVar('letter_content',    Request::getText('letter_content', ''));
300
        $letterObj->setVar('letter_templateid', Request::getInt('letter_templateid', 0));
301
        $letterObj->setVar('letter_cats',       implode('|', Request::getArray('letter_cats', [])));
302
        $letterObj->setVar('letter_account',    Request::getInt('letter_account', 0));
303
        $letterObj->setVar('letter_email_test', Request::getString('letter_email_test', ''));
304
        $letterObj->setVar('letter_submitter',  Request::getInt('letter_submitter', 0));
305
        $letterObj->setVar('letter_created',    Request::getInt('letter_created', time()));
306
307 View Code Duplication
        if ($helper->getHandler('Letter')->insert($letterObj)) {
308
            $letter_id = $letterObj->getVar('letter_id');
309
            // update existing_attachments
310
            $existing_attachments_mode = Request::getArray('existing_attachments_mode', []);
311
            foreach ($existing_attachments_mode as $attachment_id => $attachment_mode) {
312
                $attachmentObj = $helper->getHandler('Attachment')->get($attachment_id);
313
                $attachmentObj->setVar('attachment_mode', $attachment_mode);
314
                $helper->getHandler('Attachment')->insert($attachmentObj);
315
            }
316
            // upload attachments
317
            $uploadedFiles = [];
318
            require_once XOOPS_ROOT_PATH . '/class/uploader.php';
319
            $uploaddir = XOOPS_UPLOAD_PATH . $helper->getConfig('xn_attachment_path') . $letterId . '/';
320
            // check upload_dir
321
            if (!is_dir($uploaddir)) {
322
                $indexFile = XOOPS_UPLOAD_PATH . '/index.html';
323
                if (!mkdir($uploaddir, 0777) && !is_dir($uploaddir)) {
324
                    throw new \RuntimeException(sprintf('Directory "%s" was not created', $uploaddir));
325
                }
326
                chmod($uploaddir, 0777);
327
                copy($indexFile, $uploaddir . 'index.html');
328
            }
329
            $new_attachments_mode = Request::getArray('new_attachments_mode', []);
330
            for ($upl = 0; $upl < $helper->getConfig('xn_maxattachments'); ++$upl) {
331
                $uploader = new \XoopsMediaUploader($uploaddir, $helper->getConfig('xn_mimetypes'), $helper->getConfig('xn_maxsize'), null, null);
332
                if ($uploader->fetchMedia(@$_POST['xoops_upload_file'][$upl])) {
333
                    //$uploader->setPrefix("xn_") ; keep original name
334
                    $uploader->fetchMedia($_POST['xoops_upload_file'][$upl]);
335
                    if (!$uploader->upload()) {
336
                        $errors = $uploader->getErrors();
337
                        redirect_header('<script>javascript:history.go(-1)</script>', 3, $errors);
338
                    } else {
339
                        preg_match('/ne\w_attachment_index=([0-9]+)/', $_POST['xoops_upload_file'][$upl], $matches);
340
                        $index           = $matches[1];
341
                        $uploadedFiles[] = [
342
                            'name' => $uploader->getSavedFileName(),
343
                            'type' => $uploader->getMediaType(),
344
                            'size' => $uploader->getMediaSize(),
345
                            'mode' => $new_attachments_mode[$index],
346
                        ];
347
                    }
348
                }
349
            }
350
            // create items in attachments
351
            foreach ($uploadedFiles as $file) {
352
                $attachmentObj = $helper->getHandler('Attachment')->create();
353
                $attachmentObj->setVar('attachment_letter_id', $letterId);
354
                $attachmentObj->setVar('attachment_name', $file['name']);
355
                $attachmentObj->setVar('attachment_type', $file['type']);
356
                $attachmentObj->setVar('attachment_submitter', $xoopsUser->uid());
357
                $attachmentObj->setVar('attachment_created', time());
358
                $attachmentObj->setVar('attachment_size', $file['size']);
359
                $attachmentObj->setVar('attachment_mode', $file['mode']);
360
361
                $helper->getHandler('Attachment')->insert($attachmentObj);
362
            }
363
            // create item in protocol
364
            $protocolObj = $helper->getHandler('Protocol')->create();
365
            $protocolObj->setVar('protocol_letter_id', $letterId);
366
            $protocolObj->setVar('protocol_subscriber_id', 0);
367
            $protocolObj->setVar('protocol_success', true);
368
            $action = Request::getInt('letter_action', _XNEWSLETTER_LETTER_ACTION_VAL_NO);
369
            switch ($action) {
370
                case _XNEWSLETTER_LETTER_ACTION_VAL_PREVIEW:
371
                    $redirectUrl = "?op=show_preview&letter_id={$letterId}";
372
                    break;
373
                case _XNEWSLETTER_LETTER_ACTION_VAL_SEND:
374
                    $redirectUrl = "sendletter.php?op=send_letter&letter_id={$letterId}";
375
                    break;
376
                case _XNEWSLETTER_LETTER_ACTION_VAL_SENDTEST:
377
                    $redirectUrl = "sendletter.php?op=send_test&letter_id={$letterId}";
378
                    break;
379
                default:
380
                    $redirectUrl = '?op=list_letters';
381
                    break;
382
            }
383
            $protocolObj->setVar('protocol_status', _AM_XNEWSLETTER_LETTER_ACTION_SAVED); // old style
384
            $protocolObj->setVar('protocol_status_str_id', _XNEWSLETTER_PROTOCOL_STATUS_SAVED); // new from v1.3
385
            $protocolObj->setVar('protocol_status_vars', []); // new from v1.3
386
            $protocolObj->setVar('protocol_submitter', $xoopsUser->uid());
387
            $protocolObj->setVar('protocol_created', time());
388
389
            if ($helper->getHandler('Protocol')->insert($protocolObj)) {
390
                // create protocol is ok
391
                redirect_header($redirectUrl, 3, _AM_XNEWSLETTER_FORMOK);
392
            } else {
393
                $GLOBALS['xoopsTpl']->assign('error', $protocolObj->getHtmlErrors());
394
            }
395
        } else {
396
            $GLOBALS['xoopsTpl']->assign('error', $letterObj->getHtmlErrors());
397
        }
398
        break;
399
    case 'clone_letter':
400
    case 'copy_letter':
401
        $adminObject->displayNavigation($currentFile);
402
        $adminObject->addItemButton(_AM_XNEWSLETTER_NEWLETTER, '?op=new_letter', 'add');
403
        $adminObject->addItemButton(_AM_XNEWSLETTER_LETTERLIST, '?op=list', 'list');
404
        $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->renderButton('left'));
405
406
        $letterObj = $helper->getHandler('Letter')->get($letterId);
407
        $letterObj->setNew();
408
        $letterObj->setVar('letter_id', 0);
409
        $letterObj->setVar('letter_title', sprintf(_AM_XNEWSLETTER_LETTER_CLONED, $letterObj->getVar('letter_title')));
410
        $form = $letterObj->getForm($currentFile, true);
411
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
412
        break;
413
    case 'delete_letter':
414
        $letterObj = $helper->getHandler('Letter')->get($letterId);
415 View Code Duplication
        if (true === Request::getBool('ok', false, 'POST')) {
416
            if (!$GLOBALS['xoopsSecurity']->check()) {
417
                redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
418
            }
419
            if ($helper->getHandler('Letter')->delete($letterObj)) {
420
                //delete protocols
421
                $sql = 'DELETE';
422
                $sql .= " FROM `{$xoopsDB->prefix('xnewsletter_protocol')}`";
423
                $sql .= " WHERE `protocol_letter_id`={$letterId}";
424
                if (!$result = $xoopsDB->query($sql)) {
425
                    die('MySQL-Error: ' . $GLOBALS['xoopsDB']->error());
426
                }
427
                // delete attachments
428
                $attachmentCriteria = new \Criteria('attachment_letter_id', $letterId);
429
                $helper->getHandler('Attachment')->deleteAll($attachmentCriteria, true, true);
430
                redirect_header($currentFile, 3, _AM_XNEWSLETTER_FORMDELOK);
431
            } else {
432
                $GLOBALS['xoopsTpl']->assign('error', $letterObj->getHtmlErrors());
433
            }
434
        } else {
435
            xoops_confirm(['ok' => true, 'letter_id' => $letterId, 'op' => 'delete_letter'], $_SERVER['REQUEST_URI'], sprintf(_AM_XNEWSLETTER_FORMSUREDEL, $letterObj->getVar('letter_title')));
436
        }
437
        break;
438
}
439
require_once __DIR__ . '/admin_footer.php';
440