Completed
Push — master ( 592643...f454c2 )
by Michael
03:05 queued 01:21
created

Letter::getForm()   F

Complexity

Conditions 26
Paths > 20000

Size

Total Lines 200

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 26
nc 221200
nop 2
dl 0
loc 200
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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    GPL 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 Letter
37
 */
38
class Letter extends \XoopsObject
39
{
40
    public $helper = null;
41
42
    //Constructor
43
44
    public function __construct()
45
    {
46
        $this->helper = Helper::getInstance();
47
        $this->db     = \XoopsDatabaseFactory::getDatabaseConnection();
48
        $this->initVar('letter_id', XOBJ_DTYPE_INT, null, false);
49
        $this->initVar('letter_title', XOBJ_DTYPE_TXTBOX, null, true, 100);
50
        $this->initVar('letter_content', XOBJ_DTYPE_TXTAREA, null, true);
51
        $this->initVar('letter_template', XOBJ_DTYPE_TXTBOX, null, false, 100);
52
        $this->initVar('letter_cats', XOBJ_DTYPE_TXTBOX, null, false, 100); // IN PROGRESS: AN ARRAY SHOULD BE BETTER
53
        $this->initVar('letter_attachment', XOBJ_DTYPE_TXTBOX, null, false, 200);
54
        $this->initVar('letter_account', XOBJ_DTYPE_INT, null, false);
55
        $this->initVar('letter_email_test', XOBJ_DTYPE_TXTBOX, null, false, 100);
56
        $this->initVar('letter_submitter', XOBJ_DTYPE_INT, null, false);
57
        $this->initVar('letter_created', XOBJ_DTYPE_INT, time(), false); // timestamp
58
        $this->initVar('letter_sender', XOBJ_DTYPE_INT, null, false);
59
        $this->initVar('letter_sent', XOBJ_DTYPE_INT, false, false); // timestamp or false
60
    }
61
62
    /**
63
     * @param bool $action
64
     * @param bool $admin_aerea
65
     *
66
     * @return null|\XoopsThemeForm
67
     */
68
    public function getForm($action = false, $admin_aerea = false)
69
    {
70
        global $xoopsDB, $xoopsUser, $pathImageIcon;
71
72
        if (false === $action) {
73
            $action = $_SERVER['REQUEST_URI'];
74
        }
75
76
        $title = $this->isNew() ? sprintf(_AM_XNEWSLETTER_LETTER_ADD) : sprintf(_AM_XNEWSLETTER_LETTER_EDIT);
77
78
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
79
        $form = new \XoopsThemeForm($title, 'form', $action, 'post', true);
80
        $form->setExtra('enctype="multipart/form-data"');
81
82
        // letter_title
83
        $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_LETTER_TITLE, 'letter_title', 50, 255, $this->getVar('letter_title', 'e')), true);
84
85
        // letter_content
86
        $editor_configs           = [];
87
        $editor_configs['name']   = 'letter_content';
88
        $editor_configs['value']  = $this->getVar('letter_content', 'e');
89
        $editor_configs['rows']   = 40;
90
        $editor_configs['cols']   = 80;
91
        $editor_configs['width']  = '100%';
92
        $editor_configs['height'] = '800px';
93
        $editor_configs['editor'] = $this->helper->getConfig('xnewsletter_editor');
94
        $letter_content_editor    = new \XoopsFormEditor(_AM_XNEWSLETTER_LETTER_CONTENT, 'letter_content', $editor_configs);
95
        $letter_content_editor->setDescription(_AM_XNEWSLETTER_LETTER_CONTENT_DESC);
96
        $form->addElement($letter_content_editor, true);
97
98
        // letter_template
99
        $letter_template = '' == $this->getVar('letter_template') ? 'basic' : $this->getVar('letter_template');
100
        $template_select = new \XoopsFormSelect(_AM_XNEWSLETTER_LETTER_TEMPLATE, 'letter_template', $letter_template);
101
        // get template files in /modules/xnewsletter/language/{language}/templates/ directory
102
        $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/' . $GLOBALS['xoopsConfig']['language'] . '/templates/';
103
        if (!is_dir($template_path)) {
104
            $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/english/templates/';
105
        }
106
        $templateFiles = [];
107
        if (!$dirHandler = @opendir($template_path)) {
108
            die(str_replace('%p', $template_path, _AM_XNEWSLETTER_SEND_ERROR_INALID_TEMPLATE_PATH));
109
        }
110
        while ($filename = readdir($dirHandler)) {
111
            if (('.' !== $filename) and ('..' !== $filename) and ('index.html' !== $filename)) {
112
                $info            = pathinfo($filename);
113
                $templateFiles[] = basename($filename, '.' . $info['extension']);
114
            }
115
        }
116
        closedir($dirHandler);
117
        foreach ($templateFiles as $templateFile) {
118
            $template_select->addOption($templateFile, 'file:' . $templateFile);
119
        }
120
        // get template objects from database
121
        $templateCriteria = new \CriteriaCompo();
