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

Subscr::getForm()   F

Complexity

Conditions 15
Paths 1536

Size

Total Lines 110

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
nc 1536
nop 1
dl 0
loc 110
rs 1.3999
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 Subscr
37
 */
38
class Subscr 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('subscr_id', XOBJ_DTYPE_INT, null, false);
49
        $this->initVar('subscr_email', XOBJ_DTYPE_TXTBOX, '', false, 100);
50
        $this->initVar('subscr_firstname', XOBJ_DTYPE_TXTBOX, '', true, 100);
51
        $this->initVar('subscr_lastname', XOBJ_DTYPE_TXTBOX, '', false, 100);
52
        $this->initVar('subscr_uid', XOBJ_DTYPE_INT, null, false);
53
        $this->initVar('subscr_sex', XOBJ_DTYPE_TXTBOX, '', false, 100);
54
        $this->initVar('subscr_submitter', XOBJ_DTYPE_INT, null, false);
55
        $this->initVar('subscr_created', XOBJ_DTYPE_INT, time(), false);
56
        $this->initVar('subscr_actkey', XOBJ_DTYPE_TXTBOX, '', false, 255);
57
        $this->initVar('subscr_ip', XOBJ_DTYPE_TXTBOX, xoops_getenv('REMOTE_ADDR'), false, 32);
58
        $this->initVar('subscr_activated', XOBJ_DTYPE_INT, 0, false);  // IN PROGRESS: should be false or timestamp
59
        $this->initVar('subscr_actoptions', XOBJ_DTYPE_ARRAY, [], false);
60
    }
61
62
    /**
63
     * @param bool $action
64
     *
65
     * @return \XoopsThemeForm
66
     */
67
    public function getSearchForm($action = false)
68
    {
69
        global $xoopsDB;
70
71
        if (false === $action) {
72
            $action = $_SERVER['REQUEST_URI'];
73
        }
74
75
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
76
        $form = new \XoopsThemeForm(_MA_XNEWSLETTER_SUBSCRIPTION_SEARCH, 'formsearch', $action, 'post', true);
77
        $form->setExtra('enctype="multipart/form-data"');
78
79
        // subscr_email
80
        $email_field = new \XoopsFormText(_MA_XNEWSLETTER_SUBSCRIPTION_SEARCH_EMAIL, 'subscr_email', 50, 100, $this->getVar('subscr_email'));
81
        if ('' != $this->getVar('subscr_email')) {
82
            //$email_field->setExtra('disabled="disabled"');
83
        }
84
        $form->addElement($email_field, true);
85
86
        // captcha
87
        xoops_load('xoopscaptcha');
88
        $form->addElement(new \XoopsFormCaptcha('', 'xoopscaptcha', true));
89
90
        // op
91
        $form->addElement(new \XoopsFormHidden('op', 'list_subscriptions'));
92
93
        // buttons
94
        $buttonTray = new \XoopsFormElementTray('', '');
95
        $buttonTray->addElement(new \XoopsFormButton('', 'submit', _AM_XNEWSLETTER_SUBSCRIPTION_SEARCH_ADD, 'submit'));
96
        $button_reset = new \XoopsFormButton('', '', _RESET, 'reset');
97
        $buttonTray->addElement($button_reset);
98
        $button_cancel = new \XoopsFormButton('', '', _CANCEL, 'button');
99
        $button_cancel->setExtra('onclick="history.go(-1)"');
100
        $buttonTray->addElement($button_cancel);
101
        $form->addElement($buttonTray);
102
103
        return $form;
104
    }
105
106
    /**
107
     * @param bool $action
108
     *
109
     * @return \XoopsThemeForm
110
     */
111
    public function getForm($action = false)
112
    {
113
        global $xoopsDB, $xoopsUser;
114
115
        if (false === $action) {
116
            $action = $_SERVER['REQUEST_URI'];
117
        }
118
119
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
120
        $title = $this->isNew() ? sprintf(_MA_XNEWSLETTER_SUBSCRIPTION_ADD) : sprintf(_MA_XNEWSLETTER_SUBSCRIPTION_EDIT);
121
        $form  = new \XoopsThemeForm($title, 'form', $action, 'post', true);
122
        $form->setExtra('enctype="multipart/form-data"');
123
124
        $form->addElement(new \XoopsFormLabel("<span style='text-decoration:underline'>" . _MA_XNEWSLETTER_SUBSCRIPTION_INFO_PERS . '</span>', ''));
125
        $subscr_id = $this->isNew() ? 0 : $this->getVar('subscr_id');
126
127
        // subscr_email
128
        if ($subscr_id > 0 || '' != $this->getVar('subscr_email')) {
129
            $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_SUBSCR_EMAIL, $this->getVar('subscr_email')));
130
            $form->addElement(new \XoopsFormHidden('subscr_email', $this->getVar('subscr_email')));
131
        } else {
132
            $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_SUBSCR_EMAIL, 'subscr_email', 50, 255, $this->getVar('subscr_email')), true);
133
        }
