Delete.php 3.8 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
20
21


class Delete {
    /**
     * @var Database
     */
    private $db = null;

22
23
24
25
26
    /**
     * @var Store
     */
    private $store = null;

27
28
29
    /**
     *
     */
30
    public function __construct($phpUnit = false) {
31
        $this->db = new Database();
32
        $this->store = Store::getInstance('', $phpUnit);
33
34
35
36
    }

    /**
     * Deletes the record id=$recordId from table $form[F_TABLE_NAME].
37
     * 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
38
39
     * 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.
40
     *
Carsten  Rose's avatar
Carsten Rose committed
41
     * @param string  $tableName
42
     * @param integer $recordId
Carsten  Rose's avatar
Carsten Rose committed
43
     *
44
45
     * @throws CodeException
     * @throws DbException
46
     * @throws UserFormException
47
     */
48
    public function process($tableName, $recordId) {
49
50
        $msg = array();

51
        if ($tableName === false || $tableName === '') {
52
53
54
55
56
57
58
            throw new CodeException('Missing table name', ERROR_MISSING_TABLE_NAME);
        }

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

59
60
61
62
63
64
65
        // 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);
        }

66
        // Read record first.
67
        $row = $this->db->sql("SELECT * FROM $tableName WHERE id=?", ROW_EXPECT_0_1, [$recordId]);
68
69
        if (count($row) > 0) {

70
            $this->deleteReferencedFiles($row, $tableName);
71

72
            $this->db->sql("DELETE FROM $tableName WHERE id =? LIMIT 1", ROW_REGULAR, [$recordId]);
73
        } else {
74
            throw new UserFormException("Record $recordId not found in table '$tableName'.", ERROR_RECORD_NOT_FOUND);
75
76
        }

77
        chdir($cwd);
78
79
80
81
82
83
84
85
86
87
88
    }

    /**
     * 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
89
90
     * @param       $tableName
     *
91
92
93
94
95
96
97
98
99
100
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     */
    private function deleteReferencedFiles(array $row, $tableName) {

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

102
103
104
105
106
107
108
109
110
111
112
113
114
            // 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.
                $samePathFileName = $this->db->sql("SELECT COUNT(id) AS cnt FROM $tableName WHERE $key LIKE ?", ROW_EXPECT_1, [$file]);
                if ($samePathFileName['cnt'] === 1) {
                    if (!unlink($file)) {
                        throw new UserFormException("Error deleting file: $file", ERROR_IO_UNLINK);
                    }
                }
            }
        }
115
116
    }
}