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

Letter   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 245
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
F getForm() 0 183 18
A getValuesLetter() 0 21 4
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
    public $db;
42
43
    //Constructor
44
45
    public function __construct()
46
    {
47
        $this->helper = Helper::getInstance();
48
        $this->db     = \XoopsDatabaseFactory::getDatabaseConnection();
49
        $this->initVar('letter_id',        XOBJ_DTYPE_INT,    null, false);
50
        $this->initVar('letter_title',     XOBJ_DTYPE_TXTBOX, null, true, 100);
51
        $this->initVar('letter_content',   XOBJ_DTYPE_TXTAREA,null, true);
52
        $this->initVar('letter_templateid',XOBJ_DTYPE_INT,    null, false);
53
        $this->initVar('letter_cats',      XOBJ_DTYPE_TXTBOX, null, false, 100); // IN PROGRESS: AN ARRAY SHOULD BE BETTER
54
        $this->initVar('letter_attachment',XOBJ_DTYPE_TXTBOX, null, false, 200);
55
        $this->initVar('letter_account',   XOBJ_DTYPE_INT,    null, false);
56
        $this->initVar('letter_email_test',XOBJ_DTYPE_TXTBOX, null, false, 100);
57
        $this->initVar('letter_submitter', XOBJ_DTYPE_INT,    null, false);
58
        $this->initVar('letter_created',   XOBJ_DTYPE_INT,     time(), false); // timestamp
59
        $this->initVar('letter_sender',    XOBJ_DTYPE_INT,    null, false);
60
        $this->initVar('letter_sent',      XOBJ_DTYPE_INT,    false, false); // timestamp or false
61
    }
62
63
    /**
64
     * @param bool $action
65
     * @param bool $admin_aerea
66
     *
67
     * @return null|\XoopsThemeForm
68
     */
69
    public function getForm($action = false, $admin_aerea = false)
70
    {
71
        global $xoopsDB, $xoopsUser, $pathImageIcon;
72
73
        if (false === $action) {
74
            $action = $_SERVER['REQUEST_URI'];
75
        }
76
77
        $title = $this->isNew() ? sprintf(_AM_XNEWSLETTER_LETTER_ADD) : sprintf(_AM_XNEWSLETTER_LETTER_EDIT);
78
79
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
80
        $form = new \XoopsThemeForm($title, 'form', $action, 'post', true);
81
        $form->setExtra('enctype="multipart/form-data"');
82
83
        // letter_title
84
        $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_LETTER_TITLE, 'letter_title', 50, 255, $this->getVar('letter_title', 'e')), true);
85
86
        // letter_content
87
        $editor_configs           = [];
88
        $editor_configs['name']   = 'letter_content';
89
        $editor_configs['value']  = $this->getVar('letter_content', 'e');
90
        $editor_configs['rows']   = 40;
91
        $editor_configs['cols']   = 80;
92
        $editor_configs['width']  = '100%';
93
        $editor_configs['height'] = '800px';
94
        $editor_configs['editor'] = $this->helper->getConfig('xnewsletter_editor');
95
        $letter_content_editor    = new \XoopsFormEditor(_AM_XNEWSLETTER_LETTER_CONTENT, 'letter_content', $editor_configs);
96
        $letter_content_editor->setDescription(_AM_XNEWSLETTER_LETTER_CONTENT_DESC);
97
        $form->addElement($letter_content_editor, true);
98
99
        // letter_template
100
        $letterTemplateid = $this->isNew() ? 1 : $this->getVar('letter_templateid');
101
        $template_select = new \XoopsFormSelect(_AM_XNEWSLETTER_LETTER_TEMPLATE, 'letter_templateid', $letterTemplateid);
102
        // get template objects from database
103
        $templateCriteria = new \CriteriaCompo();
104
        $templateCriteria->add(new \Criteria('template_online', 1));
105
        $templateCriteria->setSort('template_title ASC, template_id');
106
        $templateCriteria->setOrder('DESC');
107
        $templateCount = $this->helper->getHandler('Template')->getCount($templateCriteria);
108
        if ($templateCount > 0) {
109
            $template_select->addOptionArray($this->helper->getHandler('Template')->getList($templateCriteria));
110
        } else {
111
            redirect_header('letter.php?op=list', 3, _MA_XNEWSLETTER_NOTEMPLATE_ONLINE);
112
        }
113
        $form->addElement($template_select, false);
114
115
        // letter_cats
116
        /** @var \XoopsGroupPermHandler $grouppermHandler */
117
        $grouppermHandler = xoops_getHandler('groupperm');
118
        /** @var \XoopsMemberHandler $memberHandler */
119
        $memberHandler    = xoops_getHandler('member');
120
        $groups           = $memberHandler->getGroupsByUser($xoopsUser->uid());
121
        $catCriteria      = new \CriteriaCompo();
122
        $catCriteria->setSort('cat_id');
123
        $catCriteria->setOrder('ASC');
124
        $letter_cats = explode('|', $this->getVar('letter_cats'));
125
        $cat_select  = new \XoopsFormCheckBox(_AM_XNEWSLETTER_LETTER_CATS, 'letter_cats', $letter_cats);
