source: extensions/BatchDownloader/include/events.inc.php @ 16400

Last change on this file since 16400 was 16400, checked in by mistic100, 12 years ago
  • display creation date
  • improve set titles for Tags and Calendar
  • add Download history admin page

TODO : User Selection, localization, change archive name to more user-friendly one in the Force-Download

File size: 5.9 KB
Line 
1<?php
2defined('BATCH_DOWNLOAD_PATH') or die('Hacking attempt!');
3
4# this file contains all functions directly called by the triggers #
5
6/* unserialize conf and load language */
7function batch_download_init()
8{
9  global $conf;
10 
11  $conf['batch_download'] = unserialize($conf['batch_download']);
12  load_language('plugin.lang', BATCH_DOWNLOAD_PATH);
13}
14
15
16/* define page section from url */
17function batch_download_section_init()
18{
19  global $tokens, $page, $conf;
20 
21  if ($tokens[0] == 'download')
22  {
23    if (check_download_access() === false) access_denied();
24   
25    $page['section'] = 'download';
26    $page['title'] = l10n('Batch Downloader').$conf['level_separator'].' ';
27   
28    switch (@$tokens[1])
29    {
30      case 'init_zip':
31        $page['sub_section'] = 'init_zip';
32        $page['title'].= l10n('Generate ZIP');
33        break;
34      case 'view':
35        $page['sub_section'] = 'view';
36        $page['title'].= l10n('Edit set');
37        break;
38      default:
39        redirect('index.php');
40    }
41  }
42}
43
44/* download section */
45function batch_download_page() 
46{
47  global $page;
48
49  if (isset($page['section']) and $page['section'] == 'download')
50  {
51    include(BATCH_DOWNLOAD_PATH . '/include/download.inc.php');
52  }
53}
54
55
56/* add buttons on thumbnails list */
57function batch_download_index_button()
58{
59  global $page, $template, $user, $conf;
60 
61  if ( !count($page['items']) or !isset($page['section']) ) return;
62 
63  if (check_download_access() === false) return;
64 
65  // download the set
66  if ( isset($_GET['action']) and $_GET['action']=='advdown_set' )
67  {
68    $set = get_set_info_from_page();
69   
70    if ($set !== false)
71    {
72      $BatchDownloader = new BatchDownloader('new', $page['items'], $set['type'], $set['id']);
73      $BatchDownloader->getEstimatedArchiveNumber();
74     
75      // if we plan only one zip with less elements than 'max_elements', the download starts immediately
76      if (
77        $BatchDownloader->getParam('nb_images') <= $conf['batch_download']['max_elements']
78        and $BatchDownloader->getParam('nb_zip') == 1
79      )
80      {
81        $BatchDownloader->createNextArchive(true); // make sure we have only one zip, even if 'max_size' is exceeded
82       
83        $u_download = BATCH_DOWNLOAD_PATH . 'download.php?set_id='.$BatchDownloader->getParam('set_id').'&amp;zip=1';
84       
85        $null = null;
86        $template->block_footer_script(null, 'setTimeout("document.location.href = \''.$u_download.'\';", 1000);', $null, $null);
87       
88        array_push($page['infos'], sprintf(l10n('The archive is downloading, if the download doesn\'t start automatically please <a href="%s">click here</a>'), $u_download));
89      }
90      // oterwise we go to summary page
91      else
92      {
93        redirect(BATCH_DOWNLOAD_PUBLIC . 'init_zip&amp;set_id='.$BatchDownloader->getParam('set_id'));
94      }
95    }
96  }
97 
98  // toolbar button
99  $button = '<li><a href="'. duplicate_index_url(array(), array('action')) .'&amp;action=advdown_set" title="'.l10n('Download all pictures of this selection').'" class="pwg-state-default pwg-button" rel="nofollow">
100                        <span class="pwg-icon" style="background:url(\'' . BATCH_DOWNLOAD_PATH . 'template/zip.png\') center center no-repeat;">&nbsp;</span><span class="pwg-button-text">'.l10n('Advanced Downloader').'</span>
101                </a></li>';
102  $template->concat('PLUGIN_INDEX_ACTIONS', $button);
103}
104
105
106/* menu block */
107function batch_download_add_menublock($menu_ref_arr)
108{
109  global $user;
110 
111  $menu = &$menu_ref_arr[0];
112  if ($menu->get_id() != 'menubar') return;
113 
114  if (check_download_access() === false) return;
115 
116  $query = '
117SELECT id
118  FROM '.BATCH_DOWNLOAD_TSETS.'
119  WHERE
120    user_id = '.$user['id'].'
121    AND status != "done"
122;';
123  $result = pwg_query($query);
124  if (!pwg_db_num_rows($result)) return;
125 
126  $menu->register_block(new RegisteredBlock('mbAdvancedDownloader', l10n('Downloads'), 'AdvancedDownloader'));
127}
128
129function batch_download_applymenu($menu_ref_arr)
130{
131  global $template, $conf, $user;
132 
133  $menu = &$menu_ref_arr[0];
134  $block = $menu->get_block('mbAdvancedDownloader');
135 
136  if ($block != null)
137  {
138    $query = '
139SELECT id
140  FROM '.BATCH_DOWNLOAD_TSETS.'
141  WHERE
142    user_id = '.$user['id'].'
143    AND status != "done"
144;';
145    $sets = array_from_query($query, 'id');
146   
147    $data = array();
148    foreach ($sets as $set_id)
149    {
150      $BatchDownloader = new BatchDownloader($set_id);
151      $set = $BatchDownloader->getSetInfo();
152     
153      array_push($data, array(
154        'URL' => BATCH_DOWNLOAD_PUBLIC . 'init_zip&amp;set_id='.$BatchDownloader->getParam('set_id'),
155        'TITLE' => strip_tags($set['COMMENT']),
156        'NAME' => $set['sNAME'],
157        'COUNT' => $set['NB_IMAGES'],
158        ));
159    }
160   
161    $template->set_template_dir(BATCH_DOWNLOAD_PATH . 'template/');
162    $block->set_title(l10n('Downloads'));
163    $block->template = 'menublock.tpl';
164    $block->data = $data;
165  }
166}
167
168
169/* archives and databse cleanup */
170function batch_download_clean()
171{
172  global $conf;
173 
174  // we only search for old downloads every hour, nevermind which user is connected
175  if ($conf['batch_download']['last_clean'] > time() - 3600) return;
176 
177  $conf['batch_download']['last_clean'] = time();
178  conf_update_param('batch_download', serialize($conf['batch_download']));
179 
180  // set old sets as done
181  $query = '
182SELECT id
183  FROM '.BATCH_DOWNLOAD_TSETS.'
184  WHERE
185    date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR)
186    AND status != "done"
187;';
188  $sets = array_from_query($query, 'id');
189 
190  foreach ($sets as $set_id)
191  {
192    $BatchDownloader = new BatchDownloader($set_id);
193    $BatchDownloader->deleteLastArchive();
194    $BatchDownloader->clearImages();
195    $BatchDownloader->updateParam('status', 'done');
196  }
197 
198  // remove old archives
199  $zips = glob(BATCH_DOWNLOAD_LOCAL . 'u-*/*.zip');
200  foreach ($zips as $zip)
201  {
202    if (filemtime($zip) < time()-$conf['batch_download']['archive_timeout']*3600)
203    {
204      unlink($zip);
205    }
206  }
207}
208
209?>
Note: See TracBrowser for help on using the repository browser.