FileHandler::delete()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 10
1
<?php declare(strict_types=1);
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
/**
16
 * @copyright       XOOPS Project (https://xoops.org)
17
 * @license         https://www.fsf.org/copyleft/gpl.html GNU public license
18
 * @since           1.0
19
 * @author          trabis <[email protected]>
20
 * @author          The SmartFactory <www.smartfactory.ca>
21
 */
22
require_once \dirname(__DIR__) . '/include/common.php';
23
24
// File status
25
//define("_PUBLISHER_STATUS_FILE_NOTSET", -1);
26
//define("_PUBLISHER_STATUS_FILE_ACTIVE", 1);
27
//define("_PUBLISHER_STATUS_FILE_INACTIVE", 2);
28
29
/**
30
 * Files handler class.
31
 * This class is responsible for providing data access mechanisms to the data source
32
 * of File class objects.
33
 *
34
 * @author  marcan <[email protected]>
35
 */
36
class FileHandler extends \XoopsPersistableObjectHandler
37
{
38
    private const TABLE      = 'publisher_files';
39
    private const ENTITY     = File::class;
40
    private const ENTITYNAME = 'File';
41
    private const KEYNAME    = 'fileid';
42
    private const IDENTIFIER = 'name';
43
    public $table_link = '';
44
    /**
45
     * @var Helper
46
     */
47
    public $helper;
48
49
    public function __construct(?\XoopsDatabase $db = null, ?Helper $helper = null)
50
    {
51
        /** @var Helper $this- >helper */
52
        $this->helper = $helper ?? Helper::getInstance();
53
        $this->db     = $db;
54
        parent::__construct($db, static::TABLE, static::ENTITY, static::KEYNAME, static::IDENTIFIER);
55
    }
56
57
    /**
58
     * delete a file from the database
59
     *
60
     * @param \XoopsObject|File $file reference to the file to delete
61
     * @param bool              $force
62
     *
63
     * @return bool FALSE if failed.
64
     */
65
    public function delete(\XoopsObject $file, $force = false) //delete(&$file, $force = false)
66
    {
67
        $ret = false;
68
        // Delete the actual file
69
        if (\is_file($file->getFilePath()) && \unlink($file->getFilePath())) {
0 ignored issues
show
Bug introduced by
The method getFilePath() does not exist on XoopsObject. It seems like you code against a sub-type of XoopsObject such as XoopsModules\Publisher\Item or XoopsModules\Publisher\File or XoopsModules\Publisher\Category. ( Ignorable by Annotation )

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

69
        if (\is_file($file->/** @scrutinizer ignore-call */ getFilePath()) && \unlink($file->getFilePath())) {
Loading history...
70
            $ret = parent::delete($file, $force);
71
        }
72
73
        return $ret;
74
    }
75
76
    /**
77
     * delete files related to an item from the database
78
     *
79
     * @param \XoopsObject $itemObj reference to the item which files to delete
80
     *
81
     * @return bool
82
     */
83
    public function deleteItemFiles(\XoopsObject $itemObj)
84
    {
85
        if ('publisheritem' !== \mb_strtolower(\get_class($itemObj))) {
86
            return false;
87
        }
88
        $files  = $this->getAllFiles($itemObj->itemid());
0 ignored issues
show
Bug introduced by
The method itemid() does not exist on XoopsObject. It seems like you code against a sub-type of XoopsObject such as XoopsModules\Publisher\Item or XoopsModules\Publisher\File or XoopsModules\Publisher\Category. ( Ignorable by Annotation )

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

88
        $files  = $this->getAllFiles($itemObj->/** @scrutinizer ignore-call */ itemid());
Loading history...
89
        $result = true;
90
        foreach ($files as $file) {
91
            if (!$this->delete($file)) {
92
                $result = false;
93
            }
94
        }
95
96
        return $result;
97
    }
98
99
    /**
100
     * retrieve all files
101
     *
102
     * @param int       $itemId
103
     * @param int|array $status
104
     * @param int       $limit
105
     * @param int       $start
106
     * @param string    $sort
107
     * @param string    $order
108
     * @param array     $category
109
     *
110
     * @return array array of {@link File} objects
111
     */
112
    public function getAllFiles($itemId = 0, $status = -1, $limit = 0, $start = 0, $sort = 'datesub', $order = 'DESC', $category = [])
113
    {
114
        $files = [];
115
116
        $this->table_link = $this->db->prefix($this->helper->getDirname() . '_items');
117
118
        $result = $GLOBALS['xoopsDB']->query('SELECT COUNT(*) FROM ' . $this->db->prefix($this->helper->getDirname() . '_files'));
119
        [$count] = $GLOBALS['xoopsDB']->fetchRow($result);
120
        if ($count > 0) {
121
            $this->field_object = 'itemid';
122
            $this->field_link   = 'itemid';
123
            $hasStatusCriteria  = false;
124
            $criteriaStatus     = new \CriteriaCompo();
125
            if (\is_array($status)) {
126
                $hasStatusCriteria = true;
127
                foreach ($status as $v) {
128
                    $criteriaStatus->add(new \Criteria('o.status', $v), 'OR');
129
                }
130
            } elseif (-1 != $status) {
131
                $hasStatusCriteria = true;
132
                $criteriaStatus->add(new \Criteria('o.status', $status), 'OR');
133
            }
134
            $hasCategoryCriteria = false;
135
            $criteriaCategory    = new \CriteriaCompo();
136
            $category            = (array)$category;
137
            if (isset($category[0]) && 0 != $category[0] && \count($category) > 0) {
138
                $hasCategoryCriteria = true;
139
                foreach ($category as $cat) {
140
                    $criteriaCategory->add(new \Criteria('l.categoryid', $cat), 'OR');
141
                }
142
            }
143
            $criteriaItemid = new \Criteria('o.itemid', $itemId);
144
            $criteria       = new \CriteriaCompo();
145
            if (0 != $itemId) {
146
                $criteria->add($criteriaItemid);
147
            }
148
            if ($hasStatusCriteria) {
149
                $criteria->add($criteriaStatus);
150
            }
151
            if ($hasCategoryCriteria) {
152
                $criteria->add($criteriaCategory);
153
            }
154
            $criteria->setSort($sort);
155
            $criteria->order = $order; // patch for XOOPS <= 2.5.10, does not set order correctly using setOrder() method
156
            $criteria->setLimit($limit);
157
            $criteria->setStart($start);
158
            $files = $this->getByLink($criteria, ['o.*'], true);
159
            //            return $files;
160
        }
161
162
        return $files;
163
    }
164
}
165