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

admin/template.php (1 issue)

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    GNU General Public License 2.0
22
 * @package    xnewsletter
23
 * @author     Goffy ( [email protected] )
24
 *
25
 * ****************************************************************************
26
 */
27
28
use Xmf\Request;
29
30
$currentFile = basename(__FILE__);
31
require_once __DIR__ . '/admin_header.php';
32
xoops_cp_header();
33
34
// set template
35
$templateMain = 'xnewsletter_admin_templates.tpl';
36
37
// We recovered the value of the argument op in the URL$
38
$op         = Request::getString('op', 'list');
39
$templateId = Request::getInt('template_id', 0);
40
41
$GLOBALS['xoopsTpl']->assign('xnewsletter_url', XNEWSLETTER_URL);
42
$GLOBALS['xoopsTpl']->assign('xnewsletter_icons_url', XNEWSLETTER_ICONS_URL);
43
44
switch ($op) {
45
    case 'list':
46
    case 'list_templates':
47
    default:
48
        $adminObject->displayNavigation($currentFile);
49
        $adminObject->addItemButton(_AM_XNEWSLETTER_NEWTEMPLATE, '?op=new_template', 'add');
50
        $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->renderButton('left'));
51
52
        //check file templates
53
        $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/' . $GLOBALS['xoopsConfig']['language'] . '/templates/';
54
        if (!is_dir($template_path)) {
55
            $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/english/templates/';
56
        }
57
        $templateFiles = [];
58
        if (!$dirHandler = @opendir($template_path)) {
59
            die(str_replace('%p', $template_path, _AM_XNEWSLETTER_SEND_ERROR_INALID_TEMPLATE_PATH));
60
        }
61
        while ($filename = readdir($dirHandler)) {
62
            if (('.' !== $filename) and ('..' !== $filename) and ('index.html' !== $filename)) {
63
                $template_title = str_replace('.tpl', '', $filename);
64
                $templateCriteria = new \CriteriaCompo();
65
                $templateCriteria->add(new \Criteria('template_title', $template_title));
66
                $templatesCount = $helper->getHandler('Template')->getCount($templateCriteria);
67
                if ($templatesCount == 0){
68
                    $templateObj = $helper->getHandler('Template')->create();
69
                    $templateObj->setVar('template_title',       Request::getString('template_title', $template_title));
70
                    $templateObj->setVar('template_description', Request::getString('template_description', '-'));
71
                    $templateObj->setVar('template_content',     Request::getText('template_content', '-'));
72
                    $templateObj->setVar('template_online',      Request::getInt('template_online', 1));
73
                    $templateObj->setVar('template_type',        Request::getInt('template_type', _XNEWSLETTER_MAILINGLIST_TPL_FILE_VAL));
74
                    $templateObj->setVar('template_submitter',   Request::getInt('template_submitter', $GLOBALS['xoopsUser']->uid()));
75
                    $templateObj->setVar('template_created',     Request::getInt('template_created', time()));
76
77
                    if ($helper->getHandler('Template')->insert($templateObj)) {
78
                        redirect_header('?op=list', 3, _AM_XNEWSLETTER_FORMOK);
79
                    }
80
                    $GLOBALS['xoopsTpl']->assign('error', $templateObj->getHtmlErrors());
81
                }
82
                unset($templateCriteria);
83
                unset($templateObj);
84
            }
85
        }
86
        closedir($dirHandler);
87
88
        // read template table
89
        $start          = Request::getInt('start', 0);
90
        $limit            = $helper->getConfig('adminperpage');
91
        $templateCriteria = new \CriteriaCompo();
92
        $templateCriteria->setSort('template_type ASC, template_id');
93
        $templateCriteria->setOrder('DESC');
94
        $templatesCount = $helper->getHandler('Template')->getCount();
95 View Code Duplication
        if ($templatesCount > $limit) {
96
            require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
97
            $pagenav = new \XoopsPageNav($templatesCount, $limit, $start, 'start', 'op=list');
98
            $GLOBALS['xoopsTpl']->assign('pagenav', $pagenav->renderNav(4));
99
        }
100
        if ($templatesCount > 0) {
101
            $templateCriteria->setStart($start);
102
            $templateCriteria->setLimit($limit);
103
            $templatesAll = $helper->getHandler('Template')->getAll($templateCriteria);
104
            $GLOBALS['xoopsTpl']->assign('templatesCount', $templatesCount);
105
            foreach ($templatesAll as $id => $templateObj) {
106
                $template = $templateObj->getValuesTemplate();
107
                // check whether template exist or not
108
                $template['template_err'] = false;
109
                if ( $templateObj->getVar('template_type') === _XNEWSLETTER_MAILINGLIST_TPL_FILE_VAL) {
110
                    $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/' . $GLOBALS['xoopsConfig']['language'] . '/templates/';
111
                    if (!is_dir($template_path)) {
112
                        $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/english/templates/';
113
                    }
114
                    $filename = $template_path . $templateObj->getVar('template_title') . '.tpl';
115 View Code Duplication
                    if (!file_exists ( $filename )) {
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...
116
                        $template['template_err'] = true;
117
                        $template['template_err_text'] = str_replace('%s', $template_path, _AM_XNEWSLETTER_TEMPLATE_ERR_FILE);
118
                    }
119
                    $template['type_text'] = $template['type_text'] . ' *';
120
                }
121
                $GLOBALS['xoopsTpl']->append('templates_list', $template);
122
                unset($template);
123
            }
124
        } else {
125
            $GLOBALS['xoopsTpl']->assign('error', _AM_XNEWSLETTER_THEREARENT_TEMPLATE);
126
        }
