Passed
Push — master ( 6af1fb...f5fe55 )
by Michael
02:53 queued 10s
created

CategoriesForm::__construct()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 96
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 96
rs 8.0266
cc 7
nc 24
nop 1

How to fix   Long Method   

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
declare(strict_types=1);
4
5
namespace XoopsModules\Adslight\Form;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
 
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
/**
17
 * Module: Adslight
18
 *
19
 * @category        Module
20
 * @author          XOOPS Development Team <https://xoops.org>
21
 * @copyright       {@link https://xoops.org/ XOOPS Project}
22
 * @license         GPL 2.0 or later
23
 */
24
25
use Xmf\Request;
26
use XoopsModules\Adslight;
27
28
29
require_once \dirname(dirname(__DIR__)) . '/include/common.php';
30
31
$moduleDirName = basename(dirname(__DIR__, 2));
32
//$helper = Adslight\Helper::getInstance();
33
$permHelper = new \Xmf\Module\Helper\Permission();
34
35
xoops_load('XoopsFormLoader');
36
/**
37
 * Class CategoriesForm
38
 */
39
class CategoriesForm extends \XoopsThemeForm
40
{
41
    public $targetObject;
42
    public $helper;
43
    /**
44
     * Constructor
45
     *
46
     * @param $target
47
     */
48
    public function __construct($target)
49
    {
50
      $this->helper = $target->helper;
51
      $this->targetObject = $target;
52
53
       $title = $this->targetObject->isNew() ? sprintf(AM_ADSLIGHT_CATEGORIES_ADD) : sprintf(AM_ADSLIGHT_CATEGORIES_EDIT);
54
        parent::__construct($title, 'form', xoops_getenv('SCRIPT_NAME'),'post', true);
55
        $this->setExtra('enctype="multipart/form-data"');
56
        
57
58
59
        //include ID field, it's needed so the module knows if it is a new form or an edited form
60
        
61
62
        $hidden = new \XoopsFormHidden('cid', $this->targetObject->getVar('cid'));
63
        $this->addElement($hidden);
64
        unset($hidden);
65
        
66
// Cid
67
            $this->addElement(new \XoopsFormLabel(AM_ADSLIGHT_CATEGORIES_CID, $this->targetObject->getVar('cid'), 'cid' ));
68
            // Pid
69
        require_once XOOPS_ROOT_PATH . '/class/tree.php';
70
       //$categoriesHandler = xoops_getModuleHandler('categories', 'adslight' );
71
         //$db     = \XoopsDatabaseFactory::getDatabaseConnection();
72
         /** @var \XoopsPersistableObjectHandler $categoriesHandler */
73
        $categoriesHandler = $this->helper->getHandler('Categories'); 
74
75
        $criteria = new \CriteriaCompo();
76
        $categoryArray = $categoriesHandler->getObjects( $criteria );
77
        if (!empty($categoryArray)) {
78
79
            $categoryTree = new \XoopsObjectTree( $categoryArray, 'cid', 'pid' );
80
81
          // if (Adslight\Utility::checkVerXoops($GLOBALS['xoopsModule'], '2.5.9')) {
82
                 $categoriesPid = $categoryTree->makeSelectElement('pid', 'title', '--', $this->targetObject->getVar('pid'), true, 0, '', AM_ADSLIGHT_CATEGORIES_PID);
83
                 $this->addElement($categoriesPid);
84
          //  } else {
85
          //      $categoriesPid = $categoryTree->makeSelBox( 'pid', 'title','--', $this->targetObject->getVar('pid', 'e' ), true );
86
          //      $this->addElement( new \XoopsFormLabel ( AM_ADSLIGHT_CATEGORIES_PID, $categoriesPid ) );
87
          //  }
88
89
        }
90
        // Title
91
        $this->addElement(new \XoopsFormText(AM_ADSLIGHT_CATEGORIES_TITLE, 'title', 50, 255, $this->targetObject->getVar('title')), false);
92
        // Cat_desc
93
        if (class_exists('XoopsFormEditor')) {
94
        $editorOptions = [];
95
        $editorOptions['name'] = 'cat_desc';
96
        $editorOptions['value'] = $this->targetObject->getVar('cat_desc', 'e');
97
        $editorOptions['rows'] = 5;
98
        $editorOptions['cols'] = 40;
99
        $editorOptions['width'] = '100%';
100
        $editorOptions['height'] = '400px';
101
        //$editorOptions['editor'] = xoops_getModuleOption('adslight_editor', 'adslight');
102
        //$this->addElement( new \XoopsFormEditor(AM_ADSLIGHT_CATEGORIES_CAT_DESC, 'cat_desc', $editorOptions), false  );
103
        if ($this->helper->isUserAdmin()) {
104
        $descEditor = new \XoopsFormEditor(AM_ADSLIGHT_CATEGORIES_CAT_DESC, $this->helper->getConfig('adslightEditorAdmin'), $editorOptions, $nohtml = false, $onfailure = 'textarea');
105
    } else {
106
        $descEditor = new \XoopsFormEditor(AM_ADSLIGHT_CATEGORIES_CAT_DESC, $this->helper->getConfig('adslightEditorUser'), $editorOptions, $nohtml = false, $onfailure = 'textarea');
107
    }
108
} else {
109
    $descEditor = new \XoopsFormDhtmlTextArea(AM_ADSLIGHT_CATEGORIES_CAT_DESC, 'description', $this->targetObject->getVar('description', 'e'), 5, 50);
110
}
111
$this->addElement($descEditor);
112
        // Cat_keywords
113
        $this->addElement(new \XoopsFormText(AM_ADSLIGHT_CATEGORIES_CAT_KEYWORDS, 'cat_keywords', 50, 255, $this->targetObject->getVar('cat_keywords')), false);
114
        // Img
115
        $img = $this->targetObject->getVar('img') ?: 'blank.png';
116
117
        $uploadDir = '/uploads/adslight/categories/';
118
        $imgtray = new \XoopsFormElementTray(AM_ADSLIGHT_CATEGORIES_IMG,'<br>');
119
        $imgpath = sprintf(AM_ADSLIGHT_FORMIMAGE_PATH, $uploadDir);
120
        $imageselect = new \XoopsFormSelect($imgpath, 'img', $img);
121
        $imageArray = \XoopsLists::getImgListAsArray( XOOPS_ROOT_PATH . $uploadDir );
122
        foreach ($imageArray as $image) {
123
            $imageselect->addOption((string)$image, $image);
124
        }
125
        $imageselect->setExtra( "onchange='showImgSelected(\"image_img\", \"img\", \"".$uploadDir.'", "", "'.XOOPS_URL."\")'" );
126
        $imgtray->addElement($imageselect);
127
        $imgtray->addElement( new \XoopsFormLabel( '', "<br><img src='".XOOPS_URL.'/'.$uploadDir.'/'.$img."' name='image_img' id='image_img' alt='' style='max-width:300px' >" ) );
128
        $fileseltray = new \XoopsFormElementTray('','<br>');
129
        $fileseltray->addElement(new \XoopsFormFile(AM_ADSLIGHT_FORMUPLOAD , 'img', $this->helper->getConfig('maxsize')));
130
        $fileseltray->addElement(new \XoopsFormLabel(''));
131
        $imgtray->addElement($fileseltray);
132
        $this->addElement($imgtray);
133
        // Cat_order
134
        $this->addElement(new \XoopsFormText(AM_ADSLIGHT_CATEGORIES_CAT_ORDER, 'cat_order', 50, 255, $this->targetObject->getVar('cat_order')), false);
135
        // Affprice
136
        $this->addElement(new \XoopsFormText(AM_ADSLIGHT_CATEGORIES_AFFPRICE, 'affprice', 50, 255, $this->targetObject->getVar('affprice')), false);
137
        // Cat_moderate
138
        $this->addElement(new \XoopsFormSelectUser(AM_ADSLIGHT_CATEGORIES_CAT_MODERATE, 'cat_moderate', false, $this->targetObject->getVar('cat_moderate'), 1, false), false);
139
        // Moderate_subcat
140
        $this->addElement(new \XoopsFormSelectUser(AM_ADSLIGHT_CATEGORIES_MODERATE_SUBCAT, 'moderate_subcat', false, $this->targetObject->getVar('moderate_subcat'), 1, false), false);
141
                
142
        $this->addElement(new \XoopsFormHidden('op', 'save'));
143
        $this->addElement(new \XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
144
    }
145
}
146