126
        $catObjs     = $this->helper->getHandler('Cat')->getAll($catCriteria);
127
        foreach ($catObjs as $cat_id => $catObj) {
128
            $cat_name = $catObj->getVar('cat_name');
129
            $show     = $grouppermHandler->checkRight('newsletter_create_cat', $cat_id, $groups, $this->helper->getModule()->mid());
130
            if (1 == $show) {
131
                $cat_select->addOption($cat_id, $cat_name);
132
            }
133
        }
134
        $form->addElement($cat_select, true);
135
136
        // attachments
137
        $attachment_tray = new \XoopsFormElementTray(_AM_XNEWSLETTER_LETTER_ATTACHMENT, '<br>');
138
        $attachment_tray->addElement(new \XoopsFormHidden('deleted_attachment_id', ''));
139
        // existing_attachments
140
        if ($this->isNew()) {
141
            $attachmentObjs = [];
142
        } else {
143
            $attachmentCriteria = new \CriteriaCompo();
144
            $attachmentCriteria->add(new \Criteria('attachment_letter_id', $this->getVar('letter_id')));
145
            $attachmentCriteria->setSort('attachment_id');
146
            $attachmentCriteria->setOrder('ASC');
147
            $attachmentObjs = $this->helper->getHandler('Attachment')->getAll($attachmentCriteria);
148
        }
149
        $i                      = 1;
150
        $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...
151
        foreach ($attachmentObjs as $attachment_id => $attachmentObj) {
152
            $delete_attachment_tray = new \XoopsFormElementTray('', '&nbsp;&nbsp;');
153
            $mode_select            = new \XoopsFormRadio(_AM_XNEWSLETTER_ATTACHMENT_MODE, "existing_attachments_mode[{$attachment_id}]", $attachmentObj->getVar('attachment_mode'), '&nbsp;');
154
            $mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_ASATTACHMENT, _AM_XNEWSLETTER_ATTACHMENT_MODE_ASATTACHMENT);
155
            $mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_ASLINK, _AM_XNEWSLETTER_ATTACHMENT_MODE_ASLINK);
156
            //$mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_AUTO, _AM_XNEWSLETTER_ATTACHMENT_MODE_AUTO); // for future features
157
            $delete_attachment_tray->addElement($mode_select);
158
            $delete_attachment_tray->addElement(new \XoopsFormLabel('', $attachmentObj->getVar('attachment_name')));
159
            $delete_button = new \XoopsFormButton('', "delete_attachment_{$i}", _DELETE, 'submit');
160
            $delete_button->setExtra("onclick='this.form.elements.op.value=\"delete_attachment\";this.form.elements.deleted_attachment_id.value=\"{$attachment_id}\";'");
161
            $delete_attachment_tray->addElement($delete_button);
162
            $attachment_tray->addElement($delete_attachment_tray);
163
            ++$i;
164
            unset($mode_select);
165
            unset($delete_attachment_tray);
166
        }
167
        // new_attachments
168
        for ($j = $i; $j < ($this->helper->getConfig('xn_maxattachments') + 1); ++$j) {
169
            $add_attachment_tray = new \XoopsFormElementTray('', '&nbsp;&nbsp;');
170
            $mode_select         = new \XoopsFormRadio(_AM_XNEWSLETTER_ATTACHMENT_MODE, "new_attachments_mode[{$j}]", _XNEWSLETTER_ATTACHMENTS_MODE_ASATTACHMENT, '&nbsp;');
171
            $mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_ASATTACHMENT, _AM_XNEWSLETTER_ATTACHMENT_MODE_ASATTACHMENT);
172
            $mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_ASLINK, _AM_XNEWSLETTER_ATTACHMENT_MODE_ASLINK);
173
            //$mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_AUTO, _AM_XNEWSLETTER_ATTACHMENT_MODE_AUTO); // for future features
174
            $add_attachment_tray->addElement($mode_select);
175
            $add_attachment_tray->addElement(new \XoopsFormFile('', "new_attachment_index={$j}", $this->helper->getConfig('xn_maxsize')));
176
            $attachment_tray->addElement($add_attachment_tray);
177
            unset($mode_select);
178
            unset($add_attachment_tray);
179
        }
180
        $form->addElement($attachment_tray);
181
182
        // letter_action
183
        $opt_nextaction = new \XoopsFormRadio('', 'letter_action', _AM_XNEWSLETTER_LETTER_ACTION_NO, '<br>');
184
        $opt_nextaction->addOption(_XNEWSLETTER_LETTER_ACTION_VAL_NO, _AM_XNEWSLETTER_LETTER_ACTION_NO);
185
        $opt_nextaction->addOption(_XNEWSLETTER_LETTER_ACTION_VAL_PREVIEW, _AM_XNEWSLETTER_LETTER_ACTION_PREVIEW);
186
        $opt_nextaction->addOption(_XNEWSLETTER_LETTER_ACTION_VAL_SEND, _AM_XNEWSLETTER_LETTER_ACTION_SEND);
