Completed
Push — master ( 658b35...b781ca )
by Richard
28s queued 23s
created

FileHandler::deleteItemFiles()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Publisher;
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
use Xoops\Core\Database\Connection;
16
use Xoops\Core\Kernel\Criteria;
17
use Xoops\Core\Kernel\CriteriaCompo;
18
use Xoops\Core\Kernel\XoopsObject;
19
use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
20
use XoopsModules\Publisher;
21
22
/**
23
 * @copyright       The XUUPS Project http://sourceforge.net/projects/xuups/
24
 * @license         GNU GPL V2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
25
 * @package         Publisher
26
 * @since           1.0
27
 * @author          trabis <[email protected]>
28
 * @author          The SmartFactory <www.smartfactory.ca>
29
 * @version         $Id$
30
 */
31
require_once \dirname(__DIR__) . '/include/common.php';
32
33
// File status
34
\define('_PUBLISHER_STATUS_FILE_NOTSET', -1);
35
\define('_PUBLISHER_STATUS_FILE_ACTIVE', 1);
36
\define('_PUBLISHER_STATUS_FILE_INACTIVE', 2);
37
38
/**
39
 * Files handler class.
40
 * This class is responsible for providing data access mechanisms to the data source
41
 * of File class objects.
42
 *
43
 * @author  marcan <[email protected]>
44
 * @package Publisher
45
 */
46
class FileHandler extends XoopsPersistableObjectHandler
47
{
48
    public function __construct(Connection $db)
49
    {
50
        parent::__construct($db, 'publisher_files', 'Publisher\File', 'fileid', 'name');
51
    }
52
53
    /**
54
     * delete a file from the database
55
     *
56
     * @param XoopsObject|Publisher\File $file reference to the file to delete
57
     * @param bool                       $force
58
     *
59
     * @return bool FALSE if failed.
60
     */
61
    public function delete(XoopsObject $file, $force = false)
62
    {
63
        $ret = false;
64
        // Delete the actual file
65
        if (\is_file($file->getFilePath()) && \unlink($file->getFilePath())) {
0 ignored issues
show
Bug introduced by
The method getFilePath() does not exist on Xoops\Core\Kernel\XoopsObject. It seems like you code against a sub-type of Xoops\Core\Kernel\XoopsObject such as XoopsModules\Publisher\File. ( Ignorable by Annotation )

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

65
        if (\is_file($file->/** @scrutinizer ignore-call */ getFilePath()) && \unlink($file->getFilePath())) {
Loading history...
66
            $ret = parent::delete($file, $force);
67
        }
68
69
        return $ret;
70
    }
71
72
    /**
73
     * delete files related to an item from the database
74
     *
75
     * @param object $itemObj reference to the item which files to delete
76
     */
77
    public function deleteItemFiles(&$itemObj): bool
78
    {
79
        if ('publisheritem' !== \mb_strtolower(\get_class($itemObj))) {
80
            return false;
81
        }
82
        $files = $this->getAllFiles($itemObj->getVar('itemid'));
83
        $result = true;
84
        foreach ($files as $file) {
85
            if (!$this->delete($file)) {
86
                $result = false;
87
            }
88
        }
89
90
        return $result;
91
    }
92
93
    /**
94
     * retrieve all files
95
     *
96
     * @param int    $itemid
97
     * @param int|array    $status
98
     * @param int    $limit
99
     * @param int    $start
100
     * @param string $sort
101
     * @param string $order
102
     * @param array  $category
103
     *
104
     * @return array array of {@link Publisher\File} objects
105
     */
106
    public function &getAllFiles($itemid = 0, $status = -1, $limit = 0, $start = 0, $sort = 'datesub', $order = 'DESC', $category = []): array
107
    {
108
        $this->table_link = $this->db2->prefix('publisher_items');
109
        $this->field_object = 'itemid';
110
        $this->field_link = 'itemid';
111
        $hasStatusCriteria = false;
112
        $criteriaStatus = new CriteriaCompo();
113
        if (\is_array($status)) {
114
            $hasStatusCriteria = true;
115
            foreach ($status as $v) {
116
                $criteriaStatus->add(new Criteria('o.status', $v), 'OR');
117
            }
118
        } elseif (-1 != $status) {
119
            $hasStatusCriteria = true;
120
            $criteriaStatus->add(new Criteria('o.status', $status), 'OR');
121
        }
122
        $hasCategoryCriteria = false;
123
        $criteriaCategory = new CriteriaCompo();
124
        $category = (array)$category;
125
        if (\count($category) > 0 && 0 != $category[0]) {
126
            $hasCategoryCriteria = true;
127
            foreach ($category as $cat) {
128
                $criteriaCategory->add(new Criteria('l.categoryid', $cat), 'OR');
129
            }
130
        }
131
        $criteriaItemid = new Criteria('o.itemid', $itemid);
132
        $criteria = new CriteriaCompo();
133
        if (0 != $itemid) {
134
            $criteria->add($criteriaItemid);
135
        }
136
        if ($hasStatusCriteria) {
137
            $criteria->add($criteriaStatus);
138
        }
139
        if ($hasCategoryCriteria) {
140
            $criteria->add($criteriaCategory);
141
        }
142
        $criteria->setSort($sort);
143
        $criteria->setOrder($order);
144
        $criteria->setLimit($limit);
145
        $criteria->setStart($start);
146
        $files = $this->getByLink($criteria, ['o.*'], true);
147
148
        return $files;
149
    }
150
}
151