Completed
Pull Request — master (#3)
by Michael
01:28
created

TDMDownloads_cat   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 134
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A get_new_enreg() 0 7 1
C getForm() 0 100 8
1
<?php
2
/**
3
 * TDMDownload
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright   Gregory Mage (Aka Mage)
13
 * @license     GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
14
 * @author      Gregory Mage (Aka Mage)
15
 */
16
17
if (!defined("XOOPS_ROOT_PATH")) {
18
    die("XOOPS root path not defined");
19
}
20
class TDMDownloads_cat extends XoopsObject
21
{
22
    // constructor
23
    /**
24
     * TDMDownloads_cat constructor.
25
     */
26
    public function __construct()
27
    {
28
        $this->initVar("cat_cid",XOBJ_DTYPE_INT,null,false,5);
29
        $this->initVar("cat_pid",XOBJ_DTYPE_INT,null,false,5);
30
        $this->initVar("cat_title",XOBJ_DTYPE_TXTBOX, null, false);
31
        $this->initVar("cat_imgurl",XOBJ_DTYPE_TXTBOX, null, false);
32
        $this->initVar("cat_description_main",XOBJ_DTYPE_TXTAREA, null, false);
33
        // Pour autoriser le html
34
        $this->initVar('dohtml', XOBJ_DTYPE_INT, 1, false);
35
        $this->initVar("cat_weight",XOBJ_DTYPE_INT,0,false,11);
36
    }
37
	
38
    /**
39
     * @return mixed
40
     */
41
    public function get_new_enreg()
42
    {
43
        global $xoopsDB;
44
        $new_enreg = $xoopsDB->getInsertId();
45
46
        return $new_enreg;
47
    }
48
	
49
	/**
50
     * @param bool $action
51
     * @return XoopsThemeForm
52
     */
53
    public function getForm($action = false)
54
    {
55
        global $xoopsDB, $xoopsModule, $xoopsModuleConfig;
56
        if ($action === false) {
57
            $action = $_SERVER['REQUEST_URI'];
58
        }
59
        include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");
60
61
        //nom du formulaire selon l'action (editer ou ajouter):
62
        $title = $this->isNew() ? sprintf(_AM_TDMDOWNLOADS_FORMADD) : sprintf(_AM_TDMDOWNLOADS_FORMEDIT);
63
64
        //création du formulaire
65
        $form = new XoopsThemeForm($title, 'form', $action, 'post', true);
66
        $form->setExtra('enctype="multipart/form-data"');
67
        //titre
68
        $form->addElement(new XoopsFormText(_AM_TDMDOWNLOADS_FORMTITLE, 'cat_title', 50, 255, $this->getVar('cat_title')), true);
69
        //editeur
70
        $editor_configs=array();
71
        $editor_configs["name"] ="cat_description_main";
72
        $editor_configs["value"] = $this->getVar('cat_description_main', 'e');
73
        $editor_configs["rows"] = 20;
74
        $editor_configs["cols"] = 160;
75
        $editor_configs["width"] = "100%";
76
        $editor_configs["height"] = "400px";
77
        $editor_configs["editor"] = $xoopsModuleConfig['editor'];
78
        $form->addElement( new XoopsFormEditor(_AM_TDMDOWNLOADS_FORMTEXT, "cat_description_main", $editor_configs), false);
79
        //image
80
        $downloadscat_img = $this->getVar('cat_imgurl') ? $this->getVar('cat_imgurl') : 'blank.gif';
81
        $uploadirectory='/uploads/TDMDownloads/images/cats';
82
        $imgtray = new XoopsFormElementTray(_AM_TDMDOWNLOADS_FORMIMG,'<br />');
83
        $imgpath=sprintf(_AM_TDMDOWNLOADS_FORMPATH, $uploadirectory );
84
        $imageselect= new XoopsFormSelect($imgpath, 'downloadscat_img',$downloadscat_img);
85
           $topics_array = XoopsLists :: getImgListAsArray( XOOPS_ROOT_PATH . $uploadirectory );
86
        foreach ($topics_array as $image) {
87
               $imageselect->addOption("$image", $image);
88
        }
89
        $imageselect->setExtra( "onchange='showImgSelected(\"image3\", \"downloadscat_img\", \"" . $uploadirectory . "\", \"\", \"" . XOOPS_URL . "\")'" );
90
        $imgtray->addElement($imageselect,false);
91
        $imgtray -> addElement( new XoopsFormLabel( '', "<br /><img src='" . XOOPS_URL . "/" . $uploadirectory . "/" . $downloadscat_img . "' name='image3' id='image3' alt='' />" ) );
92
        $fileseltray= new XoopsFormElementTray('','<br />');
93
        $fileseltray->addElement(new XoopsFormFile(_AM_TDMDOWNLOADS_FORMUPLOAD , 'attachedfile', $xoopsModuleConfig['maxuploadsize']), false);
94
        $fileseltray->addElement(new XoopsFormLabel('' ), false);
95
        $imgtray->addElement($fileseltray);
96
        $form->addElement($imgtray);
97
        // Pour faire une sous-catégorie
98
        $downloadscat_Handler = xoops_getModuleHandler('tdmdownloads_cat', 'TDMDownloads');
99
        $criteria = new CriteriaCompo();
100
        $criteria->setSort('cat_weight ASC, cat_title');
101
        $criteria->setOrder('ASC');
102
        $downloadscat_arr = $downloadscat_Handler->getall($criteria);
103
        $mytree = new XoopsObjectTree($downloadscat_arr, 'cat_cid', 'cat_pid');
104
        $form->addElement($mytree->makeSelectElement('cat_pid', 'cat_title', '--', $this->getVar('cat_pid'), true, 0, '', _AM_TDMDOWNLOADS_FORMINCAT), true);
105
        //poids de la catégorie
106
        $form->addElement(new XoopsFormText(_AM_TDMDOWNLOADS_FORMWEIGHT, 'cat_weight', 5, 5, $this->getVar('cat_weight', 'e')), false);
107
108
        //permissions
109
        $member_handler =  xoops_gethandler('member');
110
        $group_list = $member_handler->getGroupList();
111
        $gperm_handler = xoops_gethandler('groupperm');
112
        $full_list = array_keys($group_list);
113
        global $xoopsModule;
114
        if (!$this->isNew()) {
115
            $groups_ids_view = $gperm_handler->getGroupIds('tdmdownloads_view', $this->getVar('cat_cid'), $xoopsModule->getVar('mid'));
116
            $groups_ids_submit = $gperm_handler->getGroupIds('tdmdownloads_submit', $this->getVar('cat_cid'), $xoopsModule->getVar('mid'));
117
            $groups_ids_download = $gperm_handler->getGroupIds('tdmdownloads_download', $this->getVar('cat_cid'), $xoopsModule->getVar('mid'));
118
            $groups_ids_view = array_values($groups_ids_view);
119
            $groups_news_can_view_checkbox = new XoopsFormCheckBox(_AM_TDMDOWNLOADS_PERM_VIEW_DSC, 'groups_view[]', $groups_ids_view);
120
            $groups_ids_submit = array_values($groups_ids_submit);
121
            $groups_news_can_submit_checkbox = new XoopsFormCheckBox(_AM_TDMDOWNLOADS_PERM_SUBMIT_DSC, 'groups_submit[]', $groups_ids_submit);
122
            $groups_ids_download = array_values($groups_ids_download);
123
            $groups_news_can_download_checkbox = new XoopsFormCheckBox(_AM_TDMDOWNLOADS_PERM_DOWNLOAD_DSC, 'groups_download[]', $groups_ids_download);
124
        } else {
125
            $groups_news_can_view_checkbox = new XoopsFormCheckBox(_AM_TDMDOWNLOADS_PERM_VIEW_DSC, 'groups_view[]', $full_list);
126
            $groups_news_can_submit_checkbox = new XoopsFormCheckBox(_AM_TDMDOWNLOADS_PERM_SUBMIT_DSC, 'groups_submit[]', $full_list);
127
            $groups_news_can_download_checkbox = new XoopsFormCheckBox(_AM_TDMDOWNLOADS_PERM_DOWNLOAD_DSC, 'groups_download[]', $full_list);
128
        }
129
        // pour voir
130
        $groups_news_can_view_checkbox->addOptionArray($group_list);
131
        $form->addElement($groups_news_can_view_checkbox);
132
        // pour editer
133
        $groups_news_can_submit_checkbox->addOptionArray($group_list);
134
        $form->addElement($groups_news_can_submit_checkbox);
135
        // pour télécharger
136
        if ($xoopsModuleConfig['permission_download'] == 1) {
137
            $groups_news_can_download_checkbox->addOptionArray($group_list);
138
            $form->addElement($groups_news_can_download_checkbox);
139
        }
140
141
        // pour passer "cid" si on modifie la catégorie
142
        if (!$this->isNew()) {
143
            $form->addElement(new XoopsFormHidden('cat_cid', $this->getVar('cat_cid')));
144
            $form->addElement(new XoopsFormHidden('categorie_modified', true));
145
        }
146
        //pour enregistrer le formulaire
147
        $form->addElement(new XoopsFormHidden('op', 'save_cat'));
148
        //boutton d'envoi du formulaire
149
        $form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
150
151
        return $form;
152
    }
153
}
154
155
class TDMDownloadstdmdownloads_catHandler extends XoopsPersistableObjectHandler
156
{
157
	/**
158
     * TDMDownloadstdmdownloads_catHandler constructor.
159
     * @param null|XoopsDatabase $db
160
     */
161
	public function __construct(&$db)
162
    {
163
        parent::__construct($db, "tdmdownloads_cat", 'tdmdownloads_cat', 'cat_cid', 'cat_title');
164
    }
165
}
166