Last commit for src/views/helpers/IconlinkHelper.php: 2addb500315b7393a90fe66431d7832b1e7386c7

Adjust copyrights years

Chris Pollett [2024-01-03 21:Jan:rd]
Adjust copyrights years
<?php
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2023  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 - 2023
 * @filesource
 */
namespace seekquarry\yioop\views\helpers;

/**
 * This is a helper class is used to draw
 * icon buttons and links
 *
 * @author Chris Pollett
 */
class IconlinkHelper extends Helper
{
    /**
     * Types of icons supported by this helper
     * @var array
     */
    public $icon_possibilities;
    /**
     * Initials mapping between names of icons and the actual icons
     * to be used as links or buttons
     */
    public function __construct()
    {
         $this->icon_possibilities = [
            "account_home" => [ tl('iconlink_helper_account_home'), '🏠'],
            "account_settings" => [ tl('iconlink_helper_account_settings'),'βš™'],
            "add" => [ tl('iconlink_helper_add'), 'βž•'],
            "ask_join_group" => [ tl('iconlink_helper_ask_join_group'), "πŸ™‹"],
            "close" => [ tl('iconlink_helper_close'), 'X'],
            "create_group" => [ tl('iconlink_helper_create_group'), 'βž•'],
            "combined_discussions" => [
                tl('iconlink_helper_combined_discussions'), 'πŸ“ƒ'],
            "crawls" => [tl('iconlink_helper_manage_crawls'),  'πŸ•ΈοΈ'],
            "delete" => [tl('iconlink_helper_delete'), 'πŸ—‘οΈ',
                tl('iconlink_helper_confirm')],
            "discuss_page" => [
                tl('iconlink_helper_discuss_page'), 'πŸ—£οΈ'],
            "edit" => [ tl('iconlink_helper_edit'),  '✏️'],
            "groups" => [ tl('iconlink_helper_groups'), '🏘️'],
            "group_feed" => [ tl('iconlink_helper_group_discussions'), 'πŸ“ƒ'],
            "history" => [tl('iconlink_helper_history'),  'πŸ“–'],
            "information" => [tl('iconlink_helper_information'),  'ℹ️'],
            "join_group" => [ tl('iconlink_helper_join_group'), "πŸ›¬"],
            "join_group_fee" => [ tl('iconlink_helper_join_group'), "πŸ›¬",
                 'iconlink_helper_fee_warning'], //note no tl
            "join_invited_group" => [ tl('iconlink_helper_join_invited_group'),
                "🎟️"],
            "join_groups" => [ tl('iconlink_helper_join_groups'), "πŸ›¬"],
            "leave_group" => [ tl('iconlink_helper_leave_group'), "πŸ›«"],
            "localize" => [ tl('iconlink_helper_localize'), '🌐'],
            "machines" => [tl('iconlink_helper_manage_machines'), 'πŸ€–'],
            "mixes" => [tl('iconlink_helper_manage_mixes'), 'πŸ₯¨'],
            "messages" => [ tl('iconlink_helper_messages'), 'πŸ’¬'],
            "pages" => [tl('iconlink_helper_group_pages'), 'πŸ“‘'],
            "pending" => [ tl('iconlink_helper_request_pending'), "⏳",
                tl('iconlink_helper_cancel_request')],
            "play" => [tl('iconlink_helper_group_play'), '▢️'],
            "query_stats" => [ tl('iconlink_helper_query_stats'), 'πŸ“ˆ'],
            "reset" => [ tl('iconlink_helper_reset'), 'X'],
            "resources" => [ tl('iconlink_helper_resources'), 'πŸ—„οΈ'],
            "search" => [ tl('iconlink_helper_search'), 'πŸ”'],
            "settings" => [ tl('iconlink_helper_settings'),
                "<b class='black medium-font'>βš™</b>"],
            "statistics" => [ tl('iconlink_helper_statistics'), "πŸ“ˆ"],
            "view_read" => [ tl('iconlink_helper_view_read'), '↩️'],
            "view_source" => [ tl('iconlink_helper_view_source'), 'πŸ‘οΈ'],
            "wiki" => [ tl('iconlink_helper_group_main_page'),
                '🏁'],
            "wiki_syntax" => [ tl('iconlink_helper_wiki_syntax'), 'πŸ“š'],
        ];
    }
    /**
     * Draws a link button suitable to indicate the activity $type
     *
     * @param string $url  url that is sent when one clicks and confirms
     * @param string $type  one of "edit", ...
     * @param string $badge_text if not empty, text to use to draw a badge
     *  on the top right of the icon
     * @param bool $as_string whether to return a string or html
     * @param string $div_classes space separated, additional css classes
     *      to add to outermost div
     * @param string $anchor_classes space separated, additional css classes
     *      to add to anchor tag
     * @param bool $replace_css whether to appened css in $div_classes and
     *      $anchor_classes to existing or to replace.
     * @param string $tab_target whether to open link in a new tab with target
     *      $tab_target
     * @return mixed either void, or a string of html if $as_string = true
     */
    public function renderButton($url, $type = "edit", $badge_text = "",
        $as_string = false, $div_classes = "", $anchor_classes = "",
        $replace_css = false, $tab_target = "")
    {
        if (is_array($url)) {
            list($id, $url,) = $url;
            $id_attr = " id='$id' ";
        } else {
            $id_attr = "";
        }
        if ($as_string) {
            ob_start();
        }
        $div_classes = ($replace_css) ? $div_classes : "icon-button-container ".
            $div_classes;
        ?><div <?=$id_attr ?> class='<?= $div_classes ?>'><?=
            $this->renderLink($url, $type, $badge_text, $as_string,
            $anchor_classes, $replace_css, $tab_target); ?></div><?php
        if ($as_string) {
            $button = ob_get_contents();
            ob_end_clean();
            return $button;
        }
    }
    /**
     * Draws a button for a form that uses an icon as its main text
     * @param string $button type value for type attribute of html button
     * @param string $type of icon one of "edit", ...
     * @param bool $as_string whether to return a string or html
     * @param string $anchor_class space separated, additional css classes
     *      to add to anchor
     * @param string $additional_attibutes a string to enclose with the open
     *      button tag with additional attributes
     * @return mixed either void, or a string of html if $as_string = true
     */
    public function renderFormButton($button_type = "submit" , $type = "edit",
        $as_string = false, $anchor_classes = "",
        $additional_attibutes = "")
    {
        $type = (isset($this->icon_possibilities[$type])) ? $type : "submit";
        $icon_info = $this->icon_possibilities[$type];
        list($icon_text, $icon, ) = $icon_info;
        if ($as_string) {
            ob_start();
        }
        ?><button type='<?=$button_type ?>' class='<?=$anchor_classes
            ?>' <?=$additional_attibutes ?> ><span role='img' aria-label='<?=
        $icon_text ?>' ><?=$icon ?></span></button><?php
        if ($as_string) {
            $button = ob_get_contents();
            ob_end_clean();
            return $button;
        }
    }
    /**
     * Draws a link suitable to indicate the activity $type
     *
     * @param string $url  url that is sent when one clicks and confirms
     * @param string $type  one of "edit", ...
     * @param string $badge_text if not empty, text to use to draw a badge
     *  on the top right of the icon
     * @param bool $as_string whether to return a string or html
     * @param string $anchor_class space separated, additional css classes
     *      to add to anchor
     * @param bool $replace_css whether to replace default anchor css or
     *      append to it
     * @param string $tab_target whether to open link in a new tab with target
     *      $tab_target
     * @return mixed either void, or a string of html if $as_string = true
     */
    public function renderLink($url, $type = "edit", $badge_text = "",
        $as_string = false, $anchor_classes = "", $replace_css = false,
        $tab_target = "")
    {
        $type = (isset($this->icon_possibilities[$type])) ? $type : "edit";
        $icon_info = $this->icon_possibilities[$type];
        if (empty($tab_target)) {
            $target = (empty($_REQUEST['noscript'])) ? "" :
                " target='_parent' rel='noopener' ";
        } else {
            $target = " target='$tab_target' rel='noopener' ";
        }
        list($icon_text, $icon, ) = $icon_info;
        $confirm = "";
        if (is_array($url) && !empty($url[1]) && !empty($icon_info[2])) {
            list($url, $translate_aux,) = $url;
             $icon_info[2] = tl($icon_info[2], $translate_aux);
             $icon = "<div class='inline align-opposite'>$icon<br>".
                "<span class='tiny-font' style='position:relative; top: -8px;'".
                ">$translate_aux</span></div>";
        }
        if (!empty($icon_info[2])) {
            $confirm = " onclick='javascript:return ".
                "confirm(\"{$icon_info[2]}\")' ";
        }
        if ($as_string) {
            ob_start();
        }
        $anchor_classes = ($replace_css) ? $anchor_classes :
            "icon-anchor-button ". $anchor_classes;
        $badge_attribute = (empty($badge_text)) ? "" :
            " data-badge='$badge_text' ";
        ?><div class='position-context <?=$anchor_classes?>' <?=$badge_attribute
            ?> ><a <?=$confirm ?> class='<?= $anchor_classes
            ?>' <?=$target?> href='<?= $url ?>'><span role='img' aria-label='<?=
            $icon_text ?>' ><?=$icon ?></span></a></div><?php
        if ($as_string) {
            $button = ob_get_contents();
            ob_end_clean();
            return $button;
        }
    }
}
ViewGit