<?php

namespace Hura8\Components\Page\AdminController;

use Hura8\Components\Page\Controller\bPageController;
use Hura8\Interfaces\iEntityAdminController;
use Hura8\Traits\AdminEntityBaseControllerTraits;


class APageController extends bPageController implements iEntityAdminController
{

    use AdminEntityBaseControllerTraits;


    public function updateTableInfo($item_id, array $new_item_info) {
        return $this->objPageModel->updateTableInfo($item_id, $new_item_info);
    }


    protected function deleteFileBeforeDeleteItem($item_id): bool
    {
        // delete thumb files
        $item_info = $this->getInfo($item_id);
        $this->deleteThumbnailFile($item_info['thumbnail']);

        //delete media files?
        // todo:

        return true;
    }


    protected function deleteThumbnailFile($file_name): bool
    {
        if(!$file_name) {
            return false;
        }

        foreach (self::$resized_sizes as $size => $value) {
            $file_local_path = PUBLIC_DIR . "/". self::$image_folder  . "/". $size. IMAGE_FILE_SEPARATOR . $file_name;
            unlink($file_local_path);
        }

        // remove original file
        $file_local_path = PUBLIC_DIR . "/". self::$image_folder . "/". $file_name;
        return unlink($file_local_path);
    }

}
