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

namespace qfq;

require_once(__DIR__ . '/Constants.php');
require_once(__DIR__ . '/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
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
     * @param string $tableName
41
42
43
     * @param integer $recordId
     * @throws CodeException
     * @throws DbException
44
     * @throws UserFormException
45
     */
46
    public function process($tableName, $recordId) {
47
48
        $msg = array();

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

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

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

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

68
            $this->deleteReferencedFiles($row, $tableName);
69

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

75
        chdir($cwd);
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    }

    /**
     * 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
     * @param $tableName
     * @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;
            }
98

99
100
101
102
103
104
105
106
107
108
109
110
111
            // 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);
                    }
                }
            }
        }
112
113
    }
}