<?php
/*
Plugin Name: meta
Version: auto
Description: Allows to add metadata
Plugin URI: http://piwigo.org/ext/extension_view.php?eid=220
Author: ddtddt
Author URI: http://piwigo.org/
*/

if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');

global $prefixeTable;

define('meta_DIR' , basename(dirname(__FILE__)));
define('meta_PATH' , PHPWG_PLUGINS_PATH . meta_DIR . '/');
define('meta_TABLE' , $prefixeTable . 'meta');
define('meta_img_TABLE' , $prefixeTable . 'meta_img');
define('meta_cat_TABLE' , $prefixeTable . 'meta_cat');

add_event_handler('get_admin_plugin_menu_links', 'meta_admin_menu');
function meta_admin_menu($menu)
{
  array_push($menu, array(
		'NAME' => 'Meta',
    'URL' => get_admin_plugin_menu_link(meta_PATH . 'admin/admin.php')));
  return $menu;
}

//Gestion des meta dans le header
add_event_handler('loc_begin_page_header', 'Change_Meta',20 );
add_event_handler('loc_end_page_header', 'add_meta',56 );
add_event_handler('loc_end_page_header', 'add_metacat',61 );
add_event_handler('loc_end_page_header', 'add_metaimg',71 );
add_event_handler('loc_after_page_header', 'set_meta_back');

function Change_Meta()
 {
	global $template;
	$template->set_prefilter('header', 'upmata');
 }

function upmata ($content, &$smarty)
 {
  $search = '#<meta name="description" content=".*?">#';
  
  $replacement = '<meta name="description" content="{$PLUG_META}">';

  return preg_replace($search, $replacement, $content);
 }



function add_meta()
{
		global $template, $page, $meta_infos;
	$meta_infos = array();
	$meta_infos['author'] = $template->get_template_vars('INFO_AUTHOR');
	$meta_infos['related_tags'] = $template->get_template_vars('related_tags');
	$meta_infos['info'] = $template->get_template_vars('INFO_FILE');
	$meta_infos['title'] = $template->get_template_vars('PAGE_TITLE');

	$query = '
  select id,metaname,metaval
    FROM ' . meta_TABLE . '
    WHERE metaname IN (\'author\', \'keywords\', \'Description\', \'robots\')
    ;';
	$result = pwg_query($query);
	$meta = array();
		while ($row = mysql_fetch_assoc($result))
			{
			$meta[$row['metaname']] = $row['metaval'];
			}

	// Authors
	if (!empty($meta_infos['author']) and !empty($meta['author']))
	{
		$template->assign('INFO_AUTHOR', $meta_infos['author'] . ' - ' . $meta['author']);
	}
	elseif (!empty($meta['author']))
	{
    $template->assign('INFO_AUTHOR', $meta['author']);
	}
  
	// Keywords
	if (!empty($meta['keywords']))
	{
    $template->append('related_tags', array('name' => $meta['keywords']));
	}

	// Description
	if (!empty($meta_infos['title']) and !empty($meta_infos['info']) and !empty($meta['Description']))
	{
    $template->assign('PLUG_META', $meta_infos['title']. ' - ' .$meta_infos['info'] . ', ' . $meta['Description']);
	}
	elseif (!empty($meta_infos['title']) and !empty($meta['Description']))
	{
    $template->assign('PLUG_META', $meta_infos['title']. ' - ' .$meta['Description']);
	}
	elseif (!empty($meta['Description']))
	{
    $template->assign('PLUG_META', $meta['Description']);
	}
	
	// Robots
	if (!empty($meta['robots']))
	{
    $template->append('head_elements', '<meta name="robots" content="'.$meta['robots'].'">');
	}

 }
function add_metacat()
{
	global $template, $page, $meta_infos;

	//meta categories
  if ( !empty($page['category']['id']) )   
  {
    $query = '
    select id,metaKeycat
      FROM ' . meta_cat_TABLE . '
      WHERE id = \''.$page['category']['id'].'\'
      ;';
    $result = pwg_query($query);
    $row = mysql_fetch_array($result);
    if (!empty($row['metaKeycat']))
    {
      $template->append('related_tags', array('name' => $row['metaKeycat']));
    }
	
	$query = '
    select id,metadescat
      FROM ' . meta_cat_TABLE . '
      WHERE id = \''.$page['category']['id'].'\'
      ;';
    $result = pwg_query($query);
    $row = mysql_fetch_array($result);
    if (!empty($row['metadescat']))
    {
      $template->assign('PLUG_META', $row['metadescat']);
    }
  }
}

function add_metaimg()
{
	global $template, $page, $meta_infos;

  //meta images
  if ( !empty($page['image_id']) )   
  {
    $query = '
    select id,metaKeyimg
      FROM ' . meta_img_TABLE . '
      WHERE id = \''.$page['image_id'].'\'
      ;';
    $result = pwg_query($query);
    $row = mysql_fetch_array($result);
    if (!empty($row['metaKeyimg']))
    {
      $template->append('related_tags', array('name' => $row['metaKeyimg']));
    }

		$query = '
    select id,metadesimg
      FROM ' . meta_img_TABLE . '
      WHERE id = \''.$page['image_id'].'\'
      ;';
    $result = pwg_query($query);
    $row = mysql_fetch_array($result);
    if (!empty($row['metadesimg']))
    {
      $template->assign('PLUG_META', $row['metadesimg']);
    }
	
  }
}


function set_meta_back()
{
  global $template, $meta_infos;
  
  $template->assign
  (array
	(
    'INFO_AUTHOR'  => $meta_infos['author'],
    'related_tags' => $meta_infos['related_tags'],
    'INFO_FILE'    => $meta_infos['info'],
    )
  );
}
?>