forked from Piwigo/piwigo-tag_groups
/
main.inc.php
186 lines (153 loc) · 4.52 KB
/
main.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/*
Plugin Name: Tag Groups
Version: auto
Description: Create groups of tags
Plugin URI: http://piwigo.org/ext/extension_view.php?eid=
Author: plg
Author URI: http://piwigo.org
*/
if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
$render_tag_names = true;
if (defined('IN_ADMIN'))
{
$render_tag_names = false;
}
if (script_basename() == 'tags')
{
global $page, $conf;
if (isset($_GET['display_mode']))
{
if ('groups' == $_GET['display_mode'])
{
$render_tag_names = false;
}
}
elseif ('groups' == $conf['tags_default_display_mode'])
{
$render_tag_names = false;
}
$page['tg_display'] = !$render_tag_names;
}
if ($render_tag_names)
{
add_event_handler('render_tag_name', 'tg_clean_tag_name');
}
function tg_clean_tag_name($tag_name)
{
return preg_replace('/^[^:]*:/', '', $tag_name);
}
// file_get_contents('tags.tpl')
if (script_basename() == 'tags')
{
add_event_handler('loc_begin_page_header', 'tg_groups_display');
}
function tg_groups_display()
{
global $conf, $template, $user, $tags, $page;
load_language('plugin.lang', PHPWG_PLUGINS_PATH . basename(dirname(__FILE__)) . '/');
load_language('lang', PHPWG_ROOT_PATH.PWG_LOCAL_DIR, array('no_fallback'=>true, 'local'=>true) );
$template->set_prefilter('tags', 'tg_add_display_link_prefilter');
$template->assign('U_TAG_GROUPS', get_root_url().'tags.php?display_mode=groups');
if ($page['tg_display'])
{
// echo __FILE__.'::'.__LINE__.' display_mode=groups<br>';
$template->set_prefilter('tags', 'tg_groups_display_prefilter');
$template->assign('display_mode', 'groups');
// we want tags diplayed in alphabetic order
usort($tags, 'tag_alpha_compare');
$current_tag_group = null;
$nb_tags = count($tags);
$current_column = 1;
$current_tag_idx = 0;
$tag_group = array(
'tags' => array()
);
foreach ($tags as $tag)
{
// if the tag belongs to no group, we don't show it on the "tag by
// group" display mode
if (strpos($tag['name'], ':') === false)
{
continue;
}
else
{
list($tag['group'], $tag['name']) = explode(':', $tag['name'], 2);
$tag['group'] = preg_replace('/^[^=]*=/', '', $tag['group']);
}
if ($current_tag_idx == 0)
{
$current_tag_group = $tag['group'];
$tag_group['TITLE'] = $tag['group'];
}
// new group?
if ($tag['group'] !== $current_tag_group)
{
if ($current_column < $conf['tag_letters_column_number']
and $current_tag_idx > $current_column*$nb_tags/$conf['tag_letters_column_number'] )
{
$tag_group['CHANGE_COLUMN'] = true;
$current_column++;
}
$tag_group['TITLE'] = $current_tag_group;
$template->append(
'tag_groups',
$tag_group
);
$current_tag_group = $tag['group'];
$tag_group = array(
'tags' => array()
);
}
array_push(
$tag_group['tags'],
array_merge(
$tag,
array(
'URL' => make_index_url(
array(
'tags' => array($tag),
)
),
)
)
);
$current_tag_idx++;
}
// flush last group
if (count($tag_group['tags']) > 0)
{
unset($tag_group['CHANGE_COLUMN']);
$tag_group['TITLE'] = $current_tag_group;
$template->append(
'tag_groups',
$tag_group
);
}
}
}
function tg_groups_display_prefilter($content, &$smarty)
{
$template_content = file_get_contents(PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/tags.tpl');
$search = '#\{/if\}((\s*</div>\s*)+)<!-- content -->#mi';
$replace = '{/if}'."\n".$template_content.'\1 <!-- content -->';
return preg_replace($search, $replace, $content);
}
function tg_add_display_link_prefilter($content, &$smarty)
{
$search = '#\{if \$display_mode == \'letters\'\}\s*<li>#mi';
$replace = '{if $display_mode != \'cloud\'}<li>';
$content = preg_replace($search, $replace, $content);
$search = '#\{if \$display_mode == \'cloud\'\}\s*<li>#mi';
$replace = '{if $display_mode != \'letters\'}<li>';
$content = preg_replace($search, $replace, $content);
$search = '<ul class="categoryActions">';
$replace = '<ul class="categoryActions">
{if $display_mode != \'groups\'}<li>
<li><a href="{$U_TAG_GROUPS}">{\'show tag groups\'|@translate}</a></li>
{/if}
';
return str_replace($search, $replace, $content);
}
?>