source: extensions/delete_hd/main.inc.php @ 31953

Revision 12662, 2.8 KB checked in by plg, 8 years ago (diff)

initial version

Line 
1<?php
2/*
3Plugin Name: Delete HD
4Version: auto
5Description: delete the HD (high definition) version of any photo from the Batch Manager
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=590
7Author: plg (Pierrick Le Gall)
8Author URI: http://le-gall.net/pierrick
9*/
10if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
11
12add_event_handler('loc_begin_element_set_global', 'delete_hd_set_template_data');
13add_event_handler('element_set_global_action', 'delete_hd_element_action', 50, 2);
14
15function delete_hd_set_template_data()
16{
17  global $template,$lang;
18 
19  load_language('plugin.lang', dirname(__FILE__).'/');
20 
21  $template->set_filename('delete_hd', realpath(dirname(__FILE__).'/delete_hd.tpl'));
22 
23  $template->append(
24    'element_set_global_plugins_actions',
25    array(
26      'ID' => 'delete_hd',
27      'NAME' => l10n('Delete High Definition Photos'),
28      'CONTENT' => $template->parse('delete_hd', true))
29    );
30}
31
32function delete_hd_element_action($action, $collection)
33{
34  global $template, $page;
35
36  if ('delete_hd' == $action)
37  {
38    if (isset($_POST['confirm_deletion']) and 1 == $_POST['confirm_deletion'])
39    {
40      $deleted_ids = delete_hd_files($collection);
41
42      if (count($deleted_ids) > 0)
43      {
44        array_push($page['infos'], l10n('High definition photos were deleted'));
45      }
46      else
47      {
48        array_push($page['errors'], l10n('No high definition photo can be deleted'));
49      }
50    }
51    else
52    {
53      array_push($page['errors'], l10n('You need to confirm deletion'));
54    }
55
56  }
57}
58
59// Deletes all HD files (on disk) related to given image ids
60// @return image ids where HD files are deleted successfully
61//
62// code based on admin function delete_element_files
63function delete_hd_files($ids)
64{
65  if (count($ids) == 0)
66  {
67    return array();;
68  }
69
70  include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
71 
72  $new_ids = array();
73
74  $query = '
75SELECT
76    id,
77    path,
78    has_high
79  FROM '.IMAGES_TABLE.'
80  WHERE id IN ('.implode(',', $ids).')
81;';
82  $result = pwg_query($query);
83  while ($row = pwg_db_fetch_assoc($result))
84  {
85    if (url_is_remote($row['path']))
86    {
87      continue;
88    }
89   
90    $files = array();
91   
92    if (!empty($row['has_high']) and get_boolean($row['has_high']))
93    {
94      $files[] = get_high_path($row);
95    }
96   
97    $ok = true;
98    foreach ($files as $path)
99    {
100      if (is_file($path) and !unlink($path))
101      {
102        $ok = false;
103        trigger_error('"'.$path.'" cannot be removed', E_USER_WARNING);
104        break;
105      }
106    }
107   
108    if ($ok)
109    {
110      $new_ids[] += $row['id'];
111    }
112  }
113
114  if (count($new_ids) > 0)
115  {
116    $query = '
117UPDATE '.IMAGES_TABLE.'
118  SET has_high = NULL,
119    high_filesize = NULL,
120    high_width = NULL,
121    high_height = NULL
122  WHERE id IN ('.implode(',', $new_ids).')
123;';
124    pwg_query($query);
125  }
126 
127  return $new_ids;
128}
129?>
Note: See TracBrowser for help on using the repository browser.