Delete.php 3.43 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
40
41
42
43
44
45
     * 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.
     *
     * @param array $form
     * @param integer $recordId
     * @param array $msg
     * @return bool
     * @throws CodeException
     * @throws DbException
46
     * @throws UserFormException
47
48
49
50
51
     */
    public function process(array $form, $recordId, array &$msg) {
        $rc = false;
        $msg = array();

52
        if (!isset($form[F_TABLE_NAME]) || $form[F_TABLE_NAME] === '' || $form[F_TABLE_NAME] === false ) {
53
54
55
56
57
58
59
            throw new CodeException('Missing table name', ERROR_MISSING_TABLE_NAME);
        }

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

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

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
        // Read record first.
        $row = $this->db->sql("SELECT * FROM " . $form[F_TABLE_NAME] . " WHERE id=?", ROW_EXPECT_0_1, [$recordId]);
        if (count($row) > 0) {

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

                // 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 " . $form[F_TABLE_NAME] . " WHERE " . $key . " LIKE ?", ROW_EXPECT_1, [$file]);
                    if ($samePathFileName['cnt'] === 1) {
                        if (!unlink($file)) {
                            $msg[MSG_CONTENT] = "Error deleting file: " . $file;
                            $msg[MSG_ERROR_CODE] = ERROR_IO_UNLINK;
                        }
                    }
                }


            }


            $this->db->sql("DELETE FROM " . $form[F_TABLE_NAME] . " WHERE id =? LIMIT 1", ROW_REGULAR, [$recordId]);
            $rc = true;
        } else {
            $msg[MSG_CONTENT] = "Record $recordId not found in table '" . $form[F_TABLE_NAME] . "'.";
            $msg[MSG_ERROR_CODE] = ERROR_RECORD_NOT_FOUND;
        }

101
102
        chdir($cwd);

103
104
105
        return $rc;
    }
}