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

Mailinglist::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
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 Mailinglist
37
 */
38
class Mailinglist 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('mailinglist_id', XOBJ_DTYPE_INT, null, false);
50
        $this->initVar('mailinglist_name', XOBJ_DTYPE_TXTBOX, null, false, 100);
51
        $this->initVar('mailinglist_email', XOBJ_DTYPE_TXTBOX, null, false, 100);
52
        $this->initVar('mailinglist_listname', XOBJ_DTYPE_TXTBOX, null, false, 100);
53
        $this->initVar('mailinglist_subscribe', XOBJ_DTYPE_TXTBOX, null, false, 100);
54
        $this->initVar('mailinglist_unsubscribe', XOBJ_DTYPE_TXTBOX, null, false, 100);
55
        $this->initVar('mailinglist_submitter', XOBJ_DTYPE_INT, null, false);
56
        $this->initVar('mailinglist_created', XOBJ_DTYPE_INT, time(), false);
57
        $this->initVar('mailinglist_system', XOBJ_DTYPE_TXTBOX, null, false, 100);
58
        $this->initVar('mailinglist_target', XOBJ_DTYPE_TXTBOX, null, false, 200);
59
        $this->initVar('mailinglist_pwd', XOBJ_DTYPE_TXTBOX, null, false, 100);
60
        $this->initVar('mailinglist_notifyowner', XOBJ_DTYPE_INT, null, false);
61
    }
62
63
    /**
64
     * @param bool $action
65
     *
66
     * @return \XoopsThemeForm
67
     */
68
    public function getForm($action = false, $system = 0)
69
    {
70
        global $xoopsDB;
71
72
        if (false === $action) {
73
            $action = $_SERVER['REQUEST_URI'];
74
        }
75
76
        $title = $this->isNew() ? sprintf(_AM_XNEWSLETTER_MAILINGLIST_ADD) : sprintf(_AM_XNEWSLETTER_MAILINGLIST_EDIT);
77
78
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
79
        $form = new \XoopsThemeForm($title, 'xn_ml_form', $action, 'post', true);
80
        $form->setExtra('enctype="multipart/form-data"');
81
82
        $form->addElement(new \XoopsFormHidden('mailinglist_system', $system));
83
84
        if ($system === _XNEWSLETTER_MAILINGLIST_TYPE_MAILMAN_VAL) {
85
            $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_MAILINGLIST_SYSTEM, _AM_XNEWSLETTER_MAILINGLIST_SYSTEM_MAILMAN));
86
87
            $mailinglist_name = $this->isNew() ? 'myname' : $this->getVar('mailinglist_name');
88
            $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_NAME, 'mailinglist_name', 50, 255, $mailinglist_name), true);
89
90
            $mailinglist_listname = $this->isNew() ? 'nameofmylist' : $this->getVar('mailinglist_listname');
91
            $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_LISTNAME, 'mailinglist_listname', 50, 255, $mailinglist_listname), true);
92
93
            $form->addElement(new \XoopsFormHidden('mailinglist_email', ''));
94
            $form->addElement(new \XoopsFormHidden('mailinglist_subscribe', ''));
95
            $form->addElement(new \XoopsFormHidden('mailinglist_unsubscribe', ''));
96
97
            $mailinglist_target = $this->isNew() ? 'https://lists.mydomain.com' : $this->getVar('mailinglist_target');
98
            $form->addElement(new \XoopsFormText( _AM_XNEWSLETTER_MAILINGLIST_TARGET, 'mailinglist_target', 50, 255, $mailinglist_target));
99
100
            $mailinglist_pwd = $this->isNew() ? '' : $this->getVar('mailinglist_pwd');
101
            $form->addElement(new \XoopsFormText( _AM_XNEWSLETTER_MAILINGLIST_PWD, 'mailinglist_pwd', 50, 255, $mailinglist_pwd));
102
103
            $mailinglist_notifyowner = $this->isNew() ? 1 : $this->getVar('mailinglist_notifyowner');
104
            $form->addElement(new \XoopsFormRadioYN(_AM_XNEWSLETTER_MAILINGLIST_NOTIFYOWNER, 'mailinglist_notifyowner', $mailinglist_notifyowner, _YES, _NO));
105
        }
106
        if ($system === _XNEWSLETTER_MAILINGLIST_TYPE_MAJORDOMO_VAL) {
107
            $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_MAILINGLIST_SYSTEM, _AM_XNEWSLETTER_MAILINGLIST_SYSTEM_MAJORDOMO));
108
109
            $mailinglist_name = $this->isNew() ? 'myname' : $this->getVar('mailinglist_name');
110
            $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_NAME, 'mailinglist_name', 50, 255, $mailinglist_name), true);
