source: extensions/photo_update/admin.php @ 24623

Revision 24623, 6.9 KB checked in by plg, 6 years ago (diff)

ability to update the pwg_representative instead of the main file (for non picture files, see $confpicture_ext)

Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 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');
30include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
31
32define('COMMUNITY_BASE_URL', get_root_url().'admin.php?page=plugin-community');
33
34// +-----------------------------------------------------------------------+
35// | Check Access and exit when user status is not ok                      |
36// +-----------------------------------------------------------------------+
37
38check_status(ACCESS_ADMINISTRATOR);
39
40// +-----------------------------------------------------------------------+
41// | Basic checks                                                          |
42// +-----------------------------------------------------------------------+
43
44$_GET['image_id'] = $_GET['tab'];
45
46check_input_parameter('image_id', $_GET, false, PATTERN_ID);
47
48$admin_photo_base_url = get_root_url().'admin.php?page=photo-'.$_GET['image_id'];
49
50// +-----------------------------------------------------------------------+
51// | Process form                                                          |
52// +-----------------------------------------------------------------------+
53
54load_language('plugin.lang', PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');
55 
56if (isset($_FILES['photo_update']))
57{
58  include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
59 
60  if ($_FILES['photo_update']['error'] !== UPLOAD_ERR_OK)
61  {
62    $error_message = file_upload_error_message($_FILES['photo_update']['error']);
63
64    array_push(
65      $page['errors'],
66      $error_message
67      );
68  }
69  else
70  {
71    $file_to_update = 'main';
72    if (isset($_POST['file_to_update']) and in_array($_POST['file_to_update'], array('main', 'representative')))
73    {
74      $file_to_update = $_POST['file_to_update'];
75    }
76
77    $image_id = $_GET['image_id'];
78     
79    $query = '
80SELECT
81    id, path, representative_ext
82  FROM '.IMAGES_TABLE.'
83  WHERE id = '.$image_id.'
84;';
85    $result = pwg_query($query);
86    $row = pwg_db_fetch_assoc($result);
87
88    if ('main' == $file_to_update)
89    {
90      add_uploaded_file(
91        $_FILES['photo_update']['tmp_name'],
92        $_FILES['photo_update']['name'],
93        null,
94        null,
95        $_GET['image_id']
96        );
97     
98      array_push(
99        $page['infos'],
100        l10n('The photo was updated')
101        );
102    }
103   
104    if ('representative' == $file_to_update)
105    {
106      $file_path = $row['path'];
107
108      // move the uploaded file to pwg_representative sub-directory
109      $representative_file_path = dirname($file_path).'/pwg_representative/';     
110      $representative_file_path.= get_filename_wo_extension(basename($file_path)).'.';
111
112      $old_representative_file_path = $representative_file_path.$row['representative_ext'];
113
114      $representative_ext = get_extension($_FILES['photo_update']['name']);
115
116      // in case we replace a *.jpg by *.png we have to safely remove the
117      // *.jpg becase move_uploaded_file won't remove it
118      if ($representative_ext != $row['representative_ext'])
119      {
120        @unlink($representative_file_path.$row['representative_ext']);
121      }
122     
123      $representative_file_path.= $representative_ext;
124
125      prepare_directory(dirname($representative_file_path));
126
127      move_uploaded_file($_FILES['photo_update']['tmp_name'], $representative_file_path);
128
129      $file_infos = pwg_image_infos($representative_file_path);
130     
131      single_update(
132        IMAGES_TABLE,
133        array(
134          'representative_ext' => $representative_ext,
135          'width' => $file_infos['width'],
136          'height' => $file_infos['height'],
137          ),
138        array('id' => $image_id)
139        );
140     
141      array_push(
142        $page['infos'],
143        l10n('The representative picture was updated')
144        );
145    }
146
147    // force refresh of multiple sizes
148    delete_element_derivatives($row);
149  }
150}
151
152// +-----------------------------------------------------------------------+
153// | Tabs                                                                  |
154// +-----------------------------------------------------------------------+
155
156include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
157
158$page['tab'] = 'update';
159
160$tabsheet = new tabsheet();
161$tabsheet->set_id('photo');
162$tabsheet->select('update');
163$tabsheet->assign();
164
165// +-----------------------------------------------------------------------+
166// |                             template init                             |
167// +-----------------------------------------------------------------------+
168
169$template->set_filenames(
170  array(
171    'plugin_admin_content' => dirname(__FILE__).'/admin.tpl'
172    )
173  );
174
175// retrieving direct information about picture
176$query = '
177SELECT *
178  FROM '.IMAGES_TABLE.'
179  WHERE id = '.$_GET['image_id'].'
180;';
181$row = pwg_db_fetch_assoc(pwg_query($query));
182
183if (!in_array(get_extension($row['path']), $conf['picture_ext']) or !empty($row['representative_ext']))
184{
185  $template->assign('show_file_to_update', true);
186}
187
188$template->assign(
189  array(
190    'TN_SRC' => DerivativeImage::thumb_url($row),
191    'original_filename' => $row['file'],
192    'TITLE' => render_element_name($row),
193    )
194  );
195
196// +-----------------------------------------------------------------------+
197// | sending html code                                                     |
198// +-----------------------------------------------------------------------+
199
200$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
201?>
Note: See TracBrowser for help on using the repository browser.