source: extensions/properties_mass_update/admin_update.php @ 15654

Last change on this file since 15654 was 15389, checked in by ddtddt, 12 years ago

[extensions] - properties_mass_update - plg plugin

File size: 5.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2012     Pierrick LE GALL    http://le-gall.net/pierrick |
6// +-----------------------------------------------------------------------+
7// | This program is free software; you can redistribute it and/or modify  |
8// | it under the terms of the GNU General Public License as published by  |
9// | the Free Software Foundation                                          |
10// |                                                                       |
11// | This program is distributed in the hope that it will be useful, but   |
12// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
13// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
14// | General Public License for more details.                              |
15// |                                                                       |
16// | You should have received a copy of the GNU General Public License     |
17// | along with this program; if not, write to the Free Software           |
18// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
19// | USA.                                                                  |
20// +-----------------------------------------------------------------------+
21
22if( !defined("PHPWG_ROOT_PATH") )
23{
24  die ("Hacking attempt!");
25}
26
27include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
28include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
29
30$admin_base_url = get_root_url().'admin.php?page=plugin-properties_mass_update-update';
31
32// +-----------------------------------------------------------------------+
33// | Check Access and exit when user status is not ok                      |
34// +-----------------------------------------------------------------------+
35
36check_status(ACCESS_ADMINISTRATOR);
37
38// +-----------------------------------------------------------------------+
39// |                                actions                                |
40// +-----------------------------------------------------------------------+
41
42if (isset($_FILES) and !empty($_FILES['update']))
43{
44  $starttime = get_moment();
45 
46  if (UPLOAD_ERR_OK == $_FILES['update']['error'])
47  {
48    if ('text/plain' == $_FILES['update']['type'])
49    {
50      $text_file = $_FILES['update']['tmp_name'];
51    }
52    else
53    {
54      array_push($page['errors'], l10n('Wrong file, please select a plain text file'));
55    }
56
57    if (isset($text_file))
58    {
59      $raw = file_get_contents($text_file);
60      $raw_lines = explode("\n", $raw);
61     
62      $query = 'SELECT file FROM '.IMAGES_TABLE.';';
63      $existing_files = hash_from_query($query, 'file');
64           
65      $updates = array();
66      $update_files = array();
67      $missing_files = array();
68     
69      foreach ($raw_lines as $raw_line)
70      {
71        if (!preg_match('/^([^\t]+)\t+(.*)$/', $raw_line, $matches))
72        {
73          continue;
74        }
75       
76        // in case the same file is defined twice, we only save the first occurence
77        if (isset($update_files[$matches[1]]) or isset($missing_files[$matches[1]]))
78        {
79          continue;
80        }
81
82        if (isset($existing_files[$matches[1]]))
83        {       
84          $update_files[$matches[1]] = true;
85        }
86        else
87        {
88          $missing_files[$matches[1]] = true;
89          continue;
90        }
91       
92        array_push(
93          $updates,
94          array(
95            'file' => pwg_db_real_escape_string($matches[1]),
96            'comment' => pwg_db_real_escape_string($matches[2]), // TODO right trim
97            )
98          );
99      }
100     
101      mass_updates(
102        IMAGES_TABLE,
103          array(
104            'primary' => array('file'),
105            'update' => array('comment'),
106          ),
107          $updates
108        );
109
110      $endtime = get_moment();
111      $elapsed = ($endtime - $starttime);
112
113      array_push(
114        $page['infos'],
115        sprintf(
116          l10n('%d photos updated'),
117          count($updates)
118          )
119        );
120     
121      if (count($missing_files) > 0)
122      {
123        array_push(
124          $page['errors'],
125          sprintf(
126            l10n('%d photos are missing in Piwigo: %s'),
127            count($missing_files),
128            implode(', ', array_keys($missing_files))
129            )
130          );
131      }
132    }
133  }
134  else
135  {
136    array_push($page['errors'], $_FILES['mascarille_update']['error']);
137  }
138}
139
140// +-----------------------------------------------------------------------+
141// | form options                                                          |
142// +-----------------------------------------------------------------------+
143
144// image level options
145$selected_level = isset($_POST['level']) ? $_POST['level'] : 0;
146$template->assign(
147    array(
148      'level_options'=> get_privacy_level_options(),
149      'level_options_selected' => array($selected_level)
150    )
151  );
152?>
Note: See TracBrowser for help on using the repository browser.