source: trunk/admin/include/photos_add_direct_process.inc.php @ 13330

Last change on this file since 13330 was 13077, checked in by plg, 13 years ago

feature 2564: redesign on photo administration screen.

  • one screen with several tabs (for now: properties + coi)
  • double select boxes for album associations and representation have been converted to simple multiple select boxes with jQuery Chosen
  • more details about the photo in an introduction text
File size: 8.3 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24
25if (isset($_GET['processed']))
26{
27//   echo '<pre>POST'."\n"; print_r($_POST); echo '</pre>';
28//   echo '<pre>FILES'."\n"; print_r($_FILES); echo '</pre>';
29//   echo '<pre>SESSION'."\n"; print_r($_SESSION); echo '</pre>';
30//   exit();
31
32  // sometimes, you have submitted the form but you have nothing in $_POST
33  // and $_FILES. This may happen when you have an HTML upload and you
34  // exceeded the post_max_size (but not the upload_max_size)
35  if (!isset($_POST['submit_upload']))
36  {
37    array_push(
38      $page['errors'],
39      sprintf(
40        l10n('The uploaded files exceed the post_max_size directive in php.ini: %sB'),
41        ini_get('post_max_size')
42        )
43      );
44  }
45  else
46  {
47    $category_id = $_POST['category'];
48  }
49
50  if (isset($_POST['onUploadError']) and is_array($_POST['onUploadError']) and count($_POST['onUploadError']) > 0)
51  {
52    foreach ($_POST['onUploadError'] as $error)
53    {
54      array_push($page['errors'], $error);
55    }
56  }
57   
58  $image_ids = array();
59       
60  if (isset($_FILES) and !empty($_FILES['image_upload']))
61  {
62    $starttime = get_moment();
63
64  foreach ($_FILES['image_upload']['error'] as $idx => $error)
65  {
66    if (UPLOAD_ERR_OK == $error)
67    {
68      $images_to_add = array();
69     
70      $extension = pathinfo($_FILES['image_upload']['name'][$idx], PATHINFO_EXTENSION);
71      if ('zip' == strtolower($extension))
72      {
73        $upload_dir = $conf['upload_dir'].'/buffer';
74        prepare_directory($upload_dir);
75       
76        $temporary_archive_name = date('YmdHis').'-'.generate_key(10);
77        $archive_path = $upload_dir.'/'.$temporary_archive_name.'.zip';
78       
79        move_uploaded_file(
80          $_FILES['image_upload']['tmp_name'][$idx],
81          $archive_path
82          );
83
84        define('PCLZIP_TEMPORARY_DIR', $upload_dir.'/');
85        include(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php');
86        $zip = new PclZip($archive_path);
87        if ($list = $zip->listContent())
88        {
89          $indexes_to_extract = array();
90         
91          foreach ($list as $node)
92          {
93            if (1 == $node['folder'])
94            {
95              continue;
96            }
97
98            if (is_valid_image_extension(pathinfo($node['filename'], PATHINFO_EXTENSION)))
99            {
100              array_push($indexes_to_extract, $node['index']);
101             
102              array_push(
103                $images_to_add,
104                array(
105                  'source_filepath' => $upload_dir.'/'.$temporary_archive_name.'/'.$node['filename'],
106                  'original_filename' => basename($node['filename']),
107                  )
108                );
109            }
110          }
111     
112          if (count($indexes_to_extract) > 0)
113          {
114            $zip->extract(
115              PCLZIP_OPT_PATH, $upload_dir.'/'.$temporary_archive_name,
116              PCLZIP_OPT_BY_INDEX, $indexes_to_extract,
117              PCLZIP_OPT_ADD_TEMP_FILE_ON
118              );
119          }
120        }
121      }
122      elseif (is_valid_image_extension($extension))
123      {
124        array_push(
125          $images_to_add,
126          array(
127            'source_filepath' => $_FILES['image_upload']['tmp_name'][$idx],
128            'original_filename' => $_FILES['image_upload']['name'][$idx],
129            )
130          );
131      }
132
133      foreach ($images_to_add as $image_to_add)
134      {
135        $image_id = add_uploaded_file(
136          $image_to_add['source_filepath'],
137          $image_to_add['original_filename'],
138          array($category_id),
139          $_POST['level']
140          );
141
142        array_push($image_ids, $image_id);
143
144        // TODO: if $image_id is not an integer, something went wrong
145      }
146    }
147    else
148    {
149      $error_message = file_upload_error_message($error);
150     
151      array_push(
152        $page['errors'],
153        sprintf(
154          l10n('Error on file "%s" : %s'),
155          $_FILES['image_upload']['name'][$idx],
156          $error_message
157          )
158        );
159    }
160  }
161 
162  $endtime = get_moment();
163  $elapsed = ($endtime - $starttime) * 1000;
164  // printf('%.2f ms', $elapsed);
165
166  } // if (!empty($_FILES))
167
168  if (isset($_POST['upload_id']))
169  {
170    // we're on a multiple upload, with uploadify and so on
171    if (isset($_SESSION['uploads_error'][ $_POST['upload_id'] ]))
172    {
173      foreach ($_SESSION['uploads_error'][ $_POST['upload_id'] ] as $error)
174      {
175        array_push($page['errors'], $error);
176      }
177    }
178
179    if (isset($_SESSION['uploads'][ $_POST['upload_id'] ]))
180    {
181      $image_ids = $_SESSION['uploads'][ $_POST['upload_id'] ];
182    }
183  }
184 
185  $page['thumbnails'] = array();
186  foreach ($image_ids as $image_id)
187  {
188    // we could return the list of properties from the add_uploaded_file
189    // function, but I like the "double check". And it costs nothing
190    // compared to the upload process.
191    $thumbnail = array();
192     
193    $query = '
194SELECT
195    id,
196    file,
197    path,
198    tn_ext
199  FROM '.IMAGES_TABLE.'
200  WHERE id = '.$image_id.'
201;';
202    $image_infos = pwg_db_fetch_assoc(pwg_query($query));
203
204    $thumbnail['file'] = $image_infos['file'];
205   
206    $thumbnail['src'] = DerivativeImage::thumb_url($image_infos);
207
208    // TODO: when implementing this plugin in Piwigo core, we should have
209    // a function get_image_name($name, $file) (if name is null, then
210    // compute a temporary name from filename) that would be also used in
211    // picture.php. UPDATE: in fact, "get_name_from_file($file)" already
212    // exists and is used twice (batch_manager_unit + comments, but not in
213    // picture.php I don't know why) with the same pattern if
214    // (empty($name)) {$name = get_name_from_file($file)}, a clean
215    // function get_image_name($name, $file) would be better
216    $thumbnail['title'] = get_name_from_file($image_infos['file']);
217
218    $thumbnail['link'] = get_root_url().'admin.php?page=photo-'.$image_id.'&amp;cat_id='.$category_id;
219
220    array_push($page['thumbnails'], $thumbnail);
221  }
222 
223  if (!empty($page['thumbnails']))
224  {
225    array_push(
226      $page['infos'],
227      sprintf(
228        l10n('%d photos uploaded'),
229        count($page['thumbnails'])
230        )
231      );
232   
233    if (0 != $_POST['level'])
234    {
235      array_push(
236        $page['infos'],
237        sprintf(
238          l10n('Privacy level set to "%s"'),
239          l10n(
240            sprintf('Level %d', $_POST['level'])
241            )
242          )
243        );
244    }
245
246    $query = '
247SELECT
248    COUNT(*)
249  FROM '.IMAGE_CATEGORY_TABLE.'
250  WHERE category_id = '.$category_id.'
251;';
252    list($count) = pwg_db_fetch_row(pwg_query($query));
253    $category_name = get_cat_display_name_from_id($category_id, 'admin.php?page=album-');
254   
255    // information
256    array_push(
257      $page['infos'],
258      sprintf(
259        l10n('Album "%s" now contains %d photos'),
260        '<em>'.$category_name.'</em>',
261        $count
262        )
263      );
264   
265    $page['batch_link'] = PHOTOS_ADD_BASE_URL.'&batch='.implode(',', $image_ids);
266  }
267}
268
269?>
Note: See TracBrowser for help on using the repository browser.