<?php
/* -----------------------------------------------------------------------------
  Plugin     : LMT
  Author     : Grum
    email    : grum@piwigo.org
    website  : http://photos.grum.fr

    << May the Little SpaceFrog be with you ! >>
  ------------------------------------------------------------------------------
  See main.inc.php for release information

  LMT_PIP : classe to manage plugin public pages

  --------------------------------------------------------------------------- */

include_once('lmt_root.class.inc.php');
//include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCPublicIntegration.class.inc.php');

global $user;


class LMT_PIP extends LMT_root
{
  protected $section_page;

  public function __construct($prefixeTable, $filelocation)
  {
    parent::__construct($prefixeTable, $filelocation);
    $this->loadConfig();

    $this->initEvents();
  }

  public function __destruct()
  {
    unset($section_page);
    parent::__destruct();
  }

  /**
   * initialize events call for the plugin
   */
  public function initEvents()
  {
    parent::initEvents();

    if($this->config['lmt_licence_activated']=='y')
    {
      add_event_handler('user_init', array(&$this, 'loadLang'));
      add_event_handler('loc_end_picture', array(&$this, 'manageLicenceElement'));

      if($this->config['lmt_warning_visible']=='y')
      {
        add_event_handler('loc_end_page_tail', array(&$this, 'manageWarningText'));
      }
    }
  }

  public function loadCSS()
  {
    //parent::loadCSS();  -- don't call the ancestor!
    GPCCore::addHeaderCSS('lmt_icn.css', 'plugins/'.$this->getDirectory().'/'.$this->getPluginNameFiles()."_icn.css");
  }

  public function loadLang()
  {
    load_language('plugin.lang', LMT_PATH);
  }

  /* -------------------------------------------------------------------------
    FUNCTIONS TO MANAGE LMT DISPLAY
  ------------------------------------------------------------------------- */
  public function manageLicenceElement()
  {
    global $template, $page, $user, $lang;

    $licence=$this->getImageLicence($page['image_id']);
    if($licence['licence']=="")
    {
      return("");
    }

    if($this->config['lmt_redirect_activated']=='y')
    {
      $url=$this->config['lmt_redirect_url-'.strToLower($licence['licence'])][$user['language']];
      if($url=='')
      {
        $url=$this->config['lmt_redirect_url-'.strToLower($licence['licence'])]['en_UK'];
      }
    }
    else
    {
      $url="";
    }

    $alternateText=" alt='".htmlspecialchars($this->translateCopyright('lmt_lbl_cc_s-'.strToLower($licence['licence'])), ENT_QUOTES)."'
      title='".htmlspecialchars(
        $this->translateCopyright('lmt_lbl_under-'.strtolower($licence['licence'])).' '.
        $this->translateCopyright('lmt_lbl_cc_s-'.strtolower($licence['licence'])).' : '.
        $this->translateCopyright('lmt_lbl_cc-'.strtolower($licence['licence'])),ENT_QUOTES
    )."' ";