127
        $GLOBALS['xoopsTpl']->assign('template_file_info', str_replace('%s', $template_path, _AM_XNEWSLETTER_TEMPLATE_TYPE_FILE_INFO));
128
        break;
129
    case 'new_template':
130
        $adminObject->displayNavigation($currentFile);
131
        $adminObject->addItemButton(_AM_XNEWSLETTER_TEMPLATELIST, '?op=list', 'list');
132
        $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->renderButton('left'));
133
134
        $templateObj = $helper->getHandler('Template')->create();
135
        $form        = $templateObj->getForm();
136
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
137
        break;
138
    case 'save_template':
139
        if (!$GLOBALS['xoopsSecurity']->check()) {
140
            redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
141
        }
142
        $templateObj = $helper->getHandler('Template')->get($templateId);
143
        $templateObj->setVar('template_title',       Request::getString('template_title', ''));
144
        $templateObj->setVar('template_description', Request::getString('template_description', ''));
145
        $templateObj->setVar('template_content',     Request::getText('template_content', ''));
146
        $templateObj->setVar('template_online',      Request::getInt('template_online', 0));
147
        $templateObj->setVar('template_type',        Request::getInt('template_type', 0));
148
        $templateObj->setVar('template_submitter',   Request::getInt('template_submitter', 0));
149
        $templateObj->setVar('template_created',     Request::getInt('template_created', time()));
150
151
        if ($helper->getHandler('Template')->insert($templateObj)) {
152
            redirect_header('?op=list', 3, _AM_XNEWSLETTER_FORMOK);
153
        }
154
155
        $GLOBALS['xoopsTpl']->assign('error', $templateObj->getHtmlErrors());
156
        $form = $templateObj->getForm();
157
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
158
        break;
159 View Code Duplication
    case 'edit_template':
160
        $adminObject->displayNavigation($currentFile);
161
        $adminObject->addItemButton(_AM_XNEWSLETTER_NEWTEMPLATE, '?op=new_template', 'add');
162
        $adminObject->addItemButton(_AM_XNEWSLETTER_TEMPLATELIST, '?op=list', 'list');
163
        $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->renderButton('left'));
164
165
        $templateObj = $helper->getHandler('Template')->get($templateId);
166
        $form        = $templateObj->getForm();
167
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
168
        break;
169 View Code Duplication
    case 'delete_template':
170
        $templateObj = $helper->getHandler('Template')->get($templateId);
171
        if (true === Request::getBool('ok', false, 'POST')) {
172
            if (!$GLOBALS['xoopsSecurity']->check()) {
173
                redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
174
            }
175
            if ($helper->getHandler('Template')->delete($templateObj)) {
176
                redirect_header($currentFile, 3, _AM_XNEWSLETTER_FORMDELOK);
177
            } else {
178
                $GLOBALS['xoopsTpl']->assign('error', $templateObj->getHtmlErrors());
179
            }
180
        } else {
181
            xoops_confirm(['ok' => true, 'template_id' => $templateId, 'op' => 'delete_template'], $_SERVER['REQUEST_URI'], sprintf(_AM_XNEWSLETTER_FORMSUREDEL, $templateObj->getVar('template_title')));
182
        }
183
        break;
184
    case 'state_template':
185
        $templateObj = $helper->getHandler('Template')->get($templateId);
186
        $templateObj->setVar('template_online', Request::getInt('template_online', 0));
187
        if ($helper->getHandler('Template')->insert($templateObj)) {
188
            redirect_header('?op=list', 3, _AM_XNEWSLETTER_FORMOK);
189
        }
190
        $GLOBALS['xoopsTpl']->assign('error', $templateObj->getHtmlErrors());
191
        break;
192
    case 'set_template':
193
        $templateId = Request::getInt('template_id', 0);
194
        if ($templateId > 0) {
195
            $letterCount = $helper->getHandler('Letter')->getCount();
196
            if ($letterCount > 0) {
197
                $letterAll = $helper->getHandler('Letter')->getAll();
198
                foreach ($letterAll as $id => $letterObj) {
199
                    $letterObj->setVar('letter_templateid', $templateId);
200
                     if ($helper->getHandler('Letter')->insert($letterObj)) {
201
                        
202
                    } else {
203
                        $GLOBALS['xoopsTpl']->assign('error', $letterObj->getHtmlErrors());
204
                    }
205
                }
206
            }
207
            redirect_header('letter.php?op=list_letters', 3, _AM_XNEWSLETTER_FORMOK);
208
        } else {
209
            // should not be
210
            $GLOBALS['xoopsTpl']->assign('error', 'Invalid parameter template id');
211
        }
212
        break;
213
}
214
require_once __DIR__ . '/admin_footer.php';
215