source: extensions/regenerateThumbnails/main.inc.php @ 9922

Last change on this file since 9922 was 9922, checked in by patdenice, 14 years ago

Add tab to thumbnail page.

File size: 4.6 KB
Line 
1<?php
2/*
3Plugin Name: Thumbnails Regeneration
4Version: 2.2.0
5Description: Regenerate thumbnails
6Author: P@t
7*/
8
9if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
10define('REGENERATE_THUMBNAILS_PATH' , PHPWG_PLUGINS_PATH . basename(dirname(__FILE__)) . '/');
11
12add_event_handler('loc_begin_admin', 'rg_add_thumbnails_tab');
13add_event_handler('loc_begin_element_set_global', 'rg_element_set_global_add_action');
14add_event_handler('ws_add_methods', 'add_regenerate_thumbnails_method');
15
16function rg_add_thumbnails_tab()
17{
18  global $page;
19
20  if (isset($_GET['page']) and in_array($_GET['page'], array('thumbnail', 'plugin-regenerateThumbnails')))
21  {
22    load_language('plugin.lang', REGENERATE_THUMBNAILS_PATH);
23    include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
24
25    if ($_GET['page'] == 'plugin-regenerateThumbnails')
26      $page['tab'] = 'regenerateThumbnails';
27    else
28      $page['tab'] = 'thumbnail';
29
30    $tabsheet = new tabsheet();
31    $tabsheet->add('thumbnail', l10n('Thumbnail creation'), get_root_url().'admin.php?page=thumbnail');
32    $tabsheet->add('regenerateThumbnails', l10n('Regenerate Thumbnails'), get_root_url().'admin.php?page=plugin-'.basename(dirname(__FILE__)));
33    $tabsheet->select($page['tab']);
34    $tabsheet->assign();
35  }
36}
37
38function rg_element_set_global_add_action()
39{
40  global $template, $conf, $page, $pwg_loaded_plugins;
41
42  load_language('plugin.lang', REGENERATE_THUMBNAILS_PATH);
43  include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
44
45  $template->set_filename('regenerateThumbnails', realpath(REGENERATE_THUMBNAILS_PATH.'element_set_global_action.tpl'));
46
47  if (isset($_SESSION['regeneratedThumbnail']))
48  {
49    $template->delete_compiled_templates();
50    array_push($page['infos'], l10n('Thumbnails have been regenerated'));
51    unset($_SESSION['regeneratedThumbnail']);
52  }
53
54  foreach (get_upload_form_config() as $param_shortname => $param)
55  {
56    $param_name = 'upload_form_'.$param_shortname;
57    $form_values[$param_shortname] = $conf[$param_name];
58  }
59
60  if (isset($pwg_loaded_plugins['square_thumbnails']))
61  {
62    load_language('plugin.lang', SQUARE_THUMB_PATH);
63    $template->assign(array('SQUARE' => @$conf['upload_form_thumb_square']));
64  }
65
66  $redirect_url = get_root_url().'admin.php?page='.$_GET['page'];
67  if ($_GET['page'] == 'plugin')
68    $redirect_url .= '-regenerateThumbnails';
69 
70  $template->assign(array(
71    'upload_form_settings' => $form_values,
72    'all_elements' => $page['cat_elements_id'],
73    'redirect_url' => $redirect_url,
74    )
75  );
76
77  if ($template->get_template_vars('redirect_url') == null)
78    $template->assign('redirect_url', get_root_url().'admin.php?page='.$_GET['page']);
79
80  $template->append('element_set_global_plugins_actions', array(
81    'ID' => 'regenerateThumbnails',
82    'NAME' => l10n('Regenerate Thumbnails'),
83    'CONTENT' => $template->parse('regenerateThumbnails', true),
84    )
85  );
86
87  $template->set_prefilter('batch_manager_global', 'regenerate_prefilter');
88}
89
90function add_regenerate_thumbnails_method($arr)
91{
92  global $conf;
93
94  $service = &$arr[0];
95  $service->addMethod(
96    'pwg.images.regenerateThumbnail',
97    'ws_images_regenerateThumbnail',
98    array(
99      'image_id'  => array(),
100      'maxwidth'  => array('default'=>$conf['upload_form_thumb_maxwidth']),
101      'maxheight' => array('default'=>$conf['upload_form_thumb_maxheight']),
102      'quality'   => array('default'=>$conf['upload_form_thumb_quality']),
103      'square'    => array('default'=>@$conf['upload_form_thumb_square']),
104    ),
105    'Regenerate a thumbnail with given arguments.'
106  );
107}
108
109function ws_images_regenerateThumbnail($params, &$service)
110{
111  global $conf;
112
113  if (!is_admin())
114    return new PwgError(401, 'Access denied');
115
116  $query='
117SELECT id, path
118FROM '.IMAGES_TABLE.'
119WHERE id = '.(int)$params['image_id'].'
120;';
121
122  $image = pwg_db_fetch_assoc(pwg_query($query));
123  if ($image == null)
124    return new PwgError(404, "image_id not found");
125
126  include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
127
128  if ($params['square'] == 'true' or $params['square'] == 'false')
129    $params['square'] = get_boolean($params['square']);
130  if (!$params['square'])
131    remove_event_handler('upload_thumbnail_resize', 'upload_square_resize', 40);
132 
133  trigger_event(
134    'upload_thumbnail_resize',
135    false,
136    $image['path'],
137    file_path_for_type($image['path'], 'thumb'),
138    $params['maxwidth'],
139    $params['maxheight'],
140    $params['quality'],
141    true
142    );
143
144  $_SESSION['regeneratedThumbnail'] = true;
145
146  return true;
147}
148
149function regenerate_prefilter($content, $smarty)
150{
151  return str_replace('{$thumbnail.TN_SRC}', '{$thumbnail.TN_SRC}?rand='.md5(uniqid(rand(), true)), $content);
152}
153
154?>
Note: See TracBrowser for help on using the repository browser.