122
        $templateCriteria->setSort('template_title DESC, template_id');
123
        $templateCriteria->setOrder('DESC');
124
        $templateCount = $this->helper->getHandler('Template')->getCount();
125
        if ($templateCount > 0) {
126
            $templateObjs = $this->helper->getHandler('Template')->getAll($templateCriteria);
127
            foreach ($templateObjs as $templateObj) {
128
                $template_select->addOption('db:' . $templateObj->getVar('template_id'), 'db:' . $templateObj->getVar('template_title'));
129
            }
130
        }
131
        $form->addElement($template_select, false);
132
133
        // letter_cats
134
        $grouppermHandler = xoops_getHandler('groupperm');
135
        $memberHandler    = xoops_getHandler('member');
136
        $groups           = $memberHandler->getGroupsByUser($xoopsUser->uid());
137
        $catCriteria      = new \CriteriaCompo();
138
        $catCriteria->setSort('cat_id');
139
        $catCriteria->setOrder('ASC');
140
        $letter_cats = explode('|', $this->getVar('letter_cats'));
141
        $cat_select  = new \XoopsFormCheckBox(_AM_XNEWSLETTER_LETTER_CATS, 'letter_cats', $letter_cats);
142
        $catObjs     = $this->helper->getHandler('Cat')->getAll($catCriteria);
143
        foreach ($catObjs as $cat_id => $catObj) {
144
            $cat_name = $catObj->getVar('cat_name');
145
            $show     = $grouppermHandler->checkRight('newsletter_create_cat', $cat_id, $groups, $this->helper->getModule()->mid());
146
            if (1 == $show) {
147
                $cat_select->addOption($cat_id, $cat_name);
148
            }
149
        }
150
        $form->addElement($cat_select, true);
151
152
        // attachments
153
        $attachment_tray = new \XoopsFormElementTray(_AM_XNEWSLETTER_LETTER_ATTACHMENT, '<br>');
154
        $attachment_tray->addElement(new \XoopsFormHidden('deleted_attachment_id', ''));
155
        // existing_attachments
156
        if ($this->isNew()) {
157
            $attachmentObjs = [];
158
        } else {
159
            $attachmentCriteria = new \CriteriaCompo();
160
            $attachmentCriteria->add(new \Criteria('attachment_letter_id', $this->getVar('letter_id')));
161
            $attachmentCriteria->setSort('attachment_id');
162
            $attachmentCriteria->setOrder('ASC');
163
            $attachmentObjs = $this->helper->getHandler('Attachment')->getAll($attachmentCriteria);
164
        }
165
        $i                      = 1;
166
        $remove_attachment_tray = [];
0 ignored issues
show
Unused Code introduced by
$remove_attachment_tray is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
167
        foreach ($attachmentObjs as $attachment_id => $attachmentObj) {
168
            $delete_attachment_tray = new \XoopsFormElementTray('', '&nbsp;&nbsp;');
169
            $mode_select            = new \XoopsFormRadio(_AM_XNEWSLETTER_ATTACHMENT_MODE, "existing_attachments_mode[{$attachment_id}]", $attachmentObj->getVar('attachment_mode'), '&nbsp;');
170
            $mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_ASATTACHMENT, _AM_XNEWSLETTER_ATTACHMENT_MODE_ASATTACHMENT);
171
            $mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_ASLINK, _AM_XNEWSLETTER_ATTACHMENT_MODE_ASLINK);
172
            //$mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_AUTO, _AM_XNEWSLETTER_ATTACHMENT_MODE_AUTO); // for future features
173
            $delete_attachment_tray->addElement($mode_select);
174
            $delete_attachment_tray->addElement(new \XoopsFormLabel('', $attachmentObj->getVar('attachment_name')));
175
            $delete_button = new \XoopsFormButton('', "delete_attachment_{$i}", _DELETE, 'submit');
176
            $delete_button->setExtra("onclick='this.form.elements.op.value=\"delete_attachment\";this.form.elements.deleted_attachment_id.value=\"{$attachment_id}\";'");
177
            $delete_attachment_tray->addElement($delete_button);
178
            $attachment_tray->addElement($delete_attachment_tray);
179
            ++$i;
180
            unset($mode_select);
181
            unset($delete_attachment_tray);
182
        }
183
        // new_attachments
184
        for ($j = $i; $j < ($this->helper->getConfig('xn_maxattachments') + 1); ++$j) {
185
            $add_attachment_tray = new \XoopsFormElementTray('', '&nbsp;&nbsp;');
186
            $mode_select         = new \XoopsFormRadio(_AM_XNEWSLETTER_ATTACHMENT_MODE, "new_attachments_mode[{$j}]", _XNEWSLETTER_ATTACHMENTS_MODE_ASATTACHMENT, '&nbsp;');
187
            $mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_ASATTACHMENT, _AM_XNEWSLETTER_ATTACHMENT_MODE_ASATTACHMENT);
