/ src / views / helpers / HelpbuttonHelper.php
<?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 Eswara Rajesh Pinapala epinapala@live.com
 * @license https://www.gnu.org/licenses/ GPL3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2026
 * @filesource
 */
namespace seekquarry\yioop\views\helpers;

use seekquarry\yioop\configs as C;

/**
 * HelpbuttonHelper this is a helper class is used to draw help button for
 * context sensitive help.
 * @author Eswara Rajesh Pinapala
 */
class HelpbuttonHelper extends Helper
{
    /**
     * is_help_initialized stores whether or not setupHelpParams() has been
     * previously called
     * @var bool
     */
    public $is_help_initialized;
    /**
     * localization_data stores the words the help button shows, in the
     * reader's own language, ready for the page's scripts to use.
     * @var array
     */
    public $localization_data;
    /**
     * back_params stores query parameters as json array for page just came
     * from
     * @var string
     */
    public $back_params;
    /**
     * Javascript needed to open a help button page
     * @var string
     */
    public $script;
    /**
     * safe_request stores what the request carried, already made safe by the
     * controller: which controller and activity the reader is on, which help
     * page to open, and the rest of the address so they can be brought back
     * where they were. A helper draws what it is handed and cleans nothing
     * itself, so this arrives ready to write into the page.
     * @var array
     */
    public $safe_request = [];
    /**
     * __construct the constructor at this point initializes the all the
     * required code for Wiki Help initialization.
     */
    public function __construct()
    {
        $this->is_help_initialized = false;
        $this->localization_data = null;
        $this->back_params = null;
        $this->script = null;
        parent::__construct();
    }
    /**
     * This method is used to render the help button,
     * given a help point  CSRF token and target controller name.
     *
     * @param  $help_point_id used to set as help button id
     * @param  $csrf_token_value  CSRF token to make api call/open edit link
     * @param bool $as_html when true return the rendered html as a
     *      string; when false (default) echo it directly to output
     * @return String button html.
     */
    public function render($help_point_id, $csrf_token_value, $as_html=false)
    {
        if ($this->is_help_initialized == false) {
            $this->setupHelpParams();
        }
        /* Whether the reader is on a phone is set where a request came
           in over the web. A case that calls this helper on its own has
           no request behind it, so the setting may not be there at all,
           and reading it without asking first printed a warning through
           every run of the tests. */
        $is_mobile = empty($_SERVER["MOBILE"]) ? "false" : "true";
        $wiki_group_id = $this->safe_request['help_group_id'] ??
            C\HELP_GROUP_ID;
        $api_controller = "api";
        $api_wiki_action = "wiki";
        $api_wiki_mode = "read";
        $activity = $this->safe_request['activity'] ?? "";
        if ($activity == 'machineStatus') {
            $activity = 'manageMachines';
        } else if ($activity == 'crawlStatus') {
            $activity = 'manageCrawls';
        }
        $button_string = '<button type="button"
            class="help-button default"
            data-tl=\'' . $this->localization_data . '\'
            data-back-params=\'' . $this->back_params . '\'
            onclick="javascript:displayHelpForId(this,'
            . $is_mobile . ',\''
            . ($this->safe_request['controller'] ?? "") . '\',\''
            . $activity . '\',\''
            . C\p('CSRF_TOKEN') . '\',\''
            . $csrf_token_value . "','$wiki_group_id','$api_controller',"
            . "'$api_wiki_action','$api_wiki_mode" . '\')" '
            . 'data-pagename="' . $help_point_id . '"> '
            . tl('helpbutton_helper_question_mark') . '</button>';
        if ($as_html) {
            return $button_string;
        } else {
            $button_string = "<script>\n" .
                "document.write(" .json_encode($button_string) .");\n" .
                "</script>";
        }
        return $button_string;
    }
    /**
     * setupHelpParams this Helper method is used to setup params needed for
     * Context-Sensitive help to work. This gets executed if there is at least
     * one help button rendered on the page. This is executed only once with
     * the help of "is_help_initialized" variable.
     */
    public function setupHelpParams()
    {
        $this->is_help_initialized = true;
        $this->localization_data = "{" .
            'helpbutton_helper_edit :"' . tl('helpbutton_helper_edit') . '",' .
            'helpbutton_helper_not_available :"' .
                tl('helpbutton_helper_not_available') .
            '",' .
            'helpbutton_helper_create_edit :"' .
                tl('helpbutton_helper_create_edit',
                $this->safe_request['help_group'] ?? "") .
            '",' .
            'helpbutton_helper_page_no_exist :"' .
                tl('helpbutton_helper_page_no_exist','%s') .
            '",' .
            'helpbutton_helper_read :"' .
                tl('helpbutton_helper_read') . '"' .
            "}";
        $this->back_params = "{";
        /* Every value the address carried is kept, so that a reader
           who opens the help and then closes it comes back to the page
           they were on rather than to the top of the site. */

        $back_params_array = $this->safe_request['back'] ?? [];
        $back_params_only_keys = array_keys($back_params_array);
        $last_key = end($back_params_only_keys);
        foreach ($back_params_array as $key => $value) {
            $this->back_params .= $key . ' : "' . $value . '"';
            if ($key != $last_key) {
                $this->back_params .= ', ';
            }
        }
        $this->back_params .= "}";
        if (!empty($this->safe_request['open_help_page'])) {
            $help_page_to_open = $this->safe_request['open_help_page'];
            $this->script = 'var matches = '
                . 'document.querySelectorAll(\'[data-pagename="'
                . $help_page_to_open
                . '"]\');' . "\n\t\t"
                . "matches[0].click();"
                . "\n";
        }
    }
}
X