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
Line 
1<?php
2/*
3Plugin Name: meta
4Version: auto
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');
18define('meta_img_TABLE' , $prefixeTable . 'meta_img');
19define('meta_cat_TABLE' , $prefixeTable . 'meta_cat');
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
31add_event_handler('loc_begin_page_header', 'Change_Meta',20 );
32add_event_handler('loc_end_page_header', 'add_meta',56 );
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
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
54function add_meta()
55{
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');
61        $meta_infos['title'] = $template->get_template_vars('PAGE_TITLE');
62
63        $query = '
64  select id,metaname,metaval
65    FROM ' . meta_TABLE . '
66    WHERE metaname IN (\'author\', \'keywords\', \'Description\', \'robots\')
67    ;';
68        $result = pwg_query($query);
69        $meta = array();
70                while ($row = mysql_fetch_assoc($result))
71                        {
72                        $meta[$row['metaname']] = $row['metaval'];
73                        }
74
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        {
82    $template->assign('INFO_AUTHOR', $meta['author']);
83        }
84 
85        // Keywords
86        if (!empty($meta['keywords']))
87        {
88    $template->append('related_tags', array('name' => $meta['keywords']));
89        }
90
91        // Description
92        if (!empty($meta_infos['title']) and !empty($meta_infos['info']) and !empty($meta['Description']))
93        {
94    $template->assign('PLUG_META', $meta_infos['title']. ' - ' .$meta_infos['info'] . ', ' . $meta['Description']);
95        }
96        elseif (!empty($meta_infos['title']) and !empty($meta['Description']))
97        {
98    $template->assign('PLUG_META', $meta_infos['title']. ' - ' .$meta['Description']);
99        }
100        elseif (!empty($meta['Description']))
101        {
102    $template->assign('PLUG_META', $meta['Description']);
103        }
104       
105        // Robots
106        if (!empty($meta['robots']))
107        {
108    $template->append('head_elements', '<meta name="robots" content="'.$meta['robots'].'">');
109        }
110
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
121  if ( !empty($page['category']['id']) )   
122  {
123    $query = '
124    select id,metaKeycat
125      FROM ' . meta_cat_TABLE . '
126      WHERE id = \''.$page['category']['id'].'\'
127      ;';
128    $result = pwg_query($query);
129    $row = mysql_fetch_array($result);
130    if (!empty($row['metaKeycat']))
131    {
132      $template->append('related_tags', array('name' => $row['metaKeycat']));
133    }
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    {
144      $template->assign('PLUG_META', $row['metadescat']);
145    }
146  }
147}
148
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');
156
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    }
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    {
181      $template->assign('PLUG_META', $row['metadesimg']);
182    }
183       
184  }
185}
186
187
188function set_meta_back()
189{
190  global $template, $meta_infos;
191 
192  $template->assign
193  (array
194        (
195    'INFO_AUTHOR'  => $meta_infos['author'],
196    'related_tags' => $meta_infos['related_tags'],
197    'INFO_FILE'    => $meta_infos['info'],
198    )
199  );
200}
201?>
Note: See TracBrowser for help on using the repository browser.