134
135
        // subscr_sex
136
        if (1 == $this->helper->getConfig('xn_use_salutation')) {
137
            $select_subscr_sex = new \XoopsFormSelect(_AM_XNEWSLETTER_SUBSCR_SEX, 'subscr_sex', $this->getVar('subscr_sex'));
138
            $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_EMPTY, _AM_XNEWSLETTER_SUBSCR_SEX_EMPTY);
139
            $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_FEMALE, _AM_XNEWSLETTER_SUBSCR_SEX_FEMALE);
140
            $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_MALE, _AM_XNEWSLETTER_SUBSCR_SEX_MALE);
141
            $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_COMP, _AM_XNEWSLETTER_SUBSCR_SEX_COMP);
142
            $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_FAMILY, _AM_XNEWSLETTER_SUBSCR_SEX_FAMILY);
143
            $form->addElement($select_subscr_sex);
144
        }
145
146
        // subscr_firstname
147
        $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_SUBSCR_FIRSTNAME, 'subscr_firstname', 50, 255, $this->getVar('subscr_firstname')), true);
148
149
        // subscr_lastname
150
        $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_SUBSCR_LASTNAME, 'subscr_lastname', 50, 255, $this->getVar('subscr_lastname')), false);
151
152
        $form->addElement(new \XoopsFormLabel('<br><br>', ''));
153
154
        // get newsletters available for current user
155
        $opt_cat  = [];
0 ignored issues
show
Unused Code introduced by
$opt_cat 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...
156
        $opt_tray = new \XoopsFormElementTray("<span style='text-decoration:underline'>" . _MA_XNEWSLETTER_SUBSCRIPTION_CATS_AVAIL . '</span>', '<br>');
157
        $opt_tray->setDescription(_MA_XNEWSLETTER_SUBSCRIPTION_CATS_AVAIL_DESC);
158
        $grouppermHandler = xoops_getHandler('groupperm');
159
        $uid              = (is_object($xoopsUser) && isset($xoopsUser)) ? $xoopsUser->uid() : 0;
0 ignored issues
show
Unused Code introduced by
$uid 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...
160
        $groups           = is_object($xoopsUser) ? $xoopsUser->getGroups() : [0 => XOOPS_GROUP_ANONYMOUS];
161
162
        // cats[], existing_catsubcr_id_{$cat_id}, existing_catsubscr_quited_{$cat_id}
163
        $catCriteria = new \CriteriaCompo();
164
        $catCriteria->setSort('cat_id');
165
        $catCriteria->setOrder('ASC');
166
        $catObjs      = $this->helper->getHandler('Cat')->getAll($catCriteria);
167
        $cat_checkbox = new \XoopsFormCheckBox(_MA_XNEWSLETTER_SUBSCRIPTION_SELECT_CATS, 'cats', null, '<br>');
168
        $cat_checkbox->setDescription(_MA_XNEWSLETTER_SUBSCRIPTION_CATS_AVAIL_DESC);
169
        $values = [];
170
        foreach ($catObjs as $cat_id => $catObj) {
171
            // if anonymous user or Xoops user can read cat...
172
            if ($grouppermHandler->checkRight('newsletter_read_cat', $cat_id, XOOPS_GROUP_ANONYMOUS, $this->helper->getModule()->mid())
173
                || $grouppermHandler->checkRight('newsletter_read_cat', $cat_id, $groups, $this->helper->getModule()->mid())) {
174
                // get existing catsubscr
175
                $catsubscrCriteria = new \CriteriaCompo();
176
                $catsubscrCriteria->add(new \Criteria('catsubscr_catid', $cat_id));
177
                $catsubscrCriteria->add(new \Criteria('catsubscr_subscrid', $subscr_id));
178
                $catsubscrCriteria->setLimit(1);
179
                $catsubscrObjs = $this->helper->getHandler('Catsubscr')->getObjects($catsubscrCriteria);
180
                if (isset($catsubscrObjs[0])) {
181
                    $values[]         = $cat_id;
182
                    $catsubscr_quited = $catsubscrObjs[0]->getVar('catsubscr_quited');
183
                    $catsubscr_id     = $catsubscrObjs[0]->getVar('catsubscr_id');
184
                } else {
185
                    $catsubscr_quited = 0;
186
                    $catsubscr_id     = 0;
187
                }
188
                $name = $catObj->getVar('cat_name');
189
                $name .= '<div>' . $catObj->getVar('cat_info', 's') . '</div>';
190
                if (0 == $catsubscr_quited) {
191
                    // NOP
192
                } else {
193
                    $name .= '<div>';
194
                    $name .= str_replace('%q', formatTimestamp($catsubscr_quited, $this->helper->getConfig('dateformat')), _MA_XNEWSLETTER_SUBSCRIPTION_QUITED_DETAIL);
195
                    $name .= '</div>';
196
                }
197
                $name .= "<div style='clear:both'></div>";
198
                $cat_checkbox->addOption($cat_id, $name);
199
                $form->addElement(new \XoopsFormHidden("existing_catsubcr_id_{$cat_id}", $catsubscr_id));
200
                $form->addElement(new \XoopsFormHidden("existing_catsubscr_quited_{$cat_id}", $catsubscr_quited));
201
            }
202
        }
