<?php
/**
 * Demo Plugin.
 *
 * This plugin tries to completely cover Shaarli's plugin API.
 * Can be used by plugin developers to make their own plugin.
 */

/*
 * RENDER HEADER, INCLUDES, FOOTER
 *
 * Those hooks are called at every page rendering.
 * You can filter its execution by checking _PAGE_ value
 * and check user status with _LOGGEDIN_.
 */

use Shaarli\Config\ConfigManager;
use Shaarli\Plugin\PluginManager;
use Shaarli\Render\TemplatePage;

/**
 * In the footer hook, there is a working example of a translation extension for Shaarli.
 *
 * The extension must be attached to a new translation domain (i.e. NOT 'shaarli').
 * Use case: any custom theme or non official plugin can use the translation system.
 *
 * See the documentation for more information.
 */
const EXT_TRANSLATION_DOMAIN = 'clickat';

/*
 * This is not necessary, but it's easier if you don't want Poedit to mix up your translations.
 */
function clickat_t($text, $nText = '', $nb = 1)
{
    return t($text, $nText, $nb, EXT_TRANSLATION_DOMAIN);
}

/**
 * Initialization function.
 * It will be called when the plugin is loaded.
 * This function can be used to return a list of initialization errors.
 *
 * @param $conf ConfigManager instance.
 *
 * @return array List of errors (optional).
 */
function clickat_init($conf)
{
    return $errors;
}


/*
 * SPECIFIC PAGES
 */

/**
 * Hook render_linklist.
 *
 * Template placeholders:
 *   - action_plugin: next to 'private only' button.
 *   - plugin_start_zone: page start
 *   - plugin_end_zone: page end
 *   - link_plugin: icons below each links.
 *
 * Data:
 *   - _LOGGEDIN_: true/false
 *
 * @param array $data data passed to plugin
 *
 * @return array altered $data.
 */
function hook_clickat_render_linklist($data)
{
    /*
     * Action links (action_plugin):
     * A link is an array of its attributes (key="value"),
     * and a mandatory `html` key, which contains its value.
     * It's also recommended to add key 'on' or 'off' for theme rendering.
     */
   
    return clickify($data);
}

/**
 * Hook render_daily.
 *
 * Template placeholders:
 *   - plugin_start_zone: page start.
 *   - plugin_end_zone: page end.
 *
 * Data:
 *   - _LOGGEDIN_: true/false
 *
 * @param array $data data passed to plugin
 *
 * @return array altered $data.
 */
function hook_clickat_render_daily($data)
{

    return clickify($data);
}

function clickify($data)
{
        foreach ($data['links'] as &$value) {
            $value['description'] = preg_replace('/@([\w\d]+)@([\w\d_\.]+)/','<a href="https://\2/users/\1">&commat;\1</a>',$value['description']);
            $value['description'] = preg_replace('/@([\w\d]+)/','<a href="https://twitter.com/\1">&commat;\1</a>',$value['description']);
        }

    return $data;
}