    $style="";
    $text="";
    $target=($this->config['lmt_redirect_open_target']=="y")?" onclick='window.open(this.href); return false;'":"";
    switch($this->config['lmt_licence_logo'])
    {
      case '80x15':
        if($url!="")
        {
          $text = "<br/><a href='".$url."'".$target."><img $alternateText class='logo80x15' src='". LMT_PATH."img/".strToLower($licence['licence'])."_80x15.png'></a>".$licence['aut_text1']." ".$licence['aut_text2'];
        }
        else
        {
          $text = "<br/><img $alternateText class='logo80x15' src='". LMT_PATH."img/".strToLower($licence['licence'])."_80x15.png'>".$licence['aut_text1']." ".$licence['aut_text2'];
        }
      break;
      case '88x31';
        $style=" style='text-align:left;'";
        if($url!="")
        {
          $text = "<table style='margin:auto;'><tr><td><a href='".$url."'".$target."><img $alternateText class='logo88x31' src='". LMT_PATH."img/".strToLower($licence['licence'])."_88x31.png'></a></td><td>".$licence['aut_text1']."<br/>".$licence['aut_text2']."</td></tr></table>";
        }
        else
        {
          $text = "<table style='margin:auto;'><tr><td><img $alternateText class='logo88x31' src='". LMT_PATH."img/".strToLower($licence['licence'])."_88x31.png'></td><td>".$licence['aut_text1']."<br/>".$licence['aut_text2']."</td></tr></table>";
        }
        break;
      case 'text';
        if($url!="")
        {
          $text ="<br/>".$this->translateCopyright("lmt_lbl_under-".strToLower($licence['licence']))." <a href='".$url."'".$target.">".$this->translateCopyright("lmt_lbl_cc_s-".strToLower($licence['licence']))."</a> ".$licence['aut_text1']." ".$licence['aut_text2'];
        }
        else
        {
          $text ="<br/>".$this->translateCopyright("lmt_lbl_under-".strToLower($licence['licence']))." ".$this->translateCopyright("lmt_lbl_cc_s-".strToLower($licence['licence']))." ".$licence['aut_text1']." ".$licence['aut_text2'];
        }
        break;
    }

    $template->concat('COMMENT_IMG', '<span class="licencetag" '.$style.'>'.$text.'</span>');
  }

  public function manageWarningText()
  {
    global $template, $user, $lang;


    if(isset($this->config['lmt_warning_texts'][$user['language']]))
    {
      $text=$this->config['lmt_warning_texts'][$user['language']];
    }
    else
    {
      $text=$this->config['lmt_warning_texts']['en_UK'];
    }

    $alternateText=" alt='".htmlspecialchars($this->translateCopyright('lmt_lbl_cc_s-'.strtolower($this->config['lmt_licence_default'])), ENT_QUOTES)."'
      title='".htmlspecialchars(
        $this->translateCopyright('lmt_lbl_under-'.strtolower($this->config['lmt_licence_default'])).' '.
        $this->translateCopyright('lmt_lbl_cc_s-'.strtolower($this->config['lmt_licence_default'])).' : '.
        $this->translateCopyright('lmt_lbl_cc-'.strtolower($this->config['lmt_licence_default'])),ENT_QUOTES
    )."' ";

    $patterns = array('/\[logo=80x15\]/i', '/\[logo=88x31\]/i', '/\[logo=text\]/i');
    $replacements = array(
      "<img $alternateText class='logo80x15' src='". LMT_PATH."img/".strtolower($this->config['lmt_licence_default'])."_80x15.png'>",
      "<img $alternateText class='logo88x31' src='". LMT_PATH."img/".strtolower($this->config['lmt_licence_default'])."_88x31.png'>",
      $this->translateCopyright("lmt_lbl_cc_s-".strtolower($this->config['lmt_licence_default']))
    );

    if($this->config['lmt_redirect_activated']=='y')
    {
      $url=$this->config['lmt_redirect_url-'.strToLower($this->config['lmt_licence_default'])][$user['language']];
      if($url=='')
      {
        $url=$this->config['lmt_redirect_url-'.strToLower($this->config['lmt_licence_default'])]['en_UK'];
      }
      if($url!="")
      {
        $target=($this->config['lmt_redirect_open_target']=="y")?" onclick='window.open(this.href); return false;'":"";
        $replacements[0]="<a href='".$url."'".$target.">".$replacements[0]."</a>";
        $replacements[1]="<a href='".$url."'".$target.">".$replacements[1]."</a>";
        $replacements[2]="<a href='".$url."'".$target.">".$replacements[2]."</a>";
      }
    }

    $text = preg_replace($patterns, $replacements, $text);

    $template->append("footer_elements", "<span>".htmlspecialchars_decode($text)."</span>");
  }


} //class

?>
