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

Last change on this file since 21206 was 21206, checked in by mistic100, 11 years ago

-define body_id and fix breadcrumb
-fix edit page with simple and gthumb+

File size: 6.7 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/* define page section from url */
7function batch_download_section_init()
8{
9  global $tokens, $page, $conf;
10 
11  if ($tokens[0] == 'download')
12  {
13    if (check_download_access() === false) access_denied();
14   
15    add_event_handler('loc_begin_page_header', 'batch_download_page_header');
16   
17    $page['section'] = 'download';
18    $page['section_title'] = '<a href="'.get_absolute_root_url().'">'.l10n('Home').'</a>'.$conf['level_separator'].l10n('Batch Downloader').$conf['level_separator'];
19    $page['title'] = l10n('Batch Downloader');
20   
21    switch (@$tokens[1])
22    {
23      case 'init_zip':
24        $page['sub_section'] = 'init_zip';
25        $page['section_title'].= l10n('Generate ZIP');
26        break;
27      case 'view':
28        $page['sub_section'] = 'view';
29        $page['section_title'].= l10n('Edit the set');
30        break;
31      default:
32        redirect('index.php');
33    }
34  }
35}
36
37function batch_download_page_header()
38{
39  global $page;
40  $page['body_id'] = 'theBatchDownloader';
41}
42
43/* download section */
44function batch_download_page() 
45{
46  global $page;
47
48  if (isset($page['section']) and $page['section'] == 'download')
49  {
50    include(BATCH_DOWNLOAD_PATH . '/include/download.inc.php');
51  }
52}
53
54
55/* add buttons on thumbnails list */
56function batch_download_index_button()
57{
58  global $page, $template, $user, $conf;
59 
60  if ( !count($page['items']) or !isset($page['section']) ) return;
61 
62  if (check_download_access() === false) return;
63 
64  // download the set
65  if ( isset($_GET['action']) and $_GET['action']=='advdown_set' )
66  {
67    $set = get_set_info_from_page();
68   
69    if ($set !== false)
70    {
71      $BatchDownloader = new BatchDownloader('new', $page['items'], $set['type'], $set['id']);
72      $BatchDownloader->getEstimatedArchiveNumber();
73     
74      // if we plan only one zip with less elements than 'max_elements', the download starts immediately
75      if (
76        $BatchDownloader->getParam('nb_images') <= $conf['batch_download']['max_elements']
77        and $BatchDownloader->getParam('nb_zip') == 1
78      )
79      {
80        $BatchDownloader->createNextArchive(true); // make sure we have only one zip, even if 'max_size' is exceeded
81       
82        $u_download = get_root_url().BATCH_DOWNLOAD_PATH . 'download.php?set_id='.$BatchDownloader->getParam('id').'&amp;zip=1';
83       
84        $null = null;
85        $template->block_footer_script(null, 'setTimeout("document.location.href = \''.$u_download.'\';", 1000);', $null, $null);
86       
87        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));
88      }
89      // oterwise we go to summary page
90      else
91      {
92        redirect(add_url_params(BATCH_DOWNLOAD_PUBLIC . 'init_zip', array('set_id'=>$BatchDownloader->getParam('id'))));
93      }
94    }
95  }
96 
97  if ($page['section'] == 'collections')
98  {
99    $url = $_SERVER['REQUEST_URI'];
100  }
101  else
102  {
103    $url = duplicate_index_url(array(), array('action'));
104  }
105 
106  $url = add_url_params($url, array('action'=>'advdown_set'));
107 
108  // toolbar button
109  $button = '<script type="text/javascript">var batchdown_count = '.count($page['items']).'; var batchdown_string = "'.l10n('Confirm the download of %d pictures?').'";</script>
110    <li><a href="'. $url .'" title="'.l10n('Download all pictures of this selection').'" class="pwg-state-default pwg-button" rel="nofollow"
111    onClick="return confirm(batchdown_string.replace(\'%d\', batchdown_count));">
112                        <span class="pwg-icon batch-downloader-icon" style="background:url(\'' . get_root_url().BATCH_DOWNLOAD_PATH . 'template/zip.png\') center center no-repeat;">&nbsp;</span><span class="pwg-button-text">'.l10n('Download').'</span>
113                </a></li>';
114  $template->concat('PLUGIN_INDEX_ACTIONS', $button);
115  $template->concat('COLLECTION_ACTIONS', $button);
116}
117
118
119/* menu block */
120function batch_download_add_menublock($menu_ref_arr)
121{
122  global $user;
123 
124  $menu = &$menu_ref_arr[0];
125  if ($menu->get_id() != 'menubar') return;
126 
127  if (check_download_access() === false) return;
128 
129  $query = '
130SELECT id
131  FROM '.BATCH_DOWNLOAD_TSETS.'
132  WHERE
133    user_id = '.$user['id'].'
134    AND status != "done"
135  LIMIT 1
136;';
137  $result = pwg_query($query);
138  if (!pwg_db_num_rows($result)) return;
139 
140  $menu->register_block(new RegisteredBlock('mbBatchDownloader', l10n('Batch Downloader'), 'BatchDownloader'));
141}
142
143function batch_download_applymenu($menu_ref_arr)
144{
145  global $template, $conf, $user;
146 
147  $menu = &$menu_ref_arr[0];
148  $block = $menu->get_block('mbBatchDownloader');
149 
150  if ($block != null)
151  {
152    $query = '
153SELECT id
154  FROM '.BATCH_DOWNLOAD_TSETS.'
155  WHERE
156    user_id = '.$user['id'].'
157    AND status != "done"
158;';
159    $sets = array_from_query($query, 'id');
160   
161    $data = array();
162    foreach ($sets as $set_id)
163    {
164      $BatchDownloader = new BatchDownloader($set_id);
165      $set = $BatchDownloader->getSetInfo();
166     
167      array_push($data, array(
168        'URL' => add_url_params(BATCH_DOWNLOAD_PUBLIC . 'init_zip', array('set_id'=>$BatchDownloader->getParam('id'))),
169        'TITLE' => str_replace('"', "'", strip_tags($set['COMMENT'])),
170        'NAME' => $set['sNAME'],
171        'COUNT' => $set['NB_IMAGES'],
172        ));
173    }
174   
175    $template->set_template_dir(BATCH_DOWNLOAD_PATH . 'template/');
176    $block->set_title(l10n('Downloads'));
177    $block->template = 'menublock_batch_down.tpl';
178    $block->data = $data;
179  }
180}
181
182
183/* archives and databse cleanup */
184function batch_download_clean()
185{
186  global $conf;
187 
188  // we only search for old downloads every hour, nevermind which user is connected
189  if ($conf['batch_download']['last_clean'] > time() - 3600) return;
190 
191  $conf['batch_download']['last_clean'] = time();
192  conf_update_param('batch_download', serialize($conf['batch_download']));
193 
194  // set old sets as done and clean images table
195  $query = '
196DELETE i
197  FROM '.BATCH_DOWNLOAD_TIMAGES.' AS i
198    INNER JOIN '.BATCH_DOWNLOAD_TSETS.' AS s
199    ON i.set_id = s.id
200  WHERE
201    status != "done" AND
202    date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR)
203;';
204  pwg_query($query);
205 
206  $query = '
207UPDATE '.BATCH_DOWNLOAD_TSETS.'
208  SET status = "done"
209  WHERE
210    status != "done" AND
211    date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR)
212;';
213  pwg_query($query);
214 
215  // remove old archives
216  $zips = glob(BATCH_DOWNLOAD_LOCAL . 'u-*/*.zip');
217 
218  if (is_array($zips))
219  {
220    foreach ($zips as $zip)
221    {
222      if (filemtime($zip) < time()-$conf['batch_download']['archive_timeout']*3600)
223      {
224        unlink($zip);
225      }
226    }
227  }
228}
229
230?>
Note: See TracBrowser for help on using the repository browser.