source: trunk/admin/site_update.php @ 25033

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

remove all array_push (50% slower than []) + some changes missing for feature:2978

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