source: extensions/properties_mass_update/admin_update.php @ 19437

Last change on this file since 19437 was 17671, checked in by plg, 12 years ago

new feature: ability to select the property to update (title, description, author, tags)

new feature: ability to select the separator (tab, space, comma, semicolon)

File size: 6.1 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// | Checks                                                                |
34// +-----------------------------------------------------------------------+
35
36check_status(ACCESS_ADMINISTRATOR);
37
38$regex_for_separator = array(
39  'tab' => '/^([^\t]+)\t+(.*)$/',
40  'space' => '/^([^\s]+)\s+(.*)$/',
41  'comma' => '/^([^,]+),+(.*)$/',
42  'semicolon' => '/^([^;]+);+(.*)$/',
43  );
44
45if (isset($_POST['submit']))
46{
47  if (!in_array($_POST['separator'], array_keys($regex_for_separator)))
48  {
49    die('Hacking attempt!');
50  }
51
52  if (!in_array($_POST['property'], array('name', 'comment', 'author', 'tags')))
53  {
54    die('Hacking attempt!');
55  }
56}
57
58// +-----------------------------------------------------------------------+
59// | Actions                                                               |
60// +-----------------------------------------------------------------------+
61
62if (isset($_FILES) and !empty($_FILES['update']))
63{
64  $starttime = get_moment();
65 
66  if (UPLOAD_ERR_OK == $_FILES['update']['error'])
67  {
68    if ('text/plain' == $_FILES['update']['type'])
69    {
70      $text_file = $_FILES['update']['tmp_name'];
71    }
72    else
73    {
74      array_push($page['errors'], l10n('Wrong file, please select a plain text file'));
75    }
76
77    if (isset($text_file))
78    {
79      $raw = file_get_contents($text_file);
80      $raw_lines = explode("\n", $raw);
81     
82      $query = 'SELECT id, file FROM '.IMAGES_TABLE.';';
83      $existing_files = hash_from_query($query, 'file');
84           
85      $updates = array();
86      $update_files = array();
87      $missing_files = array();
88      $tags_of = array();
89     
90      foreach ($raw_lines as $raw_line)
91      {
92        if (!preg_match($regex_for_separator[$_POST['separator']], $raw_line, $matches))
93        {
94          continue;
95        }
96       
97        // in case the same file is defined twice, we only save the first occurence
98        if (isset($update_files[$matches[1]]) or isset($missing_files[$matches[1]]))
99        {
100          continue;
101        }
102
103        if (isset($existing_files[$matches[1]]))
104        {
105          $update_files[$matches[1]] = true;
106          $image_id = $existing_files[$matches[1]]['id'];
107
108          if ('tags' == $_POST['property'])
109          {
110            $tags_of[$image_id] = array();
111            $raw_tags = explode(',', $matches[2]);
112            foreach ($raw_tags as $tag)
113            {
114              $tag = trim($tag);
115              if (empty($tag))
116              {
117                continue;
118              }
119              $tag_id = tag_id_from_tag_name($tag);
120              array_push($tags_of[$image_id], $tag_id);
121            }
122          }
123          else
124          {
125            array_push(
126              $updates,
127              array(
128                'id' => $image_id,
129                $_POST['property'] => pwg_db_real_escape_string($matches[2]), // TODO right trim
130                )
131              );
132          }
133        }
134        else
135        {
136          $missing_files[$matches[1]] = true;
137        }
138      }
139     
140      if ('tags' == $_POST['property'])
141      {
142        set_tags_of($tags_of);
143      }
144      else
145      {
146        mass_updates(
147          IMAGES_TABLE,
148          array(
149            'primary' => array('id'),
150            'update' => array($_POST['property']),
151            ),
152          $updates
153          );
154      }
155
156      $endtime = get_moment();
157      $elapsed = ($endtime - $starttime);
158
159      array_push(
160        $page['infos'],
161        sprintf(
162          l10n('%d photos updated'),
163          count(array_keys($update_files))
164          )
165        );
166     
167      if (count($missing_files) > 0)
168      {
169        array_push(
170          $page['errors'],
171          sprintf(
172            l10n('%d photos are missing in Piwigo: %s'),
173            count($missing_files),
174            implode(', ', array_keys($missing_files))
175            )
176          );
177      }
178    }
179  }
180  else
181  {
182    array_push($page['errors'], $_FILES['update']['error']);
183  }
184}
185
186// +-----------------------------------------------------------------------+
187// | form options                                                          |
188// +-----------------------------------------------------------------------+
189
190// image level options
191$selected_level = isset($_POST['level']) ? $_POST['level'] : 0;
192$template->assign(
193    array(
194      'level_options'=> get_privacy_level_options(),
195      'level_options_selected' => array($selected_level)
196    )
197  );
198?>
Note: See TracBrowser for help on using the repository browser.