188
            $mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_ASLINK, _AM_XNEWSLETTER_ATTACHMENT_MODE_ASLINK);
189
            //$mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_AUTO, _AM_XNEWSLETTER_ATTACHMENT_MODE_AUTO); // for future features
190
            $add_attachment_tray->addElement($mode_select);
191
            $add_attachment_tray->addElement(new \XoopsFormFile('', "new_attachment_index={$j}", $this->helper->getConfig('xn_maxsize')));
192
            $attachment_tray->addElement($add_attachment_tray);
193
            unset($mode_select);
194
            unset($add_attachment_tray);
195
        }
196
        $form->addElement($attachment_tray);
197
198
        // letter_action
199
        $opt_nextaction = new \XoopsFormRadio('', 'letter_action', _AM_XNEWSLETTER_LETTER_ACTION_NO, '<br>');
200
        $opt_nextaction->addOption(_XNEWSLETTER_LETTER_ACTION_VAL_NO, _AM_XNEWSLETTER_LETTER_ACTION_NO);
201
        $opt_nextaction->addOption(_XNEWSLETTER_LETTER_ACTION_VAL_PREVIEW, _AM_XNEWSLETTER_LETTER_ACTION_PREVIEW);
202
        $opt_nextaction->addOption(_XNEWSLETTER_LETTER_ACTION_VAL_SEND, _AM_XNEWSLETTER_LETTER_ACTION_SEND);
203
        $opt_nextaction->addOption(_XNEWSLETTER_LETTER_ACTION_VAL_SENDTEST, _AM_XNEWSLETTER_LETTER_ACTION_SENDTEST);
204
        $opt_tray = new \XoopsFormElementTray(_AM_XNEWSLETTER_LETTER_ACTION, '<br>');
205
        $opt_tray->addElement($opt_nextaction);
206
207
        // letter_email_test
208
        $letter_email_test = $this->isNew() ? $xoopsUser->email() : $this->getVar('letter_email_test');
209
        if ('' == $letter_email_test) {
210
            $letter_email_test = $xoopsUser->email();
211
        }
212
        $opt_tray->addElement(new \XoopsFormText(_AM_XNEWSLETTER_LETTER_EMAIL_TEST . ':&nbsp;', 'letter_email_test', 50, 255, $letter_email_test), false);
213
        $form->addElement($opt_tray);
214
215
        // letter_account
216
        $accountsCriteria = new \CriteriaCompo();
217
        $accountsCriteria->setSort('accounts_id');
218
        $accountsCriteria->setOrder('ASC');
219
        $accountsCount   = $this->helper->getHandler('Accounts')->getCount($accountsCriteria);
220
        $account_default = 0;
221
        if ($this->isNew()) {
222
            $accountsObjs = $this->helper->getHandler('Accounts')->getAll($accountsCriteria);
223
            foreach ($accountsObjs as $accountsObj) {
224
                if (1 == $accountsObj->getVar('accounts_default')) {
225
                    $account_default = $accountsObj->getVar('accounts_id');
226
                }
227
            }
228
        } else {
229
            $account_default = $this->getVar('letter_account');
230
        }
231
        if (1 == $accountsCount) {
232
            $form->addElement(new \XoopsFormHidden('letter_account', $account_default));
233
        } else {
234
            $accounts_list = $this->helper->getHandler('Accounts')->getList($accountsCriteria);
235
236
            if (true === $admin_aerea) {
237
                $opt_accounts = new \XoopsFormRadio(_AM_XNEWSLETTER_LETTER_ACCOUNTS_AVAIL, 'letter_account', $account_default);
238
                $opt_accounts->addOptionArray($accounts_list);
239
                $form->addElement($opt_accounts, false);
240
            } else {
241
                $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_LETTER_ACCOUNTS_AVAIL, $accounts_list[$account_default]));
242
                $form->addElement(new \XoopsFormHidden('letter_account', $account_default));
243
            }
244
        }
245
246
        if ($this->isNew()) {
247
            $time           = time();
248
            $submitter_uid  = $GLOBALS['xoopsUser']->uid();
249
            $submitter_name = $GLOBALS['xoopsUser']->uname();
250
        } else {
251
            $time          = $this->getVar('letter_created');
252
            $submitter_uid = $this->getVar('letter_submitter');
253
            xoops_load('xoopsuserutility');
254
            $submitter_name = \XoopsUserUtility::getUnameFromId($submitter_uid);
255
        }
256
257
        $form->addElement(new \XoopsFormHidden('letter_submitter', $submitter_uid));
258
        $form->addElement(new \XoopsFormHidden('letter_created', $time));
259
260
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_LETTER_SUBMITTER, $submitter_name));
261
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_LETTER_CREATED, formatTimestamp($time, 's')));
262
263
        $form->addElement(new \XoopsFormHidden('op', 'save_letter'));
264
        $form->addElement(new \XoopsFormButton('', 'submit', _AM_XNEWSLETTER_SAVE, 'submit'));
265
266
        return $form;
267
    }
268
}
269