Dotclear

source: admin/media_item.php @ 947:983899baf3e7

Revision 947:983899baf3e7, 18.1 KB checked in by franck <carnet.franck.paul@…>, 11 years ago (diff)

Add delete button on media item page, fixes #1007

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2011 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
13require dirname(__FILE__).'/../inc/admin/prepend.php';
14
15dcPage::check('media,media_admin');
16
17$tab = empty($_REQUEST['tab']) ? '' : $_REQUEST['tab'];
18
19$post_id = !empty($_GET['post_id']) ? (integer) $_GET['post_id'] : null;
20if ($post_id) {
21     $post = $core->blog->getPosts(array('post_id'=>$post_id));
22     if ($post->isEmpty()) {
23          $post_id = null;
24     }
25     $post_title = $post->post_title;
26     unset($post);
27}
28
29$file = null;
30$popup = (integer) !empty($_GET['popup']);
31$page_url = 'media_item.php?popup='.$popup.'&post_id='.$post_id;
32$media_page_url = 'media.php?popup='.$popup.'&post_id='.$post_id;
33
34$id = !empty($_REQUEST['id']) ? (integer) $_REQUEST['id'] : '';
35
36if ($popup) {
37     $open_f = array('dcPage','openPopup');
38     $close_f = array('dcPage','closePopup');
39} else {
40     $open_f = array('dcPage','open');
41     $close_f = create_function('',"dcPage::helpBlock('core_media'); dcPage::close();");
42}
43
44$core_media_writable = false;
45try
46{
47     $core->media = new dcMedia($core);
48     
49     if ($id) {
50          $file = $core->media->getFile($id);
51     }
52     
53     if ($file === null) {
54          throw new Exception(__('Not a valid file'));
55     }
56     
57     $core->media->chdir(dirname($file->relname));
58     $core_media_writable = $core->media->writable();
59     
60     # Prepare directories combo box
61     $dirs_combo = array();
62     foreach ($core->media->getRootDirs() as $v) {
63          if ($v->w) {
64               $dirs_combo['/'.$v->relname] = $v->relname;
65          }
66     }
67     ksort($dirs_combo);
68}
69catch (Exception $e)
70{
71     $core->error->add($e->getMessage());
72}
73
74# Upload a new file
75if ($file && !empty($_FILES['upfile']) && $file->editable && $core_media_writable)
76{
77     try {
78          files::uploadStatus($_FILES['upfile']);
79          $core->media->uploadFile($_FILES['upfile']['tmp_name'],$file->basename,null,false,true);
80          http::redirect($page_url.'&id='.$id.'&fupl=1');
81     } catch (Exception $e) {
82          $core->error->add($e->getMessage());
83     }
84}
85
86# Update file
87if ($file && !empty($_POST['media_file']) && $file->editable && $core_media_writable)
88{
89     $newFile = clone $file;
90     
91     $newFile->basename = $_POST['media_file'];
92     
93     if ($_POST['media_path']) {
94          $newFile->dir = $_POST['media_path'];
95          $newFile->relname = $_POST['media_path'].'/'.$newFile->basename;
96     } else {
97          $newFile->dir = '';
98          $newFile->relname = $newFile->basename;
99     }
100     $newFile->media_title = $_POST['media_title'];
101     $newFile->media_dt = strtotime($_POST['media_dt']);
102     $newFile->media_dtstr = $_POST['media_dt'];
103     $newFile->media_priv = !empty($_POST['media_private']);
104     
105     try {
106          $core->media->updateFile($file,$newFile);
107          http::redirect($page_url.'&id='.$id.'&fupd=1&tab=media-details-tab');
108     } catch (Exception $e) {
109          $core->error->add($e->getMessage());
110     }
111}
112
113# Update thumbnails
114if (!empty($_POST['thumbs']) && $file->media_type == 'image' && $file->editable && $core_media_writable)
115{
116     try {
117          $foo = null;
118          $core->media->mediaFireRecreateEvent($file);
119          http::redirect($page_url.'&id='.$id.'&thumbupd=1&tab=media-details-tab');
120     } catch (Exception $e) {
121          $core->error->add($e->getMessage());
122     }
123}
124
125# Unzip file
126if (!empty($_POST['unzip']) && $file->type == 'application/zip' && $file->editable && $core_media_writable)
127{
128     try {
129          $unzip_dir = $core->media->inflateZipFile($file,$_POST['inflate_mode'] == 'new');
130          http::redirect($media_page_url.'&d='.$unzip_dir.'&unzipok=1');
131     } catch (Exception $e) {
132          $core->error->add($e->getMessage());
133     }
134}
135
136# Function to get image title based on meta
137function dcGetImageTitle($file,$pattern)
138{
139     $res = array();
140     $pattern = preg_split('/\s*;;\s*/',$pattern);
141     $sep = ', ';
142     
143     foreach ($pattern as $v) {
144          if ($v == 'Title') {
145               $res[] = $file->media_title;
146          } elseif ($file->media_meta->{$v}) {
147               $res[] = (string) $file->media_meta->{$v};
148          } elseif (preg_match('/^Date\((.+?)\)$/u',$v,$m)) {
149               $res[] = dt::str($m[1],$file->media_dt);
150          } elseif (preg_match('/^DateTimeOriginal\((.+?)\)$/u',$v,$m) && $file->media_meta->DateTimeOriginal) {
151               $res[] = dt::dt2str($m[1],(string) $file->media_meta->DateTimeOriginal);
152          } elseif (preg_match('/^separator\((.*?)\)$/u',$v,$m)) {
153               $sep = $m[1];
154          }
155     }
156     return implode($sep,$res);
157}
158
159/* DISPLAY Main page
160-------------------------------------------------------- */
161$starting_scripts = 
162     '<script type="text/javascript">'."\n".
163     "//<![CDATA["."\n".
164     dcPage::jsVar('dotclear.msg.confirm_delete_media',__('Are you sure to delete this media?'))."\n".
165     "//]]>".
166     "</script>".
167     dcPage::jsLoad('js/_media_item.js');
168if ($popup) {
169     $starting_scripts .=
170     dcPage::jsLoad('js/jsToolBar/popup_media.js');
171}
172call_user_func($open_f,__('Media manager'),
173     $starting_scripts.
174     dcPage::jsDatePicker().
175     dcPage::jsPageTabs($tab)
176);
177
178if ($file === null) {
179     call_user_func($close_f);
180     exit;
181}
182
183if (!empty($_GET['fupd']) || !empty($_GET['fupl'])) {
184     dcPage::message(__('File has been successfully updated.'));
185}
186if (!empty($_GET['thumbupd'])) {
187     dcPage::message(__('Thumbnails have been successfully updated.'));
188}
189
190echo '<h2><a href="'.html::escapeURL($media_page_url).'">'.__('Media manager').'</a>'.
191' / '.$core->media->breadCrumb(html::escapeURL($media_page_url).'&amp;d=%s').
192'<span class="page-title">'.$file->basename.'</span></h2>';
193
194# Insertion popup
195if ($popup)
196{
197     $media_desc = $file->media_title;
198     
199     echo
200     '<div id="media-insert" class="multi-part" title="'.__('Insert media item').'">'.
201     '<form id="media-insert-form" action="" method="get">';
202     
203     if ($file->media_type == 'image')
204     {
205          $media_type = 'image';
206          $media_desc = dcGetImageTitle($file,$core->blog->settings->system->media_img_title_pattern);
207          if ($media_desc == $file->basename) {
208               $media_desc = '';
209          }
210
211          $media_img_default_size = $core->blog->settings->system->media_img_default_size;
212          if ($media_img_default_size == '') {
213               $media_img_default_size = 'm';
214          }
215          $media_img_default_alignment = $core->blog->settings->system->media_img_default_alignment;
216          if ($media_img_default_alignment == '') {
217               $media_img_default_alignment = 'none';
218          }
219          $media_img_default_link = (boolean)$core->blog->settings->system->media_img_default_link;
220
221          echo
222          '<h3>'.__('Image size:').'</h3> ';
223         
224          $s_checked = false;
225          echo '<p>';
226          foreach (array_reverse($file->media_thumb) as $s => $v) {
227               $s_checked = ($s == $media_img_default_size);
228               echo '<label class="classic">'.
229               form::radio(array('src'),html::escapeHTML($v),$s_checked).' '.
230               $core->media->thumb_sizes[$s][2].'</label><br /> ';
231          }
232          $s_checked = (!isset($file->media_thumb[$media_img_default_size]));
233          echo '<label class="classic">'.
234          form::radio(array('src'),$file->file_url,$s_checked).' '.__('original').'</label><br /> ';
235          echo '</p>';
236         
237         
238          echo '<h3>'.__('Image alignment').'</h3>';
239          $i_align = array(
240               'none' => array(__('None'),($media_img_default_alignment == 'none' ? 1 : 0)),
241               'left' => array(__('Left'),($media_img_default_alignment == 'left' ? 1 : 0)),
242               'right' => array(__('Right'),($media_img_default_alignment == 'right' ? 1 : 0)),
243               'center' => array(__('Center'),($media_img_default_alignment == 'center' ? 1 : 0))
244          );
245         
246          echo '<p>';
247          foreach ($i_align as $k => $v) {
248               echo '<label class="classic">'.
249               form::radio(array('alignment'),$k,$v[1]).' '.$v[0].'</label><br /> ';
250          }
251          echo '</p>';
252         
253          echo
254          '<h3>'.__('Image insertion').'</h3>'.
255          '<p>'.
256          '<label for="insert1" class="classic">'.form::radio(array('insertion','insert1'),'simple',!$media_img_default_link).
257          __('As a single image').'</label><br />'.
258          '<label for="insert2" class="classic">'.form::radio(array('insertion','insert2'),'link',$media_img_default_link).
259          __('As a link to original image').'</label>'.
260          '</p>';
261     }
262     elseif ($file->type == 'audio/mpeg3')
263     {
264          $media_type = 'mp3';
265         
266          echo '<h3>'.__('MP3 disposition').'</h3>';
267          dcPage::message(__("Please note that you cannot insert mp3 files with visual editor."),false);
268         
269          $i_align = array(
270               'none' => array(__('None'),0),
271               'left' => array(__('Left'),0),
272               'right' => array(__('Right'),0),
273               'center' => array(__('Center'),1)
274          );
275         
276          echo '<p>';
277          foreach ($i_align as $k => $v) {
278               echo '<label for="alignment" class="classic">'.
279               form::radio(array('alignment'),$k,$v[1]).' '.$v[0].'</label><br /> ';
280          }
281         
282          $public_player_style = unserialize($core->blog->settings->themes->mp3player_style);
283          $public_player = dcMedia::mp3player($file->file_url,$core->blog->getQmarkURL().'pf=player_mp3.swf',$public_player_style);
284          echo form::hidden('public_player',html::escapeHTML($public_player));
285          echo '</p>';
286     }
287     elseif ($file->type == 'video/x-flv' || $file->type == 'video/mp4' || $file->type == 'video/x-m4v')
288     {
289          $media_type = 'flv';
290         
291          dcPage::message(__("Please note that you cannot insert video files with visual editor."),false);
292         
293          echo
294          '<h3>'.__('Video size').'</h3>'.
295          '<p><label for="video_w" class="classic">'.__('Width:').' '.
296          form::field('video_w',3,4,400).'  '.
297          '<label for="video_h" class="classic">'.__('Height:').' '.
298          form::field('video_h',3,4,300).
299          '</p>';
300         
301          echo '<h3>'.__('Video disposition').'</h3>';
302         
303          $i_align = array(
304               'none' => array(__('None'),0),
305               'left' => array(__('Left'),0),
306               'right' => array(__('Right'),0),
307               'center' => array(__('Center'),1)
308          );
309         
310          echo '<p>';
311          foreach ($i_align as $k => $v) {
312               echo '<label for="alignment" class="classic">'.
313               form::radio(array('alignment'),$k,$v[1]).' '.$v[0].'</label><br /> ';
314          }
315         
316          $public_player_style = unserialize($core->blog->settings->themes->flvplayer_style);
317          $public_player = dcMedia::flvplayer($file->file_url,$core->blog->getQmarkURL().'pf=player_flv.swf',$public_player_style);
318          echo form::hidden('public_player',html::escapeHTML($public_player));
319          echo '</p>';
320     }
321     else
322     {
323          $media_type = 'default';
324          echo '<p>'.__('Media item will be inserted as a link.').'</p>';
325     }
326     
327     echo
328     '<p><a id="media-insert-cancel" class="button" href="#">'.__('Cancel').'</a> - '.
329     '<strong><a id="media-insert-ok" class="button" href="#">'.__('Insert').'</a></strong>'.
330     form::hidden(array('type'),html::escapeHTML($media_type)).
331     form::hidden(array('title'),html::escapeHTML($file->media_title)).
332     form::hidden(array('description'),html::escapeHTML($media_desc)).
333     form::hidden(array('url'),$file->file_url).
334     '</p>';
335     
336     echo '</form></div>';
337}
338
339echo
340'<div class="multi-part" title="'.__('Media details').'" id="media-details-tab">'.
341'<p id="media-icon"><img src="'.$file->media_icon.'" alt="" /></p>';
342
343echo
344'<div id="media-details">';
345
346if ($file->media_image)
347{
348     $thumb_size = !empty($_GET['size']) ? $_GET['size'] : 's';
349     
350     if (!isset($core->media->thumb_sizes[$thumb_size]) && $thumb_size != 'o') {
351          $thumb_size = 's';
352     }
353     
354     echo '<p>'.__('Available sizes:').' ';
355     foreach (array_reverse($file->media_thumb) as $s => $v)
356     {
357          $strong_link = ($s == $thumb_size) ? '<strong>%s</strong>' : '%s';
358          printf($strong_link,'<a href="'.html::escapeURL($page_url).
359          '&amp;id='.$id.'&amp;size='.$s.'&amp;tab=media-details-tab">'.$core->media->thumb_sizes[$s][2].'</a> | ');
360     }
361     echo '<a href="'.html::escapeURL($page_url).'&amp;id='.$id.'&amp;size=o&amp;tab=media-details-tab">'.__('original').'</a>';
362     echo '</p>';
363     
364     if (isset($file->media_thumb[$thumb_size])) {
365          echo '<p><img src="'.$file->media_thumb[$thumb_size].'" alt="" /></p>';
366     } elseif ($thumb_size == 'o') {
367          $S = getimagesize($file->file);
368          $class = ($S[1] > 500) ? ' class="overheight"' : '';
369          unset($S);
370          echo '<p id="media-original-image"'.$class.'><img src="'.$file->file_url.'" alt="" /></p>';
371     }
372}
373
374if ($file->type == 'audio/mpeg3')
375{
376     echo dcMedia::mp3player($file->file_url,'index.php?pf=player_mp3.swf');
377}
378
379if ($file->type == 'video/x-flv' || $file->type == 'video/mp4' || $file->type == 'video/x-m4v')
380{
381     echo dcMedia::flvplayer($file->file_url,'index.php?pf=player_flv.swf');
382}
383
384echo
385'<h3>'.__('Media details').'</h3>'.
386'<ul>'.
387     '<li><strong>'.__('File owner:').'</strong> '.$file->media_user.'</li>'.
388     '<li><strong>'.__('File type:').'</strong> '.$file->type.'</li>'.
389     '<li><strong>'.__('File size:').'</strong> '.files::size($file->size).'</li>'.
390     '<li><strong>'.__('File URL:').'</strong> <a href="'.$file->file_url.'">'.$file->file_url.'</a></li>'.
391'</ul>';
392
393if (empty($_GET['find_posts']))
394{
395     echo
396     '<p><strong><a href="'.html::escapeHTML($page_url).'&amp;id='.$id.'&amp;find_posts=1&amp;tab=media-details-tab">'.
397     __('Show entries containing this media').'</a></strong></p>';
398}
399else
400{
401     echo '<h3>'.__('Entries containing this media').'</h3>';
402     $params = array(
403          'post_type' => '',
404          'from' => 'LEFT OUTER JOIN '.$core->prefix.'post_media PM ON P.post_id = PM.post_id ',
405          'sql' => 'AND ('.
406               'PM.media_id = '.(integer) $id.' '.
407               "OR post_content_xhtml LIKE '%".$core->con->escape($file->relname)."%' ".
408               "OR post_excerpt_xhtml LIKE '%".$core->con->escape($file->relname)."%' "
409     );
410     
411     if ($file->media_image)
412     { # We look for thumbnails too
413          if (preg_match('#^http(s)?://#',$core->blog->settings->system->public_url)) {
414               $media_root = $core->blog->settings->system->public_url;
415          } else {
416               $media_root = $core->blog->host.path::clean($core->blog->settings->system->public_url).'/';
417          }
418          foreach ($file->media_thumb as $v) {
419               $v = preg_replace('/^'.preg_quote($media_root,'/').'/','',$v);
420               $params['sql'] .= "OR post_content_xhtml LIKE '%".$core->con->escape($v)."%' ";
421               $params['sql'] .= "OR post_excerpt_xhtml LIKE '%".$core->con->escape($v)."%' ";
422          }
423     }
424     
425     $params['sql'] .= ') ';
426     
427     $rs = $core->blog->getPosts($params);
428     
429     if ($rs->isEmpty())
430     {
431          echo '<p>'.__('No entry seems contain this media.').'</p>';
432     }
433     else
434     {
435          echo '<ul>';
436          while ($rs->fetch()) {
437               $img = '<img alt="%1$s" title="%1$s" src="images/%2$s" />';
438               switch ($rs->post_status) {
439                    case 1:
440                         $img_status = sprintf($img,__('published'),'check-on.png');
441                         break;
442                    case 0:
443                         $img_status = sprintf($img,__('unpublished'),'check-off.png');
444                         break;
445                    case -1:
446                         $img_status = sprintf($img,__('scheduled'),'scheduled.png');
447                         break;
448                    case -2:
449                         $img_status = sprintf($img,__('pending'),'check-wrn.png');
450                         break;
451               }
452               echo '<li>'.$img_status.' '.'<a href="'.$core->getPostAdminURL($rs->post_type,$rs->post_id).'">'.
453                    $rs->post_title.'</a>'.
454                    ($rs->post_type != 'post' ? ' ('.html::escapeHTML($rs->post_type).')' : '').
455                    ' - '.dt::dt2str(__('%Y-%m-%d %H:%M'),$rs->post_dt).'</li>';
456          }
457          echo '</ul>';
458     }
459}
460
461if ($file->type == 'image/jpeg')
462{
463     echo '<h3>'.__('Image details').'</h3>';
464     
465     if (count($file->media_meta) == 0)
466     {
467          echo '<p>'.__('No detail').'</p>';
468     }
469     else
470     {
471          echo '<ul>';
472          foreach ($file->media_meta as $k => $v)
473          {
474               if ((string) $v) {
475                    echo '<li><strong>'.$k.':</strong> '.html::escapeHTML($v).'</li>';
476               }
477          }
478          echo '</ul>';
479     }
480}
481
482if ($file->editable && $core_media_writable)
483{
484     if ($file->media_type == 'image')
485     {
486          echo
487          '<form class="clear" action="'.html::escapeURL($page_url).'" method="post">'.
488          '<fieldset><legend>'.__('Update thumbnails').'</legend>'.
489          '<p>'.__('This will create or update thumbnails for this image.').'</p>'.
490          '<p><input type="submit" name="thumbs" value="'.__('Update thumbnails').'" />'.
491          form::hidden(array('id'),$id).
492          $core->formNonce().'</p>'.
493          '</fieldset></form>';
494     }
495     
496     if ($file->type == 'application/zip')
497     {
498          $inflate_combo = array(
499               __('Extract in a new directory') => 'new',
500               __('Extract in current directory') => 'current'
501          );
502         
503          echo
504          '<form class="clear" id="file-unzip" action="'.html::escapeURL($page_url).'" method="post">'.
505          '<fieldset><legend>'.__('Extract archive').'</legend>'.
506          '<ul>'.
507          '<li><strong>'.__('Extract in a new directory').'</strong> : '.
508          __('This will extract archive in a new directory that should not exist yet.').'</li>'.
509          '<li><strong>'.__('Extract in current directory').'</strong> : '.
510          __('This will extract archive in current directory and will overwrite existing files or directory.').'</li>'.
511          '</ul>'.
512          '<p><label for="inflate_mode" class="classic">'.__('Extract mode:').' '.
513          form::combo('inflate_mode',$inflate_combo,'new').'</label> '.
514          '<input type="submit" name="unzip" value="'.__('Extract').'" />'.
515          form::hidden(array('id'),$id).
516          $core->formNonce().'</p>'.
517          '</fieldset></form>';
518     }
519     
520     echo
521     '<form class="clear" action="'.html::escapeURL($page_url).'" method="post">'.
522     '<fieldset><legend>'.__('Change media properties').'</legend>'.
523     '<p><label for="media_file">'.__('File name:').
524     form::field('media_file',30,255,html::escapeHTML($file->basename)).'</label></p>'.
525     '<p><label for="media_title">'.__('File title:').
526     form::field('media_title',30,255,html::escapeHTML($file->media_title)).'</label></p>'.
527     '<p><label for="media_dt">'.__('File date:').
528     form::field('media_dt',16,16,html::escapeHTML($file->media_dtstr)).'</label></p>'.
529     '<p><label for="media_private" class="classic">'.form::checkbox('media_private',1,$file->media_priv).' '.
530     __('Private').'</label></p>'.
531     '<p><label for="media_path">'.__('New directory:').
532     form::combo('media_path',$dirs_combo,dirname($file->relname)).'</label></p>'.
533     '<p><input type="submit" accesskey="s" value="'.__('Save').'" />'.
534     form::hidden(array('id'),$id).
535     $core->formNonce().'</p>'.
536     '</fieldset></form>';
537     
538     echo
539     '<form class="clear" action="'.html::escapeURL($page_url).'" method="post" enctype="multipart/form-data">'.
540     '<fieldset><legend>'.__('Change file').'</legend>'.
541     '<div>'.form::hidden(array('MAX_FILE_SIZE'),DC_MAX_UPLOAD_SIZE).'</div>'.
542     '<p><label for="upfile">'.__('Choose a file:').
543     ' ('.sprintf(__('Maximum size %s'),files::size(DC_MAX_UPLOAD_SIZE)).') '.
544     '<input type="file" id="upfile" name="upfile" size="35" />'.
545     '</label></p>'.
546     '<p><input type="submit" value="'.__('Send').'" />'.
547     form::hidden(array('id'),$id).
548     $core->formNonce().'</p>'.
549     '</fieldset></form>';
550
551     if ($file->del) {
552          echo
553          '<form id="delete-form" method="post" action="'.html::escapeURL($media_page_url).
554          '&amp;d='.rawurlencode(dirname($file->relname)).
555          '&amp;remove='.rawurlencode($file->basename).'">'.
556          '<p><input name="delete" type="submit" class="delete" value="'.__('Delete this media').'" />'.
557          form::hidden('remove',rawurlencode($file->basename)).
558          form::hidden('rmyes',1).
559          $core->formNonce().'</p>'.
560          '</form>';
561     }
562
563
564     # --BEHAVIOR-- adminMediaItemForm
565     $core->callBehavior('adminMediaItemForm',$file);
566}
567
568echo
569'</div>'.
570'</div>';
571
572call_user_func($close_f);
573?>
Note: See TracBrowser for help on using the repository browser.

Sites map