source: trunk/admin/site_update.php @ 23425

Last change on this file since 23425 was 23376, checked in by flop25, 11 years ago

bug:2855
$confinheritance_by_default applies on FTP added albums

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