source: extensions/Google2Piwigo/admin/import.php @ 24816

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

add batch manager prefilter

File size: 8.5 KB
Line 
1<?php
2if (!defined('PICASA_WA_PATH')) die('Hacking attempt!');
3
4set_time_limit(600);
5
6include_once(PICASA_WA_PATH . 'include/functions.inc.php');
7
8if ( !test_remote_download() )
9{
10  array_push($page['errors'], l10n('No download method available'));
11  $_GET['action'] = 'error';
12}
13else
14{
15  // init Gdata API
16  set_include_path(get_include_path() . PATH_SEPARATOR . PICASA_WA_PATH.'include');
17  require_once('Zend/Loader.php');
18  Zend_Loader::loadClass('Zend_Gdata_AuthSub');
19  Zend_Loader::loadClass('Zend_Gdata_Photos');
20 
21  // generate token after authentication
22  if (!empty($_GET['token']))
23  {
24    $_SESSION['gdata_auth_token'] = Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);
25    $_GET['action'] = 'logged';
26  }
27 
28  // authentication
29  if (empty($_SESSION['gdata_auth_token']))
30  {
31    $_GET['action'] = 'login';
32  }
33  else
34  {
35    $client = Zend_Gdata_AuthSub::getHttpClient($_SESSION['gdata_auth_token']);
36    $picasa = new Zend_Gdata_Photos($client, "Piwigo-Google2Piwigo-1.0");
37  }
38}
39
40
41if (!isset($_GET['action'])) $_GET['action'] = 'main';
42
43switch ($_GET['action'])
44{
45  // button to login page
46  case 'login':
47  {
48    $login_url = Zend_Gdata_AuthSub::getAuthSubTokenUri(
49      get_absolute_root_url() . PICASA_WA_ADMIN . '-import',
50      'https://picasaweb.google.com/data', 
51      false, true
52      );
53       
54    $template->assign(array(
55      'picasa_login' => $login_url,
56      'HELP_CONTENT' => load_language('help.lang.html', PICASA_WA_PATH, array('return'=>true)),
57      ));
58    break;
59  }
60 
61  // message after login
62  case 'logged':
63  {
64    $_SESSION['page_infos'][] = l10n('Successfully logged to you Google account');
65    redirect(PICASA_WA_ADMIN . '-import');
66    break;
67  }
68 
69  // logout
70  case 'logout':
71  {
72    Zend_Gdata_AuthSub::AuthSubRevokeToken(
73      $_SESSION['gdata_auth_token'],
74      $client
75      );
76    unset($_SESSION['gdata_auth_token']);
77   
78    $_SESSION['page_infos'][] = l10n('Logged out');
79    redirect(PICASA_WA_ADMIN . '-import');
80    break;
81  }
82 
83  // main menu
84  case 'main':
85  {
86    $template->assign(array(
87      'username' => $picasa->getUserEntry( $picasa->newUserQuery() )->getGphotoNickname()->getText(),
88      'logout_url' =>      PICASA_WA_ADMIN . '-import&amp;action=logout',
89      'list_albums_url' => PICASA_WA_ADMIN . '-import&amp;action=list_albums',
90      'import_all_url' =>  PICASA_WA_ADMIN . '-import&amp;action=list_all',
91      ));
92    break;
93  }
94 
95  // list user albums
96  case 'list_albums':
97  {
98    // get all albums
99    $userFeed = $picasa->getUserFeed("default");
100   
101    $albums = array();
102    foreach ($userFeed as $userEntry)
103    {
104      array_push($albums, array(
105        'title' =>       $userEntry->title->text,
106        'description' => $userEntry->mediaGroup->description->text,
107        'photos' =>      $userEntry->gphotoNumPhotos->text,
108        'U_LIST' => PICASA_WA_ADMIN . '-import&amp;action=list_photos&amp;album=' . $userEntry->gphotoId->text,
109        ));
110    }
111   
112    $template->assign(array(
113      'total_albums' => count($albums),
114      'albums' => $albums,
115      ));
116    break;
117  }
118 
119  // list photos of an album
120  case 'list_photos':
121  {
122    $self_url = PICASA_WA_ADMIN . '-import&amp;action=list_photos&amp;album='.$_GET['album'];
123    $picasa_prefix = 'picasa-';
124   
125    // pagination
126    if (isset($_GET['start']))   $page['start'] = intval($_GET['start']);
127    else                         $page['start'] = 0;
128    if (isset($_GET['display'])) $page['display'] = $_GET['display']=='all' ? 500 : intval($_GET['display']);
129    else                         $page['display'] = 20;
130   
131    // get photos
132    $query = $picasa->newAlbumQuery();
133    $query->setUser('default');
134    $query->setAlbumId($_GET['album']);
135    $query->setImgMax('800');
136    $albumFeed = $picasa->getAlbumFeed($query);
137   
138    $all_photos = array();
139    foreach ($albumFeed as $albumEntry)
140    {
141      array_push($all_photos, array(
142        'id' =>    $albumEntry->getGphotoId()->getText(),
143        'name' =>  $albumEntry->mediaGroup->title->text,
144        'thumb' => $albumEntry->mediaGroup->thumbnail[1]->url,
145        'src' =>   $albumEntry->mediaGroup->content[0]->url,
146        'url' =>   $albumEntry->link[2]->href,
147        ));
148    }
149   
150    // get existing photos
151    $query = '
152SELECT id, file
153  FROM '.IMAGES_TABLE.'
154  WHERE file LIKE "'.$picasa_prefix.'%"
155;';
156    $existing_photos = simple_hash_from_query($query, 'id', 'file');
157    $existing_photos = array_map(create_function('$p', 'return preg_replace("#^'.$picasa_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p);'), $existing_photos);
158   
159    // remove existing photos
160    $duplicates = 0;
161    foreach ($all_photos as $i => $photo)
162    {
163      if (in_array($photo['id'], $existing_photos))
164      {
165        unset($all_photos[$i]);
166        $duplicates++;
167      }
168    }
169   
170    if ($duplicates>0)
171    {
172      $page['infos'][] = '<a href="admin.php?page=batch_manager&amp;prefilter=picasa">'
173          .l10n_dec('One picture is not displayed because already existing in the database.', '%d pictures are not displayed because already existing in the database.', $duplicates)
174        .'</a>';
175    }
176   
177    // displayed photos
178    $page_photos = array_slice($all_photos, $page['start'], $page['display']);
179    $all_elements = array_map(create_function('$p', 'return  \'"\'.$p["id"].\'"\';'), $all_photos);
180 
181    $template->assign(array(
182      'nb_thumbs_set' =>  count($all_photos),
183      'nb_thumbs_page' => count($page_photos),
184      'thumbnails' =>     $page_photos,
185      'all_elements' =>   $all_elements,
186      'album' =>          $_GET['album'],
187      'F_ACTION' =>       PICASA_WA_ADMIN.'-import&amp;action=import_set',
188      'U_DISPLAY' =>      $self_url,
189      ));
190     
191    // get piwigo categories
192    $query = '
193SELECT id, name, uppercats, global_rank
194  FROM '.CATEGORIES_TABLE.'
195;';
196    display_select_cat_wrapper($query, array(), 'category_parent_options');
197   
198    // get navbar
199    $nav_bar = create_navigation_bar(
200      $self_url,
201      count($all_elements),
202      $page['start'],
203      $page['display']
204      );
205    $template->assign('navbar', $nav_bar);
206    break;
207  }
208 
209  // list all photos of the user
210  case 'list_all':
211  {
212    $picasa_prefix = 'picasa-';
213   
214    // get all photos in all albums
215    $userFeed = $picasa->getUserFeed("default");
216
217    $all_photos = array();
218    foreach ($userFeed as $userEntry)
219    {
220      $query = $picasa->newAlbumQuery();
221      $query->setUser('default');
222      $query->setAlbumId( $userEntry->gphotoId->text );
223      $albumFeed = $picasa->getAlbumFeed($query);
224     
225      foreach ($albumFeed as $albumEntry)
226      {
227        $all_photos[ $albumEntry->getGphotoId()->getText() ] = $userEntry->gphotoId->text;
228      }
229    }
230   
231    // get existing photos
232    $query = '
233SELECT id, file
234  FROM '.IMAGES_TABLE.'
235  WHERE file LIKE "'.$picasa_prefix.'%"
236;';
237    $existing_photos = simple_hash_from_query($query, 'id', 'file');
238    $existing_photos = array_map(create_function('$p', 'return preg_replace("#^'.$picasa_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p);'), $existing_photos);
239   
240    // remove existing photos
241    $duplicates = 0;
242    foreach ($all_photos as $id => &$photo)
243    {
244      if (in_array($id, $existing_photos))
245      {
246        unset($all_photos[$id]);
247        $duplicates++;
248      }
249      else
250      {
251        $photo = array(
252          'id' => $id,
253          'album' => $photo,
254          );
255      }
256    }
257    unset($photo);
258    $all_photos = array_values($all_photos);
259   
260    if ($duplicates>0)
261    {
262      $page['infos'][] = '<a href="admin.php?page=batch_manager&amp;prefilter=picasa">'
263          .l10n_dec('One picture is not displayed because already existing in the database.', '%d pictures are not displayed because already existing in the database.', $duplicates)
264        .'</a>';
265    }
266   
267    $template->assign(array(
268      'nb_elements' =>  count($all_photos),
269      'all_elements' => json_encode($all_photos),
270      'F_ACTION' =>     PICASA_WA_ADMIN . '-import&amp;action=import_set',
271      ));
272     
273    // get piwigo categories
274    $query = '
275SELECT id, name, uppercats, global_rank
276  FROM '.CATEGORIES_TABLE.'
277;';
278    display_select_cat_wrapper($query, array(), 'category_parent_options');
279    break;
280  }
281 
282  // success message after import
283  case 'import_set':
284  {
285    if (isset($_POST['done']))
286    {
287      $_SESSION['page_infos'][] = sprintf(l10n('%d pictures imported'), $_POST['done']);
288    }
289    redirect(PICASA_WA_ADMIN . '-import');
290  }
291}
292
293
294$template->assign(array(
295  'ACTION' => $_GET['action'],
296  'GMAPS_LOADED' => !empty($pwg_loaded_plugins['rv_gmaps']),
297  ));
298
299$template->set_filename('picasa_web_albums', realpath(PICASA_WA_PATH . '/admin/template/import.tpl'));
300
301?>
Note: See TracBrowser for help on using the repository browser.