Completed
Push — master ( 819fc9...f42eea )
by Goffy
18s queued 11s
created

Template::getForm()   C

Complexity

Conditions 9
Paths 256

Size

Total Lines 67

Duplication

Lines 5
Ratio 7.46 %

Importance

Changes 0
Metric Value
cc 9
nc 256
nop 1
dl 5
loc 67
rs 5.9377
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace XoopsModules\Xnewsletter;
4
5
/**
6
 * ****************************************************************************
7
 *  - A Project by Developers TEAM For Xoops - ( https://xoops.org )
8
 * ****************************************************************************
9
 *  XNEWSLETTER - MODULE FOR XOOPS
10
 *  Copyright (c) 2007 - 2012
11
 *  Goffy ( wedega.com )
12
 *
13
 *  You may not change or alter any portion of this comment or credits
14
 *  of supporting developers from this source code or any supporting
15
 *  source code which is considered copyrighted (c) material of the
16
 *  original comment or credit authors.
17
 *
18
 *  This program is distributed in the hope that it will be useful,
19
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 *  GNU General Public License for more details.
22
 *  ---------------------------------------------------------------------------
23
 * @copyright  Goffy ( wedega.com )
24
 * @license    GNU General Public License 2.0
25
 * @package    xnewsletter
26
 * @author     Goffy ( [email protected] )
27
 *
28
 * ****************************************************************************
29
 */
30
31
//use XoopsModules\Xnewsletter;
32
33
require_once dirname(__DIR__) . '/include/common.php';
34
35
/**
36
 * Class Template
37
 */
38
class Template extends \XoopsObject
39
{
40
    public $helper = null;
41
    public $db;
42
43
    //Constructor
44
45 View Code Duplication
    public function __construct()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
46
    {
47
        $this->helper = Helper::getInstance();
48
        $this->db     = \XoopsDatabaseFactory::getDatabaseConnection();
49
        $this->initVar('template_id', XOBJ_DTYPE_INT, null, false);
50
        $this->initVar('template_title', XOBJ_DTYPE_TXTBOX, '', true, 100);
51
        $this->initVar('template_description', XOBJ_DTYPE_TXTAREA, '', false);
52
        $this->initVar('template_content', XOBJ_DTYPE_TXTAREA, '', true);
53
        $this->initVar('template_online', XOBJ_DTYPE_INT, null, false);
54
        $this->initVar('template_type', XOBJ_DTYPE_INT, null, false);
55
        $this->initVar('template_submitter', XOBJ_DTYPE_INT, null, false);
56
        $this->initVar('template_created', XOBJ_DTYPE_INT, time(), false);
57
    }
58
59
    /**
60
     * @param bool $action
61
     *
62
     * @return \XoopsThemeForm
63
     */
64
    public function getForm($action = false)
65
    {
66
        global $xoopsDB;
67
68
        if (false === $action) {
69
            $action = $_SERVER['REQUEST_URI'];
70
        }
71
72
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
73
        $title = $this->isNew() ? sprintf(_AM_XNEWSLETTER_TEMPLATE_ADD) : sprintf(_AM_XNEWSLETTER_TEMPLATE_EDIT);
74
        $form  = new \XoopsThemeForm($title, 'form', $action, 'post', true);
75
        $form->setExtra('enctype="multipart/form-data"');
76
77
        // template_title
78 View Code Duplication
        if ( $this->getVar('template_type') == _XNEWSLETTER_MAILINGLIST_TPL_FILE_VAL ) {
0 ignored issues
show
Duplication introduced by
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...
79
            $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_TEMPLATE_TITLE, $this->getVar('template_title')));
80
        } else {
81
            $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_TEMPLATE_TITLE, 'template_title', 50, 255, $this->getVar('template_title', 'e')), true);
82
        }
83
84
        // template_description
85
        if ( $this->getVar('template_type') == _XNEWSLETTER_MAILINGLIST_TPL_FILE_VAL ) {
86
            $form->addElement(new \XoopsFormHidden('template_description', '-'));
87
        } else {
88
            $template_description_textarea = new \XoopsFormTextArea(_AM_XNEWSLETTER_TEMPLATE_DESCRIPTION, 'template_description', $this->getVar('template_description', 'e'), 5, 50);
89
            $template_description_textarea->setDescription(_AM_XNEWSLETTER_TEMPLATE_DESCRIPTION_DESC);
90
            $form->addElement($template_description_textarea, false);
91
        }
