Completed
Push — master ( 692213...d4ec0d )
by Goffy
03:18 queued 01:37
created

letter.php (13 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 - ( http://www.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
 *  Version : $Id: letter.php 12559 2014-06-02 08:10:39Z beckmi $
26
 * ****************************************************************************
27
 */
28
29
$currentFile = basename(__FILE__);
30
include_once __DIR__ . "/header.php";
31
32
$uid = (is_object($xoopsUser) && isset($xoopsUser)) ? $xoopsUser->uid() : 0;
33
$groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : array(0 => XOOPS_GROUP_ANONYMOUS);
34
35
$op         = XoopsRequest::getString('op', 'list_letters');
36
$letter_id  = XoopsRequest::getInt('letter_id', 0);
37
$cat_id     = XoopsRequest::getInt('cat_id', 0);
38
39
//check the rights of current user first
40
if (!xnewsletter_userAllowedCreateCat()) redirect_header("index.php", 3, _NOPERM);
41
42
$delete_att_1 = XoopsRequest::getString('delete_attachment_1', 'none');
43
$delete_att_2 = XoopsRequest::getString('delete_attachment_2', 'none');
44
$delete_att_3 = XoopsRequest::getString('delete_attachment_3', 'none');
45
$delete_att_4 = XoopsRequest::getString('delete_attachment_4', 'none');
46
$delete_att_5 = XoopsRequest::getString('delete_attachment_5', 'none');
47
48 View Code Duplication
if ($delete_att_1 != 'none') {
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...
49
    $op = "delete_attachment";
50
    $id_del = 1;
51
} elseif ($delete_att_2 != 'none') {
52
    $op = "delete_attachment";
53
    $id_del = 2;
54
} elseif ($delete_att_3 != 'none') {
55
    $op = "delete_attachment";
56
    $id_del = 3;
57
} elseif ($delete_att_4 != 'none') {
58
    $op = "delete_attachment";
59
    $id_del = 4;
60
} elseif ($delete_att_5 != 'none') {
61
    $op = "delete_attachment";
62
    $id_del = 5;
63
} else {
64
    $id_del = 0;
65
}
66
67
switch ($op) {
68
    case "list_subscrs" :
69
        $xoopsOption['template_main'] = 'xnewsletter_letter_list_subscrs.tpl';
70
        include_once XOOPS_ROOT_PATH . "/header.php";
71
72
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
73
        $xoTheme->addMeta('meta', 'keywords', $xnewsletter->getConfig('keywords')); // keywords only for index page
74
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
75
76
        // Breadcrumb
77
        $breadcrumb = new XnewsletterBreadcrumb();
78
        $breadcrumb->addLink($xnewsletter->getModule()->getVar('name'), XNEWSLETTER_URL);
79
        $breadcrumb->addLink(_MD_XNEWSLETTER_LIST_SUBSCR, '');
80
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
81
82
        // check right to edit/delete subscription of other persons
83
        $permissionChangeOthersSubscriptions = false;
84
        foreach ($groups as $group) {
85
            if (in_array($group, $xnewsletter->getConfig('xn_groups_change_other')) || XOOPS_GROUP_ADMIN == $group) {
86
                $permissionChangeOthersSubscriptions = true;
87
                break;
88
            }
89
        }
90
        $xoopsTpl->assign('permissionChangeOthersSubscriptions', $permissionChangeOthersSubscriptions);
91
        // get search subscriber form
92
        if ($permissionChangeOthersSubscriptions) {
93
            include_once XOOPS_ROOT_PATH . "/class/xoopsformloader.php";
94
            $form = new XoopsThemeForm(_AM_XNEWSLETTER_FORMSEARCH_SUBSCR_EXIST, 'form_search', 'subscription.php', 'post', true);
95
            $form->setExtra('enctype="multipart/form-data"');
96
            $form->addElement(new XoopsFormText(_AM_XNEWSLETTER_SUBSCR_EMAIL, 'subscr_email', 60, 255, '', true));
97
            $form->addElement(new XoopsFormButton('', 'submit', _AM_XNEWSLETTER_SEARCH, 'submit'));
98
            $xoopsTpl->assign('searchSubscriberForm', $form->render());
99
        } else {
100
            $xoopsTpl->assign('searchSubscriberForm', '');
101
        }
102
        // get cat objects
103
        $catCriteria = new CriteriaCompo();
104
        $catCriteria->setSort('cat_id');
105
        $catCriteria->setOrder('ASC');
106
        $catObjs = $xnewsletter->getHandler('cat')->getAll($catCriteria, null, true, true);
107
        // cats table
108
        foreach ($catObjs as $cat_id => $catObj) {
109
            $permissionShowCats[$cat_id] = $gperm_handler->checkRight('newsletter_list_cat', $cat_id, $groups, $xnewsletter->getModule()->mid());
110
            if ($permissionShowCats[$cat_id] == true) {
111
                $cat_array = $catObj->toArray();
112
                $catsubscrCriteria = new CriteriaCompo();
113
                $catsubscrCriteria->add(new Criteria("catsubscr_catid", $cat_id));
114
                $cat_array['catsubscrCount'] = $xnewsletter->getHandler('catsubscr')->getCount($catsubscrCriteria);
115
                $xoopsTpl->append('cats', $cat_array);
116
            }
117
        }
118
        // get cat_id
119
        $cat_id = XoopsRequest::getInt('cat_id', 0);
120
        $xoopsTpl->assign('cat_id', $cat_id);
121
        if ($cat_id > 0) {
122
            $catObj = $xnewsletter->getHandler('cat')->get($cat_id);
123
            // subscrs table
124
            if ($permissionShowCats[$cat_id] == true) {
125
                $counter = 1;
126
                $sql ="SELECT `subscr_sex`, `subscr_lastname`, `subscr_firstname`, `subscr_email`, `subscr_id`";
127
                $sql.= " FROM {$xoopsDB->prefix("xnewsletter_subscr")} INNER JOIN {$xoopsDB->prefix("xnewsletter_catsubscr")} ON `subscr_id` = `catsubscr_subscrid`";
128
                $sql.= " WHERE (((`catsubscr_catid`)={$cat_id}) AND ((`catsubscr_quited`)=0)) ORDER BY `subscr_lastname`, `subscr_email`;";
129
                if(!$subscrs = $xoopsDB->query($sql)) die ("MySQL-Error: " . $xoopsDB->error());
130
                while ($subscr_array = $xoopsDB->fetchArray($subscrs)) {
131
                    $subscr_array['counter'] = ++$counter;
132
                    $xoopsTpl->append('subscrs', $subscr_array);
133
                }
134
            }
135
        }
136
        break;
137
138
    case "delete_attachment" :
139
//$xoopsOption['template_main'] = 'xnewsletter_letter.tpl'; // IN PROGRESS
140
        include_once XOOPS_ROOT_PATH . "/header.php";
141
142
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
143
        $xoTheme->addMeta('meta', 'keywords', $xnewsletter->getConfig('keywords')); // keywords only for index page
144
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
145
146
        // Breadcrumb
147
        $breadcrumb = new XnewsletterBreadcrumb();
148
        $breadcrumb->addLink($xnewsletter->getModule()->getVar('name'), XNEWSLETTER_URL);
149
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
150
151
// IN PROGRESS FROM HERE
152
        // get attachment
153
        $attachment_id = XoopsRequest::getString("attachment_{$id_del}", 'none');
154
        if ($attachment_id == 'none') {
155
            redirect_header($currentFile, 3, _AM_XNEWSLETTER_LETTER_ERROR_INVALID_ATT_ID);
156
        }
157
        $attachmentObj = $xnewsletter->getHandler('attachment')->get($attachment_id);
158
        $attachment_name = $attachmentObj->getVar("attachment_name");
159
        // delete attachment
160
        if ($xnewsletter->getHandler('attachment')->delete($attachmentObj, true)) {
161
            // delete file
162
            $uploadDir = XOOPS_UPLOAD_PATH . $xnewsletter->getConfig('xn_attachment_path') . $letter_id;
163
            if (file_exists($uploadDir . '/' . $attachment_name)) {
164
                unlink($uploadDir . '/' . $attachment_name);
165
            }
166
            // get letter
167
            $letterObj = $xnewsletter->getHandler('letter')->get($letter_id);
168
            $letterObj->setVar("letter_title", $_REQUEST['letter_title']);
169
            $letterObj->setVar("letter_content", $_REQUEST['letter_content']);
170
            $letterObj->setVar("letter_template", $_REQUEST['letter_template']);
171
// IN PROGRESS
172
// IN PROGRESS
173
// IN PROGRESS
174
            //Form letter_cats
175
            $letter_cats = '';
176
            //$cat_arr = isset($_REQUEST["letter_cats"]) ? $_REQUEST["letter_cats"] : "";
177
            $cats_arr = XoopsRequest::getArray('letter_cats', array());
178 View Code Duplication
            if (count($cats_arr) > 0) {
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...
179
                foreach ($cats_arr as $cat) {
180
                    $letter_cats .= $cat . '|';
181
                }
182
                $letter_cats = substr($letter_cats, 0, -1);
183
            } else {
184
                $letter_cats = $cats_arr;
185
            }
186
            //no cat
187 View Code Duplication
            if ($letter_cats == false) {
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...
188
                $form = $letterObj->getForm();
189
                $content = $form->render();
190
                $xoopsTpl->assign('content', $content);
191
                break;
192
            }
193
            $letterObj->setVar("letter_cats", $letter_cats);
194
// IN PROGRESS
195
// IN PROGRESS
196
// IN PROGRESS
197
            $letterObj->setVar("letter_account", $_REQUEST['letter_account']);
198
            $letterObj->setVar("letter_email_test", $_REQUEST['letter_email_test']);
199
            // get letter form
200
            $form = $letterObj->getForm(false, true);
201
            $form->display();
202
        } else {
203
            echo $attachmentObj->getHtmlErrors();
204
        }
205
        break;
206
207
    case "show_preview" :
208
    case "show_letter_preview" :
209
        $xoopsOption['template_main'] = 'xnewsletter_letter_preview.tpl';
210
        include XOOPS_ROOT_PATH . "/header.php";
211
212
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
213
        $xoTheme->addMeta('meta', 'keywords', $xnewsletter->getConfig('keywords')); // keywords only for index page
214
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
215
216
        // Breadcrumb
217
        $breadcrumb = new XnewsletterBreadcrumb();
218
        $breadcrumb->addLink($xnewsletter->getModule()->getVar('name'), XNEWSLETTER_URL);
219
        $breadcrumb->addLink(_MD_XNEWSLETTER_LIST, XNEWSLETTER_URL . '/letter.php?op=list_letters');
220
        $breadcrumb->addLink(_MD_XNEWSLETTER_LETTER_PREVIEW, '');
221
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
222
223
        // get letter_id
224
        $letter_id = XoopsRequest::getInt('letter_id', 0);
225
        // get letter object
226
        $letterObj = $xnewsletter->getHandler('letter')->get($letter_id);
227
        // subscr data
228
        $xoopsTpl->assign('sex', _AM_XNEWSLETTER_SUBSCR_SEX_PREVIEW);
229
        $xoopsTpl->assign('salutation', _AM_XNEWSLETTER_SUBSCR_SEX_PREVIEW); // new from v1.3
230
        $xoopsTpl->assign('firstname', _AM_XNEWSLETTER_SUBSCR_FIRSTNAME_PREVIEW);
231
        $xoopsTpl->assign('lastname', _AM_XNEWSLETTER_SUBSCR_LASTNAME_PREVIEW);
232
        $xoopsTpl->assign('subscr_email', _AM_XNEWSLETTER_SUBSCR_EMAIL_PREVIEW);
233
        $xoopsTpl->assign('email', _AM_XNEWSLETTER_SUBSCR_EMAIL_PREVIEW); // new from v1.3
234
        // letter data
235
        $xoopsTpl->assign('title', $letterObj->getVar('letter_title', 'n')); // new from v1.3
236
        $xoopsTpl->assign('content', $letterObj->getVar('letter_content', 'n'));
237
        // extra data
238
        $xoopsTpl->assign('date', time()); // new from v1.3
239
        $xoopsTpl->assign('unsubscribe_url', XOOPS_URL . '/modules/xnewsletter/');
240
        $xoopsTpl->assign('catsubscr_id', '0');
241
242
        $letter_array = $letterObj->toArray();
243
244
        preg_match('/db:([0-9]*)/', $letterObj->getVar("letter_template"), $matches);
245 View Code Duplication
        if(isset($matches[1]) && ($templateObj = $xnewsletter->getHandler('template')->get((int)$matches[1]))) {
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...
246
            // get template from database
247
            $htmlBody = $xoopsTpl->fetchFromData($templateObj->getVar('template_content', "n"));
248
        } else {
249
            // get template from filesystem
250
            $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/' . $GLOBALS['xoopsConfig']['language'] . '/templates/';
251
            if (!is_dir($template_path)) $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/english/templates/';
252
            $template = $template_path . $letterObj->getVar("letter_template") . ".tpl";
253
            $htmlBody = $xoopsTpl->fetch($template);
254
        }
255
        $textBody = xnewsletter_html2text($htmlBody); // new from v1.3
256
257
        $letter_array['letter_content_templated'] = $htmlBody;
258
        $letter_array['letter_content_templated_html'] = $htmlBody;
259
        $letter_array['letter_content_templated_text'] = $textBody; // new from v1.3
260
        $letter_array['letter_created_timestamp'] = formatTimestamp($letterObj->getVar('letter_created'), $xnewsletter->getConfig('dateformat'));
261
        $letter_array['letter_submitter_name'] = XoopsUserUtility::getUnameFromId($letterObj->getVar('letter_submitter'));
262
        $xoopsTpl->assign('letter', $letter_array);
263
        break;
264
265
    case "list" :
266
exit("IN_PROGRESS: use op=list_letters instead of op=list");
267
break;
268
    case "list_letters" :
269
    default :
270
        $xoopsOption['template_main'] = 'xnewsletter_letter_list_letters.tpl';
271
        include_once XOOPS_ROOT_PATH . "/header.php";
272
273
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
274
        $xoTheme->addMeta('meta', 'keywords', $xnewsletter->getConfig('keywords')); // keywords only for index page
275
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
276
277
        // Breadcrumb
278
        $breadcrumb = new XnewsletterBreadcrumb();
279
        $breadcrumb->addLink($xnewsletter->getModule()->getVar('name'), XNEWSLETTER_URL);
280
        $breadcrumb->addLink(_MD_XNEWSLETTER_LIST, '');
281
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
282
283
        // get letters array
284
        $letterCriteria = new CriteriaCompo();
285
        $letterCriteria->setSort("letter_id");
286
        $letterCriteria->setOrder("DESC");
287
        $letterCount = $xnewsletter->getHandler('letter')->getCount();
288
        $start = XoopsRequest::getInt('start', 0);
289
        $limit = $xnewsletter->getConfig('adminperpage');
290
        $letterCriteria->setStart($start);
291
        $letterCriteria->setLimit($limit);
292 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...
293
            include_once XOOPS_ROOT_PATH . "/class/pagenav.php";
294
            $pagenav = new XoopsPageNav($letterCount, $limit, $start, 'start', "op={$op}");
295
            $pagenav = $pagenav->renderNav(4);
296
        } else {
297
            $pagenav = '';
298
        }
299
        $xoopsTpl->assign('pagenav', $pagenav);
300
        $letterObjs = $xnewsletter->getHandler('letter')->getAll($letterCriteria, null, true, true);
301
        // letters table
302
        if ($letterCount> 0) {
303
            foreach ($letterObjs as $letter_id => $letterObj) {
304
                $userPermissions = array();
305
                $userPermissions = xnewsletter_getUserPermissionsByLetter($letter_id);
306
                if ($userPermissions["read"]) {
307
                    $letter_array = $letterObj->toArray();
308
                    $letter_array['letter_created_timestamp'] = formatTimestamp($letterObj->getVar('letter_created'), $xnewsletter->getConfig('dateformat'));
309
                    $letter_array['letter_submitter_name'] = XoopsUserUtility::getUnameFromId($letterObj->getVar('letter_submitter'));
310
                    // get categories
311
                    $catsAvailableCount = 0;
312
                    $cats_string = '';
313
                    $cat_ids = explode('|' , $letterObj->getVar('letter_cats'));
314
                    unset($letter_array['letter_cats']); // IN PROGRESS
315 View Code Duplication
                    foreach ($cat_ids as $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...
316
                        $catObj = $xnewsletter->getHandler('cat')->get($cat_id);
317
                        if ($gperm_handler->checkRight('newsletter_read_cat', $catObj->getVar('cat_id'), $groups, $xnewsletter->getModule()->mid())) {
318
                            ++$catsAvailableCount;
319
                            $letter_array['letter_cats'][] = $catObj->toArray();
320
                        }
321
                        unset($catObj);
322
                    }
323
                    if ($catsAvailableCount > 0) {
324
                        $letters_array[] = $letter_array;
325
                    }
326
                    // count letter attachements
327
                    $attachmentCriteria = new CriteriaCompo();
328
                    $attachmentCriteria->add(new Criteria('attachment_letter_id', $letterObj->getVar("letter_id")));
329
                    $letter_array['attachmentCount'] = $xnewsletter->getHandler('attachment')->getCount($attachmentCriteria);
330
                    // get protocols
331
                    if ($userPermissions["edit"]) {
332
                        // take last item protocol_subscriber_id=0 from table protocol as actual status
333
                        $protocolCriteria = new CriteriaCompo();
334
                        $protocolCriteria->add(new Criteria('protocol_letter_id', $letterObj->getVar("letter_id")));
335
                        //$criteria->add(new Criteria('protocol_subscriber_id', '0'));
336
                        $protocolCriteria->setSort("protocol_id");
337
                        $protocolCriteria->setOrder("DESC");
338
                        $protocolCriteria->setLimit(1);
339
                        $protocolObjs = $xnewsletter->getHandler('protocol')->getAll($protocolCriteria);
340
                        $protocol_status = "";
341
                        $protocol_letter_id = 0;
342
                        foreach ($protocolObjs as $protocolObj) {
343
                            $letter_array['protocols'][] = array(
344
                                'protocol_status' => $protocolObj->getVar("protocol_status"),
345
                                'protocol_letter_id' => $protocolObj->getVar("protocol_letter_id")
346
                                );
347
                        }
348
                    }
349
350
                    $letter_array['userPermissions'] = $userPermissions;
351
                    $xoopsTpl->append('letters', $letter_array);
352
                }
353
            }
354
        } else {
355
            // NOP
356
        }
357
        break;
358
359
    case "new_letter" :
360
$xoopsOption['template_main'] = 'xnewsletter_letter.tpl'; // IN PROGRESS
361
        include_once XOOPS_ROOT_PATH . "/header.php";
362
363
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
364
        $xoTheme->addMeta('meta', 'keywords', $xnewsletter->getConfig('keywords')); // keywords only for index page
365
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
366
367
        // Breadcrumb
368
        $breadcrumb = new XnewsletterBreadcrumb();
369
        $breadcrumb->addLink($xnewsletter->getModule()->getVar('name'), XNEWSLETTER_URL);
370
        $breadcrumb->addLink(_MD_XNEWSLETTER_LETTER_CREATE, '');
371
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
372
373
// IN PROGRESS FROM HERE
374
        $letterObj = $xnewsletter->getHandler('letter')->create();
375
        $form = $letterObj->getForm();
376
        $content = $form->render();
377
        $xoopsTpl->assign('content', $content);
378
        break;
379
380
    case "copy_letter":
381
    case "clone_letter":
382
$xoopsOption['template_main'] = 'xnewsletter_letter.tpl'; // IN PROGRESS
383
        include_once XOOPS_ROOT_PATH . "/header.php";
384
385
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
386
        $xoTheme->addMeta('meta', 'keywords', $xnewsletter->getConfig('keywords')); // keywords only for index page
387
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
388
389
        // Breadcrumb
390
        $breadcrumb = new XnewsletterBreadcrumb();
391
        $breadcrumb->addLink($xnewsletter->getModule()->getVar('name'), XNEWSLETTER_URL);
392
        $breadcrumb->addLink(_MD_XNEWSLETTER_LIST, XNEWSLETTER_URL . '/letter.php?op=list_letters');
393
        $breadcrumb->addLink(_MD_XNEWSLETTER_LETTER_COPY, '');
394
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
395
396
// IN PROGRESS FROM HERE
397
398
        $letterObj_old = $xnewsletter->getHandler('letter')->get($letter_id);
399
        $letterObj_new = $xnewsletter->getHandler('letter')->create();
400
401
        $letterObj_new->setVar("letter_title", $letterObj_old->getVar("letter_title"));
402
        $letterObj_new->setVar("letter_content", $letterObj_old->getVar("letter_content","n"));
403
        $letterObj_new->setVar("letter_template", $letterObj_old->getVar("letter_template"));
404
        $letterObj_new->setVar("letter_cats", $letterObj_old->getVar("letter_cats"));
405
        $letterObj_new->setVar("letter_account", $letterObj_old->getVar("letter_account"));
406
        $letterObj_new->setVar("letter_email_test", $letterObj_old->getVar("letter_email_test"));
407
        unset($letterObj_old);
408
        $action = XOOPS_URL . "/modules/xnewsletter/{$currentFile}?op=copy_letter";
409
        $form = $letterObj_new->getForm($action);
410
        $content = $form->render();
411
        $xoopsTpl->assign('content', $content);
412
        break;
413
414
    case "save_letter" :
415
$xoopsOption['template_main'] = 'xnewsletter_letter.tpl'; // IN PROGRESS
416
        include_once XOOPS_ROOT_PATH . "/header.php";
417
418
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
419
        $xoTheme->addMeta('meta', 'keywords', $xnewsletter->getConfig('keywords')); // keywords only for index page
420
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
421
422
        // Breadcrumb
423
        $breadcrumb = new XnewsletterBreadcrumb();
424
        $breadcrumb->addLink($xnewsletter->getModule()->getVar('name'), XNEWSLETTER_URL);
425
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
426
427
// IN PROGRESS FROM HERE
428
429
        if ( !$GLOBALS["xoopsSecurity"]->check() ) {
430
            redirect_header($currentFile, 3, implode(",", $GLOBALS["xoopsSecurity"]->getErrors()));
431
        }
432
        $letterObj = $xnewsletter->getHandler('letter')->get($letter_id);
433
434
        //Form letter_title
435
        $letterObj->setVar("letter_title", $_REQUEST['letter_title']);
436
        //Form letter_content
437
        $letterObj->setVar("letter_content", $_REQUEST['letter_content']);
438
        //Form letter_template
439
        $letterObj->setVar("letter_template", $_REQUEST['letter_template']);
440
        //Form letter_cats
441
        $letter_cats = "";
442
        //$cat_arr = isset($_REQUEST["letter_cats"]) ? $_REQUEST["letter_cats"] : "";
443
        $cat_arr = XoopsRequest::getArray('letter_cats', array());
444 View Code Duplication
        if (count($cat_arr) > 0) {
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...
445
            foreach ($cat_arr as $cat) {
446
                $letter_cats .= $cat . '|';
447
            }
448
            $letter_cats = substr($letter_cats, 0, -1);
449
        } else {
450
            $letter_cats = $cat_arr;
451
        }
452
        //no cat
453 View Code Duplication
        if ($letter_cats == false) {
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...
454
            $form = $letterObj->getForm();
455
            $content = $form->render();
456
            $xoopsTpl->assign('content', $content);
457
            break;
458
        }
459
460
        $letterObj->setVar("letter_cats", $letter_cats);
461
462
        // Form letter_account
463
        $letterObj->setVar("letter_account", $_REQUEST["letter_account"]);
464
        // Form letter_email_test
465
        $letterObj->setVar("letter_email_test", $_REQUEST["letter_email_test"]);
466
        // Form letter_submitter
467
        $letterObj->setVar("letter_submitter", XoopsRequest::getInt('letter_submitter', 0));
468
        // Form letter_created
469
        $letterObj->setVar("letter_created", XoopsRequest::getInt('letter_created', 0));
470
        if ($xnewsletter->getHandler('letter')->insert($letterObj)) {
471
            $letter_id = $letterObj->getVar("letter_id");
472
473
            //upload attachments
474
            $uploaded_files = array();
475
            include_once XOOPS_ROOT_PATH . "/class/uploader.php";
476
            $uploaddir = XOOPS_UPLOAD_PATH . $xnewsletter->getConfig('xn_attachment_path') . $letter_id . '/';
477 View Code Duplication
            if (!is_dir($uploaddir)) {
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...
478
                $indexFile = XOOPS_UPLOAD_PATH . "/index.html";
479
                mkdir($uploaddir, 0777);
480
                chmod($uploaddir, 0777);
481
                copy($indexFile, $uploaddir . "index.html");
482
            }
483
            $uploader = new XoopsMediaUploader($uploaddir, $xnewsletter->getConfig('xn_mimetypes'), $xnewsletter->getConfig('xn_maxsize'), null, null);
484
            for ($upl = 0 ;$upl < 5; ++$upl) {
485 View Code Duplication
                if ($uploader->fetchMedia($_POST['xoops_upload_file'][$upl])) {
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...
486
                    //$uploader->setPrefix("xn_") ; keep original name
487
                    $uploader->fetchMedia($_POST['xoops_upload_file'][$upl]);
488
                    if (!$uploader->upload()) {
489
                        $errors = $uploader->getErrors();
490
                        redirect_header("javascript:history.go(-1)", 3, $errors);
491
                    } else {
492
                        $uploaded_files[] = array("name" => $uploader->getSavedFileName(), "origname" => $uploader->getMediaType());
493
                    }
494
                }
495
            }
496
497
            // create items in attachments
498 View Code Duplication
            foreach ($uploaded_files as $file) {
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...
499
                $attachmentObj = $xnewsletter->getHandler('attachment')->create();
500
                //Form attachment_letter_id
501
                $attachmentObj->setVar("attachment_letter_id", $letter_id);
502
                //Form attachment_name
503
                $attachmentObj->setVar("attachment_name", $file["name"]);
504
                //Form attachment_type
505
                $attachmentObj->setVar("attachment_type", $file["origname"]);
506
                //Form attachment_submitter
507
                $attachmentObj->setVar("attachment_submitter", $xoopsUser->uid());
508
                //Form attachment_created
509
                $attachmentObj->setVar("attachment_created", time());
510
                $xnewsletter->getHandler('attachment')->insert($attachmentObj);
511
            }
512
            //create item in protocol
513
            $protocolObj = $xnewsletter->getHandler('protocol')->create();
514
            $protocolObj->setVar("protocol_letter_id", $letter_id);
515
            $protocolObj->setVar("protocol_subscriber_id", '0');
516
            $protocolObj->setVar("protocol_success", '1');
517
            $action = "";
518
            //$action = isset($_REQUEST["letter_action"]) ? $_REQUEST["letter_action"] : 0;
519
            $action = XoopsRequest::getInt('letter_action', 0);
520
            switch ($action) {
521
                case _AM_XNEWSLETTER_LETTER_ACTION_VAL_PREVIEW :
522
                    $url = "{$currentFile}?op=show_preview&letter_id={$letter_id}";
523
                    break;
524
                case _AM_XNEWSLETTER_LETTER_ACTION_VAL_SEND :
525
                    $url = "sendletter.php?op=send_letter&letter_id={$letter_id}";
526
                    break;
527
                case _AM_XNEWSLETTER_LETTER_ACTION_VAL_SENDTEST :
528
                    $url = "sendletter.php?op=send_test&letter_id={$letter_id}";
529
                    break;
530
                default:
531
                    $url = "{$currentFile}?op=list_letters";
532
                    break;
533
            }
534
            $protocolObj->setVar("protocol_status", _AM_XNEWSLETTER_LETTER_ACTION_SAVED);
535
            $protocolObj->setVar("protocol_submitter", $xoopsUser->uid());
536
            $protocolObj->setVar("protocol_created", time());
537
538
            if ($xnewsletter->getHandler('protocol')->insert($protocolObj)) {
539
                // create protocol is ok
540
                redirect_header($url, 3, _AM_XNEWSLETTER_FORMOK);
541
            }
542
        } else {
543
            echo "Error create protocol: " . $protocolObj->getHtmlErrors();
544
        }
545
        break;
546
547
    case "edit_letter" :
548
$xoopsOption['template_main'] = 'xnewsletter_letter.tpl'; // IN PROGRESS
549
        include_once XOOPS_ROOT_PATH . "/header.php";
550
551
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
552
        $xoTheme->addMeta('meta', 'keywords', $xnewsletter->getConfig('keywords')); // keywords only for index page
553
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
554
555
        // Breadcrumb
556
        $breadcrumb = new XnewsletterBreadcrumb();
557
        $breadcrumb->addLink($xnewsletter->getModule()->getVar('name'), XNEWSLETTER_URL);
558
        $breadcrumb->addLink(_MD_XNEWSLETTER_LIST, XNEWSLETTER_URL . '/letter.php?op=list_letters');
559
        $breadcrumb->addLink(_MD_XNEWSLETTER_LETTER_EDIT, '');
560
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
561
562
// IN PROGRESS FROM HERE
563
564
        $letterObj = $xnewsletter->getHandler('letter')->get($letter_id);
565
        $form = $letterObj->getForm();
566
        $content = $form->render();
567
        $xoopsTpl->assign('content', $content);
568
        break;
569
570
    case "delete_letter":
571
$xoopsOption['template_main'] = 'xnewsletter_letter.tpl'; // IN PROGRESS
572
        include_once XOOPS_ROOT_PATH . "/header.php";
573
574
        $xoTheme->addStylesheet(XNEWSLETTER_URL . '/assets/css/module.css');
575
        $xoTheme->addMeta('meta', 'keywords', $xnewsletter->getConfig('keywords')); // keywords only for index page
576
        $xoTheme->addMeta('meta', 'description', strip_tags(_MA_XNEWSLETTER_DESC)); // description
577
578
        // Breadcrumb
579
        $breadcrumb = new XnewsletterBreadcrumb();
580
        $breadcrumb->addLink($xnewsletter->getModule()->getVar('name'), XNEWSLETTER_URL);
581
        $breadcrumb->addLink(_MD_XNEWSLETTER_LIST, XNEWSLETTER_URL . '/letter.php?op=list_letters');
582
        $breadcrumb->addLink(_MD_XNEWSLETTER_LETTER_DELETE, '');
583
        $xoopsTpl->assign('xnewsletter_breadcrumb', $breadcrumb->render());
584
585
// IN PROGRESS FROM HERE
586
587
        $letterObj = $xnewsletter->getHandler('letter')->get($letter_id);
588
        if (isset($_REQUEST["ok"]) && $_REQUEST["ok"] == 1) {
589
            if ( !$GLOBALS["xoopsSecurity"]->check() ) {
590
                redirect_header($currentFile, 3, implode(",", $GLOBALS["xoopsSecurity"]->getErrors()));
591
            }
592
593
            if ($xnewsletter->getHandler('letter')->delete($letterObj)) {
594
                // delete protocol
595
                $sql = "DELETE FROM `{$xoopsDB->prefix("xnewsletter_protocol")}`";
596
                $sql.= " WHERE `protocol_letter_id`={$letter_id}";
597
                if(!$result = $xoopsDB->query($sql)) die("MySQL-Error: " . $xoopsDB->error());
598
599
                // delete attachments
600
                $attachmentCriteria = new CriteriaCompo();
601
                $attachmentCriteria->add(new Criteria("attachment_letter_id", $letter_id));
602
                $attachmentObjs = $xnewsletter->getHandler('attachment')->getAll($attachmentCriteria);
603 View Code Duplication
                foreach (array_keys($attachmentObjs) as $attachment_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...
604
                    $attachmentObj = $xnewsletter->getHandler('attachment')->get($attachment_id);
605
                    $attachment_name = $attachmentObj->getVar("attachment_name");
606
                    $xnewsletter->getHandler('attachment')->delete($attachmentObj, true);
607
                    // delete file
608
                    $uploaddir = XOOPS_UPLOAD_PATH . $xnewsletter->getConfig('xn_attachment_path') . $letter_id . "/";
609
                    unlink($uploaddir . $attachment_name);
610
                }
611
                redirect_header($currentFile, 3, _AM_XNEWSLETTER_FORMDELOK);
612
            } else {
613
                echo $letterObj->getHtmlErrors();
614
            }
615 View Code Duplication
        } else {
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...
616
            xoops_confirm(array("ok" => 1, "letter_id" => $letter_id, "op" => "delete_letter"), $_SERVER["REQUEST_URI"], sprintf(_AM_XNEWSLETTER_FORMSUREDEL, $letterObj->getVar("letter_title")));
617
        }
618
        break;
619
}
620
621
include __DIR__ . '/footer.php';
622