1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
12 | if (!defined('DC_CONTEXT_ADMIN')) { return; } |
---|
13 | |
---|
14 | $_menu['Blog']->addItem(__('Tags'),'plugin.php?p=tags&m=tags','index.php?pf=tags/icon.png', |
---|
15 | preg_match('/plugin.php\?p=tags&m=tag(s|_posts)?(&.*)?$/',$_SERVER['REQUEST_URI']), |
---|
16 | $core->auth->check('usage,contentadmin',$core->blog->id)); |
---|
17 | |
---|
18 | require dirname(__FILE__).'/_widgets.php'; |
---|
19 | |
---|
20 | $core->addBehavior('adminPostFormSidebar',array('tagsBehaviors','tagsField')); |
---|
21 | |
---|
22 | $core->addBehavior('adminAfterPostCreate',array('tagsBehaviors','setTags')); |
---|
23 | $core->addBehavior('adminAfterPostUpdate',array('tagsBehaviors','setTags')); |
---|
24 | |
---|
25 | $core->addBehavior('adminPostHeaders',array('tagsBehaviors','postHeaders')); |
---|
26 | $core->addBehavior('adminPostsActionsHeaders',array('tagsBehaviors','postsActionsHeaders')); |
---|
27 | |
---|
28 | $core->addBehavior('adminPostsActionsCombo',array('tagsBehaviors','adminPostsActionsCombo')); |
---|
29 | $core->addBehavior('adminPostsActions',array('tagsBehaviors','adminPostsActions')); |
---|
30 | $core->addBehavior('adminPostsActionsContent',array('tagsBehaviors','adminPostsActionsContent')); |
---|
31 | |
---|
32 | $core->addBehavior('adminPreferencesForm',array('tagsBehaviors','adminUserForm')); |
---|
33 | $core->addBehavior('adminBeforeUserOptionsUpdate',array('tagsBehaviors','setTagListFormat')); |
---|
34 | |
---|
35 | $core->addBehavior('adminUserForm',array('tagsBehaviors','adminUserForm')); |
---|
36 | $core->addBehavior('adminBeforeUserCreate',array('tagsBehaviors','setTagListFormat')); |
---|
37 | $core->addBehavior('adminBeforeUserUpdate',array('tagsBehaviors','setTagListFormat')); |
---|
38 | |
---|
39 | $core->addBehavior('coreInitWikiPost',array('tagsBehaviors','coreInitWikiPost')); |
---|
40 | |
---|
41 | $core->addBehavior('adminDashboardFavs',array('tagsBehaviors','dashboardFavs')); |
---|
42 | |
---|
43 | # BEHAVIORS |
---|
44 | class tagsBehaviors |
---|
45 | { |
---|
46 | public static function dashboardFavs($core,$favs) |
---|
47 | { |
---|
48 | $favs['tags'] = new ArrayObject(array('tags','Tags','plugin.php?p=tags&m=tags', |
---|
49 | 'index.php?pf=tags/icon.png','index.php?pf=tags/icon-big.png', |
---|
50 | 'usage,contentadmin',null,null)); |
---|
51 | } |
---|
52 | |
---|
53 | public static function coreInitWikiPost($wiki2xhtml) |
---|
54 | { |
---|
55 | $wiki2xhtml->registerFunction('url:tag',array('tagsBehaviors','wiki2xhtmlTag')); |
---|
56 | } |
---|
57 | |
---|
58 | public static function wiki2xhtmlTag($url,$content) |
---|
59 | { |
---|
60 | $url = substr($url,4); |
---|
61 | if (strpos($content,'tag:') === 0) { |
---|
62 | $content = substr($content,4); |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | $tag_url = html::stripHostURL($GLOBALS['core']->blog->url.$GLOBALS['core']->url->getURLFor('tag')); |
---|
67 | $res['url'] = $tag_url.'/'.rawurlencode(dcMeta::sanitizeMetaID($url)); |
---|
68 | $res['content'] = $content; |
---|
69 | |
---|
70 | return $res; |
---|
71 | } |
---|
72 | |
---|
73 | public static function tagsField($post) |
---|
74 | { |
---|
75 | $meta =& $GLOBALS['core']->meta; |
---|
76 | |
---|
77 | if (!empty($_POST['post_tags'])) { |
---|
78 | $value = $_POST['post_tags']; |
---|
79 | } else { |
---|
80 | $value = ($post) ? $meta->getMetaStr($post->post_meta,'tag') : ''; |
---|
81 | } |
---|
82 | |
---|
83 | echo |
---|
84 | '<h5><label for="post_tags">'.__('Tags:').'</label></h5>'. |
---|
85 | '<div class="p" id="tags-edit">'.form::textarea('post_tags',20,3,$value,'maximal').'</div>'; |
---|
86 | } |
---|
87 | |
---|
88 | public static function setTags($cur,$post_id) |
---|
89 | { |
---|
90 | $post_id = (integer) $post_id; |
---|
91 | |
---|
92 | if (isset($_POST['post_tags'])) { |
---|
93 | $tags = $_POST['post_tags']; |
---|
94 | $meta =& $GLOBALS['core']->meta; |
---|
95 | $meta->delPostMeta($post_id,'tag'); |
---|
96 | |
---|
97 | foreach ($meta->splitMetaValues($tags) as $tag) { |
---|
98 | $meta->setPostMeta($post_id,'tag',$tag); |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | public static function postHeaders() |
---|
104 | { |
---|
105 | $tag_url = $GLOBALS['core']->blog->url.$GLOBALS['core']->url->getURLFor('tag'); |
---|
106 | |
---|
107 | $opts = $GLOBALS['core']->auth->getOptions(); |
---|
108 | $type = isset($opts['tag_list_format']) ? $opts['tag_list_format'] : 'more'; |
---|
109 | |
---|
110 | return |
---|
111 | '<script type="text/javascript" src="index.php?pf=tags/js/jquery.autocomplete.js"></script>'. |
---|
112 | '<script type="text/javascript" src="index.php?pf=tags/js/post.js"></script>'. |
---|
113 | '<script type="text/javascript">'."\n". |
---|
114 | "//<![CDATA[\n". |
---|
115 | "metaEditor.prototype.meta_url = 'plugin.php?p=tags&m=tag_posts&tag=';\n". |
---|
116 | "metaEditor.prototype.meta_type = '".html::escapeJS($type)."';\n". |
---|
117 | "metaEditor.prototype.text_confirm_remove = '".html::escapeJS(__('Are you sure you want to remove this %s?'))."';\n". |
---|
118 | "metaEditor.prototype.text_add_meta = '".html::escapeJS(__('Add a %s to this entry'))."';\n". |
---|
119 | "metaEditor.prototype.text_choose = '".html::escapeJS(__('Choose from list'))."';\n". |
---|
120 | "metaEditor.prototype.text_all = '".html::escapeJS(__('all'))."';\n". |
---|
121 | "metaEditor.prototype.text_separation = '';\n". |
---|
122 | "jsToolBar.prototype.elements.tag.title = '".html::escapeJS(__('Tag'))."';\n". |
---|
123 | "jsToolBar.prototype.elements.tag.url = '".html::escapeJS($tag_url)."';\n". |
---|
124 | "dotclear.msg.tags_autocomplete = '".html::escapeJS(__('used in %e - frequency %p%'))."';\n". |
---|
125 | "dotclear.msg.entry = '".html::escapeJS(__('entry'))."';\n". |
---|
126 | "dotclear.msg.entries = '".html::escapeJS(__('entries'))."';\n". |
---|
127 | "\n//]]>\n". |
---|
128 | "</script>\n". |
---|
129 | '<link rel="stylesheet" type="text/css" href="index.php?pf=tags/style.css" />'; |
---|
130 | } |
---|
131 | |
---|
132 | public static function postsActionsHeaders() |
---|
133 | { |
---|
134 | if (($_POST['action'] == 'tags') || ($_POST['action'] == 'tags_remove')) { |
---|
135 | $tag_url = $GLOBALS['core']->blog->url.$GLOBALS['core']->url->getURLFor('tag'); |
---|
136 | |
---|
137 | $opts = $GLOBALS['core']->auth->getOptions(); |
---|
138 | $type = isset($opts['tag_list_format']) ? $opts['tag_list_format'] : 'more'; |
---|
139 | |
---|
140 | return |
---|
141 | '<script type="text/javascript" src="index.php?pf=tags/js/jquery.autocomplete.js"></script>'. |
---|
142 | '<script type="text/javascript" src="index.php?pf=tags/js/posts_actions.js"></script>'. |
---|
143 | '<script type="text/javascript">'."\n". |
---|
144 | "//<![CDATA[\n". |
---|
145 | "metaEditor.prototype.meta_url = 'plugin.php?p=tags&m=tag_posts&tag=';\n". |
---|
146 | "metaEditor.prototype.meta_type = '".html::escapeJS($type)."';\n". |
---|
147 | "metaEditor.prototype.text_confirm_remove = '".html::escapeJS(__('Are you sure you want to remove this %s?'))."';\n". |
---|
148 | "metaEditor.prototype.text_add_meta = '".html::escapeJS(__('Add a %s to this entry'))."';\n". |
---|
149 | "metaEditor.prototype.text_choose = '".html::escapeJS(__('Choose from list'))."';\n". |
---|
150 | "metaEditor.prototype.text_all = '".html::escapeJS(__('all'))."';\n". |
---|
151 | "metaEditor.prototype.text_separation = '".html::escapeJS(__('Enter tags separated by coma'))."';\n". |
---|
152 | "dotclear.msg.tags_autocomplete = '".html::escapeJS(__('used in %e - frequency %p%'))."';\n". |
---|
153 | "dotclear.msg.entry = '".html::escapeJS(__('entry'))."';\n". |
---|
154 | "dotclear.msg.entries = '".html::escapeJS(__('entries'))."';\n". |
---|
155 | "\n//]]>\n". |
---|
156 | "</script>\n". |
---|
157 | '<link rel="stylesheet" type="text/css" href="index.php?pf=tags/style.css" />'; |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | public static function adminPostsActionsCombo($args) |
---|
162 | { |
---|
163 | $args[0][__('Tags')] = array(__('Add tags') => 'tags'); |
---|
164 | |
---|
165 | if ($GLOBALS['core']->auth->check('delete,contentadmin',$GLOBALS['core']->blog->id)) { |
---|
166 | $args[0][__('Tags')] = array_merge($args[0][__('Tags')], |
---|
167 | array(__('Remove tags') => 'tags_remove')); |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | public static function adminPostsActions($core,$posts,$action,$redir) |
---|
172 | { |
---|
173 | if ($action == 'tags' && !empty($_POST['new_tags'])) |
---|
174 | { |
---|
175 | try |
---|
176 | { |
---|
177 | |
---|
178 | $meta =& $GLOBALS['core']->meta; |
---|
179 | $tags = $meta->splitMetaValues($_POST['new_tags']); |
---|
180 | |
---|
181 | while ($posts->fetch()) |
---|
182 | { |
---|
183 | # Get tags for post |
---|
184 | $post_meta = $meta->getMetadata(array( |
---|
185 | 'meta_type' => 'tag', |
---|
186 | 'post_id' => $posts->post_id)); |
---|
187 | $pm = array(); |
---|
188 | while ($post_meta->fetch()) { |
---|
189 | $pm[] = $post_meta->meta_id; |
---|
190 | } |
---|
191 | |
---|
192 | foreach ($tags as $t) { |
---|
193 | if (!in_array($t,$pm)) { |
---|
194 | $meta->setPostMeta($posts->post_id,'tag',$t); |
---|
195 | } |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | http::redirect($redir); |
---|
200 | } |
---|
201 | catch (Exception $e) |
---|
202 | { |
---|
203 | $core->error->add($e->getMessage()); |
---|
204 | } |
---|
205 | } |
---|
206 | elseif ($action == 'tags_remove' && !empty($_POST['meta_id']) && $core->auth->check('delete,contentadmin',$core->blog->id)) |
---|
207 | { |
---|
208 | try |
---|
209 | { |
---|
210 | $meta =& $GLOBALS['core']->meta; |
---|
211 | while ($posts->fetch()) |
---|
212 | { |
---|
213 | foreach ($_POST['meta_id'] as $v) |
---|
214 | { |
---|
215 | $meta->delPostMeta($posts->post_id,'tag',$v); |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | http::redirect($redir); |
---|
220 | } |
---|
221 | catch (Exception $e) |
---|
222 | { |
---|
223 | $core->error->add($e->getMessage()); |
---|
224 | } |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | public static function adminPostsActionsContent($core,$action,$hidden_fields,$form_uri="posts_actions.php") |
---|
229 | { |
---|
230 | if ($action == 'tags') |
---|
231 | { |
---|
232 | echo |
---|
233 | '<h2 class="page-title">'.__('Add tags to entries').'</h2>'. |
---|
234 | '<form action="'.$form_uri.'" method="post">'. |
---|
235 | '<div><label for="new_tags" class="area">'.__('Tags to add:').'</label> '. |
---|
236 | form::textarea('new_tags',60,3). |
---|
237 | '</div>'. |
---|
238 | $hidden_fields. |
---|
239 | $core->formNonce(). |
---|
240 | form::hidden(array('action'),'tags'). |
---|
241 | '<p><input type="submit" value="'.__('Save').'" '. |
---|
242 | 'name="save_tags" /></p>'. |
---|
243 | '</form>'; |
---|
244 | } |
---|
245 | elseif ($action == 'tags_remove') |
---|
246 | { |
---|
247 | $meta =& $GLOBALS['core']->meta; |
---|
248 | $tags = array(); |
---|
249 | |
---|
250 | foreach ($_POST['entries'] as $id) { |
---|
251 | $post_tags = $meta->getMetadata(array( |
---|
252 | 'meta_type' => 'tag', |
---|
253 | 'post_id' => (integer) $id))->toStatic()->rows(); |
---|
254 | foreach ($post_tags as $v) { |
---|
255 | if (isset($tags[$v['meta_id']])) { |
---|
256 | $tags[$v['meta_id']]++; |
---|
257 | } else { |
---|
258 | $tags[$v['meta_id']] = 1; |
---|
259 | } |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | echo '<h2 class="page-title">'.__('Remove selected tags from entries').'</h2>'; |
---|
264 | |
---|
265 | if (empty($tags)) { |
---|
266 | echo '<p>'.__('No tags for selected entries').'</p>'; |
---|
267 | return; |
---|
268 | } |
---|
269 | |
---|
270 | $posts_count = count($_POST['entries']); |
---|
271 | |
---|
272 | echo |
---|
273 | '<form action="'.$form_uri.'" method="post">'. |
---|
274 | '<fieldset><legend>'.__('Following tags have been found in selected entries:').'</legend>'; |
---|
275 | |
---|
276 | foreach ($tags as $k => $n) { |
---|
277 | $label = '<label class="classic">%s %s</label>'; |
---|
278 | if ($posts_count == $n) { |
---|
279 | $label = sprintf($label,'%s','<strong>%s</strong>'); |
---|
280 | } |
---|
281 | echo '<p>'.sprintf($label, |
---|
282 | form::checkbox(array('meta_id[]'),html::escapeHTML($k)), |
---|
283 | html::escapeHTML($k)). |
---|
284 | '</p>'; |
---|
285 | } |
---|
286 | |
---|
287 | echo |
---|
288 | '<p><input type="submit" value="'.__('ok').'" /></p>'. |
---|
289 | $hidden_fields. |
---|
290 | $core->formNonce(). |
---|
291 | form::hidden(array('action'),'tags_remove'). |
---|
292 | '</fieldset></form>'; |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | public static function adminUserForm($args) |
---|
297 | { |
---|
298 | if ($args instanceof dcCore) { |
---|
299 | $opts = $args->auth->getOptions(); |
---|
300 | } |
---|
301 | elseif ($args instanceof record) { |
---|
302 | $opts = $args->options(); |
---|
303 | } |
---|
304 | else { |
---|
305 | $opts = array(); |
---|
306 | } |
---|
307 | |
---|
308 | $combo = array(); |
---|
309 | $combo[__('short')] = 'more'; |
---|
310 | $combo[__('extended')] = 'all'; |
---|
311 | |
---|
312 | $value = array_key_exists('tag_list_format',$opts) ? $opts['tag_list_format'] : 'more'; |
---|
313 | |
---|
314 | echo |
---|
315 | '<fieldset><legend>'.__('Tags').'</legend>'. |
---|
316 | '<p><label for="user_tag_list_format">'.__('Tags list format:').' '. |
---|
317 | form::combo('user_tag_list_format',$combo,$value). |
---|
318 | '</label></p></fieldset>'; |
---|
319 | } |
---|
320 | |
---|
321 | public static function setTagListFormat($cur,$user_id = null) |
---|
322 | { |
---|
323 | if (!is_null($user_id)) { |
---|
324 | $cur->user_options['tag_list_format'] = $_POST['user_tag_list_format']; |
---|
325 | } |
---|
326 | } |
---|
327 | } |
---|
328 | ?> |
---|