Completed
Push — master ( 72613d...069c91 )
by Michael
02:16
created

public-modify.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * ExtGallery User area
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   {@link https://xoops.org/ XOOPS Project}
13
 * @license     GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
14
 * @author      Zoullou (http://www.zoullou.net)
15
 * @package     ExtGallery
16
 */
17
18
include __DIR__ . '/header.php';
19
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
20
require_once __DIR__ . '/class/utility.php';
21
22
if (isset($_GET['op'])) {
23
    $op = $_GET['op'];
24
} else {
25
    $op = 'default';
26
}
27
28
if (isset($_POST['step'])) {
29
    $step = $_POST['step'];
30
} else {
31
    $step = 'default';
32
}
33
34
if (!isset($GLOBALS['xoopsUser'])) {
35
    redirect_header('index.php');
36
} elseif (!$GLOBALS['xoopsUser']->isAdmin()) {
37
    redirect_header('index.php');
38
}
39
$moduleDirName = basename(__DIR__);
40
$utilityClass  = ucfirst($moduleDirName) . 'Utility';
41
switch ($op) {
42
43
    case 'edit':
44
45
        switch ($step) {
46
47
            case 'enreg':
48
                /** @var ExtgalleryPublicPhotoHandler $photoHandler */
49
                $photoHandler = xoops_getModuleHandler('publicphoto', 'extgallery');
50
                $myts         = MyTextSanitizer::getInstance();
51
                $photo        = $photoHandler->getPhoto($_POST['photo_id']);
52
53
                $data['cat_id']       = $_POST['cat_id'];
54
                $data['photo_desc']   = $_POST['photo_desc'];
55
                $data['photo_title']  = $_POST['photo_title'];
56
                $data['photo_weight'] = $_POST['photo_weight'];
57
58
                if (isset($_POST['photo_extra'])) {
59
                    $data['photo_extra'] = $_POST['photo_extra'];
60
                }
61
62
                $photoHandler->modifyPhoto((int)$_POST['photo_id'], $data);
63
64
                // For xoops tag
65
                if ((1 == $xoopsModuleConfig['usetag']) && is_dir('../tag')) {
66
                    $tagHandler = xoops_getModuleHandler('tag', 'tag');
67
                    $tagHandler->updateByItem($_POST['tag'], $_POST['photo_id'], $xoopsModule->getVar('dirname'), 0);
68
                }
69
70
                // If the photo category change
71
                if ($photo->getVar('cat_id') != $_POST['cat_id']) {
72
                    $catHandler = xoops_getModuleHandler('publiccat', 'extgallery');
73
                    $oldCat     = $catHandler->getCat($photo->getVar('cat_id'));
74
                    $newCat     = $catHandler->getCat($_POST['cat_id']);
75
76
                    // Set new category as album
77
                    $catHandler->modifyCat(['cat_id' => (int)$_POST['cat_id'], 'cat_isalbum' => 1]);
78
79
                    // Update album count
80 View Code Duplication
                    if (1 == $oldCat->getVar('cat_nb_photo')) {
0 ignored issues
show
This code seems to be duplicated across 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...
81
                        $criteria = new CriteriaCompo();
82
                        $criteria->add(new Criteria('nleft', $oldCat->getVar('nleft'), '<'));
83
                        $criteria->add(new Criteria('nright', $oldCat->getVar('nright'), '>'));
84
                        $catHandler->updateFieldValue('cat_nb_album', 'cat_nb_album - 1', $criteria);
85
                    }
86
87 View Code Duplication
                    if (0 == $newCat->getVar('cat_nb_photo')) {
0 ignored issues
show
This code seems to be duplicated across 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...
88
                        $criteria = new CriteriaCompo();
89
                        $criteria->add(new Criteria('nleft', $newCat->getVar('nleft'), '<'));
90
                        $criteria->add(new Criteria('nright', $newCat->getVar('nright'), '>'));
91
                        $catHandler->updateFieldValue('cat_nb_album', 'cat_nb_album + 1', $criteria);
92
                    }
93
94
                    // Update photo count
95
                    $criteria = new CriteriaCompo();
96
                    $criteria->add(new Criteria('nleft', $newCat->getVar('nleft'), '<='));
97
                    $criteria->add(new Criteria('nright', $newCat->getVar('nright'), '>='));
98
                    $catHandler->updateFieldValue('cat_nb_photo', 'cat_nb_photo + 1', $criteria);
99
100
                    $criteria = new CriteriaCompo();
101
                    $criteria->add(new Criteria('nleft', $oldCat->getVar('nleft'), '<='));
102
                    $criteria->add(new Criteria('nright', $oldCat->getVar('nright'), '>='));
103
                    $catHandler->updateFieldValue('cat_nb_photo', 'cat_nb_photo - 1', $criteria);
104
105
                    // If the old album don't contains other photo
106
                    if (0 == $photoHandler->nbPhoto($oldCat)) {
107
                        $catHandler->modifyCat(['cat_id' => $photo->getVar('cat_id'), 'cat_isalbum' => 0]);
108
                        redirect_header('public-categories.php?id=' . $photo->getVar('cat_id'), 3, _MD_EXTGALLERY_PHOTO_UPDATED);
109
                    } else {
110
                        redirect_header('public-album.php?id=' . $photo->getVar('cat_id'), 3, _MD_EXTGALLERY_PHOTO_UPDATED);
111
                    }
112
                } else {
113
                    redirect_header('public-photo.php?photoId=' . $photo->getVar('photo_id'), 3, _MD_EXTGALLERY_PHOTO_UPDATED);
114
                }
115
116
                break;
117
118
            case 'default':
119
            default:
120
121
                require_once XOOPS_ROOT_PATH . '/header.php';
122
                $myts = MyTextSanitizer::getInstance();
123
                /** @var ExtgalleryPublicCatHandler $catHandler */
124
                $catHandler = xoops_getModuleHandler('publiccat', 'extgallery');
125
                /** @var ExtgalleryPublicPhotoHandler $photoHandler */
126
                $photoHandler = xoops_getModuleHandler('publicphoto', 'extgallery');
127
128
                $photo = $photoHandler->getPhoto((int)$_GET['id']);
129
130
                echo '<img src="' . XOOPS_URL . '/uploads/extgallery/public-photo/thumb/thumb_' . $photo->getVar('photo_name') . '">';
131
132
                $form = new XoopsThemeForm(_MD_EXTGALLERY_MODIFY_PHOTO, 'add_photo', 'public-modify.php?op=edit', 'post', true);
133
                $form->addElement(new XoopsFormLabel(_MD_EXTGALLERY_CATEGORY, $catHandler->getLeafSelect('cat_id', false, $photo->getVar('cat_id'))));
134
                $form->addElement(new XoopsFormText(_MD_EXTGALLERY_PHOTO_WEIGHT, 'photo_weight', '3', '11', $photo->getVar('photo_weight')), false);
135
                $form->addElement(new XoopsFormText(_MD_EXTGALLERY_PHOTO_TITLE, 'photo_title', '50', '150', $photo->getVar('photo_title')), false);
136
                //DNPROSSI - wysiwyg editors from xoopseditors
137
                //TODO dohtml - dobr
138
                $photo_desc = $myts->displayTarea($photo->getVar('photo_desc'), 0, 1, 1, 1, 0);
139
                $editor     = $utilityClass::getWysiwygForm(_MD_EXTGALLERY_DESC, 'photo_desc', $photo_desc, 15, 60, '100%', '350px', 'hometext_hidden');
140
                $form->addElement($editor, false);
141
                if ($xoopsModuleConfig['display_extra_field']) {
142
                    $form->addElement(new XoopsFormTextArea(_MD_EXTGALLERY_EXTRA_INFO, 'photo_extra', $photo->getVar('photo_extra')));
143
                }
144
145
                // For xoops tag
146
                if ((1 == $xoopsModuleConfig['usetag']) && is_dir('../tag')) {
147
                    $tagId = $photo->isNew() ? 0 : $photo->getVar('photo_id');
148
                    require_once XOOPS_ROOT_PATH . '/modules/tag/include/formtag.php';
149
                    $form->addElement(new TagFormTag('tag', 60, 255, $tagId, 0));
150
                }
151
152
                $form->addElement(new XoopsFormHidden('photo_id', $_GET['id']));
153
                $form->addElement(new XoopsFormHidden('step', 'enreg'));
154
                $form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
155
                $form->display();
156
157
                include XOOPS_ROOT_PATH . '/footer.php';
158
159
                break;
160
161
        }
162
163
        break;
164
165
    case 'delete':
166
        /** @var ExtgalleryPublicCatHandler $catHandler */
167
        $catHandler = xoops_getModuleHandler('publiccat', 'extgallery');
168
        /** @var ExtgalleryPublicPhotoHandler $photoHandler */
169
        $photoHandler = xoops_getModuleHandler('publicphoto', 'extgallery');
170
171
        $photo = $photoHandler->getPhoto((int)$_GET['id']);
172
        $photoHandler->deletePhoto($photo);
173
174
        $cat = $catHandler->getCat($photo->getVar('cat_id'));
175
176
        // Update photo count
177
        $criteria = new CriteriaCompo();
178
        $criteria->add(new Criteria('nleft', $cat->getVar('nleft'), '<='));
179
        $criteria->add(new Criteria('nright', $cat->getVar('nright'), '>='));
180
        $catHandler->updateFieldValue('cat_nb_photo', 'cat_nb_photo - 1', $criteria);
181
182
        if (1 == $cat->getVar('cat_nb_photo')) {
0 ignored issues
show
The method getVar cannot be called on $cat (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
183
184
            // Update album count
185
            $criteria = new CriteriaCompo();
186
            $criteria->add(new Criteria('nleft', $cat->getVar('nleft'), '<'));
187
            $criteria->add(new Criteria('nright', $cat->getVar('nright'), '>'));
188
            $catHandler->updateFieldValue('cat_nb_album', 'cat_nb_album - 1', $criteria);
189
190
            $catHandler->modifyCat(['cat_id' => $photo->getVar('cat_id'), 'cat_isalbum' => 0]);
191
192
            redirect_header('public-categories.php?id=' . $photo->getVar('cat_id'));
193
        } else {
194
            redirect_header('public-album.php?id=' . $photo->getVar('cat_id'));
195
        }
196
197
        break;
198
199
}
200