source: extensions/upload_form/upload.php @ 4811

Last change on this file since 4811 was 4811, checked in by plg, 14 years ago

feature 1412 added: link to bulk management after upload

File size: 10.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2010      Pierrick LE GALL             http://piwigo.org |
6// +-----------------------------------------------------------------------+
7// | This program is free software; you can redistribute it and/or modify  |
8// | it under the terms of the GNU General Public License as published by  |
9// | the Free Software Foundation                                          |
10// |                                                                       |
11// | This program is distributed in the hope that it will be useful, but   |
12// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
13// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
14// | General Public License for more details.                              |
15// |                                                                       |
16// | You should have received a copy of the GNU General Public License     |
17// | along with this program; if not, write to the Free Software           |
18// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
19// | USA.                                                                  |
20// +-----------------------------------------------------------------------+
21
22if( !defined("PHPWG_ROOT_PATH") )
23{
24  die ("Hacking attempt!");
25}
26
27include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
28include_once(UPLOAD_FORM_PATH.'include/functions_upload.inc.php');
29load_language('plugin.lang', UPLOAD_FORM_PATH);
30
31$admin_base_url = get_root_url().'admin.php?page=plugin&section=upload_form%2Fupload.php';
32
33// +-----------------------------------------------------------------------+
34// | Check Access and exit when user status is not ok                      |
35// +-----------------------------------------------------------------------+
36check_status(ACCESS_ADMINISTRATOR);
37
38// +-----------------------------------------------------------------------+
39// |                        batch management request                       |
40// +-----------------------------------------------------------------------+
41
42if (isset($_GET['batch']))
43{
44  check_input_parameter('batch', $_GET['batch'], false, '/^\d+(,\d+)*$/');
45
46  $query = '
47DELETE FROM '.CADDIE_TABLE.'
48  WHERE user_id = '.$user['id'].'
49;';
50  pwg_query($query);
51
52  $inserts = array();
53  foreach (explode(',', $_GET['batch']) as $image_id)
54  {
55    array_push(
56      $inserts,
57      array(
58        'user_id' => $user['id'],
59        'element_id' => $image_id,
60        )
61      );
62  }
63  mass_inserts(
64    CADDIE_TABLE,
65    array_keys($inserts[0]),
66    $inserts
67    );
68
69  redirect(get_root_url().'admin.php?page=element_set&cat=caddie');
70}
71
72// +-----------------------------------------------------------------------+
73// |                             process form                              |
74// +-----------------------------------------------------------------------+
75
76if (isset($_POST['submit_upload']))
77{
78  $category_id = null;
79  if ('existing' == $_POST['category_type'])
80  {
81    $category_id = $_POST['category'];
82  }
83  elseif ('new' == $_POST['category_type'])
84  {
85    $output_create = create_virtual_category(
86      $_POST['category_name'],
87      (0 == $_POST['category_parent'] ? null : $_POST['category_parent'])
88      );
89   
90    $category_id = $output_create['id'];
91
92    if (isset($output_create['error']))
93    {
94      array_push($page['errors'], $output_create['error']);
95    }
96    else
97    {
98      $category_name = get_cat_display_name_from_id($category_id, 'admin.php?page=cat_modify&amp;cat_id=');
99      // information
100      array_push(
101        $page['infos'],
102        sprintf(
103          l10n('Category "%s" has been added'),
104          '<em>'.$category_name.'</em>'
105          )
106        );
107      // TODO: add the onclick="window.open(this.href); return false;"
108      // attribute with jQuery on upload.tpl side for href containing
109      // "cat_modify"
110    }
111  }
112
113  $image_ids = array();
114  $page['thumbnails'] = array();
115     
116  $starttime = get_moment();
117 
118  foreach ($_FILES['image_upload']['error'] as $idx => $error)
119  {
120    if (UPLOAD_ERR_OK == $error)
121    {
122      $source_filepath = $_FILES['image_upload']['tmp_name'][$idx];
123      $original_filename = $_FILES['image_upload']['name'][$idx];
124     
125      $image_id = add_uploaded_file(
126        $source_filepath,
127        $original_filename,
128        array($category_id),
129        $_POST['level']
130        );
131
132      array_push($image_ids, $image_id);
133
134      // TODO: if $image_id is not an integer, something went wrong
135
136      // we could return the list of properties from the add_uploaded_file
137      // function, but I like the "double check". And it costs nothing
138      // compared to the upload process.
139      $thumbnail = array();
140     
141      $query = '
142SELECT
143    file,
144    path,
145    tn_ext
146  FROM '.IMAGES_TABLE.'
147  WHERE id = '.$image_id.'
148;';
149      $image_infos = mysql_fetch_assoc(pwg_query($query));
150
151      $thumbnail['file'] = $image_infos['file'];
152     
153      $thumbnail['src'] = get_thumbnail_location(
154        array(
155          'path' => $image_infos['path'],
156          'tn_ext' => $image_infos['tn_ext'],
157          )
158        );
159
160      // TODO: when implementing this plugin in Piwigo core, we should have
161      // a function get_image_name($name, $file) (if name is null, then
162      // compute a temporary name from filename) that would be also used in
163      // picture.php. UPDATE: in fact, "get_name_from_file($file)" already
164      // exists and is used twice (element_set_unit + comments, but not in
165      // picture.php I don't know why) with the same pattern if
166      // (empty($name)) {$name = get_name_from_file($file)}, a clean
167      // function get_image_name($name, $file) would be better
168      $thumbnail['title'] = get_name_from_file($image_infos['file']);
169
170      $thumbnail['link'] = PHPWG_ROOT_PATH.'admin.php?page=picture_modify'
171        .'&amp;image_id='.$image_id
172        .'&amp;cat_id='.$category_id
173        ;
174
175      array_push($page['thumbnails'], $thumbnail);
176    }
177  }
178 
179  $endtime = get_moment();
180  $elapsed = ($endtime - $starttime) * 1000;
181  // printf('%.2f ms', $elapsed);
182
183  if (!empty($page['thumbnails']))
184  {
185    array_push(
186      $page['infos'],
187      sprintf(
188        l10n('%d photos uploaded'),
189        count($page['thumbnails'])
190        )
191      );
192   
193    if (0 != $_POST['level'])
194    {
195      array_push(
196        $page['infos'],
197        sprintf(
198          l10n('Privacy level set to "%s"'),
199          l10n(
200            sprintf('Level %d', $_POST['level'])
201            )
202          )
203        );
204    }
205
206    if ('existing' == $_POST['category_type'])
207    {
208      $query = '
209SELECT
210    COUNT(*)
211  FROM '.IMAGE_CATEGORY_TABLE.'
212  WHERE category_id = '.$category_id.'
213;';
214      list($count) = mysql_fetch_row(pwg_query($query));
215      $category_name = get_cat_display_name_from_id($category_id, 'admin.php?page=cat_modify&amp;cat_id=');
216     
217      // information
218      array_push(
219        $page['infos'],
220        sprintf(
221          l10n('Category "%s" now contains %d photos'),
222          '<em>'.$category_name.'</em>',
223          $count
224          )
225        );
226    }
227
228    $page['batch_link'] = $admin_base_url.'&batch='.implode(',', $image_ids);
229  }
230}
231
232// +-----------------------------------------------------------------------+
233// |                             template init                             |
234// +-----------------------------------------------------------------------+
235
236$template->set_filenames(
237  array(
238    'plugin_admin_content' => dirname(__FILE__).'/upload.tpl'
239    )
240  );
241
242$template->assign(
243    array(
244      'F_ADD_ACTION'=> $admin_base_url,
245      'plugin_path' => UPLOAD_FORM_PATH,
246    )
247  );
248
249if (isset($page['thumbnails']))
250{
251  $template->assign(
252    array(
253      'thumbnails' => $page['thumbnails'],
254      )
255    );
256
257  // only display the batch link if we have more than 1 photo
258  if (count($page['thumbnails']) > 1)
259  {
260    $template->assign(
261      array(
262        'batch_link' => $page['batch_link'],
263        'batch_label' => sprintf(
264          l10n('Manage this set of %d photos'),
265          count($page['thumbnails'])
266          ),
267        )
268      );
269  }
270}
271
272$query = '
273SELECT id,name,uppercats,global_rank
274  FROM '.CATEGORIES_TABLE.'
275;';
276
277display_select_cat_wrapper(
278  $query,
279  array(),
280  'category_options'
281  );
282
283// image level options
284$tpl_options = array();
285foreach ($conf['available_permission_levels'] as $level)
286{
287  $tpl_options[$level] = l10n( sprintf('Level %d', $level) );
288}
289$selected_level = isset($_POST['level']) ? $_POST['level'] : 0;
290$template->assign(
291    array(
292      'level_options'=> $tpl_options,
293      'level_options_selected' => array($selected_level)
294    )
295  );
296
297// +-----------------------------------------------------------------------+
298// |                             setup errors                              |
299// +-----------------------------------------------------------------------+
300
301$setup_errors = array();
302
303$upload_base_dir = 'upload';
304$upload_dir = PHPWG_ROOT_PATH.$upload_base_dir;
305
306if (!is_dir($upload_dir))
307{
308  if (!is_writable(PHPWG_ROOT_PATH))
309  {
310    array_push(
311      $setup_errors,
312      sprintf(
313        l10n('Create the "%s" directory at the root of your Piwigo installation'),
314        $upload_base_dir
315        )
316      );
317  }
318}
319else
320{
321  if (!is_writable($upload_dir))
322  {
323    @chmod($upload_dir, 0777);
324
325    if (!is_writable($upload_dir))
326    {
327      array_push(
328        $setup_errors,
329        sprintf(
330          l10n('Give write access (chmod 777) to "%s" directory at the root of your Piwigo installation'),
331          $upload_base_dir
332          )
333        );
334    }
335  }
336}
337
338$template->assign(
339    array(
340      'setup_errors'=> $setup_errors,
341    )
342  );
343
344// +-----------------------------------------------------------------------+
345// |                           sending html code                           |
346// +-----------------------------------------------------------------------+
347
348$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
349?>
Note: See TracBrowser for help on using the repository browser.