187
        $opt_nextaction->addOption(_XNEWSLETTER_LETTER_ACTION_VAL_SENDTEST, _AM_XNEWSLETTER_LETTER_ACTION_SENDTEST);
188
        $opt_tray = new \XoopsFormElementTray(_AM_XNEWSLETTER_LETTER_ACTION, '<br>');
189
        $opt_tray->addElement($opt_nextaction);
190
191
        // letter_email_test
192
        $letter_email_test = $this->isNew() ? $xoopsUser->email() : $this->getVar('letter_email_test');
193
        if ('' == $letter_email_test) {
194
            $letter_email_test = $xoopsUser->email();
195
        }
196
        $opt_tray->addElement(new \XoopsFormText(_AM_XNEWSLETTER_LETTER_EMAIL_TEST . ':&nbsp;', 'letter_email_test', 50, 255, $letter_email_test), false);
197
        $form->addElement($opt_tray);
198
199
        // letter_account
200
        $accountsCriteria = new \CriteriaCompo();
201
        $accountsCriteria->setSort('accounts_id');
202
        $accountsCriteria->setOrder('ASC');
203
        $accountsCount   = $this->helper->getHandler('Accounts')->getCount($accountsCriteria);
204
        $account_default = 0;
205
        if ($this->isNew()) {
206
            $accountsObjs = $this->helper->getHandler('Accounts')->getAll($accountsCriteria);
207
            foreach ($accountsObjs as $accountsObj) {
208
                if (1 == $accountsObj->getVar('accounts_default')) {
209
                    $account_default = $accountsObj->getVar('accounts_id');
210
                }
211
            }
212
        } else {
213
            $account_default = $this->getVar('letter_account');
214
        }
215
        if (1 == $accountsCount) {
216
            $form->addElement(new \XoopsFormHidden('letter_account', $account_default));
217
        } else {
218
            $accounts_list = $this->helper->getHandler('Accounts')->getList($accountsCriteria);
219
220
            if (true === $admin_aerea) {
221
                $opt_accounts = new \XoopsFormRadio(_AM_XNEWSLETTER_LETTER_ACCOUNTS_AVAIL, 'letter_account', $account_default);
222
                $opt_accounts->addOptionArray($accounts_list);
223
                $form->addElement($opt_accounts, false);
224
            } else {
225
                $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_LETTER_ACCOUNTS_AVAIL, $accounts_list[$account_default]));
226
                $form->addElement(new \XoopsFormHidden('letter_account', $account_default));
227
            }
228
        }
229
230
        if ($this->isNew()) {
231
            $time           = time();
232
            $submitter_uid  = $GLOBALS['xoopsUser']->uid();
233
            $submitter_name = $GLOBALS['xoopsUser']->uname();
234
        } else {
235
            $time          = $this->getVar('letter_created');
236
            $submitter_uid = $this->getVar('letter_submitter');
237
            xoops_load('xoopsuserutility');
238
            $submitter_name = \XoopsUserUtility::getUnameFromId($submitter_uid);
239
        }
240
241
        $form->addElement(new \XoopsFormHidden('letter_submitter', $submitter_uid));
242
        $form->addElement(new \XoopsFormHidden('letter_created', $time));
243
244
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_SUBMITTER, $submitter_name));
245
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_CREATED, formatTimestamp($time, 's')));
246
247
        $form->addElement(new \XoopsFormHidden('op', 'save_letter'));
248
        $form->addElement(new \XoopsFormButtonTray('', _SUBMIT, 'submit', '', false));
249
250
        return $form;
251
    }
252
253
    /**
254
     * Get Values
255
     * @param null $keys
256
     * @param string|null $format
257
     * @param int|null $maxDepth
258
     * @return array
259
     */
260
    public function getValuesLetter($keys = null, $format = null, $maxDepth = null)
261
    {
262
        $ret = $this->getValues($keys, $format, $maxDepth);
263
        $ret['id']             = $this->getVar('letter_id');
264
        $ret['title']          = $this->getVar('letter_title');
265
        $ret['content']        = $this->getVar('letter_content');
266
        $ret['templateid']     = $this->getVar('letter_templateid');
267
        $templateObj           = $this->helper->getHandler('Template')->get($this->getVar('letter_templateid'));
268
        if (is_object($templateObj)) {
269
            $ret['template_title'] = $templateObj->getVar('template_title');
270
        }
271
        $ret['cats']           = $this->getVar('letter_cats');
272
        $ret['attachment']     = $this->getVar('letter_attachment');
273
        $ret['account']        = $this->getVar('letter_account');
274
        $ret['email_test']     = $this->getVar('letter_email_test');
275
        $ret['sender']         = $this->getVar('letter_sender') > 0 ? \XoopsUser::getUnameFromId($this->getVar('letter_sender')) : '-';
276
        $ret['sent']           = $this->getVar('letter_sent') > 1 ? formatTimestamp($this->getVar('letter_sent'), 's') : '-';
277
        $ret['created']        = formatTimestamp($this->getVar('letter_created'), 's');
278
        $ret['submitter']      = \XoopsUser::getUnameFromId($this->getVar('letter_submitter'));
279
        return $ret;
280
    }
281
282
}
283