source: extensions/Back2Front/Back2Front.php @ 21212

Last change on this file since 21212 was 21212, checked in by mistic100, 11 years ago

update for Piwigo 2.5, multisize is disabled for photos with verso

File size: 11.6 KB
Line 
1<?php 
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
4include_once(B2F_PATH.'functions.inc.php');
5
6/*
7 * Add verso link on picture page
8 */
9function Back2Front_picture_content($content, $element_info)
10{
11  global $template, $user, $conf;
12
13  /* search for a verso picture */
14  $query = "
15    SELECT i.*
16    FROM ".IMAGES_TABLE." as i
17      INNER JOIN ".B2F_TABLE." as v
18      ON i.id = v.verso_id
19      AND v.image_id = ".$element_info['id']."
20  ;";
21  $result = pwg_query($query);
22
23  if (pwg_db_num_rows($result)) 
24  {
25    $verso = pwg_db_fetch_assoc($result);
26    $conf['back2front'] = explode(',',$conf['back2front']);
27    $deriv_type = pwg_get_session_var('picture_deriv', $conf['derivative_default_size']);
28   
29    $verso['src_image'] = new SrcImage($verso);
30    $verso['derivatives'] = DerivativeImage::get_all($verso['src_image']);
31    $verso['element_path'] = get_element_path($verso);
32    $verso['selected_derivative'] = $verso['derivatives'][$deriv_type];
33   
34    /* websize picture */
35    $template->assign(array(
36      'B2F_PATH' => B2F_PATH,
37      'verso' => $verso,
38      ));
39   
40    /* admin link */
41    if (is_admin())
42    {
43      $template->assign('VERSO_U_ADMIN', get_root_url().'admin.php?page=photo-'.$verso['id']);
44      $template->set_filename('B2F_admin_button', dirname(__FILE__).'/template/admin_button.tpl');
45      $template->concat('PLUGIN_PICTURE_ACTIONS', $template->parse('B2F_admin_button', true));
46    }
47   
48    /* link name */
49    $conf['back2front'][4] = unserialize($conf['back2front'][4]);
50    if (!empty($conf['back2front'][4][$user['language']]))
51    {
52      if (strpos($conf['back2front'][4][$user['language']], '|') !== false)
53      {
54        $conf['back2front'][4] = explode('|', $conf['back2front'][4][$user['language']]);
55      }
56      else
57      {
58        $conf['back2front'][4] = array($conf['back2front'][4][$user['language']], $conf['back2front'][4][$user['language']]);
59      }
60    }
61    else if (!empty($conf['back2front'][4]['default']))
62    {
63      if (strpos($conf['back2front'][4]['default'], '|') != false)
64      {
65        $conf['back2front'][4] = explode('|', $conf['back2front'][4]['default']);
66      }
67      else
68      {
69        $conf['back2front'][4] = array($conf['back2front'][4]['default'], $conf['back2front'][4]['default']);
70      }
71    }
72    else
73    {
74      $conf['back2front'][4] = array(l10n('See back'), l10n('See front'));
75    }
76   
77    if ($conf['back2front'][2] == 'fade' and $conf['back2front'][3] ==  'bottom')
78    {
79      $conf['back2front'][3] = 'top';
80    }
81   
82
83    /* template & output */
84    $template->set_filename('B2F_picture_content', dirname(__FILE__).'/template/picture_content.tpl');   
85    $template->assign(array(
86      'b2f_switch_mode' => $conf['back2front'][1],
87      'b2f_transition' => $conf['back2front'][2],
88      'b2f_position' => $conf['back2front'][3],
89      'b2f_see_back' => $conf['back2front'][4][0],
90      'b2f_see_front' => $conf['back2front'][4][1],
91    ));
92   
93    switch ($conf['back2front'][3])
94    {
95      case 'toolbar':
96        $template->concat('PLUGIN_PICTURE_ACTIONS', $template->parse('B2F_picture_content', true));
97        break;
98      case 'top':
99        return $template->parse('B2F_picture_content', true)."\n".$content;
100        break;
101      case 'bottom':
102        return $content."\n".$template->parse('B2F_picture_content', true);
103        break;
104    }   
105  }
106 
107  return $content;
108}
109
110
111/*
112 * Add field on picture modify page
113 */
114function Back2Front_picture_modify()
115{
116  global $page, $template, $conf;
117 
118  if ($page['page'] != 'photo') return;
119  if (isset($_GET['tab']) && $_GET['tab']!='properties') return;
120 
121 
122  $conf['back2front'] = explode(',',$conf['back2front']);
123 
124/* SAVE VALUES */
125  if (isset($_POST['b2f_submit']))
126  {
127    /* catch all verso and recto ids and original categories */
128    $query = "SELECT image_id, verso_id, categories
129      FROM ".B2F_TABLE.";";
130    $rectos = array_from_query($query, 'image_id');
131    $versos = array_from_query($query, 'verso_id');
132    $cats = array_from_query($query, 'categories');
133   
134    if (count($rectos) != 0)
135    {
136      $all_recto_verso = array_combine($rectos, $versos);
137      $verso_cats = array_combine($versos, $cats);
138    }
139    else
140    {
141      $all_recto_verso = array(0=>0);
142      $verso_cats = array(0=>NULL);
143    }
144    unset($rectos, $versos, $cats);
145   
146    /* picture is verso */
147    if (isset($_POST['b2f_is_verso']))
148    {     
149      /* verso don't exists */
150      if (!picture_exists($_POST['b2f_front_id']))
151      {
152        array_push(
153          $page['errors'], 
154          sprintf(
155            l10n('Unknown id %d for frontside picture'), 
156            $_POST['b2f_front_id']
157            )
158          );
159      }
160      /* verso same as recto  */
161      else if ($_POST['b2f_front_id'] == $_GET['image_id'])
162      {
163        array_push($page['errors'], l10n('Backside and frontside can\'t be the same picture'));
164      }
165      /* recto has already a verso */
166      else if (in_array($_POST['b2f_front_id'], array_keys($all_recto_verso)) AND $all_recto_verso[$_POST['b2f_front_id']] != $_GET['image_id'])
167      {
168          $recto_current_verso['id'] = $all_recto_verso[$_POST['b2f_front_id']];
169          $recto_current_verso['link'] = get_root_url().'admin.php?page=photo-'.$recto_current_verso['id'];
170          array_push(
171            $page['errors'], 
172            sprintf(
173              l10n('The picture n°%d has already a backside : %s'), 
174              $_POST['b2f_front_id'], 
175              '<a href="'.$recto_current_verso['link'].'">'.$recto_current_verso['id'].'</a>'
176              )
177            );
178      }
179      /* recto is already a verso */
180      else if (in_array($_POST['b2f_front_id'], array_values($all_recto_verso)))
181      {
182          $recto_is_verso['id'] = $_POST['b2f_front_id'];
183          $recto_is_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;image_id='.$recto_is_verso['id'];
184          array_push(
185            $page['errors'], 
186            sprintf(
187              l10n('The picture n°%s is already a backside'), 
188              '<a href="'.$recto_is_verso['link'].'">'.$recto_is_verso['id'].'</a>'
189              )
190            );
191      }
192      /* everything is fine */
193      else
194      {
195        // move the verso - if first save
196        if (isset($_POST['b2f_move_verso']) AND (!array_key_exists($_GET['image_id'], $verso_cats) OR $verso_cats[$_GET['image_id']] == NULL))
197        {
198          // get current categories
199          $query = "SELECT category_id FROM ".IMAGE_CATEGORY_TABLE." WHERE image_id = ".$_GET['image_id'].";";
200          $verso_categories = array_from_query($query, 'category_id');
201         
202          pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE."
203            WHERE image_id = ".$_GET['image_id'].";");
204          pwg_query("INSERT INTO ".IMAGE_CATEGORY_TABLE."(image_id, category_id)
205            VALUES(".$_GET['image_id'].", ".$conf['back2front'][0].");");
206           
207          // random representant for each categories
208          set_random_representant($verso_categories);
209         
210          $verso_categories = isset($verso_cats[$_GET['image_id']]) ? $verso_cats[$_GET['image_id']] : implode(',',$verso_categories);
211          $template->assign('B2F_MOVE_VERSO', 'checked="checked"');
212        }
213        // restore the verso - if precedently moved
214        else if (!isset($_POST['b2f_move_verso']) AND array_key_exists($_GET['image_id'], $verso_cats) AND $verso_cats[$_GET['image_id']] != NULL)
215        {
216          $item['verso_id'] = $_GET['image_id'];
217          $item['categories'] = $verso_cats[$_GET['image_id']];
218          back2front_restaure_categories($item);
219         
220          $verso_categories = 'NULL';
221          $template->assign('B2F_MOVE_VERSO', '');
222        }
223        // leave the verso
224        else
225        {
226          $verso_categories = isset($verso_cats[$_GET['image_id']]) ? $verso_cats[$_GET['image_id']] : 'NULL';
227          $template->assign('B2F_MOVE_VERSO', isset($verso_cats[$_GET['image_id']]) ? 'checked="checked"' : '');
228        }
229       
230        // insert or update verso associations
231        pwg_query("INSERT INTO ".B2F_TABLE."
232          VALUES(".$_POST['b2f_front_id'].", ".$_GET['image_id'].", '".$verso_categories."')
233          ON DUPLICATE KEY UPDATE image_id = ".$_POST['b2f_front_id'].", categories = '".$verso_categories."';");
234     
235        $template->assign(array(
236          'B2F_IS_VERSO' => 'checked="checked"',
237          'B2F_FRONT_ID' => $_POST['b2f_front_id'],
238        ));
239       
240        $verso['id'] = $_POST['b2f_front_id'];
241        $verso['link'] = get_root_url().'admin.php?page=photo-'.$verso['id'];
242        array_push($page['infos'], l10n_args(get_l10n_args('This picture is now the backside of the picture n°%s', '<a href="'.$verso['link'].'">'.$verso['id'].'</a>')));
243      }
244    }
245    /* picture isn't verso */
246    else
247    {
248      /* search if it was a verso */
249      $query = "SELECT categories
250        FROM ".B2F_TABLE."
251        WHERE verso_id = ".$_GET['image_id'].";";
252      $result = pwg_query($query);
253     
254      /* it must be restored to its original categories (see criteria on maintain.inc) */
255      if (pwg_db_num_rows($result))
256      {
257        $item['verso_id'] = $_GET['image_id'];
258        list($item['categories']) = pwg_db_fetch_row($result);
259        back2front_restaure_categories($item);
260       
261        pwg_query("DELETE FROM ".B2F_TABLE."
262          WHERE verso_id = ".$_GET['image_id'].";");
263         
264        array_push($page['infos'], l10n('This picture is no longer a backside'));
265      }
266    }
267  }
268 
269/* GET SAVED VALUES */
270  if ($template->get_template_vars('B2F_IS_VERSO') == null)
271  {
272    $template->assign('B2F_MOVE_VERSO', 'checked="checked"');
273   
274    /* is the picture a verso ? */
275    $query = "
276      SELECT image_id, categories
277      FROM ".B2F_TABLE."
278      WHERE verso_id = ".$_GET['image_id']."
279    ;";
280    $result = pwg_query($query);
281   
282    if (pwg_db_num_rows($result))
283    {
284      list($recto_id, $cats) = pwg_db_fetch_row($result);
285      $template->assign(array(
286        'B2F_IS_VERSO' => 'checked="checked"',
287        'B2F_FRONT_ID' => $recto_id,
288        'B2F_MOVE_VERSO' => $cats != NULL ? 'checked="checked"' : '',
289      ));
290    }
291    /* is the picture a front ? */
292    else
293    {
294      $query = "SELECT verso_id
295        FROM ".B2F_TABLE."
296        WHERE image_id = ".$_GET['image_id'].";";
297      $result = pwg_query($query);
298     
299      if (pwg_db_num_rows($result))
300      {     
301        $item = pwg_db_fetch_assoc($result);
302
303        $template->assign(array(
304          'B2F_VERSO_ID' => $item['verso_id'],
305          'B2F_VERSO_URL' => get_root_url().'admin.php?page=photo-'.$item['verso_id'],
306        ));
307      }
308    }
309  }
310 
311  $template->set_prefilter('picture_modify', 'Back2front_picture_modify_prefilter');
312}
313
314
315function Back2front_picture_modify_prefilter($content, &$smarty)
316{
317  $search = '</form>';
318  $replacement = $search."\n\n".file_get_contents(B2F_PATH.'template/picture_modify.tpl');
319  return str_replace($search, $replacement, $content);
320}
321
322
323/*
324 * Add mark on thumbnails list
325 */
326function Back2Front_thumbnails($tpl_thumbnails_var)
327{
328  global $conf, $selection;
329 
330  $conf['back2front'] = explode(',',$conf['back2front']);
331  if (!$conf['back2front'][5]) return $tpl_thumbnails_var;
332  if (empty($tpl_thumbnails_var)) return $tpl_thumbnails_var;
333   
334  /* has the pictures a verso ? */
335  $query = "SELECT image_id
336    FROM ".B2F_TABLE."
337    WHERE image_id IN(".implode(',', $selection).");";
338  $ids = array_from_query($query, 'image_id');
339 
340  $root_path = get_absolute_root_url();
341 
342  foreach($tpl_thumbnails_var as &$tpl_var)
343  {
344    if (in_array($tpl_var['id'], $ids))
345    {
346      $tpl_var['NAME'].= ' <img class="has_verso" src="'.$root_path.B2F_PATH.'template/rotate_1.png" title="'.l10n('This picture has a backside :').'"/>';
347    }
348  }
349 
350  return $tpl_thumbnails_var;
351}
352
353?>
Note: See TracBrowser for help on using the repository browser.