source: extensions/meta/main.inc.php @ 7624

Last change on this file since 7624 was 7624, checked in by ddtddt, 13 years ago

[extensions] - meta - uses a different tag than the default

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
RevLine 
[3978]1<?php
2/*
3Plugin Name: meta
[7468]4Version: auto
[3978]5Description: Allows to add metadata
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=220
7Author: ddtddt
8Author URI: http://piwigo.org/
9*/
10
11if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
12
13global $prefixeTable;
14
15define('meta_DIR' , basename(dirname(__FILE__)));
16define('meta_PATH' , PHPWG_PLUGINS_PATH . meta_DIR . '/');
17define('meta_TABLE' , $prefixeTable . 'meta');
[4174]18define('meta_img_TABLE' , $prefixeTable . 'meta_img');
19define('meta_cat_TABLE' , $prefixeTable . 'meta_cat');
[3978]20
21add_event_handler('get_admin_plugin_menu_links', 'meta_admin_menu');
22function meta_admin_menu($menu)
23{
24  array_push($menu, array(
25                'NAME' => 'Meta',
26    'URL' => get_admin_plugin_menu_link(meta_PATH . 'admin/admin.php')));
27  return $menu;
28}
29
30//Gestion des meta dans le header
[7624]31add_event_handler('loc_begin_page_header', 'Change_Meta',20 );
32add_event_handler('loc_end_page_header', 'add_meta',56 );
[7518]33add_event_handler('loc_begin_page_header', 'add_metacat',61 );
34add_event_handler('loc_begin_page_header', 'add_metaimg',71 );
35add_event_handler('loc_after_page_header', 'set_meta_back',81);
36
[7624]37function Change_Meta()
38 {
39        global $template;
40        $template->set_prefilter('header', 'upmata');
41 }
42
43function upmata ($content, &$smarty)
44 {
45  $search = '#<meta name="description" content=".*?">#';
46 
47  $replacement = '<meta name="description" content="{$PLUG_META}">';
48
49  return preg_replace($search, $replacement, $content);
50 }
51
52
53
[3978]54function add_meta()
55{
[7518]56                global $template, $page, $meta_infos;
57        $meta_infos = array();
58        $meta_infos['author'] = $template->get_template_vars('INFO_AUTHOR');
59        $meta_infos['related_tags'] = $template->get_template_vars('related_tags');
60        $meta_infos['info'] = $template->get_template_vars('INFO_FILE');
[7624]61        $meta_infos['title'] = $template->get_template_vars('PAGE_TITLE');
[3978]62
[7518]63        $query = '
[3978]64  select id,metaname,metaval
65    FROM ' . meta_TABLE . '
66    WHERE metaname IN (\'author\', \'keywords\', \'Description\', \'robots\')
67    ;';
[7518]68        $result = pwg_query($query);
69        $meta = array();
70                while ($row = mysql_fetch_assoc($result))
71                        {
72                        $meta[$row['metaname']] = $row['metaval'];
73                        }
[3978]74
[7518]75        // Authors
76        if (!empty($meta_infos['author']) and !empty($meta['author']))
77        {
78                $template->assign('INFO_AUTHOR', $meta_infos['author'] . ' - ' . $meta['author']);
79        }
80        elseif (!empty($meta['author']))
81        {
[3978]82    $template->assign('INFO_AUTHOR', $meta['author']);
[7518]83        }
[3978]84 
[7518]85        // Keywords
86        if (!empty($meta['keywords']))
87        {
[3978]88    $template->append('related_tags', array('name' => $meta['keywords']));
[7518]89        }
[3978]90
[7518]91        // Description
[7624]92        if (!empty($meta_infos['title']) and !empty($meta_infos['info']) and !empty($meta['Description']))
[7518]93        {
[7624]94    $template->assign('PLUG_META', $meta_infos['title']. ' - ' .$meta_infos['info'] . ', ' . $meta['Description']);
[7518]95        }
[7624]96        elseif (!empty($meta_infos['title']) and !empty($meta['Description']))
97        {
98    $template->assign('PLUG_META', $meta_infos['title']. ' - ' .$meta['Description']);
99        }
[7518]100        elseif (!empty($meta['Description']))
101        {
[7624]102    $template->assign('PLUG_META', $meta['Description']);
[7518]103        }
[7624]104       
[7518]105        // Robots
106        if (!empty($meta['robots']))
107        {
[3978]108    $template->append('head_elements', '<meta name="robots" content="'.$meta['robots'].'">');
[7518]109        }
[3978]110
[7518]111 }
112function add_metacat()
113{
114        global $template, $page, $meta_infos;
115  $meta_infos = array();
116  $meta_infos['author'] = $template->get_template_vars('INFO_AUTHOR');
117  $meta_infos['related_tags'] = $template->get_template_vars('related_tags');
118  $meta_infos['info'] = $template->get_template_vars('INFO_FILE');
119
120        //meta categories
[4174]121  if ( !empty($page['category']['id']) )   
[3978]122  {
123    $query = '
[4174]124    select id,metaKeycat
125      FROM ' . meta_cat_TABLE . '
[3978]126      WHERE id = \''.$page['category']['id'].'\'
127      ;';
128    $result = pwg_query($query);
129    $row = mysql_fetch_array($result);
[4174]130    if (!empty($row['metaKeycat']))
[3978]131    {
[4174]132      $template->append('related_tags', array('name' => $row['metaKeycat']));
[3978]133    }
[7468]134       
135        $query = '
136    select id,metadescat
137      FROM ' . meta_cat_TABLE . '
138      WHERE id = \''.$page['category']['id'].'\'
139      ;';
140    $result = pwg_query($query);
141    $row = mysql_fetch_array($result);
142    if (!empty($row['metadescat']))
143    {
[7624]144      $template->assign('PLUG_META', $row['metadescat']);
[7468]145    }
[3978]146  }
[7518]147}
[4174]148
[7518]149function add_metaimg()
150{
151        global $template, $page, $meta_infos;
152  $meta_infos = array();
153  $meta_infos['author'] = $template->get_template_vars('INFO_AUTHOR');
154  $meta_infos['related_tags'] = $template->get_template_vars('related_tags');
155  $meta_infos['info'] = $template->get_template_vars('INFO_FILE');
[7624]156
[4174]157  //meta images
158  if ( !empty($page['image_id']) )   
159  {
160    $query = '
161    select id,metaKeyimg
162      FROM ' . meta_img_TABLE . '
163      WHERE id = \''.$page['image_id'].'\'
164      ;';
165    $result = pwg_query($query);
166    $row = mysql_fetch_array($result);
167    if (!empty($row['metaKeyimg']))
168    {
169      $template->append('related_tags', array('name' => $row['metaKeyimg']));
170    }
[7468]171
172                $query = '
173    select id,metadesimg
174      FROM ' . meta_img_TABLE . '
175      WHERE id = \''.$page['image_id'].'\'
176      ;';
177    $result = pwg_query($query);
178    $row = mysql_fetch_array($result);
179    if (!empty($row['metadesimg']))
180    {
[7624]181      $template->assign('PLUG_META', $row['metadesimg']);
[7468]182    }
183       
[4174]184  }
[3978]185}
186
[7518]187
[3978]188function set_meta_back()
189{
190  global $template, $meta_infos;
191 
[7518]192  $template->assign
193  (array
194        (
[3978]195    'INFO_AUTHOR'  => $meta_infos['author'],
196    'related_tags' => $meta_infos['related_tags'],
[7518]197    'INFO_FILE'    => $meta_infos['info'],
[3978]198    )
199  );
200}
201?>
Note: See TracBrowser for help on using the repository browser.