203
        $cat_checkbox->setValue($values);
204
        $form->addElement($cat_checkbox);
205
206
        // op
207
        $form->addElement(new \XoopsFormHidden('op', 'save_subscription'));
208
209
        // button
210
        $buttonTray = new \XoopsFormElementTray('', '');
211
        $buttonTray->addElement(new \XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
212
        $button_reset = new \XoopsFormButton('', '', _RESET, 'reset');
213
        $buttonTray->addElement($button_reset);
214
        $button_cancel = new \XoopsFormButton('', '', _CANCEL, 'button');
215
        $button_cancel->setExtra('onclick="history.go(-1)"');
216
        $buttonTray->addElement($button_cancel);
217
        $form->addElement($buttonTray);
218
219
        return $form;
220
    }
221
222
    //**********************************************************************************************
223
    //   form for admin aerea    *******************************************************************
224
    //**********************************************************************************************
225
226
    /**
227
     * @param bool $action
228
     *
229
     * @return \XoopsThemeForm
230
     */
231
    public function getFormAdmin($action = false)
232
    {
233
        global $xoopsDB;
234
235
        if (false === $action) {
236
            $action = $_SERVER['REQUEST_URI'];
237
        }
238
239
        $title = $this->isNew() ? sprintf(_AM_XNEWSLETTER_SUBSCR_ADD) : sprintf(_AM_XNEWSLETTER_SUBSCR_EDIT);
240
241
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
242
        $form = new \XoopsThemeForm($title, 'form', $action, 'post', true);
243
        $form->setExtra('enctype="multipart/form-data"');
244
245
        $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_SUBSCR_EMAIL, 'subscr_email', 50, 255, $this->getVar('subscr_email')), true);
246
        $select_subscr_sex = new \XoopsFormSelect(_AM_XNEWSLETTER_SUBSCR_SEX, 'subscr_sex', $this->getVar('subscr_sex'));
247
        $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_EMPTY, _AM_XNEWSLETTER_SUBSCR_SEX_EMPTY);
248
        $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_FEMALE, _AM_XNEWSLETTER_SUBSCR_SEX_FEMALE);
249
        $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_MALE, _AM_XNEWSLETTER_SUBSCR_SEX_MALE);
250
        $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_COMP, _AM_XNEWSLETTER_SUBSCR_SEX_COMP);
251
        $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_FAMILY, _AM_XNEWSLETTER_SUBSCR_SEX_FAMILY);
252
        $form->addElement($select_subscr_sex);
253
        $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_SUBSCR_FIRSTNAME, 'subscr_firstname', 50, 255, $this->getVar('subscr_firstname')), false);
254
        $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_SUBSCR_LASTNAME, 'subscr_lastname', 50, 255, $this->getVar('subscr_lastname')), false);
255
256
        $form->addElement(new \XoopsFormSelectUser(_AM_XNEWSLETTER_SUBSCR_UID, 'subscr_uid', true, $this->getVar('subscr_uid'), 1, false), false);
257
258
        $form->addElement(new \XoopsFormHidden('subscr_submitter', $GLOBALS['xoopsUser']->uid()));
259
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_SUBSCR_SUBMITTER, $GLOBALS['xoopsUser']->uname()));
260
        //$form->addElement(new \XoopsFormSelectUser(_AM_XNEWSLETTER_SUBSCR_SUBMITTER, 'subscr_submitter', false, $this->getVar('subscr_submitter'), 1, false), true);
261
262
        if ($this->getVar('subscr_id') > 0) {
263
            $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_SUBSCR_CREATED, formatTimestamp($this->getVar('subscr_created'), $this->helper->getConfig('dateformat')) . ' [' . $this->getVar('subscr_ip') . ']'));
264
            $form->addElement(new \XoopsFormHidden('subscr_created', $this->getVar('subscr_created')));
265
            $form->addElement(new \XoopsFormHidden('subscr_ip', $this->getVar('subscr_ip')));
266
        } else {
267
            $time = time();
268
            $ip   = xoops_getenv('REMOTE_ADDR');
269
            $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_SUBSCR_CREATED, formatTimestamp($time, 's') . " [{$ip}]"));
270
            $form->addElement(new \XoopsFormHidden('subscr_created', $time));
271
            $form->addElement(new \XoopsFormHidden('subscr_ip', $ip));
272
        }
273
        $form->addElement(new \XoopsFormRadioYN(_AM_XNEWSLETTER_SUBSCR_ACTIVATED, 'subscr_activated', $this->getVar('subscr_activated')));
274
        $form->addElement(new \XoopsFormHidden('subscr_actkey', ''));
275
        $form->addElement(new \XoopsFormHidden('op', 'save_subscr'));
276
        $form->addElement(new \XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
277
278
        return $form;
279
    }
280
}
281