VideoHandler::deleteAll()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 3
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Suico;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * @category        Module
17
 * @copyright       {@link https://xoops.org/ XOOPS Project}
18
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
19
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
20
 */
21
22
use CriteriaElement;
23
use XoopsDatabase;
24
use XoopsFormButton;
25
use XoopsFormHidden;
26
use XoopsFormLabel;
27
use XoopsFormText;
28
use XoopsFormTextArea;
29
use XoopsObject;
30
use XoopsPersistableObjectHandler;
31
use XoopsThemeForm;
32
33
/**
34
 * suico_videohandler class.
35
 * This class provides simple mechanism for Video object
36
 */
37
class VideoHandler extends XoopsPersistableObjectHandler
38
{
39
    public Helper $helper;
40
    public        $isAdmin;
41
42
    /**
43
     * Constructor
44
     * @param \XoopsDatabase|null             $xoopsDatabase
45
     * @param \XoopsModules\Suico\Helper|null $helper
46
     */
47
    public function __construct(
48
        ?XoopsDatabase $xoopsDatabase = null,
49
        $helper = null
50
    ) {
51
        /** @var \XoopsModules\Suico\Helper $this- >helper */
52
        if (null === $helper) {
53
            $this->helper = Helper::getInstance();
54
        } else {
55
            $this->helper = $helper;
56
        }
57
        $this->isAdmin = $this->helper->isUserAdmin();
58
        parent::__construct($xoopsDatabase, 'suico_videos', Video::class, 'video_id', 'video_title', 'video_desc');
0 ignored issues
show
Unused Code introduced by
The call to XoopsPersistableObjectHandler::__construct() has too many arguments starting with 'video_desc'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        parent::/** @scrutinizer ignore-call */ 
59
                __construct($xoopsDatabase, 'suico_videos', Video::class, 'video_id', 'video_title', 'video_desc');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
59
    }
60
61
    /**
62
     * create a new Groups
63
     *
64
     * @param bool $isNew flag the new objects as "new"?
65
     * @return \XoopsObject Groups
66
     */
67
    public function create(
68
        $isNew = true
69
    ) {
70
        $obj = parent::create($isNew);
71
        if ($isNew) {
72
            $obj->setNew();
73
        } else {
74
            $obj->unsetNew();
75
        }
76
        if ($isNew) {
77
            $obj->helper = $this->helper;
78
        }
79
80
        return $obj;
81
    }
82
83
    /**
84
     * retrieve a Video
85
     *
86
     * @param int|null $id of the Video
87
     * @param null     $fields
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fields is correct as it would always require null to be passed?
Loading history...
88
     * @return false|\XoopsModules\Suico\Video reference to the {@link Video} object, FALSE if failed
89
     */
90
    public function get2(
91
        $id = null,
92
        $fields = null
0 ignored issues
show
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

92
        /** @scrutinizer ignore-unused */ $fields = null

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    ) {
94
        $sql = 'SELECT * FROM ' . $this->db->prefix('suico_videos') . ' WHERE video_id=' . $id;
95
        if (!$result = $this->db->query($sql)) {
96
            return false;
97
        }
98
        $numrows = $this->db->getRowsNum($result);
99
        if (1 === $numrows) {
100
            $video = new Video();
101
            $video->assignVars($this->db->fetchArray($result));
102
103
            return $video;
104
        }
105
106
        return false;
107
    }
108
109
    /**
110
     * insert a new Video in the database
111
     *
112
     * @param \XoopsObject $object    reference to the {@link Video}
113
     *                                     object
114
     * @param bool         $force
115
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
116
     */
117
    public function insert2(
118
        XoopsObject $object,
119
        $force = false
120
    ) {
121
        global $xoopsConfig;
122
        if (!$object instanceof Video) {
123
            return false;
124
        }
125
        if (!$object->isDirty()) {
126
            return true;
127
        }
128
        if (!$object->cleanVars()) {
129
            return false;
130
        }
131
        $uid_owner = '';
132
        $video_id  = '';
133
        foreach ($object->cleanVars as $k => $v) {
134
            ${$k} = $v;
135
        }
136
        //        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
137
        if ($object->isNew()) {
138
            // ajout/modification d'un Video
139
            $object = new Video();
140
            $format      = 'INSERT INTO %s (video_id, uid_owner, video_title, video_desc, youtube_code, featured_video)';
141
            $format      .= 'VALUES (%u, %u, %s, %s, %s, %s)';
142
            $sql         = \sprintf(
143
                $format,
144
                $this->db->prefix('suico_videos'),
145
                $video_id,
146
                $uid_owner,
147
                $this->db->quoteString($video_title),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $video_title does not exist. Did you maybe mean $video_id?
Loading history...
148
                $this->db->quoteString($video_desc),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $video_desc does not exist. Did you maybe mean $video_id?
Loading history...
149
                $this->db->quoteString($youtube_code),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $youtube_code seems to be never defined.
Loading history...
150
                $this->db->quoteString($featured_video)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $featured_video seems to be never defined.
Loading history...
151
            );
152
            $force       = true;
153
        } else {
154
            $format = 'UPDATE %s SET ';
155
            $format .= 'video_id=%u, uid_owner=%u, video_title=%s, video_desc=%s, youtube_code=%s, featured_video=%s';
156
            $format .= ' WHERE video_id = %u';
157
            $sql    = \sprintf(
158
                $format,
159
                $this->db->prefix('suico_videos'),
160
                $video_id,
161
                $uid_owner,
162
                $this->db->quoteString($video_title),
163
                $this->db->quoteString($video_desc),
164
                $this->db->quoteString($youtube_code),
165
                $this->db->quoteString($featured_video),
166
                $video_id
167
            );
168
        }
169
        if ($force) {
170
            $result = $this->db->queryF($sql);
171
        } else {
172
            $result = $this->db->query($sql);
173
        }
174
        if (!$result) {
175
            return false;
176
        }
177
        if (empty($video_id)) {
178
            $video_id = $this->db->getInsertId();
179
        }
180
        $object->assignVar('video_id', $video_id);
181
182
        return true;
183
    }
184
185
    /**
186
     * delete a Video from the database
187
     *
188
     * @param \XoopsObject $object reference to the Video to delete
189
     * @param bool         $force
190
     * @return bool FALSE if failed.
191
     */
192
    public function delete(
193
        XoopsObject $object,
194
        $force = false
195
    ) {
196
        if (!$object instanceof Video) {
197
            return false;
198
        }
199
        $sql = \sprintf(
200
            'DELETE FROM %s WHERE video_id = %u',
201
            $this->db->prefix('suico_videos'),
202
            (int)$object->getVar('video_id')
203
        );
204
        if ($force) {
205
            $result = $this->db->queryF($sql);
206
        } else {
207
            $result = $this->db->query($sql);
208
        }
209
        if (!$result) {
210
            return false;
211
        }
212
213
        return true;
214
    }
215
216
    /**
217
     * retrieve suico_videos from the database
218
     *
219
     * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} conditions to be met
220
     * @param bool                                 $id_as_key       use the UID as key for the array?
221
     * @param bool                                 $as_object
222
     * @return array array of {@link Video} objects
223
     */
224
    public function &getObjects(
225
        ?CriteriaElement $criteria = null,
226
        $id_as_key = false,
227
        $as_object = true
228
    ) {
229
        $ret   = [];
230
        $start = 0;
231
        $limit = 0;
232
        $sql   = 'SELECT * FROM ' . $this->db->prefix('suico_videos');
233
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
234
            $sql   .= ' ' . $criteria->renderWhere();
0 ignored issues
show
Bug introduced by
The method renderWhere() does not exist on CriteriaElement. Did you maybe mean render()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

234
            $sql   .= ' ' . $criteria->/** @scrutinizer ignore-call */ renderWhere();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
235
            $sort  = 'video_id';
236
            $order = 'DESC';
237
            //if ('' !== $criteria->getSort()) {
238
            //    $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
239
            //}
240
            if ('' !== $sort) {
0 ignored issues
show
introduced by
The condition '' !== $sort is always true.
Loading history...
241
                $sql .= ' ORDER BY ' . $sort . ' ' . $order;
242
            }
243
            $limit = $criteria->getLimit();
244
            $start = $criteria->getStart();
245
        }
246
        $result = $this->db->query($sql, $limit, $start);
247
        if (!$result) {
248
            return $ret;
249
        }
250
        while (false !== ($myrow = $this->db->fetchArray($result))) {
251
            $video = new Video();
252
            $video->assignVars($myrow);
253
            if ($id_as_key) {
254
                $ret[$myrow['video_id']] = &$video;
255
            } else {
256
                $ret[] = &$video;
257
            }
258
            unset($video);
259
        }
260
261
        return $ret;
262
    }
263
264
    /**
265
     * count suico_videos matching a condition
266
     *
267
     * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} to match
268
     * @return int count of suico_videos
269
     */
270
    public function getCount(
271
        ?CriteriaElement $criteria = null
272
    ) {
273
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_videos');
274
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
275
            $sql .= ' ' . $criteria->renderWhere();
276
        }
277
        $result = $this->db->query($sql);
278
        if (!$result) {
279
            return 0;
280
        }
281
        [$count] = $this->db->fetchRow($result);
282
283
        return (int)$count;
284
    }
