Skip to content
Snippets Groups Projects
Delete.php 4.46 KiB
Newer Older
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 6/1/16
 * Time: 8:52 AM
 */

Marc Egger's avatar
Marc Egger committed
namespace IMATHUZH\Qfq\Core;
Marc Egger's avatar
Marc Egger committed
use IMATHUZH\Qfq\Core\Database\Database;
use IMATHUZH\Qfq\Core\Store\Store;
 
use IMATHUZH\Qfq\Core\Helper\HelperFile;
class Delete {
    /**
     * @var Database
     */
    private $db = null;

    /**
     * @var Store
     */
    private $store = null;

     * @param bool $dbIndexData
     * @param bool $phpUnit
Marc Egger's avatar
Marc Egger committed
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
    public function __construct($dbIndexData = false, $phpUnit = false) {

        $this->db = new Database($dbIndexData);
        $this->store = Store::getInstance('', $phpUnit);
     * Deletes the record id=$recordId from table $tableName.
     * 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
     * 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.
Carsten  Rose's avatar
Carsten Rose committed
     * @param string $tableName
     * @param integer $recordId
     * @param string $primaryKey
Marc Egger's avatar
Marc Egger committed
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
    public function process($tableName, $recordId, $primaryKey = F_PRIMARY_KEY_DEFAULT) {
        if ($tableName === false || $tableName === '') {
Marc Egger's avatar
Marc Egger committed
            throw new \CodeException('Missing table name', ERROR_MISSING_TABLE_NAME);
        }

        if ($recordId === 0 || $recordId === '') {
Marc Egger's avatar
Marc Egger committed
            throw new \CodeException('Invalid record id', ERROR_MISSING_RECORD_ID);
        // Take care the necessary target directories exist.
        $cwd = getcwd();
        $sitePath = $this->store->getVar(SYSTEM_SITE_PATH, STORE_SYSTEM);
        if ($cwd === false || $sitePath === false || !HelperFile::chdir($sitePath)) {
Marc Egger's avatar
Marc Egger committed
            throw new \UserFormException(
Marc Egger's avatar
Marc Egger committed
                json_encode([ERROR_MESSAGE_TO_USER => 'getcwd() failed or SITE_PATH undefined or chdir() failed', ERROR_MESSAGE_TO_DEVELOPER => "getcwd() failed or SITE_PATH undefined or chdir('$sitePath') failed."]),
        // Read record first.
        $row = $this->db->sql("SELECT * FROM $tableName WHERE $primaryKey=?", ROW_EXPECT_0_1, [$recordId]);
        if (count($row) > 0) {

            $this->deleteReferencedFiles($row, $tableName, $primaryKey);
            $this->db->sql("DELETE FROM $tableName WHERE $primaryKey =? LIMIT 1", ROW_REGULAR, [$recordId]);
Marc Egger's avatar
Marc Egger committed
            throw new \UserFormException(
Marc Egger's avatar
Marc Egger committed
                json_encode([ERROR_MESSAGE_TO_USER => 'Record not found in table', ERROR_MESSAGE_TO_DEVELOPER => "Record $recordId not found in table '$tableName'."]),
    }

    /**
     * Iterates over array $row and searches for column names with substring COLUMN_PATH_FILE_NAME.
     * For any column found, check if it references a writable 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
     * @param       $tableName
     *
     * @param $primaryKey
Marc Egger's avatar
Marc Egger committed
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
    private function deleteReferencedFiles(array $row, $tableName, $primaryKey) {
            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($primaryKey) AS cnt FROM $tableName WHERE $key LIKE ?", ROW_EXPECT_1, [$file]);
                if ($samePathFileName['cnt'] === 1) {
                    $this->db->deleteSplitFileAndRecord($row[$primaryKey], $tableName);