source: extensions/BatchDownloader/include/download.inc.php @ 28627

Last change on this file since 28627 was 25932, checked in by mistic100, 10 years ago

update for Piwigo 2.6 + code cleaning + fix unable to cancel set during generation

File size: 6.3 KB
Line 
1<?php
2defined('BATCH_DOWNLOAD_PATH') or die('Hacking attempt!');
3
4global $page, $template, $conf, $user;
5
6switch ($page['sub_section'])
7{
8  /* download page */
9  case 'init_zip':
10  {
11    $template->set_filename('batchdwn', realpath(BATCH_DOWNLOAD_PATH . 'template/init_zip.tpl'));
12
13    try
14    {
15      $BatchDownloader = new BatchDownloader($_GET['set_id']);
16
17      // delete set
18      if ( isset($_GET['cancel']) )
19      {
20        $BatchDownloader->deleteArchives();
21        $BatchDownloader->clearImages();
22        pwg_query('DELETE FROM '.BATCH_DOWNLOAD_TSETS.' WHERE id = '.$_GET['set_id'].';');
23        $_SESSION['page_infos'][] = l10n('Download set deleted');
24        redirect(get_absolute_root_url());
25      }
26
27      // prepare next zip
28      if ( isset($_GET['zip']) and $BatchDownloader->getParam('status') != 'new' and $BatchDownloader->getParam('status') != 'done' )
29      {
30        if ($_GET['zip'] > $BatchDownloader->getParam('last_zip'))
31        {
32          if ($conf['batch_download']['one_archive']) $BatchDownloader->deleteArchives();
33          $BatchDownloader->createNextArchive();
34        }
35
36        if ($conf['batch_download']['one_archive'])
37        {
38          $next_file = $BatchDownloader->getParam('last_zip')+1;
39        }
40        else
41        {
42          $next_file = $_GET['zip'];
43        }
44      }
45
46      // alert limit overflow
47      if ($BatchDownloader->getParam('nb_images') > $conf['batch_download']['max_elements'])
48      {
49        $template->assign('elements_error', l10n(
50          'You choose to download %d pictures, but the system is limited to %d. You can edit the set, or the last %d pictures will not be downloaded.',
51          $BatchDownloader->getParam('nb_images'),
52          $conf['batch_download']['max_elements'],
53          $BatchDownloader->getParam('nb_images') - $conf['batch_download']['max_elements']
54          ));
55      }
56      else
57      {
58        if ($BatchDownloader->getParam('status') == 'new')
59        {
60          $missing_derivatives = $BatchDownloader->getMissingDerivatives(true);
61
62          // generate missing files
63          if (count($missing_derivatives))
64          {
65            $template->assign('missing_derivatives', $missing_derivatives);
66          }
67          // set is ready
68          else
69          {
70            $BatchDownloader->updateParam('status', 'ready');
71          }
72        }
73
74        // display download links
75        if ($BatchDownloader->getParam('status') != 'new')
76        {
77          $template->assign('zip_links', $BatchDownloader->getDownloadList(BATCH_DOWNLOAD_PUBLIC . 'init_zip'));
78        }
79      }
80
81      $set = $BatchDownloader->getSetInfo();
82
83      // link to the zip
84      if (isset($next_file))
85      {
86        $set['U_DOWNLOAD'] = get_root_url().BATCH_DOWNLOAD_PATH . 'download.php?set_id='.$_GET['set_id'].'&zip='.$_GET['zip'];
87        $page['infos'][] = l10n('The archive is downloading, if the download doesn\'t start automatically please <a href="%s">click here</a>', $set['U_DOWNLOAD']);
88      }
89
90      // link to edit page
91      if ($BatchDownloader->getParam('status') != 'download' and $BatchDownloader->getParam('status') != 'done' and $BatchDownloader->getParam('nb_images') > 0)
92      {
93        $set['U_EDIT_SET'] = add_url_params(BATCH_DOWNLOAD_PUBLIC . 'view', array('set_id'=>$_GET['set_id']));
94      }
95
96      // cancel link
97      if ($BatchDownloader->getParam('last_zip') != $BatchDownloader->getParam('nb_zip')
98        or (isset($missing_derivatives) and count($missing_derivatives))
99        )
100      {
101        $set['U_CANCEL'] = add_url_params(BATCH_DOWNLOAD_PUBLIC . 'init_zip', array('set_id'=>$_GET['set_id'], 'cancel'=>'true'));
102      }
103
104      $template->assign(array(
105        'set' => $set,
106        'archive_timeout' => $conf['batch_download']['archive_timeout'],
107        ));
108    }
109    catch (Exception $e)
110    {
111      $page['errors'][] = $e->getMessage();
112    }
113
114    break;
115  }
116
117  /* edition page */
118  case 'view':
119  {
120    $self_url = add_url_params(BATCH_DOWNLOAD_PUBLIC . 'view', array('set_id'=>$_GET['set_id']));
121
122    $template->set_filename('batchdwn', realpath(BATCH_DOWNLOAD_PATH . 'template/view.tpl'));
123    $template->assign(array(
124      'BATCH_DOWNLOAD_PATH' => BATCH_DOWNLOAD_PATH,
125      'U_VIEW' => $self_url,
126      'U_INIT_ZIP' => add_url_params(BATCH_DOWNLOAD_PUBLIC . 'init_zip', array('set_id'=>$_GET['set_id'])),
127      'SET_ID' => $_GET['set_id'],
128      ));
129
130    try
131    {
132      $BatchDownloader = new BatchDownloader($_GET['set_id']);
133
134      if ($BatchDownloader->getParam('status') != 'new' && $BatchDownloader->getParam('status') != 'ready')
135      {
136        $page['errors'][] = l10n('You can not edit this set');
137        break;
138      }
139
140      if ( isset($_GET['remove']) and preg_match('#^[0-9]+$#', $_GET['remove']) )
141      {
142        $BatchDownloader->removeImages(array($_GET['remove']));
143      }
144
145      $template->assign('set', $BatchDownloader->getSetInfo());
146
147      $template->set_prefilter('index_thumbnails', 'batch_download_thumbnails_list_prefilter');
148
149      $page['start'] = isset($_GET['start']) ? $_GET['start'] : 0;
150      $page['items'] = array_keys($BatchDownloader->getImages());
151
152      if (count($page['items']) > $page['nb_image_page'])
153      {
154        $page['navigation_bar'] = create_navigation_bar(
155          $self_url,
156          count($page['items']),
157          $page['start'],
158          $page['nb_image_page'],
159          false
160          );
161        $template->assign('navbar', $page['navigation_bar']);
162      }
163
164      include(PHPWG_ROOT_PATH . 'include/category_default.inc.php');
165    }
166    catch (Exception $e)
167    {
168      $page['errors'][] = $e->getMessage();
169    }
170
171    break;
172  }
173}
174
175$template->assign(array(
176  'BATCH_DOWNLOAD_PATH' => BATCH_DOWNLOAD_PATH,
177  'BATCH_DOWNLOAD_ABS_PATH' => realpath(BATCH_DOWNLOAD_PATH).'/',
178  ));
179
180$template->assign_var_from_handle('CONTENT', 'batchdwn');
181
182
183function batch_download_thumbnails_list_prefilter($content, &$smarty)
184{
185  // add links
186  $search = '#(<li>|<li class="gthumb">)#';
187  $replace = '$1
188{strip}<a class="removeSet" href="{$U_VIEW}&amp;remove={$thumbnail.id}" data-id="{$thumbnail.id}" rel="nofollow">
189{\'Remove from download set\'|translate}&nbsp;<img src="{$ROOT_URL}{$BATCH_DOWNLOAD_PATH}template/images/image_delete.png" title="{\'Remove from download set\'|translate}">
190</a>{/strip}';
191
192  // custom CSS and AJAX request
193  $content.= file_get_contents(BATCH_DOWNLOAD_PATH.'template/thumbnails_css_js.tpl');
194
195  return preg_replace($search, $replace, $content);
196}
Note: See TracBrowser for help on using the repository browser.