285
286
    /**
287
     * delete suico_videos matching a set of conditions
288
     *
289
     * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement}
290
     * @param bool                                 $force
291
     * @param bool                                 $asObject
292
     * @return bool FALSE if deletion failed
293
     */
294
    public function deleteAll(
295
        ?CriteriaElement $criteria = null,
296
        $force = true,
297
        $asObject = false
298
    ) {
299
        $sql = 'DELETE FROM ' . $this->db->prefix('suico_videos');
300
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
301
            $sql .= ' ' . $criteria->renderWhere();
302
        }
303
        if (!$result = $this->db->query($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
304
            return false;
305
        }
306
307
        return true;
308
    }
309
310
    /**
311
     * Render a form to send videos
312
     *
313
     * @param \XoopsTpl $xoopsTpl the one in which the form will be rendered
314
     * @return bool TRUE
315
     *
316
     * obs: Some functions wont work on php 4 so edit lines down under acording to your version
317
     */
318
    public function renderFormSubmit(
319
        $xoopsTpl
320
    ) {
321
        $form       = new XoopsThemeForm(\_MD_SUICO_ADDFAVORITEVIDEOS, 'form_videos', 'submitVideo.php', 'post', true);
322
        $field_code = new XoopsFormText(\_MD_SUICO_YOUTUBECODE, 'videourl', 50, 250);
323
        $field_desc = new XoopsFormTextArea(\_MD_SUICO_CAPTION, 'caption');
324
        $form->setExtra('enctype="multipart/form-data"');
325
        $buttonSend = new XoopsFormButton('', 'submit_button', \_MD_SUICO_ADDVIDEO, 'submit');
326
        $form->addElement($field_warning);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $field_warning seems to be never defined.
Loading history...
327
        $form->addElement($field_code, true);
328
        $form->addElement($field_desc);
329
        $form->addElement($buttonSend);
330
        $form->assign($xoopsTpl); //If your server is php 5
331
        //$form->display();
332
        return true;
333
    }
