source: extensions/Back2Front/include/Back2Front.php

Last change on this file was 29253, checked in by mistic100, 10 years ago

update for 2.7

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