92
93
        // template_content
94
        if ( $this->getVar('template_type') == _XNEWSLETTER_MAILINGLIST_TPL_FILE_VAL ) {
95
            $form->addElement(new \XoopsFormHidden('template_content', '-'));
96
        } else {
97
            $editor_configs = [];
98
            $editor_configs['name'] = 'template_content';
99
            $editor_configs['value'] = $this->getVar('template_content', 'e');
100
            $editor_configs['rows'] = 40;
101
            $editor_configs['cols'] = 80;
102
            $editor_configs['width'] = '100%';
103
            $editor_configs['height'] = '800px';
104
            $editor_configs['editor'] = $this->helper->getConfig('template_editor');
105
            $template_content_editor = new \XoopsFormEditor(_AM_XNEWSLETTER_TEMPLATE_CONTENT, 'template_content', $editor_configs);
106
            $template_content_editor->setDescription(_AM_XNEWSLETTER_TEMPLATE_CONTENT_DESC);
107
            $form->addElement($template_content_editor, true);
108
        }
109
110
        $templateOnline = $this->isNew() ? 1 : $this->getVar('template_online');
111
        $form->addElement(new \XoopsFormRadioYN(_AM_XNEWSLETTER_TEMPLATE_ONLINE, 'template_online', $templateOnline, _YES, _NO));
112
113
        $templateType = $this->isNew() ? _XNEWSLETTER_MAILINGLIST_TPL_CUSTOM_VAL : $this->getVar('template_type');
114
        $form->addElement(new \XoopsFormHidden('template_type', $templateType));
115
116
        $time = $this->isNew() ? time() : $this->getVar('template_created');
117
        $form->addElement(new \XoopsFormHidden('template_submitter', $GLOBALS['xoopsUser']->uid()));
118
        $form->addElement(new \XoopsFormHidden('template_created', $time));
119
120
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_SUBMITTER, $GLOBALS['xoopsUser']->uname()));
121
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_CREATED, formatTimestamp($time, 's')));
122
123
        //$form->addElement(new \XoopsFormSelectUser(_AM_XNEWSLETTER_SUBMITTER, "template_submitter", false, $this->getVar("template_submitter"), 1, false), true);
124
        //$form->addElement(new \XoopsFormTextDateSelect(_AM_XNEWSLETTER_CREATED, "template_created", "", $this->getVar("template_created")));
125
126
        $form->addElement(new \XoopsFormHidden('op', 'save_template'));
127
        $form->addElement(new \XoopsFormButtonTray('', _SUBMIT, 'submit', '', false));
128
129
        return $form;
130
    }
131
132
    /**
133
     * Get Values
134
     * @param null $keys
135
     * @param string|null $format
136
     * @param int|null $maxDepth
137
     * @return array
138
     */
139
    public function getValuesTemplate($keys = null, $format = null, $maxDepth = null)
140
    {
141
        $ret = $this->getValues($keys, $format, $maxDepth);
142
        $ret['id']          = $this->getVar('template_id');
143
        $ret['title']       = $this->getVar('template_title');
144
        $ret['description'] = $this->getVar('template_description');
145
        $ret['content']     = $this->getVar('template_content');
146
        $ret['online']      = $this->getVar('template_online');
147
        $ret['online_text'] = $this->getVar('template_online') == 1 ? _YES : _NO;
148
        $ret['type']        = $this->getVar('template_type');
149
        if ( $this->getVar('template_type') == _XNEWSLETTER_MAILINGLIST_TPL_FILE_VAL ) {
150
            $ret['type_text'] = _AM_XNEWSLETTER_TEMPLATE_TYPE_FILE;
151
        } else {
152
            $ret['type_text'] = _AM_XNEWSLETTER_TEMPLATE_TYPE_CUSTOM;
153
        }
154
155
        $ret['created']     = formatTimestamp($this->getVar('template_created'), 's');
156
        $ret['submitter']   = \XoopsUser::getUnameFromId($this->getVar('template_submitter'));
157
        return $ret;
158
    }
159
}
160