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

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

[extensions] - meta - Add new feature / Ability to customize the meta description for categories and images

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 4.0 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', 'add_meta' );
32function add_meta()
33{
34  global $template, $page, $meta_infos;
35
36  $meta_infos = array();
37  $meta_infos['author'] = $template->get_template_vars('INFO_AUTHOR');
38  $meta_infos['related_tags'] = $template->get_template_vars('related_tags');
39  $meta_infos['info'] = $template->get_template_vars('INFO_FILE');
40
41  $query = '
42  select id,metaname,metaval
43    FROM ' . meta_TABLE . '
44    WHERE metaname IN (\'author\', \'keywords\', \'Description\', \'robots\')
45    ;';
46  $result = pwg_query($query);
47  $meta = array();
48  while ($row = mysql_fetch_assoc($result))
49  {
50    $meta[$row['metaname']] = $row['metaval'];
51  }
52
53  // Authors
54  if (!empty($meta_infos['author']) and !empty($meta['author']))
55  {
56    $template->assign('INFO_AUTHOR', $meta_infos['author'] . ' - ' . $meta['author']);
57  }
58  elseif (!empty($meta['author']))
59  {
60    $template->assign('INFO_AUTHOR', $meta['author']);
61  }
62 
63  // Keywords
64  if (!empty($meta['keywords']))
65  {
66    $template->append('related_tags', array('name' => $meta['keywords']));
67  }
68
69  // Description
70  if (!empty($meta_infos['info']) and !empty($meta['Description']))
71  {
72    $template->assign('INFO_FILE', $meta_infos['info'] . ', ' . $meta['Description']);
73  }
74  elseif (!empty($meta['Description']))
75  {
76    $template->assign('INFO_FILE', $meta['Description']);
77  }
78
79  // Robots
80  if (!empty($meta['robots']))
81  {
82    $template->append('head_elements', '<meta name="robots" content="'.$meta['robots'].'">');
83  }
84
85  //meta categories
86  if ( !empty($page['category']['id']) )   
87  {
88
89    $query = '
90    select id,metaKeycat
91      FROM ' . meta_cat_TABLE . '
92      WHERE id = \''.$page['category']['id'].'\'
93      ;';
94    $result = pwg_query($query);
95    $row = mysql_fetch_array($result);
96    if (!empty($row['metaKeycat']))
97    {
98      $template->append('related_tags', array('name' => $row['metaKeycat']));
99    }
100       
101        $query = '
102    select id,metadescat
103      FROM ' . meta_cat_TABLE . '
104      WHERE id = \''.$page['category']['id'].'\'
105      ;';
106    $result = pwg_query($query);
107    $row = mysql_fetch_array($result);
108    if (!empty($row['metadescat']))
109    {
110      $template->assign('INFO_FILE', $row['metadescat']);
111    }
112       
113       
114       
115  }
116
117
118  //meta images
119  if ( !empty($page['image_id']) )   
120  {
121    $query = '
122    select id,metaKeyimg
123      FROM ' . meta_img_TABLE . '
124      WHERE id = \''.$page['image_id'].'\'
125      ;';
126    $result = pwg_query($query);
127    $row = mysql_fetch_array($result);
128    if (!empty($row['metaKeyimg']))
129    {
130      $template->append('related_tags', array('name' => $row['metaKeyimg']));
131    }
132
133                $query = '
134    select id,metadesimg
135      FROM ' . meta_img_TABLE . '
136      WHERE id = \''.$page['image_id'].'\'
137      ;';
138    $result = pwg_query($query);
139    $row = mysql_fetch_array($result);
140    if (!empty($row['metadesimg']))
141    {
142      $template->assign('COMMENT_IMG', $row['metadesimg']);
143          $template->clear_assign('INFO_FILE');
144    }
145       
146  }
147}
148
149add_event_handler('loc_after_page_header', 'set_meta_back');
150function set_meta_back()
151{
152  global $template, $meta_infos;
153 
154  $template->assign(array(
155    'INFO_AUTHOR'  => $meta_infos['author'],
156    'related_tags' => $meta_infos['related_tags'],
157    'INFO_FILE'    => $meta_infos['info']
158    )
159  );
160}
161?>
Note: See TracBrowser for help on using the repository browser.