<?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\controllers\components;

use seekquarry\yioop as B;
use seekquarry\yioop\configs as C;
use seekquarry\yioop\library as L;
use seekquarry\yioop\library\mail as ML;
use seekquarry\yioop\models\MailAccountModel;
use seekquarry\yioop\models\SigninModel;
use seekquarry\yioop\library\CrawlConstants;
use seekquarry\yioop\library\mail\MailScheduledDispatcher;
use seekquarry\yioop\library\mail\MailHeaderParser;
use seekquarry\yioop\library\mail\MailSiteFactory;
use seekquarry\yioop\library\mail\SmtpClient;
use seekquarry\yioop\library\UrlParser;
use seekquarry\yioop\library\wiki\WikiParser;
use seekquarry\yioop\library\FetchUrl;
use seekquarry\yioop\library\language_processing\PhraseParser;
use seekquarry\yioop\library\processors\ImageProcessor;
use seekquarry\yioop\library\mail\ImapEnvelopeParser;
use seekquarry\yioop\library\mail\ImapListing;
use seekquarry\yioop\library\mail\ImapFolderListParser;
use seekquarry\yioop\library\mail\ImapResponseParser;
use seekquarry\yioop\library\mail\MimeMessage;
use seekquarry\yioop\library\mail\MailComposeBuilder;
use seekquarry\yioop\views\elements\MailElement;
use seekquarry\yioop\library\media_jobs as LMJ;
use seekquarry\yioop\library\version_control as LVC;
use seekquarry\yioop\library\wiki as LW;
/**
 * ResourceComponent handles the files a group keeps beside its wiki
 * pages: a picture, a document, a sound file, anything a writer
 * uploads.
 *
 * It takes an uploaded file and puts it in the folder that page keeps
 * its files in. It makes a small picture of a file where one can be
 * made, so a list of files can show what each one is. It serves a file
 * back to a reader, and copies or moves one from one page to another.
 *
 * This component extends SocialComponent, the base every component
 * about a group extends. The base holds what more than one of them
 * needs, so no component calls another.
 *
 * @author Chris Pollett
 */
class ResourceComponent extends SocialComponent
{

    /**
     * isStaticFolderPage whether a page's settings say it is served as a static
     * HTML folder. This lets a controller know, before it does anything that
     * would send the reader elsewhere, that what was asked for is a file of a
     * folder and has to be answered where it was asked for.
     * @param array $head the page's settings as saved with it
     * @return bool whether the page is a static HTML folder
     */
    public function isStaticFolderPage($head)
    {
        $settings = $this->staticFolderSettings($head);
        return !empty($settings['static_html_folder']);
    }
    /**
     * deleteClip takes one post off the clipboard.
     *
     * groupFeeds calls it for the deleteclip argument, so each thing
     * a reader can do to a feed is read on its own rather than
     * inside one long switch.
     *
     * @param array $data what the page will show, added to here
     * @param int $user_id which reader is asking
     * @return mixed what is handed back to the browser, or
     *     nothing where the page is drawn as usual
     */
    public function deleteClip(&$data, $user_id)
    {
        $parent = $this->parent;
        $group_model = $parent->model("group");
        $wiki_model = $parent->model("wiki");
        $feed_model = $parent->model("feed");
        $clipboard_id = $feed_model->getFeedClipboardId($user_id);
        $clip_group_id = $group_model->getPersonalGroupId($user_id);
        $post_id = $parent->clean($_REQUEST['post_id'], "int");
        $group_item = $feed_model->getGroupItem($post_id);
        if (empty($group_item)) {
            return $parent->redirectWithMessage(
                tl('social_component_invalid_message_id'));
        }
        if ($group_item) {
            // this method checks if user can delete post
            $success =
                $feed_model->deleteGroupItem($post_id, $user_id);
        }
        if ($success) {
            $wiki_model->deleteResources($clip_group_id,
                "post" . $post_id);
            return $parent->redirectWithMessage(
                tl('social_component_clip_item_deleted'));
        }
        return $parent->redirectWithMessage(
            tl('social_component_not_clip_item_deleted'));
    }
    /**
     * emptyClip takes every post off the clipboard.
     *
     * groupFeeds calls it for the emptyclip argument, so each thing
     * a reader can do to a feed is read on its own rather than
     * inside one long switch.
     *
     * @param array $data what the page will show, added to here
     * @param int $user_id which reader is asking
     * @return mixed what is handed back to the browser, or
     *     nothing where the page is drawn as usual
     */
    public function emptyClip(&$data, $user_id)
    {
        $parent = $this->parent;
        $group_model = $parent->model("group");
        $wiki_model = $parent->model("wiki");
        $feed_model = $parent->model("feed");
        $clipboard_id = $feed_model->getFeedClipboardId($user_id);
        $clip_group_id = $group_model->getPersonalGroupId($user_id);
        $clip_search_array = [
            ["parent_id", "=", $clipboard_id, ""]];
        $clip_items = $feed_model->getGroupItems(0,
            C\MAX_THREAD_CLIPBOARD_ITEMS, $clip_search_array,
            $user_id, -2);
        foreach ($clip_items as $clip_item) {
            if ($clip_item['DESCRIPTION'] == 'start_feed_clip') {
                continue;
            }
            $success = $feed_model->deleteGroupItem(
                $clip_item['ID'], $user_id);
            if ($success) {
                $wiki_model->deleteResources($clip_group_id,
                    "post" . $clip_item['ID']);
            }
        }
        return $parent->redirectWithMessage(
            tl('social_component_clipboard_emptied'));
    }
}
