Dotclear

source: admin/media_item.php @ 887:c8f4fe2ab8a1

Revision 887:c8f4fe2ab8a1, 17.0 KB checked in by franck <carnet.franck.paul@…>, 13 years ago (diff)

Add default attributes settings for image insertion

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 = dcPage::jsLoad('js/_media_item.js');
162if ($popup) {
163     $starting_scripts .=
164     dcPage::jsLoad('js/jsToolBar/popup_media.js');
165}
166call_user_func($open_f,__('Media manager'),
167     $starting_scripts.
168     dcPage::jsDatePicker().
169     dcPage::jsPageTabs($tab)
170);
171
172if ($file === null) {
173     call_user_func($close_f);
174     exit;
175}
176
177if (!empty($_GET['fupd']) || !empty($_GET['fupl'])) {
178     echo '<p class="message">'.__('File has been successfully updated.').'</p>';
179}
180if (!empty($_GET['thumbupd'])) {
181     echo '<p class="message">'.__('Thumbnails have been successfully updated.').'</p>';
182}
183
184echo '<h2><a href="'.html::escapeURL($media_page_url).'">'.__('Media manager').'</a>'.
185' / '.$core->media->breadCrumb(html::escapeURL($media_page_url).'&amp;d=%s').
186'<span class="page-title">'.$file->basename.'</span></h2>';
187
188# Insertion popup
189if ($popup)
190{
191     $media_desc = $file->media_title;
192     
193     echo
194     '<div id="media-insert" class="multi-part" title="'.__('Insert media item').'">'.
195     '<form id="media-insert-form" action="" method="get">';
196     
197     if ($file->media_type == 'image')
198     {
199          $media_type = 'image';
200          $media_desc = dcGetImageTitle($file,$core->blog->settings->system->media_img_title_pattern);
201          if ($media_desc == $file->basename) {
202               $media_desc = '';
203          }
204
205          $media_img_default_size = $core->blog->settings->system->media_img_default_size;
206          if ($media_img_default_size == '') {
207               $media_img_default_size = 'm';
208          }
209          $media_img_default_alignment = $core->blog->settings->system->media_img_default_alignment;
210          if ($media_img_default_alignment == '') {
211               $media_img_default_alignment = 'none';
212          }
213          $media_img_default_link = (boolean)$core->blog->settings->system->media_img_default_link;
214
215          echo
216          '<h3>'.__('Image size:').'</h3> ';
217         
218          $s_checked = false;
219          echo '<p>';
220          foreach (array_reverse($file->media_thumb) as $s => $v) {
221               $s_checked = ($s == $media_img_default_size);
222               echo '<label class="classic">'.
223               form::radio(array('src'),html::escapeHTML($v),$s_checked).' '.
224               $core->media->thumb_sizes[$s][2].'</label><br /> ';
225          }
226          $s_checked = (!isset($file->media_thumb[$media_img_default_size]));
227          echo '<label class="classic">'.
228          form::radio(array('src'),$file->file_url,$s_checked).' '.__('original').'</label><br /> ';
229          echo '</p>';
230         
231         
232          echo '<h3>'.__('Image alignment').'</h3>';
233          $i_align = array(
234               'none' => array(__('None'),($media_img_default_alignment == 'none' ? 1 : 0)),
235               'left' => array(__('Left'),($media_img_default_alignment == 'left' ? 1 : 0)),
236               'right' => array(__('Right'),($media_img_default_alignment == 'right' ? 1 : 0)),
237               'center' => array(__('Center'),($media_img_default_alignment == 'center' ? 1 : 0))
238          );
239         
240          echo '<p>';
241          foreach ($i_align as $k => $v) {
242               echo '<label class="classic">'.
243               form::radio(array('alignment'),$k,$v[1]).' '.$v[0].'</label><br /> ';
244          }
245          echo '</p>';
246         
247          echo
248          '<h3>'.__('Image insertion').'</h3>'.
249          '<p>'.
250          '<label for="insert1" class="classic">'.form::radio(array('insertion','insert1'),'simple',!$media_img_default_link).
251          __('As a single image').'</label><br />'.
252          '<label for="insert2" class="classic">'.form::radio(array('insertion','insert2'),'link',$media_img_default_link).
253          __('As a link to original image').'</label>'.
254          '</p>';
255     }
256     elseif ($file->type == 'audio/mpeg3')
257     {
258          $media_type = 'mp3';
259         
260          echo '<h3>'.__('MP3 disposition').'</h3>'.
261          '<p class="message">'.__("Please note that you cannot insert mp3 files with visual editor.").'</p>';
262         
263          $i_align = array(
264               'none' => array(__('None'),0),
265               'left' => array(__('Left'),0),
266               'right' => array(__('Right'),0),
267               'center' => array(__('Center'),1)
268          );
269         
270          echo '<p>';
271          foreach ($i_align as $k => $v) {
272               echo '<label for="alignment" class="classic">'.
273               form::radio(array('alignment'),$k,$v[1]).' '.$v[0].'</label><br /> ';
274          }
275         
276          $public_player_style = unserialize($core->blog->settings->themes->mp3player_style);
277          $public_player = dcMedia::mp3player($file->file_url,$core->blog->getQmarkURL().'pf=player_mp3.swf',$public_player_style);
278          echo form::hidden('public_player',html::escapeHTML($public_player));
279          echo '</p>';
280     }
281     elseif ($file->type == 'video/x-flv' || $file->type == 'video/mp4' || $file->type == 'video/x-m4v')
282     {
283          $media_type = 'flv';
284         
285          echo
286          '<p class="message">'.__("Please note that you cannot insert video files with visual editor.").'</p>';
287         
288          echo
289          '<h3>'.__('Video size').'</h3>'.
290          '<p><label for="video_w" class="classic">'.__('Width:').' '.
291          form::field('video_w',3,4,400).'  '.
292          '<label for="video_h" class="classic">'.__('Height:').' '.
293          form::field('video_h',3,4,300).
294          '</p>';
295         
296          echo '<h3>'.__('Video disposition').'</h3>';
297         
298          $i_align = array(
299               'none' => array(__('None'),0),
300               'left' => array(__('Left'),0),
301               'right' => array(__('Right'),0),
302               'center' => array(__('Center'),1)
303          );
304         
305          echo '<p>';
306          foreach ($i_align as $k => $v) {
307               echo '<label for="alignment" class="classic">'.
308               form::radio(array('alignment'),$k,$v[1]).' '.$v[0].'</label><br /> ';
309          }
310         
311          $public_player_style = unserialize($core->blog->settings->themes->flvplayer_style);
312          $public_player = dcMedia::flvplayer($file->file_url,$core->blog->getQmarkURL().'pf=player_flv.swf',$public_player_style);
313          echo form::hidden('public_player',html::escapeHTML($public_player));
314          echo '</p>';
315     }
316     else
317     {
318          $media_type = 'default';
319          echo '<p>'.__('Media item will be inserted as a link.').'</p>';
320     }
321     
322     echo
323     '<p><a id="media-insert-cancel" class="button" href="#">'.__('Cancel').'</a> - '.
324     '<strong><a id="media-insert-ok" class="button" href="#">'.__('Insert').'</a></strong>'.
325     form::hidden(array('type'),html::escapeHTML($media_type)).
326     form::hidden(array('title'),html::escapeHTML($file->media_title)).
327     form::hidden(array('description'),html::escapeHTML($media_desc)).
328     form::hidden(array('url'),$file->file_url).
329     '</p>';
330     
331     echo '</form></div>';
332}
333
334echo
335'<div class="multi-part" title="'.__('Media details').'" id="media-details-tab">'.
336'<p id="media-icon"><img src="'.$file->media_icon.'" alt="" /></p>';
337
338echo
339'<div id="media-details">';
340
341if ($file->media_image)
342{
343     $thumb_size = !empty($_GET['size']) ? $_GET['size'] : 's';
344     
345     if (!isset($core->media->thumb_sizes[$thumb_size]) && $thumb_size != 'o') {
346          $thumb_size = 's';
347     }
348     
349     echo '<p>'.__('Available sizes:').' ';
350     foreach (array_reverse($file->media_thumb) as $s => $v)
351     {
352          $strong_link = ($s == $thumb_size) ? '<strong>%s</strong>' : '%s';
353          printf($strong_link,'<a href="'.html::escapeURL($page_url).
354          '&amp;id='.$id.'&amp;size='.$s.'&amp;tab=media-details-tab">'.$core->media->thumb_sizes[$s][2].'</a> | ');
355     }
356     echo '<a href="'.html::escapeURL($page_url).'&amp;id='.$id.'&amp;size=o&amp;tab=media-details-tab">'.__('original').'</a>';
357     echo '</p>';
358     
359     if (isset($file->media_thumb[$thumb_size])) {
360          echo '<p><img src="'.$file->media_thumb[$thumb_size].'" alt="" /></p>';
361     } elseif ($thumb_size == 'o') {
362          $S = getimagesize($file->file);
363          $class = ($S[1] > 500) ? ' class="overheight"' : '';
364          unset($S);
365          echo '<p id="media-original-image"'.$class.'><img src="'.$file->file_url.'" alt="" /></p>';
366     }
367}
368
369if ($file->type == 'audio/mpeg3')
370{
371     echo dcMedia::mp3player($file->file_url,'index.php?pf=player_mp3.swf');
372}
373
374if ($file->type == 'video/x-flv' || $file->type == 'video/mp4' || $file->type == 'video/x-m4v')
375{
376     echo dcMedia::flvplayer($file->file_url,'index.php?pf=player_flv.swf');
377}
378
379echo
380'<h3>'.__('Media details').'</h3>'.
381'<ul>'.
382     '<li><strong>'.__('File owner:').'</strong> '.$file->media_user.'</li>'.
383     '<li><strong>'.__('File type:').'</strong> '.$file->type.'</li>'.
384     '<li><strong>'.__('File size:').'</strong> '.files::size($file->size).'</li>'.
385     '<li><strong>'.__('File URL:').'</strong> <a href="'.$file->file_url.'">'.$file->file_url.'</a></li>'.
386'</ul>';
387
388if (empty($_GET['find_posts']))
389{
390     echo
391     '<p><strong><a href="'.html::escapeHTML($page_url).'&amp;id='.$id.'&amp;find_posts=1&amp;tab=media-details-tab">'.
392     __('Show entries containing this media').'</a></strong></p>';
393}
394else
395{
396     echo '<h3>'.__('Entries containing this media').'</h3>';
397     $params = array(
398          'post_type' => '',
399          'from' => 'LEFT OUTER JOIN '.$core->prefix.'post_media PM ON P.post_id = PM.post_id ',
400          'sql' => 'AND ('.
401               'PM.media_id = '.(integer) $id.' '.
402               "OR post_content_xhtml LIKE '%".$core->con->escape($file->relname)."%' ".
403               "OR post_excerpt_xhtml LIKE '%".$core->con->escape($file->relname)."%' "
404     );
405     
406     if ($file->media_image)
407     { # We look for thumbnails too
408          if (preg_match('#^http(s)?://#',$core->blog->settings->system->public_url)) {
409               $media_root = $core->blog->settings->system->public_url;
410          } else {
411               $media_root = $core->blog->host.path::clean($core->blog->settings->system->public_url).'/';
412          }
413          foreach ($file->media_thumb as $v) {
414               $v = preg_replace('/^'.preg_quote($media_root,'/').'/','',$v);
415               $params['sql'] .= "OR post_content_xhtml LIKE '%".$core->con->escape($v)."%' ";
416               $params['sql'] .= "OR post_excerpt_xhtml LIKE '%".$core->con->escape($v)."%' ";
417          }
418     }
419     
420     $params['sql'] .= ') ';
421     
422     $rs = $core->blog->getPosts($params);
423     
424     if ($rs->isEmpty())
425     {
426          echo '<p>'.__('No entry seems contain this media.').'</p>';
427     }
428     else
429     {
430          echo '<ul>';
431          while ($rs->fetch()) {
432               echo '<li><a href="'.$core->getPostAdminURL($rs->post_type,$rs->post_id).'">'.
433               $rs->post_title.'</a>'.
434               ($rs->post_type != 'post' ? ' ('.html::escapeHTML($rs->post_type).')' : '').
435               ' - '.dt::dt2str(__('%Y-%m-%d %H:%M'),$rs->post_dt).'</li>';
436          }
437          echo '</ul>';
438     }
439}
440
441if ($file->type == 'image/jpeg')
442{
443     echo '<h3>'.__('Image details').'</h3>';
444     
445     if (count($file->media_meta) == 0)
446     {
447          echo '<p>'.__('No detail').'</p>';
448     }
449     else
450     {
451          echo '<ul>';
452          foreach ($file->media_meta as $k => $v)
453          {
454               if ((string) $v) {
455                    echo '<li><strong>'.$k.':</strong> '.html::escapeHTML($v).'</li>';
456               }
457          }
458          echo '</ul>';
459     }
460}
461
462if ($file->editable && $core_media_writable)
463{
464     if ($file->media_type == 'image')
465     {
466          echo
467          '<form class="clear" action="'.html::escapeURL($page_url).'" method="post">'.
468          '<fieldset><legend>'.__('Update thumbnails').'</legend>'.
469          '<p>'.__('This will create or update thumbnails for this image.').'</p>'.
470          '<p><input type="submit" name="thumbs" value="'.__('Update thumbnails').'" />'.
471          form::hidden(array('id'),$id).
472          $core->formNonce().'</p>'.
473          '</fieldset></form>';
474     }
475     
476     if ($file->type == 'application/zip')
477     {
478          $inflate_combo = array(
479               __('Extract in a new directory') => 'new',
480               __('Extract in current directory') => 'current'
481          );
482         
483          echo
484          '<form class="clear" id="file-unzip" action="'.html::escapeURL($page_url).'" method="post">'.
485          '<fieldset><legend>'.__('Extract archive').'</legend>'.
486          '<ul>'.
487          '<li><strong>'.__('Extract in a new directory').'</strong> : '.
488          __('This will extract archive in a new directory that should not exist yet.').'</li>'.
489          '<li><strong>'.__('Extract in current directory').'</strong> : '.
490          __('This will extract archive in current directory and will overwrite existing files or directory.').'</li>'.
491          '</ul>'.
492          '<p><label for="inflate_mode" class="classic">'.__('Extract mode:').' '.
493          form::combo('inflate_mode',$inflate_combo,'new').'</label> '.
494          '<input type="submit" name="unzip" value="'.__('Extract').'" />'.
495          form::hidden(array('id'),$id).
496          $core->formNonce().'</p>'.
497          '</fieldset></form>';
498     }
499     
500     echo
501     '<form class="clear" action="'.html::escapeURL($page_url).'" method="post">'.
502     '<fieldset><legend>'.__('Change media properties').'</legend>'.
503     '<p><label for="media_file">'.__('File name:').
504     form::field('media_file',30,255,html::escapeHTML($file->basename)).'</label></p>'.
505     '<p><label for="media_title">'.__('File title:').
506     form::field('media_title',30,255,html::escapeHTML($file->media_title)).'</label></p>'.
507     '<p><label for="media_dt">'.__('File date:').
508     form::field('media_dt',16,16,html::escapeHTML($file->media_dtstr)).'</label></p>'.
509     '<p><label for="media_private" class="classic">'.form::checkbox('media_private',1,$file->media_priv).' '.
510     __('Private').'</label></p>'.
511     '<p><label for="media_path">'.__('New directory:').
512     form::combo('media_path',$dirs_combo,dirname($file->relname)).'</label></p>'.
513     '<p><input type="submit" accesskey="s" value="'.__('Save').'" />'.
514     form::hidden(array('id'),$id).
515     $core->formNonce().'</p>'.
516     '</fieldset></form>';
517     
518     echo
519     '<form class="clear" action="'.html::escapeURL($page_url).'" method="post" enctype="multipart/form-data">'.
520     '<fieldset><legend>'.__('Change file').'</legend>'.
521     '<div>'.form::hidden(array('MAX_FILE_SIZE'),DC_MAX_UPLOAD_SIZE).'</div>'.
522     '<p><label for="upfile">'.__('Choose a file:').
523     ' ('.sprintf(__('Maximum size %s'),files::size(DC_MAX_UPLOAD_SIZE)).') '.
524     '<input type="file" id="upfile" name="upfile" size="35" />'.
525     '</label></p>'.
526     '<p><input type="submit" value="'.__('Send').'" />'.
527     form::hidden(array('id'),$id).
528     $core->formNonce().'</p>'.
529     '</fieldset></form>';
530
531     # --BEHAVIOR-- adminMediaItemForm
532     $core->callBehavior('adminMediaItemForm',$file);
533}
534
535echo
536'</div>'.
537'</div>';
538
539call_user_func($close_f);
540?>
Note: See TracBrowser for help on using the repository browser.

Sites map