<?php
/**
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Etienne Mauvais <emauvaisfr@yahoo.fr>
 */

// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'action.php');

class action_plugin_lytebox extends DokuWiki_Action_Plugin {

    /**
     * return some info
     */
    function getInfo() {
        return array(
                'author' => 'Etienne M.',
                'email'  => 'emauvaisfr@yahoo.fr',
                'date'   => @file_get_contents(DOKU_PLUGIN.'lytebox/VERSION'),
                'name'   => 'lytebox Plugin',
                'desc'   => 'Affiche les images dans lytebox / Displays the pictures using lytebox',
                'url'    => 'http://www.dokuwiki.org/fr:plugin:lytebox',
                );
    }

    /**
     * Constructor
     */
    function action_plugin_lytebox() {
      $this->setupLocale();
    }
                              
    /**
     * register the eventhandlers
     */
    function register(&$contr) {
        //$contr->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, '_handle_act', array());
        $contr->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, '_handle_tpl_metaheader',      array());
        $contr->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE',   $this, '_handle_tpl_content_display', array());
    }

    function _handle_tpl_metaheader(&$event, $param) {
      // Adding lytebox stylesheet 
      $event->data["link"][] = array (
        "type" => "text/css",
        "rel" => "stylesheet", 
        "href" => DOKU_BASE."lib/plugins/lytebox/lytebox.css",
      );
 
      // Adding lytebox JavaScript File
      $event->data["script"][] = array (
        "type" => "text/javascript",
        "src" => DOKU_BASE."lib/plugins/lytebox/lytebox.js",
        "_data" => "",
      );
    }

    function _handle_tpl_content_display(&$event, $param) {
      $event->data = preg_replace_callback(
                            '/<a href="(.*?)" class="media".*?title="(.*?)".*?><img src="(.*?)" class="(.*?)".*?(title|alt)="(.*?)"(.*?)\/><\/a>/',
                            array('action_plugin_lytebox','_callback'),
                            $event->data
                           );
    }

    function _callback($matches) {
      //1 : a_href
      //2 : a_title
      //3 : img_src
      //4 : img_media
      //5 : img_alt_title_tag
      //6 : img_alt_title
      //7 : img_reste

      //S'il n'y a pas "_detail" dans le lien ni de /lib/exe/fetch.php (image link), on ne fait rien
      if (strpos(strtolower($matches[1]),"/_detail/")) $type="int";
      else if (strpos(strtolower($matches[1]),"/lib/exe/fetch.php")) $type="ext";
      else return $matches[0];

      //print "<pre>".print_r($matches,true)."</pre>";

      $rel="lytebox[0]";
      $rev="";

      //Pour les images internes
      if ($type=="int") {
        preg_match("/(.*?)\?/",$matches[3],$img_src);
      }
      //Pour les images externes
      else {
        preg_match("/(.*)/",$matches[3],$img_src);
      }

      if ($img_src) $img_src = $img_src[1];
      else $img_src=$matches[3];

      $a_title=$matches[2];
      //Pour les images internes
      if ($type=="int") { 
        $a_title = "<a href=\"".preg_replace("/_media/","_detail",$img_src)."\" target=\"_blank\">$a_title</a>";
      }
      //Pour les images externes
      else {
        $a_title = "<a href=\"$img_src\" target=\"_blank\">$a_title</a>";
      }
      if ($matches[6]) $a_title = $matches[6]."<br />".$a_title;
      else $matches[6]="&nbsp;";

      return "<a href=\"$img_src\" class=\"media\" title=\"".htmlspecialchars($a_title)."\" rel=\"$rel\" $rev><img src=\"$matches[3]\" class=\"$matches[4]\" alt=\"$matches[6]\" title=\"$matches[6]\" $matches[7] /></a>";
    }
}

// vim:ts=4:sw=4:et:enc=utf-8:
