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
RevLine 
[10819]1<?php 
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
[10852]4include_once(B2F_PATH.'functions.inc.php');
5
[10819]6/*
7 * Add verso link on picture page
8 */
[21212]9function Back2Front_picture_content($content, $element_info)
[12361]10{
[10852]11  global $template, $user, $conf;
[10819]12
13  /* search for a verso picture */
14  $query = "
[21212]15    SELECT i.*
[10819]16    FROM ".IMAGES_TABLE." as i
17      INNER JOIN ".B2F_TABLE." as v
18      ON i.id = v.verso_id
[21212]19      AND v.image_id = ".$element_info['id']."
[10819]20  ;";
21  $result = pwg_query($query);
22
23  if (pwg_db_num_rows($result)) 
24  {
25    $verso = pwg_db_fetch_assoc($result);
[10852]26    $conf['back2front'] = explode(',',$conf['back2front']);
[21212]27    $deriv_type = pwg_get_session_var('picture_deriv', $conf['derivative_default_size']);
[10852]28   
[21212]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   
[10819]34    /* websize picture */
[12361]35    $template->assign(array(
36      'B2F_PATH' => B2F_PATH,
[21212]37      'verso' => $verso,
[12361]38      ));
[10819]39   
40    /* admin link */
41    if (is_admin())
42    {
[21212]43      $template->assign('VERSO_U_ADMIN', get_root_url().'admin.php?page=photo-'.$verso['id']);
[10819]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    }
[11219]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    }
[21212]76   
77    if ($conf['back2front'][2] == 'fade' and $conf['back2front'][3] ==  'bottom')
78    {
79      $conf['back2front'][3] = 'top';
80    }
81   
[10819]82
83    /* template & output */
[12361]84    $template->set_filename('B2F_picture_content', dirname(__FILE__).'/template/picture_content.tpl');   
[11219]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    ));
[10819]92   
[11219]93    switch ($conf['back2front'][3])
94    {
95      case 'toolbar':
[12361]96        $template->concat('PLUGIN_PICTURE_ACTIONS', $template->parse('B2F_picture_content', true));
[11219]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    }   
[10819]105  }
[11219]106 
107  return $content;
[10819]108}
109
110
111/*
112 * Add field on picture modify page
113 */
[11219]114function Back2Front_picture_modify()
[10819]115{
[10821]116  global $page, $template, $conf;
[10852]117 
[21212]118  if ($page['page'] != 'photo') return;
119  if (isset($_GET['tab']) && $_GET['tab']!='properties') return;
120 
121 
[10821]122  $conf['back2front'] = explode(',',$conf['back2front']);
[10819]123 
[10823]124/* SAVE VALUES */
125  if (isset($_POST['b2f_submit']))
126  {
[10852]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   
[10823]146    /* picture is verso */
147    if (isset($_POST['b2f_is_verso']))
[10852]148    {     
[10823]149      /* verso don't exists */
150      if (!picture_exists($_POST['b2f_front_id']))
[10819]151      {
[11599]152        array_push(
153          $page['errors'], 
154          sprintf(
155            l10n('Unknown id %d for frontside picture'), 
156            $_POST['b2f_front_id']
157            )
158          );
[10823]159      }
160      /* verso same as recto  */
161      else if ($_POST['b2f_front_id'] == $_GET['image_id'])
162      {
[10852]163        array_push($page['errors'], l10n('Backside and frontside can\'t be the same picture'));
[10823]164      }
165      /* recto has already a verso */
[10852]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'])
[10823]167      {
168          $recto_current_verso['id'] = $all_recto_verso[$_POST['b2f_front_id']];
[21212]169          $recto_current_verso['link'] = get_root_url().'admin.php?page=photo-'.$recto_current_verso['id'];
[11599]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            );
[10823]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'];
[10828]183          $recto_is_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;image_id='.$recto_is_verso['id'];
[11599]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            );
[10823]191      }
192      /* everything is fine */
193      else
194      {
[10852]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))
[10819]197        {
[10852]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         
[10823]202          pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE."
203            WHERE image_id = ".$_GET['image_id'].";");
[10981]204          pwg_query("INSERT INTO ".IMAGE_CATEGORY_TABLE."(image_id, category_id)
205            VALUES(".$_GET['image_id'].", ".$conf['back2front'][0].");");
[10821]206           
[10823]207          // random representant for each categories
208          set_random_representant($verso_categories);
[10852]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"');
[10819]212        }
[10852]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."')
[12648]233          ON DUPLICATE KEY UPDATE image_id = ".$_POST['b2f_front_id'].", categories = '".$verso_categories."';");
[10823]234     
235        $template->assign(array(
236          'B2F_IS_VERSO' => 'checked="checked"',
237          'B2F_FRONT_ID' => $_POST['b2f_front_id'],
238        ));
239       
[10852]240        $verso['id'] = $_POST['b2f_front_id'];
[21212]241        $verso['link'] = get_root_url().'admin.php?page=photo-'.$verso['id'];
[10852]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>')));
[10823]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      {
[10852]257        $item['verso_id'] = $_GET['image_id'];
[10823]258        list($item['categories']) = pwg_db_fetch_row($result);
[10852]259        back2front_restaure_categories($item);
[10819]260       
[10823]261        pwg_query("DELETE FROM ".B2F_TABLE."
262          WHERE verso_id = ".$_GET['image_id'].";");
[10821]263         
[10852]264        array_push($page['infos'], l10n('This picture is no longer a backside'));
[10819]265      }
266    }
[10823]267  }
268 
269/* GET SAVED VALUES */
270  if ($template->get_template_vars('B2F_IS_VERSO') == null)
271  {
[10852]272    $template->assign('B2F_MOVE_VERSO', 'checked="checked"');
273   
[10823]274    /* is the picture a verso ? */
275    $query = "
[10852]276      SELECT image_id, categories
[10823]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    {
[10852]284      list($recto_id, $cats) = pwg_db_fetch_row($result);
[10823]285      $template->assign(array(
286        'B2F_IS_VERSO' => 'checked="checked"',
287        'B2F_FRONT_ID' => $recto_id,
[10852]288        'B2F_MOVE_VERSO' => $cats != NULL ? 'checked="checked"' : '',
[10823]289      ));
290    }
291    /* is the picture a front ? */
[10819]292    else
293    {
[10823]294      $query = "SELECT verso_id
[10819]295        FROM ".B2F_TABLE."
[10823]296        WHERE image_id = ".$_GET['image_id'].";";
[10819]297      $result = pwg_query($query);
298     
299      if (pwg_db_num_rows($result))
[10852]300      {     
[10823]301        $item = pwg_db_fetch_assoc($result);
[10852]302
[10819]303        $template->assign(array(
[10852]304          'B2F_VERSO_ID' => $item['verso_id'],
[21212]305          'B2F_VERSO_URL' => get_root_url().'admin.php?page=photo-'.$item['verso_id'],
[10819]306        ));
307      }
308    }
309  }
[10823]310 
[10852]311  $template->set_prefilter('picture_modify', 'Back2front_picture_modify_prefilter');
[10819]312}
313
[10852]314
315function Back2front_picture_modify_prefilter($content, &$smarty)
[10819]316{
[21212]317  $search = '</form>';
318  $replacement = $search."\n\n".file_get_contents(B2F_PATH.'template/picture_modify.tpl');
[10852]319  return str_replace($search, $replacement, $content);
[10819]320}
321
[12361]322
323/*
324 * Add mark on thumbnails list
325 */
[21212]326function Back2Front_thumbnails($tpl_thumbnails_var)
[12361]327{
[21212]328  global $conf, $selection;
[12361]329 
330  $conf['back2front'] = explode(',',$conf['back2front']);
331  if (!$conf['back2front'][5]) return $tpl_thumbnails_var;
[21212]332  if (empty($tpl_thumbnails_var)) return $tpl_thumbnails_var;
[12361]333   
334  /* has the pictures a verso ? */
[21212]335  $query = "SELECT image_id
[12361]336    FROM ".B2F_TABLE."
[21212]337    WHERE image_id IN(".implode(',', $selection).");";
338  $ids = array_from_query($query, 'image_id');
[12361]339 
[21212]340  $root_path = get_absolute_root_url();
[12361]341 
342  foreach($tpl_thumbnails_var as &$tpl_var)
343  {
[21212]344    if (in_array($tpl_var['id'], $ids))
[12361]345    {
[21212]346      $tpl_var['NAME'].= ' <img class="has_verso" src="'.$root_path.B2F_PATH.'template/rotate_1.png" title="'.l10n('This picture has a backside :').'"/>';
[12361]347    }
348  }
349 
350  return $tpl_thumbnails_var;
351}
352
[10819]353?>
Note: See TracBrowser for help on using the repository browser.