Completed
Pull Request — master (#29)
by Goffy
01:40
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 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...
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
                    if (!file_exists ( $filename )) {
116
                        $template['template_err'] = true;
117
                        $template['template_err_text'] = str_replace('%s', $template_path, _AM_XNEWSLETTER_TEMPLATE_ERR_FILE);
118
                    }
119
                }
120
                $GLOBALS['xoopsTpl']->append('templates_list', $template);
121
                unset($template);
122
            }
123
        } else {
124
            $GLOBALS['xoopsTpl']->assign('error', _AM_XNEWSLETTER_THEREARENT_TEMPLATE);
125
        }
126
        break;
127
    case 'new_template':
128
        $adminObject->displayNavigation($currentFile);
129
        $adminObject->addItemButton(_AM_XNEWSLETTER_TEMPLATELIST, '?op=list', 'list');
130
        $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->renderButton('left'));
131
132
        $templateObj = $helper->getHandler('Template')->create();
133
        $form        = $templateObj->getForm();
134
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
135
        break;
136
    case 'save_template':
137
        if (!$GLOBALS['xoopsSecurity']->check()) {
138
            redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
139
        }
140
        $templateObj = $helper->getHandler('Template')->get($templateId);
141
        $templateObj->setVar('template_title',       Request::getString('template_title', ''));
142
        $templateObj->setVar('template_description', Request::getString('template_description', ''));
143
        $templateObj->setVar('template_content',     Request::getText('template_content', ''));
144
        $templateObj->setVar('template_online',      Request::getInt('template_online', 0));
145
        $templateObj->setVar('template_type',        Request::getInt('template_type', 0));
146
        $templateObj->setVar('template_submitter',   Request::getInt('template_submitter', 0));
147
        $templateObj->setVar('template_created',     Request::getInt('template_created', time()));
148
149
        if ($helper->getHandler('Template')->insert($templateObj)) {
150
            redirect_header('?op=list', 3, _AM_XNEWSLETTER_FORMOK);
151
        }
152
153
        $GLOBALS['xoopsTpl']->assign('error', $templateObj->getHtmlErrors());
154
        $form = $templateObj->getForm();
155
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
156
        break;
157 View Code Duplication
    case 'edit_template':
158
        $adminObject->displayNavigation($currentFile);
159
        $adminObject->addItemButton(_AM_XNEWSLETTER_NEWTEMPLATE, '?op=new_template', 'add');
160
        $adminObject->addItemButton(_AM_XNEWSLETTER_TEMPLATELIST, '?op=list', 'list');
161
        $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->renderButton('left'));
162
163
        $templateObj = $helper->getHandler('Template')->get($templateId);
164
        $form        = $templateObj->getForm();
165
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
166
        break;
167 View Code Duplication
    case 'delete_template':
168
        $templateObj = $helper->getHandler('Template')->get($templateId);
169
        if (true === Request::getBool('ok', false, 'POST')) {
170
            if (!$GLOBALS['xoopsSecurity']->check()) {
171
                redirect_header($currentFile, 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
172
            }
173
            if ($helper->getHandler('Template')->delete($templateObj)) {
174
                redirect_header($currentFile, 3, _AM_XNEWSLETTER_FORMDELOK);
175
            } else {
176
                $GLOBALS['xoopsTpl']->assign('error', $templateObj->getHtmlErrors());
177
            }
178
        } else {
179
            xoops_confirm(['ok' => true, 'template_id' => $templateId, 'op' => 'delete_template'], $_SERVER['REQUEST_URI'], sprintf(_AM_XNEWSLETTER_FORMSUREDEL, $templateObj->getVar('template_title')));
180
        }
181
        break;
182
    case 'state_template':
183
        $templateObj = $helper->getHandler('Template')->get($templateId);
184
        $templateObj->setVar('template_online', Request::getInt('template_online', 0));
185
        if ($helper->getHandler('Template')->insert($templateObj)) {
186
            redirect_header('?op=list', 3, _AM_XNEWSLETTER_FORMOK);
187
        }
188
        $GLOBALS['xoopsTpl']->assign('error', $templateObj->getHtmlErrors());
189
        break;
190
}
191
require_once __DIR__ . '/admin_footer.php';
192