source: extensions/Copyrights/admin.php @ 11656

Last change on this file since 11656 was 11656, checked in by J.Commelin, 13 years ago

Refactored code and added comments

File size: 5.9 KB
RevLine 
[10874]1<?php
[10931]2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 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// +-----------------------------------------------------------------------+
[10874]23
24if (!defined("PHPWG_ROOT_PATH")){
25  die("Hacking attempt!");
26}
27
28include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
[11268]29load_language('plugin.lang', COPYRIGHTS_PATH);
[10874]30
31// Check access and exit when user status is not ok
32check_status(ACCESS_ADMINISTRATOR);
33
[11656]34// Default is to create a copyright, if changed to 1, show the edit page
[10874]35$edit = 0;
36
37// The values for the form fields
38$CRid = 0;
39$CRname = '';
40$CRurl = '';
[11084]41$CRdescr = '';
[10874]42$CRvisible = 0;
43
44// Do managing of copyrights
45if (isset($_GET['tab'])) {
[11656]46  // Create a new copyright
[10874]47  if ($_GET['tab'] == 'create') {
[11656]48    // Fetch the values from the form
[10931]49    $name = pwg_db_real_escape_string($_REQUEST['name']);
50    $url = pwg_db_real_escape_string($_REQUEST['url']);
[11084]51    $descr = pwg_db_real_escape_string($_REQUEST['descr']);
[10874]52    $visible = (isset($_REQUEST['visible']) ? 1 : 0);
[11656]53
54    // Check whether a copyright with such a name exists
55    // Therefore count the number of copyrights with that name
[10931]56    $query = sprintf(
[11625]57      'SELECT COUNT(*)
58      FROM %s
[11635]59      WHERE `name` = \'%s\'
[10931]60      ;',
[11625]61      COPYRIGHTS_ADMIN, $name);
62    list($counter) = pwg_db_fetch_row(pwg_query($query));
[11656]63
64    if ($counter != 0) { // The copyright exists already
[11625]65      array_push($page['errors'], l10n('This copyright already exists'));
[11656]66    } else { // The copyright did not yet exist
67      // Compose a query to insert the copyright
[11625]68      $query = sprintf(
69        'INSERT INTO %s
70        (`name`,`url`,`descr`,`visible`) VALUES
71        ("%s","%s","%s",%d)
72        ;',
73        COPYRIGHTS_ADMIN, $name, $url, $descr, $visible);
[11656]74      pwg_query($query); // Execute the query
[11625]75    }
[10874]76  }
77
[11656]78  // Edit an existing copyright
[10874]79  if ($_GET['tab'] == 'edit') {
[11656]80    $edit = 1; // Show the edit page
81    $CRid = $_REQUEST['id']; // Fetch the id of the copyright to be edited
82
83    // Fetch the current attributes to the copyright
[10931]84    $query = sprintf(
85      'SELECT *
86      FROM %s
87      WHERE `cr_id`=%d
88      ;',
89      COPYRIGHTS_ADMIN, $CRid);
[10874]90    $result = pwg_query($query);
91    $row = pwg_db_fetch_assoc($result);
[11656]92
93    // Save the attributes in convenient variables
[10874]94    $CRname = $row['name'];
95    $CRurl = $row['url'];
[11084]96    $CRdescr = $row['descr'];
[10874]97    $CRvisible = $row['visible'];
98  }
99
[11656]100  // Update an existing copyright
[10874]101  if ($_GET['tab'] == 'update') {
[11656]102    // Fetch the values from the edit form
[10931]103    $id = pwg_db_real_escape_string($_REQUEST['id']);
104    $name = pwg_db_real_escape_string($_REQUEST['name']);
105    $url = pwg_db_real_escape_string($_REQUEST['url']);
[11084]106    $descr= pwg_db_real_escape_string($_REQUEST['descr']);
[10874]107    $visible = (isset($_REQUEST['visible']) ? 1 : 0);
[11656]108
109    // Compose a query to update the copyright
[10931]110    $query = sprintf(
111      'UPDATE %s
[11084]112      SET `name`="%s", `url`="%s", `descr`="%s", `visible`=%d
[10931]113      WHERE `cr_id`=%d
114      ;',
[11084]115      COPYRIGHTS_ADMIN, $name, $url, $descr, $visible, $id);
[11656]116    pwg_query($query); // Execute the query
[10874]117  }
[11656]118
119  // Delete an existing copyright
[10874]120  if ($_GET['tab'] == 'delete') {
[11656]121    $id = $_REQUEST['id']; // Fetch the id of the copyright to be deleted
122
123    // Compose a query to delete the copyright
[10931]124    $query = sprintf(
125      'DELETE FROM %s
126      WHERE `cr_id`=%d
127      ;',
128      COPYRIGHTS_ADMIN, $id);
[11656]129    pwg_query($query); // Execute the query
[10874]130  }
131}
132
[11656]133/* Assign variables to the template */
[10874]134global $template;
135
[11656]136// Add the admin.tpl template
[10874]137$template->set_filenames(
138  array(
139    'plugin_admin_content' => dirname(__FILE__).'/admin.tpl'
140  )
141);
142
[11656]143// Select the existing copyrights
[10931]144$query = sprintf(
145  'SELECT *
146  FROM %s
147  ;',
[11084]148  COPYRIGHTS_ADMIN);
[10874]149$result = pwg_query($query);
150
[11656]151// Append the copyrights to the Smarty array
[10874]152while ($row = pwg_db_fetch_assoc($result)) {
153  $template->append(
154    'CRs',
155    array(
156      'cr_id'   => $row['cr_id'],
157      'name'    => $row['name'],
158      'url'     => $row['url'],
[11084]159      'descr'    => $row['descr'],
[10874]160      'visible' => $row['visible']
161    )
162  );
163}
164
[11656]165// Assign the path for URL forming
[10874]166$template->assign(
167  'COPYRIGHTS_PATH',
168  COPYRIGHTS_WEB_PATH
169);
170
[11656]171// Assign all the variables we constructed above
[10874]172$template->assign('edit', $edit);
173$template->assign('CRid', $CRid);
174$template->assign('CRname', $CRname);
175$template->assign('CRurl', $CRurl);
[11084]176$template->assign('CRdescr', $CRdescr);
[10874]177$template->assign('CRvisible', $CRvisible);
178
[11656]179// Get it up and running
[10874]180$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
181
182?>
Note: See TracBrowser for help on using the repository browser.