<?php /*

 Composr
 Copyright (c) Christopher Graham, 2004-2024

 See docs/LICENSE.md for full licensing information.


 NOTE TO PROGRAMMERS:
   Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
   **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****

*/

/**
 * @license    http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
 * @copyright  Christopher Graham
 * @package    galleries
 */

/**
 * Hook class.
 */
class Hook_task_delete_gallery_media
{
    /**
     * Run the task hook.
     *
     * @param  ID_TEXT $name The gallery that was deleted
     * @return ?array A tuple of at least 2: Return mime-type, content (either Tempcode, or a string, or a filename and file-path pair to a temporary file), map of HTTP headers if transferring immediately, map of ini_set commands if transferring immediately (null: show standard success message)
     */
    public function run(string $name) : ?array
    {
        require_code('galleries2');

        do {
            cms_extend_time_limit(TIME_LIMIT_EXTEND__MODEST);

            $images = $GLOBALS['SITE_DB']->query_select('images', ['id'], ['cat' => $name], '', 200);
            foreach ($images as $image) {
                delete_image($image['id']);
            }
        } while (!empty($images));

        do {
            cms_extend_time_limit(TIME_LIMIT_EXTEND__MODEST);

            $videos = $GLOBALS['SITE_DB']->query_select('videos', ['id'], ['cat' => $name], '', 200);
            foreach ($videos as $video) {
                delete_video($video['id']);
            }
        } while (!empty($videos));

        return null;
    }
}
