<?php
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2026  Chris Pollett chris@pollett.org
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * END LICENSE
 *
 * @author Chris Pollett chris@pollett.org
 * @license https://www.gnu.org/licenses/ GPL3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2026
 * @filesource
 */
namespace seekquarry\yioop\views;

use seekquarry\yioop as B;
use seekquarry\yioop\configs as C;
use seekquarry\yioop\library\CrawlConstants;

/**
 * View used to draw and allow editing of a single media resource when a
 * media resource gallery is draw in detail view.
 *
 * @author Chris Pollett
 */
class MediadetailView extends View implements CrawlConstants
{
    /** This view is drawn on a web layout
     * @var string
     */
    public $layout = "web";
    /**
     * This method is responsible for drawing both the layout and the view. It
     * should not be modified to change the display of the view (in subclasses).
     * Instead, implement renderView. For MediadetailView, we do override
     * to modify the $data array passed to renderview so as to make sure the
     * background color is set to white
     *
     * @param array $data  an array of values set up by a controller to be used
     *     in rendering the view
     */
    public function render($data)
    {
        $data['BACKGROUND_COLOR'] = 'white';
        $this->layout_object->render($data);
    }
    /**
     * Draws details about a particular wiki resource item
     *
     * @param array $data  contains the static page contents
     * the view
     */
    public function renderView($data)
    {
        $read_mode = empty($data['EDIT']);
        $resource = $data['RESOURCE_INFO'];
        $name = $resource['name'];
        $name_parts = pathinfo($name);
        $written_name = $name;
        $use_editable_thumb = false;
        $url_prefix = $data['URL_PREFIX'];
        $folder_prefix = $data['FOLDER_PREFIX'];
        $token_string = (empty($data[C\p('CSRF_TOKEN')])) ? "" :
            C\p('CSRF_TOKEN').'='.$data[C\p('CSRF_TOKEN')];
        $base_url = htmlentities(B\wikiUrl($data['PAGE_NAME'] , true,
            $data['CONTROLLER'], $data['GROUP']['GROUP_ID'])) .
            $token_string;
        if (!empty($data['settings'])) {
            $base_url .= "&amp;settings=" . $data['settings'];
        }
        if (C\REDIRECTS_ON) {
            $original_url_prefix = preg_replace("/\/-\//u", "/$token_string/",
                $data['ORIGINAL_URL_PREFIX']);
            $thumb_prefix =  preg_replace("/\/-\//u", "/$token_string/",
                $data['THUMB_PREFIX']);
            $athumb_prefix =  preg_replace("/\/-\//u", "/$token_string/",
                $data['ATHUMB_PREFIX']);
        } else {
            $original_url_prefix = $data['ORIGINAL_URL_PREFIX'] .
                "&amp;$token_string";
            $thumb_prefix = $data['THUMB_PREFIX'] .
                "&amp;$token_string";
            $athumb_prefix = $data['ATHUMB_PREFIX'] .
                "&amp;$token_string";
        }
        if (!empty($name_parts['extension'])) {
            if (in_array($name_parts['extension'], ['txt',
                'csv', 'tex', 'php', 'sql', 'html', 'java', 'py',
                'pl', 'P', 'srt'])) {
                $use_editable_thumb = true;
            } elseif (in_array($name_parts['extension'], ['mov',
                'mp4', 'm4v', 'webm', 'mkv', 'm2ts'])) {
                $written_name = $name_parts['filename'] .
                    "[".tl('media_view_video')."]";
            } elseif (in_array($name_parts['extension'], ['wav',
                'mp3','aac', 'aif', 'aiff', 'oga', 'ogg', "m4a"])) {
                $written_name = $name_parts['filename'] .
                    "[".tl('media_view_audio')."]";
            }
        }
        $disabled = " disabled='disabled' ";
        if (!empty($data['CAN_EDIT']) && !empty($resource['is_writable'])) {
            $disabled = "";
        }
        if (!$read_mode) {
            $written_name = $name;
        }
        $thumb_connect = (C\REDIRECTS_ON) ? "/" : "&amp;n=";
        $encode_name = urlencode($name);
        $original_current_url = "$original_url_prefix".
            "$thumb_connect$encode_name";
        $current_url = "$url_prefix&amp;n=$encode_name";
        $clear_url = "$folder_prefix&amp;clear=" .
            $encode_name;
        $current_thumb = "$thumb_prefix$thumb_connect" .
            $encode_name;
        $current_animated_thumb = "$athumb_prefix$thumb_connect" .
            $encode_name;
        if (!empty($data['SUB_PATH'])) {
            if (!str_contains($current_url, "sf=")) {
                $encode_sub_path = urlencode($data['SUB_PATH']);
                $add_sub_path = "&amp;sf=$encode_sub_path";
                $current_url .= $add_sub_path;
                $clear_url .= $add_sub_path;
            }
            if (empty($resource['has_thumb'])) {
                $current_thumb = $data['DEFAULT_THUMB_URL'];
                if (!$read_mode &&!$disabled &&
                    $use_editable_thumb) {
                    $current_thumb = $data['DEFAULT_EDITABLE_THUMB_URL'];
                    $current_url = "$folder_prefix&amp;n=".
                        "$encode_name&amp;arg=edit";
                }
            }
            if (!empty($resource['is_dir'])) {
                $current_url = $folder_prefix;
                $current_thumb = $data['DEFAULT_FOLDER_THUMB_URL'];
            }
        } else {
            if (empty($resource['has_thumb'])) {
                $current_thumb = $data['DEFAULT_THUMB_URL'];
                if (!$read_mode &&!$disabled &&
                    $use_editable_thumb) {
                    $current_thumb = $data['DEFAULT_EDITABLE_THUMB_URL'];
                    $current_url = "$folder_prefix&amp;n=".
                        urlencode($name) . "&amp;arg=edit";
                }
            }
            if (!empty($resource['is_dir'])) {
                $current_url = "$folder_prefix&amp;sf=".
                    urlencode($name);
                $current_thumb = $data['DEFAULT_FOLDER_THUMB_URL'];
            }
        }
        $num_files = "";
        if (!empty($resource['is_dir'])) {
            if (isset($resource['num_files'])) {
                $num_files = ". <b>".
                    tl("mediadetail_view_num_files") . "</b>" .
                    $resource['num_files'];
            }
        }
        $img_url = ($read_mode || !empty($resource['is_dir']) || (!$disabled &&
            $use_editable_thumb)) ? $current_url : $original_current_url;
        $animated_thumb_info = "";
        if ($resource['has_animated_thumb']) {
            $animated_thumb_info = "class='animated' " .
                "data-src='$current_animated_thumb' ".
                "onmouseover= 'let src = this.src; ".
                "this.src = this.dataset.src; ".
                "this.dataset.src =src' " .
                "onmouseout= 'let src = this.src; ".
                "this.src = this.dataset.src; ".
                "this.dataset.src =src' ";
        }
        ?>
        <div>
        <div class="media-container center">
        <a href="<?=$img_url ?>" target="_parent" ><img class="photo"
            src="<?= $current_thumb ?>" <?=$animated_thumb_info ?> ></a>
        </div>
        <?php
        if ($read_mode) {?>
            <h2 class="center"><?= $written_name ?></h2>
            <?php
        } else {
            e("<h2 class='resource-field center' tabindex='0'>".
                "<input type='text' ".
                "id='resource-media' aria-label='".
                tl('mediadetail_view_name')."' ".
                "value='" . str_replace("'", "&#39;", $name).
                "' $disabled ><br>");
            if (!$disabled) {
                ?><button onclick='parent.renameResource("<?=
                urlencode($name);?>",
                document.getElementById("resource-media"))' ><?=
                tl('mediadetail_view_rename') ?></button><?php
            }
            if (!$disabled && empty($resource['is_dir'])
                && (!isset($data['HEAD']['page_type']) ||
                $data['HEAD']['page_type'] != 'media_list')) { ?>
                <script>
                document.write(
                "<button onclick='parent.addToPage(" + '"<?=
                urlencode($name);
                ?>", "wiki-page"<?= (empty($data['SUB_PATH'])) ?
                "" : ", \"{$data['SUB_PATH']}\""
                ?>)' + "'><?=tl('mediadetail_view_add_to_page')
                ?></button>");
                </script><?php
            }
            $append = "";
            if (isset($data['OTHER_BACK_URL'])) {
                $append .= $data['OTHER_BACK_URL'];
            }
            if (!$disabled && !empty($resource['is_compressed'])) {
                $extract_url = $base_url .
                    "&amp;arg=edit&amp;extract=". urlencode($name) .
                        $append;
                if (!empty($data['SUB_PATH'])) {
                    $extract_url .= "&amp;sf=". urlencode($data['SUB_PATH']);
                }?>
                <a class="media-anchor-button" target="_parent"
                href='<?=$extract_url
                ?>'><?=tl('mediadetail_view_extract');
                ?></a><?php
            }
            $copy_url = $base_url .
                "&amp;arg=edit&amp;clip_copy=". urlencode($name) . $append;
            if (!empty($data['SUB_PATH'])) {
                $copy_url .= "&amp;sf=". urlencode($data['SUB_PATH']);
            }?>
            <a class="media-anchor-button" target="_parent" href='<?=$copy_url
                ?>'><?=tl('mediadetail_view_clip_copy');?></a><?php
            if (!$disabled) {
                $cut_url = $base_url .
                    "&amp;arg=edit&amp;clip_cut=". urlencode($name) . $append;
                if (!empty($data['SUB_PATH'])) {
                    $cut_url .= "&amp;sf=".
                        urlencode($data['SUB_PATH']);
                }?>
                <a class="media-anchor-button" target="_parent"
                href='<?=$cut_url
                ?>'><?=tl('mediadetail_view_clip_cut');?></a><?php
            }
            e("</h2>");
        } ?>
        <div class="gray center">
        <b><?=tl('mediadetail_element_modified') ?></b> <?=date("r",
            $resource['modified']) . $num_files ?>.
        <b><?=tl('mediadetail_element_size')?> </b><?=$resource['size'] ?>.
        </div>
        <div class="media-detail-description" tabindex="0" >
        <h3><?=tl('mediadetail_element_description'); ?></h3><?php
        if ($read_mode) {?>
            <div class='resource-description'><?=
                $data['RESOURCE_DESCRIPTION'];
            ?></div>
            <?php
        } else {
            $arg = ($data['CONTROLLER'] == 'static') ? 'read' :
                ((stristr($data['MODE'],'media-detail') !== false) ? 'edit' :
                $data['MODE']);
            $a = ($data['CONTROLLER'] == 'static') ? 'showPage' : 'wiki';
            $page_name = ($data['CONTROLLER'] == 'static') ? 'p' : 'page_name';
            $child_frame = (!empty($data['TARGET']));
            ?>
            <form <?= $child_frame ? "" : 'target="_parent"' ?>>
            <input type="hidden" name='<?=C\p('CSRF_TOKEN')?>'
                value='<?=$data[C\p('CSRF_TOKEN')]?>' >
            <input type="hidden" name='arg' value='<?=$arg ?>' >
            <input type="hidden" name='a' value='<?=$a ?>' >
            <input type="hidden" name='c' value='<?=$data['CONTROLLER'] ?>' >
            <input type="hidden" name='n' value='<?=$name ?>' ><?=
             ($child_frame) ?
                "<input type='hidden' name='target' value='child' >" : "" ?>
            <input type="hidden" name='<?= $page_name ?>'
                value='<?=$data['PAGE_NAME']?>' >
            <?php
            if (!empty($data['SUB_PATH'])) {
                $sub_path = $data['SUB_PATH'];
                if (!empty($resource['is_dir'])) {
                    $sub_path_parts = explode("/", $data['SUB_PATH']);
                    array_pop($sub_path_parts);
                    $sub_path = implode("/", $sub_path_parts);
                }
                ?>
                <input type="hidden" name='sf'
                    value='<?=$sub_path; ?>' >
                <?php
            }
            ?>
            <textarea name='resource_description'><?=
            $data['RESOURCE_DESCRIPTION'];
            ?></textarea>
            <div>
            <button type="submit"><?=tl('mediadetail_view_save') ?></button>
            </div>
            </form><?php
        } ?>
        </div>
        </div>
        <?php
    }
}
