Delete.php 4.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 6/1/16
 * Time: 8:52 AM
 */

namespace qfq;

require_once(__DIR__ . '/Constants.php');
12
require_once(__DIR__ . '/database/Database.php');
13
require_once(__DIR__ . '/store/Store.php');
14
15


16
17
18
19
/**
 * Class Delete
 * @package qfq
 */
20
21
22
23
24
25
class Delete {
    /**
     * @var Database
     */
    private $db = null;

26
27
28
29
30
    /**
     * @var Store
     */
    private $store = null;

31
    /**
32
33
     * @param bool $dbIndexData
     * @param bool $phpUnit
34
     * @throws CodeException
35
     * @throws DbException
36
     * @throws UserFormException
37
     * @throws UserReportException
38
     */
39
40
41
    public function __construct($dbIndexData = false, $phpUnit = false) {

        $this->db = new Database($dbIndexData);
42
        $this->store = Store::getInstance('', $phpUnit);
43
44
45
    }

    /**
46
     * Deletes the record id=$recordId from table $tableName.
47
     * If the table has a column named COLUMN_PATH_FILE_NAME and the value of that specific record column points
Carsten  Rose's avatar
Carsten Rose committed
48
49
     * to a file: delete such a file if their are no other records in the same table which also have a reference to
     * that file.
50
     *
Carsten  Rose's avatar
Carsten Rose committed
51
     * @param string $tableName
52
     * @param integer $recordId
Carsten  Rose's avatar
Carsten Rose committed
53
     *
54
55
     * @throws CodeException
     * @throws DbException
56
     * @throws UserFormException
57
     */
58
    public function process($tableName, $recordId, $primaryKey = F_PRIMARY_KEY_DEFAULT) {
59
60
        $msg = array();

61
        if ($tableName === false || $tableName === '') {
62
63
64
65
66
67
68
            throw new CodeException('Missing table name', ERROR_MISSING_TABLE_NAME);
        }

        if ($recordId === 0 || $recordId === '') {
            throw new CodeException('Invalid record id', ERROR_MISSING_RECORD_ID);
        }

69
70
71
72
73
74
75
        // Take care the necessary target directories exist.
        $cwd = getcwd();
        $sitePath = $this->store->getVar(SYSTEM_SITE_PATH, STORE_SYSTEM);
        if ($cwd === false || $sitePath === false || !chdir($sitePath)) {
            throw new UserFormException("getcwd() failed or SITE_PATH undefined or chdir('$sitePath') failed.", ERROR_IO_CHDIR);
        }

76
        // Read record first.
77
        $row = $this->db->sql("SELECT * FROM $tableName WHERE $primaryKey=?", ROW_EXPECT_0_1, [$recordId]);
78
79
        if (count($row) > 0) {

80
            $this->deleteReferencedFiles($row, $tableName, $primaryKey);
81

82
            $this->db->sql("DELETE FROM $tableName WHERE $primaryKey =? LIMIT 1", ROW_REGULAR, [$recordId]);
83
        } else {
84
            throw new UserFormException("Record $recordId not found in table '$tableName'.", ERROR_RECORD_NOT_FOUND);
85
86
        }

87
        chdir($cwd);
88
89
90
91
92
93
94
95
96
97
98
    }

    /**
     * Iterates over array $row and searches for column names with substring COLUMN_PATH_FILE_NAME.
     * For any found, check if it references a writeable file.
     * If yes: check if there are other records (same table, same column) which references the same file.
     *         If no: delete the file
     *         If yes: do nothing, continue with the next column.
     * If no: do nothing, continue with the next column.
     *
     * @param array $row
Carsten  Rose's avatar
Carsten Rose committed
99
100
     * @param       $tableName
     *
101
102
103
104
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     */
105
    private function deleteReferencedFiles(array $row, $tableName, $primaryKey) {
106
107
108
109
110

        foreach ($row AS $key => $file) {
            if (false === strpos($key, COLUMN_PATH_FILE_NAME)) {
                continue;
            }
111

112
113
114
115
116
            // check if there is a file referenced in the record which have to be deleted too.
            if ($file !== '' && is_writable($file)) {

                // check if there are other records referencing the same file: do not delete the file now.
                // This check won't find duplicates, if they are spread over different columns or tables.
117
                $samePathFileName = $this->db->sql("SELECT COUNT($primaryKey) AS cnt FROM $tableName WHERE $key LIKE ?", ROW_EXPECT_1, [$file]);
118
119
120
121
122
123
124
                if ($samePathFileName['cnt'] === 1) {
                    if (!unlink($file)) {
                        throw new UserFormException("Error deleting file: $file", ERROR_IO_UNLINK);
                    }
                }
            }
        }
125
126
    }
}