Completed
Push — master ( 692213...d4ec0d )
by Goffy
03:18 queued 01:37
created

XnewsletterLetterHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 5
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 5
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * ****************************************************************************
4
 *  - A Project by Developers TEAM For Xoops - ( http://www.xoops.org )
5
 * ****************************************************************************
6
 *  XNEWSLETTER - MODULE FOR XOOPS
7
 *  Copyright (c) 2007 - 2012
8
 *  Goffy ( wedega.com )
9
 *
10
 *  You may not change or alter any portion of this comment or credits
11
 *  of supporting developers from this source code or any supporting
12
 *  source code which is considered copyrighted (c) material of the
13
 *  original comment or credit authors.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *  ---------------------------------------------------------------------------
20
 *
21
 * @copyright  Goffy ( wedega.com )
22
 * @license    GPL 2.0
23
 * @package    xnewsletter
24
 * @author     Goffy ( [email protected] )
25
 *
26
 *  Version : $Id $
27
 * ****************************************************************************
28
 */
29
30
// defined("XOOPS_ROOT_PATH") || die("XOOPS root path not defined");
31
include_once dirname(__DIR__) . '/include/common.php';
32
33
/**
34
 * Class XnewsletterLetter
35
 */
36
class XnewsletterLetter extends XoopsObject
37
{
38
    public $xnewsletter = null;
39
40
    //Constructor
41
    /**
42
     *
43
     */
44 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...
45
    {
46
        $this->xnewsletter = xnewsletterxnewsletter::getInstance();
47
        $this->db          = XoopsDatabaseFactory::getDatabaseConnection();
48
        $this->initVar("letter_id", XOBJ_DTYPE_INT, null, false, 8);
49
        $this->initVar("letter_title", XOBJ_DTYPE_TXTBOX, null, false, 100);
50
        $this->initVar("letter_content", XOBJ_DTYPE_TXTAREA, null, false);
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, 8);
55
        $this->initVar("letter_email_test", XOBJ_DTYPE_TXTBOX, null, false, 100);
56
        $this->initVar("letter_submitter", XOBJ_DTYPE_INT, null, false, 10);
57
        $this->initVar("letter_created", XOBJ_DTYPE_INT, null, false, 10);
58
        $this->initVar('att_to_add', XOBJ_DTYPE_ARRAY, array());
59
    }
60
61
    /**
62
     * @param bool $action
63
     * @param bool $admin_aerea
64
     *
65
     * @return XoopsThemeForm
0 ignored issues
show
Documentation introduced by
Should the return type not be XoopsThemeForm|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
66
     */
67
    public function getForm($action = false, $admin_aerea = false)
68
    {
69
        global $xoopsDB, $xoopsUser, $pathImageIcon;
70
71
        if ($action === false) {
72
            $action = $_SERVER["REQUEST_URI"];
73
        }
74
75
        $title = $this->isNew() ? sprintf(_AM_XNEWSLETTER_LETTER_ADD) : sprintf(_AM_XNEWSLETTER_LETTER_EDIT);
76
77
        include_once XOOPS_ROOT_PATH . "/class/xoopsformloader.php";
78
        $form = new XoopsThemeForm($title, "form", $action, "post", true);
79
        $form->setExtra('enctype="multipart/form-data"');
80
81
        $form->addElement(new XoopsFormText(_AM_XNEWSLETTER_LETTER_TITLE, "letter_title", 50, 255, $this->getVar("letter_title", 'e')), true);
82
83
        $editor_configs           = array();
84
        $editor_configs["name"]   = "letter_content";
85
        $editor_configs["value"]  = $this->getVar("letter_content", "e");
86
        $editor_configs["rows"]   = 10;
87
        $editor_configs["cols"]   = 80;
88
        $editor_configs["width"]  = "100%";
89
        $editor_configs["height"] = "400px";
90
        $editor_configs["editor"] = $this->xnewsletter->getConfig('xnewsletter_editor');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $editor_configs['editor'] is correct as $this->xnewsletter->getC...g('xnewsletter_editor') (which targets xnewsletterxnewsletter::getConfig()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
91
        $letter_content_editor = new XoopsFormEditor(_AM_XNEWSLETTER_LETTER_CONTENT, "letter_content", $editor_configs);
92
        $letter_content_editor->setDescription(_AM_XNEWSLETTER_LETTER_CONTENT_DESC);
93
        $form->addElement($letter_content_editor, true);
94
95
        $letter_template = $this->getVar("letter_template") == "" ? "basic" : $this->getVar("letter_template");
96
97
        $template_select = new XoopsFormSelect(_AM_XNEWSLETTER_LETTER_TEMPLATE, "letter_template", $letter_template);
98
        // get template files in /modules/xnewsletter/language/{language}/templates/ directory
99
        $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/' . $GLOBALS['xoopsConfig']['language'] . '/templates/';
100
        if (!is_dir($template_path)) {
101
            $template_path = XOOPS_ROOT_PATH . '/modules/xnewsletter/language/english/templates/';
102
        }
103
        $templateFiles = array();
104
        if (!$dirHandler = @opendir($template_path)) die(str_replace("%p", $template_path, _AM_XNEWSLETTER_SEND_ERROR_INALID_TEMPLATE_PATH));
105
        while ($filename = readdir($dirHandler)) {
106
            if (($filename != ".") and ($filename != "..") and ($filename != "index.html")) {
107
                $info            = pathinfo($filename);
108
                $templateFiles[] = basename($filename, '.' . $info['extension']);
109
            }
110
        }
111
        closedir($dirHandler);
112
        foreach ($templateFiles as $templateFile) {
113
            $template_select->addOption($templateFile, "file:" . $templateFile);
114
        }
115
        // get template objects from database
116
        $templateCriteria = new CriteriaCompo();
117
        $templateCriteria->setSort("template_title DESC, template_id");
118
        $templateCriteria->setOrder("DESC");
119
        $templateCount = $this->xnewsletter->getHandler('template')->getCount();
120
        if($templateCount > 0) {
121
            $templateObjs = $this->xnewsletter->getHandler('template')->getAll($templateCriteria);
122
            foreach($templateObjs as $templateObj) {
123
                $template_select->addOption("db:" . $templateObj->getVar('template_id'), "db:" . $templateObj->getVar('template_title'));
124
            }
125
        }
126
        $form->addElement($template_select, false);
127
128
        $gperm_handler  = xoops_gethandler('groupperm');
129
        $member_handler = xoops_gethandler('member');
130
        $my_group_ids   = $member_handler->getGroupsByUser($xoopsUser->uid());
131
132
        $catCriteria = new CriteriaCompo();
133
        $catCriteria->setSort('cat_id');
134
        $catCriteria->setOrder('ASC');
135
        $letter_cats = explode("|", $this->getVar("letter_cats"));
136
        $cat_select  = new XoopsFormCheckBox(_AM_XNEWSLETTER_LETTER_CATS, "letter_cats", $letter_cats);
137
        $catObjs     = $this->xnewsletter->getHandler('cat')->getAll($catCriteria);
138
        foreach ($catObjs as $cat_id => $catObj) {
139
            $cat_name = $catObj->getVar("cat_name");
140
            $show     = $gperm_handler->checkRight('newsletter_create_cat', $cat_id, $my_group_ids, $this->xnewsletter->getModule()->mid());
141
            if ($show == 1) {
142
                $cat_select->addOption($cat_id, $cat_name);
143
            }
144
        }
145
        $form->addElement($cat_select, true);
146
147
        $attachment_tray = new XoopsFormElementTray(_AM_XNEWSLETTER_LETTER_ATTACHMENT, '<br>');
148
        if ($this->isNew()) {
149
            $attachmentObjs = array();
150
        } else {
151
            $attachmentCriteria = new CriteriaCompo();
152
            $attachmentCriteria->add(new Criteria('attachment_letter_id', $this->getVar("letter_id")));
153
            $attachmentCriteria->setSort("attachment_id");
154
            $attachmentCriteria->setOrder("ASC");
155
            $attachmentObjs = $this->xnewsletter->getHandler('attachment')->getAll($attachmentCriteria);
156
        }
157
        $i = 1;
158
        $remove_attachment_tray = array();
159
        foreach ($attachmentObjs as $attachment_id => $attachmentObj) {
160
            $remove_attachment_tray[$attachment_id] = new XoopsFormElementTray("", '&nbsp;&nbsp;');
161
            $remove_attachment_tray[$attachment_id]->addElement(new XoopsFormLabel("", $attachmentObj->getVar("attachment_name")));
162
            $remove_attachment_tray[$attachment_id]->addElement(new XoopsFormButton("", "delete_attachment_{$i}", _DELETE, "submit"));
163
            $remove_attachment_tray[$attachment_id]->addElement(new XoopsFormHidden("attachment_{$i}", $attachment_id));
164
            $attachment_tray->addElement($remove_attachment_tray[$attachment_id]);
165
            ++$i;
166
        }
167
        //$add_att_tray = array();
168
        for ($j = $i; $j < 6; ++$j) {
169
            $attachment_tray->addElement(new XoopsFormFile("", "letter_attachment_{$j}", $this->xnewsletter->getConfig('xn_maxsize')));
170
        }
171
        $form->addElement($attachment_tray);
172
173
        $opt_nextaction = new XoopsFormRadio("", "letter_action", "0", "<br />");
174
        $opt_nextaction->addOption(_AM_XNEWSLETTER_LETTER_ACTION_VAL_NO, _AM_XNEWSLETTER_LETTER_ACTION_NO);
175
        $opt_nextaction->addOption(_AM_XNEWSLETTER_LETTER_ACTION_VAL_PREVIEW, _AM_XNEWSLETTER_LETTER_ACTION_PREVIEW);
176
        $opt_nextaction->addOption(_AM_XNEWSLETTER_LETTER_ACTION_VAL_SEND, _AM_XNEWSLETTER_LETTER_ACTION_SEND);
177
        $opt_nextaction->addOption(_AM_XNEWSLETTER_LETTER_ACTION_VAL_SENDTEST, _AM_XNEWSLETTER_LETTER_ACTION_SENDTEST);
178
        $opt_tray = new XoopsFormElementTray(_AM_XNEWSLETTER_LETTER_ACTION, '<br/>');
179
        $opt_tray->addElement($opt_nextaction);
180
181
        $letter_email_test = $this->isNew() ? $xoopsUser->email() : $this->getVar("letter_email_test");
182
        if ($letter_email_test == '') {
183
            $letter_email_test = $xoopsUser->email();
184
        }
185
        $opt_tray->addElement(new XoopsFormText(_AM_XNEWSLETTER_LETTER_EMAIL_TEST . ":&nbsp;", "letter_email_test", 50, 255, $letter_email_test), false);
186
        $form->addElement($opt_tray);
187
188
        $accountsCriteria = new CriteriaCompo();
189
        $accountsCriteria->setSort("accounts_id");
190
        $accountsCriteria->setOrder("ASC");
191
        $numrows_accounts = $this->xnewsletter->getHandler('accounts')->getCount($accountsCriteria);
192
        $account_default  = 0;
193
        if ($this->isNew()) {
194
            $accountsObjs = $this->xnewsletter->getHandler('accounts')->getAll($accountsCriteria);
195
            foreach ($accountsObjs as $accountsObj) {
196
                if ($accountsObj->getVar("accounts_default") == 1) {
197
                    $account_default = $accountsObj->getVar("accounts_id");
198
                }
199
            }
200
        } else {
201
            $account_default = $this->getVar("letter_account");
202
        }
203
        if ($numrows_accounts == 1) {
204
            $form->addElement(new XoopsFormHidden("letter_account", $account_default));
205
        } else {
206
            $accounts_list = $this->xnewsletter->getHandler('accounts')->getList($accountsCriteria);
207
208
            if ($admin_aerea == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
209
                $opt_accounts = new XoopsFormRadio(_AM_XNEWSLETTER_LETTER_ACCOUNTS_AVAIL, "letter_account", $account_default);
210
                $opt_accounts->addOptionArray($accounts_list);
211
                $form->addElement($opt_accounts, false);
212
            } else {
213
                $form->addElement(new XoopsFormLabel(_AM_XNEWSLETTER_LETTER_ACCOUNTS_AVAIL, $accounts_list[$account_default]));
214
                $form->addElement(new XoopsFormHidden("letter_account", $account_default));
215
            }
216
        }
217
218
        if ($this->isNew()) {
219
            $time           = time();
220
            $submitter_uid  = $GLOBALS['xoopsUser']->uid();
221
            $submitter_name = $GLOBALS['xoopsUser']->uname();
222
        } else {
223
            $time          = $this->getVar("letter_created");
224
            $submitter_uid = $this->getVar("letter_submitter");
225
            xoops_load("xoopsuserutility");
226
            $submitter_name = XoopsUserUtility::getUnameFromId($submitter_uid);
227
        }
228
229
        $form->addElement(new XoopsFormHidden("letter_submitter", $submitter_uid));
230
        $form->addElement(new XoopsFormHidden("letter_created", $time));
231
232
        $form->addElement(new XoopsFormLabel(_AM_XNEWSLETTER_LETTER_SUBMITTER, $submitter_name));
233
        $form->addElement(new XoopsFormLabel(_AM_XNEWSLETTER_LETTER_CREATED, formatTimestamp($time, 's')));
234
235
        $form->addElement(new XoopsFormHidden("op", "save_letter"));
236
        $form->addElement(new XoopsFormButton("", "submit", _AM_XNEWSLETTER_SAVE, "submit"));
237
238
        return $form;
239
    }
240
}
241
242
/**
243
 * Class XnewsletterLetterHandler
244
 */
245 View Code Duplication
class XnewsletterLetterHandler extends XoopsPersistableObjectHandler
0 ignored issues
show
Duplication introduced by
This class 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...
246
{
247
    /**
248
     * @var xnewsletterxnewsletter
249
     * @access public
250
     */
251
    public $xnewsletter = null;
252
253
    /**
254
     * @param null|object $db
255
     */
256
    public function __construct(&$db)
257
    {
258
        parent::__construct($db, "xnewsletter_letter", "XnewsletterLetter", "letter_id", "letter_title");
259
        $this->xnewsletter = xnewsletterxnewsletter::getInstance();
260
    }
261
}
262