Delete.php 4.43 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
     * @param string $primaryKey
55
56
     * @throws CodeException
     * @throws DbException
57
     * @throws UserFormException
58
     */
59
    public function process($tableName, $recordId, $primaryKey = F_PRIMARY_KEY_DEFAULT) {
60

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
        // Take care the necessary target directories exist.
        $cwd = getcwd();
        $sitePath = $this->store->getVar(SYSTEM_SITE_PATH, STORE_SYSTEM);
72
        if ($cwd === false || $sitePath === false || !HelperFile::chdir($sitePath)) {
73
74
75
76
            throw new UserFormException(
                json_encode([ERROR_MESSAGE_TO_USER => 'getcwd() failed or SITE_PATH undefined or chdir() failed', ERROR_MESSAGE_SUPPORT => "getcwd() failed or SITE_PATH undefined or chdir('$sitePath') failed."]),
                ERROR_IO_CHDIR);

77
78
        }

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

83
            $this->deleteReferencedFiles($row, $tableName, $primaryKey);
84

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

91
92
        }

93
        HelperFile::chdir($cwd);
94
95
96
97
    }

    /**
     * Iterates over array $row and searches for column names with substring COLUMN_PATH_FILE_NAME.
98
     * For any found, check if it references a writable file.
99
100
101
102
103
104
     * 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
105
106
     * @param       $tableName
     *
107
     * @param $primaryKey
108
109
110
111
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     */
112
    private function deleteReferencedFiles(array $row, $tableName, $primaryKey) {
113
114
115
116
117

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

119
120
121
122
123
            // 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.
124
                $samePathFileName = $this->db->sql("SELECT COUNT($primaryKey) AS cnt FROM $tableName WHERE $key LIKE ?", ROW_EXPECT_1, [$file]);
125
                if ($samePathFileName['cnt'] === 1) {
126
                    HelperFile::unlink($file);
127
                    $this->db->deleteSplitFileAndRecord($row[$primaryKey], $tableName);
128
129
130
                }
            }
        }
131
    }
132

133
}