<?php
// This file is part of mod_datalynx for Moodle - http://moodle.org/
//
// It 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.
//
// It 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 Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 *
 * @package datalynxview_pdf
 * @copyright 2013 onwards edulabs.org and associated programmers
 * @copyright based on the work by 2012 Itamar Tzadok
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace datalynxview_pdf;

use mod_datalynx\local\view\datalynxview_patterns;
use stdClass;

/**
 * View patterns for the PDF view type.
 */
class view_patterns extends datalynxview_patterns {
    /**
     * Get replacements for tags.
     *
     * @param array $tags
     * @param stdClass $entry
     * @param array $options
     * @return array
     */
    public function get_replacements($tags = null, $entry = null, array $options = []) {
        $replacements = parent::get_replacements($tags, $entry, $options);

        $view = $this->view;
        $baseurl = new \moodle_url($view->get_baseurl());
        $baseurl->param('sesskey', sesskey());

        foreach ($tags as $tag) {
            switch ($tag) {
                case '##export:all##':
                    $actionurl = new \moodle_url($baseurl, ['pdfexportall' => true]);
                    $label = \html_writer::tag('span', get_string('exportall', 'datalynx'));
                    $replacements[$tag] = \html_writer::link(
                        $actionurl,
                        $label,
                        ['class' => 'actionlink exportall']
                    );

                    break;
                case '##export:page##':
                    $actionurl = new \moodle_url($baseurl, ['pdfexportpage' => true]);
                    $label = \html_writer::tag('span', get_string('exportpage', 'datalynx'));
                    $replacements[$tag] = \html_writer::link(
                        $actionurl,
                        $label,
                        ['class' => 'actionlink exportpage']
                    );

                    break;
                case '##pagebreak##':
                    $replacements[$tag] = $view::PAGE_BREAK;

                    break;
            }
        }

        return $replacements;
    }

    /**
     * Get patterns.
     *
     * @param bool $checkvisibility
     * @return array
     */
    protected function patterns($checkvisibility = true): array {
        $patterns = parent::patterns($checkvisibility);
        $cat = get_string('pluginname', 'datalynxview_pdf');
        $patterns['##export:all##'] = [true, $cat];
        $patterns['##export:page##'] = [true, $cat];
        $patterns['##pagebreak##'] = [true, $cat];

        return $patterns;
    }
}