111
112
            $mailinglist_listname = $this->isNew() ? 'nameofmylist' : $this->getVar('mailinglist_listname');
113
            $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_LISTNAME, 'mailinglist_listname', 50, 255, $mailinglist_listname), true);
114
115
            $mailinglist_email = $this->isNew() ? '[email protected]' : $this->getVar('mailinglist_email');
116
            $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_EMAIL_DESC, 'mailinglist_email', 50, 255, $mailinglist_email));
117
118
            $mailinglist_subscribe = $this->isNew() ? 'subscribe nameofmylist {email}' : $this->getVar('mailinglist_subscribe');
119
            $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_SUBSCRIBE . "<br><span style='font-size:0,75em'>" . _AM_XNEWSLETTER_MAILINGLIST_SUBSCRIBE_DESC . '</span>', 'mailinglist_subscribe', 50, 255, $mailinglist_subscribe));
120
121
            $mailinglist_unsubscribe = $this->isNew() ? 'unsubscribe nameofmylist {email}' : $this->getVar('mailinglist_unsubscribe');
122
            $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_UNSUBSCRIBE . "<br><span style='font-size:0,75em'>" . _AM_XNEWSLETTER_MAILINGLIST_SUBSCRIBE_DESC . '</span>', 'mailinglist_unsubscribe', 50, 255, $mailinglist_unsubscribe));
123
124
            $form->addElement(new \XoopsFormHidden('mailinglist_target', ''));
125
            $form->addElement(new \XoopsFormHidden('mailinglist_pwd', ''));
126
            $form->addElement(new \XoopsFormHidden('mailinglist_notifyowner', 0));
127
        }
128
129
        $time = $this->isNew() ? time() : $this->getVar('mailinglist_created');
130
        $form->addElement(new \XoopsFormHidden('mailinglist_submitter', $GLOBALS['xoopsUser']->uid()));
131
        $form->addElement(new \XoopsFormHidden('mailinglist_created', $time));
132
133
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_SUBMITTER, $GLOBALS['xoopsUser']->uname()));
134
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_CREATED, formatTimestamp($time, 's')));
135
136
        $form->addElement(new \XoopsFormHidden('mailinglist_id', $this->getVar('mailinglist_id')));
137
        $form->addElement(new \XoopsFormHidden('op', 'save_mailinglist'));
138
        $form->addElement(new \XoopsFormButtonTray('save', _SUBMIT, 'submit', '', false));
139
140
        return $form;
141
    }
142
143
    /**
144
     * Get Values
145
     * @param null $keys
146
     * @param string|null $format
147
     * @param int|null $maxDepth
148
     * @return array
149
     */
150
    public function getValuesMailinglist($keys = null, $format = null, $maxDepth = null)
151
    {
152
        $ret['id']          = $this->getVar('mailinglist_id');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ret was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ret = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
153
        $ret['name']        = $this->getVar('mailinglist_name');
154
        $ret['email']       = $this->getVar('mailinglist_email');
155
        $ret['listname']    = $this->getVar('mailinglist_listname');
156
        $ret['subscribe']   = $this->getVar('mailinglist_subscribe');
157
        $ret['unsubscribe'] = $this->getVar('mailinglist_unsubscribe');
158
        $ret['system']      = $this->getVar('mailinglist_system');
159
        switch ($this->getVar('mailinglist_system')) {
160
            case _XNEWSLETTER_MAILINGLIST_TYPE_MAILMAN_VAL:
161
                $ret['system_text'] = _AM_XNEWSLETTER_MAILINGLIST_SYSTEM_MAILMAN;
162
                break;
163
            case _XNEWSLETTER_MAILINGLIST_TYPE_MAJORDOMO_VAL:
164
                $ret['system_text'] = _AM_XNEWSLETTER_MAILINGLIST_SYSTEM_MAJORDOMO;
165
                break;
166
            case _XNEWSLETTER_MAILINGLIST_TYPE_DEFAULT_VAL:
167
            default:
168
            $ret['system_text'] = _AM_XNEWSLETTER_MAILINGLIST_SYSTEM_DEFAULT;
169
                break;
170
        }
171
        $ret['target']           = $this->getVar('mailinglist_target');
172
        $ret['pwd']              = $this->getVar('mailinglist_pwd');
173
        $ret['notifyowner']      = $this->getVar('mailinglist_notifyowner');
174
        $ret['notifyowner_text'] = $this->getVar('mailinglist_notifyowner') == 1 ? _YES : _NO;
175
        $ret['created']          = formatTimestamp($this->getVar('mailinglist_created'), 's');
176
        $ret['submitter']        = \XoopsUser::getUnameFromId($this->getVar('mailinglist_submitter'));
177
        return $ret;
178
    }
179
}
180