334
335
    /**
336
     * Render a form to edit the description of the pictures
337
     *
338
     * @param        $title
339
     * @param string $caption  The description of the picture
340
     * @param int    $video_id the id of the image in database
341
     * @param string $filename the url to the thumb of the image so it can be displayed
342
     * @return bool TRUE
343
     */
344
    public function renderFormEdit(
345
        $title,
346
        $caption,
347
        $video_id,
348
        $filename
349
    ) {
350
        $form        = new XoopsThemeForm(\_MD_SUICO_EDIT_VIDEO, 'form_picture', 'editvideo.php', 'post', true);
351
        $field_title = new XoopsFormText($title, 'title', 35, 55);
352
        $field_desc  = new XoopsFormText($caption, 'caption', 35, 55);
353
        $form->setExtra('enctype="multipart/form-data"');
354
        $buttonSend     = new XoopsFormButton('', 'submit_button', \_MD_SUICO_SUBMIT, 'submit');
355
        $field_warning  = new XoopsFormLabel(
356
            '<object width="425" height="353">
357
<param name="movie" value="https://www.youtube.com/v/' . $filename . '"></param>
358
<param name="wmode" value="transparent"></param>
359
<embed src="https://www.youtube.com/v/' . $filename . '" type="application/x-shockwave-flash" wmode="transparent" width="425" height="353"></embed>
360
</object>'
361
        );
362
        $field_video_id = new XoopsFormHidden('video_id', $video_id);
363
        $field_marker   = new XoopsFormHidden('marker', 1);
364
        $form->addElement($field_warning);
365
        $form->addElement($field_title);
366
        $form->addElement($field_desc);
367
        $form->addElement($field_video_id, true);
368
        $form->addElement($field_marker);
369
        $form->addElement($buttonSend);
370
        $form->display();
371
372
        return true;
373
    }
374
375
    /**
376
     * @param null $uid_owner
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $uid_owner is correct as it would always require null to be passed?
Loading history...
377
     * @return bool
378
     */
379
    public function unsetAllMainsbyID($uid_owner = null)
380
    {
381
        $sql = 'UPDATE ' . $this->db->prefix('suico_videos') . ' SET featured_video=0 WHERE uid_owner=' . $uid_owner;
382
        if (!$result = $this->db->query($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
383
            return false;
384
        }
385
386
        return true;
387
    }
388
}
389