source: trunk/admin/site_update.php @ 8528

Last change on this file since 8528 was 8464, checked in by plg, 13 years ago

feature 2095 added: new column images.added_by (foreign key on users.id)

  • Property svn:eol-style set to LF
File size: 27.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 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
24if (!defined('PHPWG_ROOT_PATH'))
25{
26  die('Hacking attempt!');
27}
28
29include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
30
31// +-----------------------------------------------------------------------+
32// | Check Access and exit when user status is not ok                      |
33// +-----------------------------------------------------------------------+
34check_status(ACCESS_ADMINISTRATOR);
35
36if (!is_numeric($_GET['site']))
37{
38  die ('site param missing or invalid');
39}
40$site_id = $_GET['site'];
41
42$query='
43SELECT galleries_url
44  FROM '.SITES_TABLE.'
45  WHERE id = '.$site_id;
46list($site_url) = pwg_db_fetch_row(pwg_query($query));
47if (!isset($site_url))
48{
49  die('site '.$site_id.' does not exist');
50}
51$site_is_remote = url_is_remote($site_url);
52
53list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
54define('CURRENT_DATE', $dbnow);
55
56$error_labels = array(
57  'PWG-UPDATE-1' => array(
58    l10n('wrong filename'),
59    l10n('The name of directories and files must be composed of letters, numbers, "-", "_" or "."')
60    ),
61  'PWG-UPDATE-2' => array(
62    l10n('missing thumbnail'),
63    l10n('a picture filetype requires a thumbnail. The thumbnail must be present in the sub-directory "thumbnail" of the album directory. The thumbnail filename must start with the configured thumbnail prefix and the extension must be among the following list :').implode(',', $conf['picture_ext'])
64    ),
65  'PWG-ERROR-NO-FS' => array(
66    l10n('File/directory read error'),
67    l10n('The file or directory cannot be accessed (either it does not exist or the access is denied)')
68    ),
69  'PWG-ERROR-VERSION' => array(
70    l10n('Piwigo version differs on the remote site'),
71    l10n('Version of create_listing_file.php on the remote site and Piwigo must be the same')
72    ),
73  'PWG-ERROR-NOLISTING' => array(
74    l10n('listing.xml file was not found'),
75    l10n('listing.xml file was not found on the remote site. This file is generated by choosing the "generate listing" command in the Site manager')
76    )
77  );
78$errors = array();
79$infos = array();
80
81if ($site_is_remote)
82{
83  include_once(PHPWG_ROOT_PATH.'admin/site_reader_remote.php');
84  $local_listing = null;
85  if ( isset($_GET['local_listing'])
86      and $_GET['local_listing'] )
87  {
88    $local_listing = PHPWG_ROOT_PATH.'listing.xml';
89  }
90  $site_reader = new RemoteSiteReader($site_url, $local_listing);
91}
92else
93{
94  include_once( PHPWG_ROOT_PATH.'admin/site_reader_local.php');
95  $site_reader = new LocalSiteReader($site_url);
96}
97
98$general_failure = true;
99if (isset($_POST['submit']))
100{
101  if (!isset($conf['flip_picture_ext']))
102  {
103    $conf['flip_picture_ext'] = array_flip($conf['picture_ext']);
104  }
105  if ($site_reader->open())
106  {
107    $general_failure = false;
108  }
109
110  // shall we simulate only
111  if (isset($_POST['simulate']) and $_POST['simulate'] == 1)
112  {
113    $simulate = true;
114  }
115  else
116  {
117    $simulate = false;
118  }
119}
120
121// +-----------------------------------------------------------------------+
122// |                      directories / categories                         |
123// +-----------------------------------------------------------------------+
124if (isset($_POST['submit'])
125    and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files'))
126{
127  $counts['new_categories'] = 0;
128  $counts['del_categories'] = 0;
129  $counts['del_elements'] = 0;
130  $counts['new_elements'] = 0;
131  $counts['upd_elements'] = 0;
132}
133
134
135if (isset($_POST['submit'])
136    and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files')
137    and !$general_failure)
138{
139  $start = get_moment();
140  // which categories to update ?
141  $query = '
142SELECT id, uppercats, global_rank, status, visible
143  FROM '.CATEGORIES_TABLE.'
144  WHERE dir IS NOT NULL
145    AND site_id = '.$site_id;
146  if (isset($_POST['cat']) and is_numeric($_POST['cat']))
147  {
148    if (isset($_POST['subcats-included']) and $_POST['subcats-included'] == 1)
149    {
150      $query.= '
151    AND uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$_POST['cat'].'(,|$)\'
152';
153    }
154    else
155    {
156      $query.= '
157    AND id = '.$_POST['cat'].'
158';
159    }
160  }
161  $db_categories = hash_from_query($query, 'id');
162
163  // get categort full directories in an array for comparison with file
164  // system directory tree
165  $db_fulldirs = get_fulldirs(array_keys($db_categories));
166
167  // what is the base directory to search file system sub-directories ?
168  if (isset($_POST['cat']) and is_numeric($_POST['cat']))
169  {
170    $basedir = $db_fulldirs[$_POST['cat']];
171  }
172  else
173  {
174    $basedir = preg_replace('#/*$#', '', $site_url);
175  }
176
177  // we need to have fulldirs as keys to make efficient comparison
178  $db_fulldirs = array_flip($db_fulldirs);
179
180  // finding next rank for each id_uppercat. By default, each category id
181  // has 1 for next rank on its sub-categories to create
182  $next_rank['NULL'] = 1;
183
184  $query = '
185SELECT id
186  FROM '.CATEGORIES_TABLE;
187  $result = pwg_query($query);
188  while ($row = pwg_db_fetch_assoc($result))
189  {
190    $next_rank[$row['id']] = 1;
191  }
192
193  // let's see if some categories already have some sub-categories...
194  $query = '
195SELECT id_uppercat, MAX(rank)+1 AS next_rank
196  FROM '.CATEGORIES_TABLE.'
197  GROUP BY id_uppercat';
198  $result = pwg_query($query);
199  while ($row = pwg_db_fetch_assoc($result))
200  {
201    // for the id_uppercat NULL, we write 'NULL' and not the empty string
202    if (!isset($row['id_uppercat']) or $row['id_uppercat'] == '')
203    {
204      $row['id_uppercat'] = 'NULL';
205    }
206    $next_rank[$row['id_uppercat']] = $row['next_rank'];
207  }
208
209  // next category id available
210  $next_id = pwg_db_nextval('id', CATEGORIES_TABLE);
211
212  // retrieve sub-directories fulldirs from the site reader
213  $fs_fulldirs = $site_reader->get_full_directories($basedir);
214
215  // get_full_directories doesn't include the base directory, so if it's a
216  // category directory, we need to include it in our array
217  if (isset($_POST['cat']))
218  {
219    array_push($fs_fulldirs, $basedir);
220  }
221  // If $_POST['subcats-included'] != 1 ("Search in sub-albums" is unchecked)
222  // $db_fulldirs doesn't include any subdirectories and $fs_fulldirs does
223  // So $fs_fulldirs will be limited to the selected basedir
224  // (if that one is in $fs_fulldirs)
225  if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1)
226  {
227    $fs_fulldirs = array_intersect($fs_fulldirs, array_keys($db_fulldirs));
228  }
229  $inserts = array();
230  // new categories are the directories not present yet in the database
231  foreach (array_diff($fs_fulldirs, array_keys($db_fulldirs)) as $fulldir)
232  {
233    $dir = basename($fulldir);
234    if (preg_match('/^[a-zA-Z0-9-_.]+$/', $dir))
235    {
236      $insert = array(
237        'id'          => $next_id++,
238        'dir'         => $dir,
239        'name'        => str_replace('_', ' ', $dir),
240        'site_id'     => $site_id,
241        'commentable' =>
242          boolean_to_string($conf['newcat_default_commentable']),
243        'uploadable'  => $site_is_remote
244          ? 'false'
245          : boolean_to_string($conf['newcat_default_uploadable']),
246        'status'      => $conf['newcat_default_status'],
247        'visible'     => boolean_to_string($conf['newcat_default_visible']),
248        );
249
250      if (isset($db_fulldirs[dirname($fulldir)]))
251      {
252        $parent = $db_fulldirs[dirname($fulldir)];
253
254        $insert['id_uppercat'] = $parent;
255        $insert['uppercats'] =
256          $db_categories[$parent]['uppercats'].','.$insert['id'];
257        $insert['rank'] = $next_rank[$parent]++;
258        $insert['global_rank'] =
259          $db_categories[$parent]['global_rank'].'.'.$insert['rank'];
260        if ('private' == $db_categories[$parent]['status'])
261        {
262          $insert['status'] = 'private';
263        }
264        if ('false' == $db_categories[$parent]['visible'])
265        {
266          $insert['visible'] = 'false';
267        }
268      }
269      else
270      {
271        $insert['uppercats'] = $insert['id'];
272        $insert{'rank'} = $next_rank['NULL']++;
273        $insert['global_rank'] = $insert['rank'];
274      }
275
276      array_push($inserts, $insert);
277      array_push(
278        $infos,
279        array(
280          'path' => $fulldir,
281          'info' => l10n('added')
282          )
283        );
284
285      // add the new category to $db_categories and $db_fulldirs array
286      $db_categories[$insert{'id'}] =
287        array(
288          'id' => $insert['id'],
289          'status' => $insert['status'],
290          'visible' => $insert['visible'],
291          'uppercats' => $insert['uppercats'],
292          'global_rank' => $insert['global_rank']
293          );
294      $db_fulldirs[$fulldir] = $insert['id'];
295      $next_rank[$insert{'id'}] = 1;
296    }
297    else
298    {
299      array_push(
300        $errors,
301        array(
302          'path' => $fulldir,
303          'type' => 'PWG-UPDATE-1'
304          )
305        );
306    }
307  }
308
309  if (count($inserts) > 0)
310  {
311    if (!$simulate)
312    {
313      $dbfields = array(
314        'id','dir','name','site_id','id_uppercat','uppercats','commentable',
315        'uploadable','visible','status','rank','global_rank'
316        );
317      mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts);
318    }
319
320    $counts['new_categories'] = count($inserts);
321  }
322
323  // to delete categories
324  $to_delete = array();
325  foreach (array_diff(array_keys($db_fulldirs), $fs_fulldirs) as $fulldir)
326  {
327    array_push($to_delete, $db_fulldirs[$fulldir]);
328    unset($db_fulldirs[$fulldir]);
329    array_push($infos, array('path' => $fulldir,
330                             'info' => l10n('deleted')));
331  }
332  if (count($to_delete) > 0)
333  {
334    if (!$simulate)
335    {
336      delete_categories($to_delete);
337    }
338    $counts['del_categories'] = count($to_delete);
339  }
340
341  $template->append('footer_elements', '<!-- scanning dirs : '
342    . get_elapsed_time($start, get_moment())
343    . ' -->' );
344}
345// +-----------------------------------------------------------------------+
346// |                           files / elements                            |
347// +-----------------------------------------------------------------------+
348if (isset($_POST['submit']) and $_POST['sync'] == 'files'
349      and !$general_failure)
350{
351  $start_files = get_moment();
352  $start= $start_files;
353
354  $fs = $site_reader->get_elements($basedir);
355  $template->append('footer_elements', '<!-- get_elements: '
356    . get_elapsed_time($start, get_moment())
357    . ' -->' );
358
359  $cat_ids = array_diff(array_keys($db_categories), $to_delete);
360
361  $db_elements = array();
362  $db_unvalidated = array();
363
364  if (count($cat_ids) > 0)
365  {
366    $query = '
367SELECT id, path
368  FROM '.IMAGES_TABLE.'
369  WHERE storage_category_id IN ('
370      .wordwrap(
371        implode(', ', $cat_ids),
372        80,
373        "\n"
374        ).')';
375    $db_elements = simple_hash_from_query($query, 'id', 'path');
376
377    // searching the unvalidated waiting elements (they must not be taken into
378    // account)
379    $query = '
380SELECT file,storage_category_id
381  FROM '.WAITING_TABLE.'
382  WHERE storage_category_id IN (
383'.wordwrap(implode(', ', $cat_ids), 80, "\n").')
384    AND validated = \'false\'';
385    $result = pwg_query($query);
386    while ($row = pwg_db_fetch_assoc($result))
387    {
388      array_push(
389        $db_unvalidated,
390        array_search(
391          $row['storage_category_id'],
392          $db_fulldirs)
393        .'/'.$row['file']
394        );
395    }
396  }
397
398  // next element id available
399  $next_element_id = pwg_db_nextval('id', IMAGES_TABLE);
400
401  $start = get_moment();
402
403  $inserts = array();
404  $insert_links = array();
405
406  foreach (array_diff(array_keys($fs), $db_elements, $db_unvalidated) as $path)
407  {
408    $insert = array();
409    // storage category must exist
410    $dirname = dirname($path);
411    if (!isset($db_fulldirs[$dirname]))
412    {
413      continue;
414    }
415    $filename = basename($path);
416    if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename))
417    {
418      array_push(
419        $errors,
420        array(
421          'path' => $path,
422          'type' => 'PWG-UPDATE-1'
423          )
424        );
425
426      continue;
427    }
428
429    if ( isset( $conf['flip_picture_ext'][get_extension($filename)] )
430          and !isset($fs[$path]['tn_ext']) )
431    { // For a picture thumbnail is mandatory and for non picture element,
432      // thumbnail and representative are optionnal
433      array_push(
434        $errors,
435        array(
436          'path' => $path,
437          'type' => 'PWG-UPDATE-2'
438          )
439        );
440    }
441    else
442    {
443      $insert = array(
444        'id'             => $next_element_id++,
445        'file'           => $filename,
446        'date_available' => CURRENT_DATE,
447        'path'           => $path,
448        'tn_ext'         => isset($fs[$path]['tn_ext'])
449          ? $fs[$path]['tn_ext']
450          : null,
451        'storage_category_id' => $db_fulldirs[$dirname],
452        'added_by' => $user['id'],
453        );
454
455      if ( $_POST['privacy_level']!=0 )
456      {
457        $insert['level'] = $_POST['privacy_level'];
458      }
459
460      array_push(
461        $inserts,
462        $insert
463        );
464
465      array_push(
466        $insert_links,
467        array(
468          'image_id'    => $insert['id'],
469          'category_id' => $insert['storage_category_id'],
470          )
471        );
472
473      array_push(
474        $infos,
475        array(
476          'path' => $insert['path'],
477          'info' => l10n('added')
478          )
479        );
480
481      $caddiables[] = $insert['id'];
482    }
483  }
484
485  if (count($inserts) > 0)
486  {
487    if (!$simulate)
488    {
489      // inserts all new elements
490      mass_inserts(
491        IMAGES_TABLE,
492        array_keys($inserts[0]),
493        $inserts
494        );
495
496      // inserts all links between new elements and their storage category
497      mass_inserts(
498        IMAGE_CATEGORY_TABLE,
499        array_keys($insert_links[0]),
500        $insert_links
501        );
502
503      // add new elements to caddie
504      if (isset($_POST['add_to_caddie']) and $_POST['add_to_caddie'] == 1)
505      {
506        fill_caddie($caddiables);
507      }
508    }
509    $counts['new_elements'] = count($inserts);
510  }
511
512  // delete elements that are in database but not in the filesystem
513  $to_delete_elements = array();
514  foreach (array_diff($db_elements, array_keys($fs)) as $path)
515  {
516    array_push($to_delete_elements, array_search($path, $db_elements));
517    array_push($infos, array('path' => $path,
518                             'info' => l10n('deleted')));
519  }
520  if (count($to_delete_elements) > 0)
521  {
522    if (!$simulate)
523    {
524      delete_elements($to_delete_elements);
525    }
526    $counts['del_elements'] = count($to_delete_elements);
527  }
528
529  $template->append('footer_elements', '<!-- scanning files : '
530    . get_elapsed_time($start_files, get_moment())
531    . ' -->' );
532
533  // retrieving informations given by uploaders
534  if (!$simulate and count($cat_ids) > 0)
535  {
536    $query = '
537SELECT id,file,storage_category_id,infos
538  FROM '.WAITING_TABLE.'
539  WHERE storage_category_id IN (
540'.wordwrap(implode(', ', $cat_ids), 80, "\n").')
541    AND validated = \'true\'';
542    $result = pwg_query($query);
543
544    $datas = array();
545    $fields =
546      array(
547        'primary' => array('id'),
548        'update'  => array('date_creation', 'author', 'name', 'comment')
549        );
550
551    $waiting_to_delete = array();
552
553    while ($row = pwg_db_fetch_assoc($result))
554    {
555      $data = array();
556
557      $query = '
558SELECT id
559  FROM '.IMAGES_TABLE.'
560  WHERE storage_category_id = '.$row['storage_category_id'].'
561    AND file = \''.$row['file'].'\'';
562      list($data['id']) = pwg_db_fetch_row(pwg_query($query));
563
564      foreach ($fields['update'] as $field)
565      {
566        $data[$field] = addslashes( getAttribute($row['infos'], $field) );
567      }
568
569      array_push($datas, $data);
570      array_push($waiting_to_delete, $row['id']);
571    }
572
573    if (count($datas) > 0)
574    {
575      mass_updates(IMAGES_TABLE, $fields, $datas);
576
577      // delete now useless waiting elements
578      $query = '
579DELETE
580  FROM '.WAITING_TABLE.'
581  WHERE id IN ('.implode(',', $waiting_to_delete).')
582;';
583      pwg_query($query);
584    }
585  }
586}
587
588// +-----------------------------------------------------------------------+
589// |                          synchronize files                            |
590// +-----------------------------------------------------------------------+
591if (isset($_POST['submit'])
592    and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files')
593    and !$general_failure )
594{
595  if (!$simulate)
596  {
597    $start = get_moment();
598    update_category('all');
599    $template->append('footer_elements', '<!-- update_category(all) : '
600      . get_elapsed_time($start,get_moment())
601      . ' -->' );
602    $start = get_moment();
603    update_global_rank();
604    $template->append('footer_elements', '<!-- ordering categories : '
605      . get_elapsed_time($start, get_moment())
606      . ' -->');
607  }
608
609  if ($_POST['sync'] == 'files')
610  {
611    $start = get_moment();
612    $opts['category_id'] = '';
613    $opts['recursive'] = true;
614    if (isset($_POST['cat']))
615    {
616      $opts['category_id'] = $_POST['cat'];
617      if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1)
618      {
619        $opts['recursive'] = false;
620      }
621    }
622    $files = get_filelist($opts['category_id'], $site_id,
623                          $opts['recursive'],
624                          false);
625    $template->append('footer_elements', '<!-- get_filelist : '
626      . get_elapsed_time($start, get_moment())
627      . ' -->');
628    $start = get_moment();
629
630    $datas = array();
631    foreach ( $files as $id=>$file )
632    {
633      $data = $site_reader->get_element_update_attributes($file);
634      if ( !is_array($data) )
635      {
636        continue;
637      }
638      $extension = get_extension($file);
639      if ( isset($conf['flip_picture_ext'][$extension]) )
640      {
641        if ( !isset($data['tn_ext']) )
642        {
643          array_push(
644            $errors,
645            array(
646              'path' => $file,
647              'type' => 'PWG-UPDATE-2'
648              )
649            );
650          continue;
651        }
652      }
653
654      $data['id']=$id;
655      array_push($datas, $data);
656    } // end foreach file
657
658    $counts['upd_elements'] = count($datas);
659    if (!$simulate and count($datas)>0 )
660    {
661      mass_updates(
662        IMAGES_TABLE,
663        // fields
664        array(
665          'primary' => array('id'),
666          'update'  => $site_reader->get_update_attributes(),
667          ),
668        $datas
669        );
670    }
671    $template->append('footer_elements', '<!-- update files : '
672      . get_elapsed_time($start,get_moment())
673      . ' -->');
674  }// end if sync files
675}
676
677// +-----------------------------------------------------------------------+
678// |                          synchronize files                            |
679// +-----------------------------------------------------------------------+
680if (isset($_POST['submit'])
681    and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files'))
682{
683  $template->assign(
684    'update_result',
685    array(
686      'NB_NEW_CATEGORIES'=>$counts['new_categories'],
687      'NB_DEL_CATEGORIES'=>$counts['del_categories'],
688      'NB_NEW_ELEMENTS'=>$counts['new_elements'],
689      'NB_DEL_ELEMENTS'=>$counts['del_elements'],
690      'NB_UPD_ELEMENTS'=>$counts['upd_elements'],
691      'NB_ERRORS'=>count($errors),
692      ));
693}
694
695// +-----------------------------------------------------------------------+
696// |                          synchronize metadata                         |
697// +-----------------------------------------------------------------------+
698if (isset($_POST['submit']) and isset($_POST['sync_meta'])
699         and !$general_failure)
700{
701  // sync only never synchronized files ?
702  $opts['only_new'] = isset($_POST['meta_all']) ? false : true;
703  $opts['category_id'] = '';
704  $opts['recursive'] = true;
705
706  if (isset($_POST['cat']))
707  {
708    $opts['category_id'] = $_POST['cat'];
709    // recursive ?
710    if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1)
711    {
712      $opts['recursive'] = false;
713    }
714  }
715  $start = get_moment();
716  $files = get_filelist($opts['category_id'], $site_id,
717                        $opts['recursive'],
718                        $opts['only_new']);
719
720  $template->append('footer_elements', '<!-- get_filelist : '
721    . get_elapsed_time($start, get_moment())
722    . ' -->');
723
724  $start = get_moment();
725  $datas = array();
726  $tags_of = array();
727
728  $has_high_images = array();
729
730  $image_ids = array();
731  foreach ($files as $id => $file)
732  {
733    array_push($image_ids, $id);
734  }
735
736  if (count($image_ids) > 0)
737  {
738    $query = '
739SELECT id
740  FROM '.IMAGES_TABLE.'
741  WHERE has_high = \'true\'
742    AND id IN (
743'.wordwrap(implode(', ', $image_ids), 80, "\n").'
744)';
745    $has_high_images = array_from_query($query, 'id' );
746  }
747
748  foreach ( $files as $id=>$file )
749  {
750    $data = $site_reader->get_element_metadata(
751      $file,
752      in_array($id, $has_high_images)
753      );
754
755    if ( is_array($data) )
756    {
757      $data['date_metadata_update'] = CURRENT_DATE;
758      $data['id']=$id;
759      array_push($datas, $data);
760
761      foreach (array('keywords', 'tags') as $key)
762      {
763        if (isset($data[$key]))
764        {
765          if (!isset($tags_of[$id]))
766          {
767            $tags_of[$id] = array();
768          }
769
770          foreach (explode(',', $data[$key]) as $tag_name)
771          {
772            array_push(
773              $tags_of[$id],
774              tag_id_from_tag_name($tag_name)
775              );
776          }
777        }
778      }
779    }
780    else
781    {
782      array_push($errors, array('path' => $file, 'type' => 'PWG-ERROR-NO-FS'));
783    }
784  }
785
786  if (!$simulate)
787  {
788    if (count($datas) > 0)
789    {
790      mass_updates(
791        IMAGES_TABLE,
792        // fields
793        array(
794          'primary' => array('id'),
795          'update'  => array_unique(
796            array_merge(
797              array_diff(
798                $site_reader->get_metadata_attributes(),
799                // keywords and tags fields are managed separately
800                array('keywords', 'tags')
801                ),
802              array('date_metadata_update'))
803            )
804          ),
805        $datas,
806        isset($_POST['meta_empty_overrides']) ? 0 : MASS_UPDATES_SKIP_EMPTY
807        );
808    }
809    set_tags_of($tags_of);
810  }
811
812  $template->append('footer_elements', '<!-- metadata update : '
813    . get_elapsed_time($start, get_moment())
814    . ' -->');
815
816  $template->assign(
817    'metadata_result',
818    array(
819      'NB_ELEMENTS_DONE' => count($datas),
820      'NB_ELEMENTS_CANDIDATES' => count($files),
821      'NB_ERRORS' => count($errors),
822      ));
823}
824
825// +-----------------------------------------------------------------------+
826// |                        template initialization                        |
827// +-----------------------------------------------------------------------+
828$template->set_filenames(array('update'=>'site_update.tpl'));
829$result_title = '';
830if (isset($simulate) and $simulate)
831{
832  $result_title.= l10n('[Simulation]').' ';
833}
834
835// used_metadata string is displayed to inform admin which metadata will be
836// used from files for synchronization
837$used_metadata = implode( ', ', $site_reader->get_metadata_attributes());
838if ($site_is_remote and !isset($_POST['submit']) )
839{
840  $used_metadata.= ' + ...';
841}
842
843$template->assign(
844  array(
845    'SITE_URL'=>$site_url,
846    'U_SITE_MANAGER'=> get_root_url().'admin.php?page=site_manager',
847    'L_RESULT_UPDATE'=>$result_title.l10n('Search for new images in the directories'),
848    'L_RESULT_METADATA'=>$result_title.l10n('Metadata synchronization results'),
849    'METADATA_LIST' => $used_metadata,
850    'U_HELP' => get_root_url().'admin/popuphelp.php?page=synchronize',
851    ));
852
853// +-----------------------------------------------------------------------+
854// |                        introduction : choices                         |
855// +-----------------------------------------------------------------------+
856if (isset($_POST['submit']))
857{
858  $tpl_introduction = array(
859      'sync'  => $_POST['sync'],
860      'sync_meta'  => isset($_POST['sync_meta']) ? true : false,
861      'display_info' => isset($_POST['display_info']) and $_POST['display_info']==1,
862      'add_to_caddie' => isset($_POST['add_to_caddie']) and $_POST['add_to_caddie']==1,
863      'subcats_included' => isset($_POST['subcats-included']) and $_POST['subcats-included']==1,
864      'privacy_level_selected' => (int)@$_POST['privacy_level'],
865      'meta_all'  => isset($_POST['meta_all']) ? true : false,
866      'meta_empty_overrides'  => isset($_POST['meta_empty_overrides']) ? true : false,
867    );
868
869  if (isset($_POST['cat']) and is_numeric($_POST['cat']))
870  {
871    $cat_selected = array($_POST['cat']);
872  }
873  else
874  {
875    $cat_selected = array();
876  }
877}
878else
879{
880  $tpl_introduction = array(
881      'sync'  => 'dirs',
882      'sync_meta'  => true,
883      'display_info' => false,
884      'add_to_caddie' => false,
885      'subcats_included' => true,
886      'privacy_level_selected' => 0,
887      'meta_all'  => false,
888      'meta_empty_overrides'  => false,
889    );
890
891  $cat_selected = array();
892}
893
894$tpl_introduction['privacy_level_options'] = get_privacy_level_options();
895
896$template->assign('introduction', $tpl_introduction);
897
898$query = '
899SELECT id,name,uppercats,global_rank
900  FROM '.CATEGORIES_TABLE.'
901  WHERE site_id = '.$site_id;
902display_select_cat_wrapper($query,
903                           $cat_selected,
904                           'category_options',
905                           false);
906
907
908if (count($errors) > 0)
909{
910  foreach ($errors as $error)
911  {
912    $template->append(
913      'sync_errors',
914      array(
915        'ELEMENT' => $error['path'],
916        'LABEL' => $error['type'].' ('.$error_labels[$error['type']][0].')'
917        ));
918  }
919
920  foreach ($error_labels as $error_type=>$error_description)
921  {
922    $template->append(
923      'sync_error_captions',
924      array(
925        'TYPE' => $error_type,
926        'LABEL' => $error_description[1]
927        ));
928  }
929}
930
931if (count($infos) > 0
932    and isset($_POST['display_info'])
933    and $_POST['display_info'] == 1)
934{
935  foreach ($infos as $info)
936  {
937    $template->append(
938      'sync_infos',
939      array(
940        'ELEMENT' => $info['path'],
941        'LABEL' => $info['info']
942        ));
943  }
944}
945
946// +-----------------------------------------------------------------------+
947// |                          sending html code                            |
948// +-----------------------------------------------------------------------+
949$template->assign_var_from_handle('ADMIN_CONTENT', 'update');
950?>
Note: See TracBrowser for help on using the repository browser.