Completed
Pull Request — master (#29)
by Goffy
01:40
created

Attachment::getValuesAttachment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 12
rs 9.8666
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 Attachment
37
 */
38
class Attachment extends \XoopsObject
39
{
40
    public $helper = null;
41
    public $db;
42
43
    //Constructor
44
45 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...
46
    {
47
        $this->helper = Helper::getInstance();
48
        $this->db     = \XoopsDatabaseFactory::getDatabaseConnection();
49
        $this->initVar('attachment_id', XOBJ_DTYPE_INT, null, false);
50
        $this->initVar('attachment_letter_id', XOBJ_DTYPE_INT, null, false);
51
        $this->initVar('attachment_name', XOBJ_DTYPE_TXTBOX, null, false, 200);
52
        $this->initVar('attachment_type', XOBJ_DTYPE_TXTBOX, null, false, 100);
53
        $this->initVar('attachment_submitter', XOBJ_DTYPE_INT, null, false);
54
        $this->initVar('attachment_created', XOBJ_DTYPE_INT, time(), false);
55
        $this->initVar('attachment_size', XOBJ_DTYPE_INT, 0, false);
56
        $this->initVar('attachment_mode', XOBJ_DTYPE_INT, _XNEWSLETTER_ATTACHMENTS_MODE_ASATTACHMENT, false);
57
    }
58
59
    /**
60
     * @param bool $action
61
     *
62
     * @return \XoopsThemeForm
63
     */
64
    public function getForm($action = false)
65
    {
66
        global $xoopsDB;
67
68
        if (false === $action) {
69
            $action = $_SERVER['REQUEST_URI'];
70
        }
71
72
        $title = $this->isNew() ? sprintf(_AM_XNEWSLETTER_ATTACHMENT_ADD) : sprintf(_AM_XNEWSLETTER_ATTACHMENT_EDIT);
73
74
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
75
        $form = new \XoopsThemeForm($title, 'form', $action, 'post', true);
76
        $form->setExtra('enctype="multipart/form-data"');
77
78
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_ATTACHMENT_NAME, $this->getVar('attachment_name')));
79
80
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_ATTACHMENT_SIZE, "<span title='" . $this->getVar('attachment_size') . " B'>" . xnewsletter_bytesToSize1024($this->getVar('attachment_size')) . '</span>'));
81
82
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_ATTACHMENT_TYPE, $this->getVar('attachment_type')));
83
84
        // attachment_mode
85
        $mode_select = new \XoopsFormRadio(_AM_XNEWSLETTER_ATTACHMENT_MODE, 'attachment_mode', $this->getVar('attachment_mode'));
86
        $mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_ASATTACHMENT, _AM_XNEWSLETTER_ATTACHMENT_MODE_ASATTACHMENT);
87
        $mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_ASLINK, _AM_XNEWSLETTER_ATTACHMENT_MODE_ASLINK);
88
        //$mode_select->addOption(_XNEWSLETTER_ATTACHMENTS_MODE_AUTO, _AM_XNEWSLETTER_ATTACHMENT_MODE_AUTO);  // for future features
89
        $form->addElement($mode_select);
90
91
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_SUBMITTER, $GLOBALS['xoopsUser']->uname()));
92
        $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_CREATED, formatTimestamp($time, 's')));
0 ignored issues
show
Bug introduced by
The variable $time does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
93
94
        $form->addElement(new \XoopsFormHidden('op', 'save_attachment'));
95
        $form->addElement(new \XoopsFormButtonTray('', _SUBMIT, 'submit', '', false));
96
97
        return $form;
98
    }
99
100
    /**
101
     * Get Values
102
     * @param null $keys
103
     * @param string|null $format
104
     * @param int|null $maxDepth
105
     * @return array
106
     */
107
    public function getValuesAttachment($keys = null, $format = null, $maxDepth = null)
108
    {
109
        $ret['id']        = $this->getVar('attachment_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...
110
        $ret['letter_id'] = $this->getVar('attachment_letter_id');
111
        $ret['name']      = $this->getVar('attachment_name');
112
        $ret['type']      = $this->getVar('attachment_type');
113
        $ret['size']      = $this->getVar('attachment_size');
114
        $ret['mode']      = $this->getVar('attachment_mode');
115
        $ret['created']   = formatTimestamp($this->getVar('attachment_created'), 's');
116
        $ret['submitter'] = \XoopsUser::getUnameFromId($this->getVar('attachment_submitter'));
117
        return $ret;
118
    }
119
}
120