<?php

namespace app\foreignone;

use engine\Model;

class PreviousText
    extends Model
{
    const DB_TABLE = 'previous_texts';

    const int TYPE_POST_TEXT = 0x0;
    const int TYPE_PAGE = 0x1;

    public int $id;
    public int $objectType;
    public int $objectId;
    public string $md;
    public int $ts;


    /**
     * Static methods
     */

    public static function add(int $object_type, int $object_id, string $md, int $ts): void {
        $db = getDB();
        $db->insert(PreviousText::DB_TABLE, [
            'object_type' => $object_type,
            'object_id' => $object_id,
            'md' => $md,
            'ts' => $ts
        ]);
    }

    public static function delete(int $object_type, int|array $object_id): void {
        $sql = "DELETE FROM ".PreviousText::DB_TABLE." WHERE object_type=? AND object_id";
        $args = [$object_type];
        if (is_array($object_id))
            $sql .= " IN (".implode(',', $object_id).")";
        else {
            $sql .= '=?';
            $args[] = $object_id;
        }
        getDB()->query($sql, ...$args);
    }
}