source: extensions/Back2Front/Back2Front.php @ 14963

Last change on this file since 14963 was 14963, checked in by mistic100, 12 years ago

fix exceptional sql error

File size: 11.4 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, $image)
10{
11  global $template, $user, $conf;
12
13  /* search for a verso picture */
14  $query = "
15    SELECT
16      i.id,
17      i.path,
18      i.has_high,
19      i.width,
20      i.height
21    FROM ".IMAGES_TABLE." as i
22      INNER JOIN ".B2F_TABLE." as v
23      ON i.id = v.verso_id
24    WHERE
25      v.image_id = ".$image['id']."
26  ;";
27  $result = pwg_query($query);
28
29  if (pwg_db_num_rows($result)) 
30  {
31    $verso = pwg_db_fetch_assoc($result);
32    $conf['back2front'] = explode(',',$conf['back2front']);
33   
34    /* websize picture */
35    $template->assign(array(
36      'B2F_PATH' => B2F_PATH,
37      'VERSO_URL' => $verso['path'],
38      ));
39   
40    /* admin link */
41    if (is_admin())
42    {
43      $template->assign('VERSO_U_ADMIN', get_root_url().'admin.php?page=picture_modify&amp;image_id='.$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    /* high picture */
49    if ($verso['has_high'])
50    {
51      $template->assign('VERSO_HD', get_high_url($verso));
52    }
53   
54    /* link name */
55    $conf['back2front'][4] = unserialize($conf['back2front'][4]);
56    if (!empty($conf['back2front'][4][$user['language']]))
57    {
58      if (strpos($conf['back2front'][4][$user['language']], '|') !== false)
59      {
60        $conf['back2front'][4] = explode('|', $conf['back2front'][4][$user['language']]);
61      }
62      else
63      {
64        $conf['back2front'][4] = array($conf['back2front'][4][$user['language']], $conf['back2front'][4][$user['language']]);
65      }
66    }
67    else if (!empty($conf['back2front'][4]['default']))
68    {
69      if (strpos($conf['back2front'][4]['default'], '|') != false)
70      {
71        $conf['back2front'][4] = explode('|', $conf['back2front'][4]['default']);
72      }
73      else
74      {
75        $conf['back2front'][4] = array($conf['back2front'][4]['default'], $conf['back2front'][4]['default']);
76      }
77    }
78    else
79    {
80      $conf['back2front'][4] = array(l10n('See back'), l10n('See front'));
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'] != 'picture_modify') return;
119  $conf['back2front'] = explode(',',$conf['back2front']);
120 
121/* SAVE VALUES */
122  if (isset($_POST['b2f_submit']))
123  {
124    /* catch all verso and recto ids and original categories */
125    $query = "SELECT image_id, verso_id, categories
126      FROM ".B2F_TABLE.";";
127    $rectos = array_from_query($query, 'image_id');
128    $versos = array_from_query($query, 'verso_id');
129    $cats = array_from_query($query, 'categories');
130   
131    if (count($rectos) != 0)
132    {
133      $all_recto_verso = array_combine($rectos, $versos);
134      $verso_cats = array_combine($versos, $cats);
135    }
136    else
137    {
138      $all_recto_verso = array(0=>0);
139      $verso_cats = array(0=>NULL);
140    }
141    unset($rectos, $versos, $cats);
142   
143    /* picture is verso */
144    if (isset($_POST['b2f_is_verso']))
145    {     
146      /* verso don't exists */
147      if (!picture_exists($_POST['b2f_front_id']))
148      {
149        array_push(
150          $page['errors'], 
151          sprintf(
152            l10n('Unknown id %d for frontside picture'), 
153            $_POST['b2f_front_id']
154            )
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)) AND $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=picture_modify&amp;image_id='.$recto_current_verso['id'];
167          array_push(
168            $page['errors'], 
169            sprintf(
170              l10n('The picture n°%d has already a backside : %s'), 
171              $_POST['b2f_front_id'], 
172              '<a href="'.$recto_current_verso['link'].'">'.$recto_current_verso['id'].'</a>'
173              )
174            );
175      }
176      /* recto is already a verso */
177      else if (in_array($_POST['b2f_front_id'], array_values($all_recto_verso)))
178      {
179          $recto_is_verso['id'] = $_POST['b2f_front_id'];
180          $recto_is_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;image_id='.$recto_is_verso['id'];
181          array_push(
182            $page['errors'], 
183            sprintf(
184              l10n('The picture n°%s is already a backside'), 
185              '<a href="'.$recto_is_verso['link'].'">'.$recto_is_verso['id'].'</a>'
186              )
187            );
188      }
189      /* everything is fine */
190      else
191      {
192        // move the verso - if first save
193        if (isset($_POST['b2f_move_verso']) AND (!array_key_exists($_GET['image_id'], $verso_cats) OR $verso_cats[$_GET['image_id']] == NULL))
194        {
195          // get current categories
196          $query = "SELECT category_id FROM ".IMAGE_CATEGORY_TABLE." WHERE image_id = ".$_GET['image_id'].";";
197          $verso_categories = array_from_query($query, 'category_id');
198         
199          pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE."
200            WHERE image_id = ".$_GET['image_id'].";");
201          pwg_query("INSERT INTO ".IMAGE_CATEGORY_TABLE."(image_id, category_id)
202            VALUES(".$_GET['image_id'].", ".$conf['back2front'][0].");");
203           
204          // random representant for each categories
205          set_random_representant($verso_categories);
206         
207          $verso_categories = isset($verso_cats[$_GET['image_id']]) ? $verso_cats[$_GET['image_id']] : implode(',',$verso_categories);
208          $template->assign('B2F_MOVE_VERSO', 'checked="checked"');
209        }
210        // restore the verso - if precedently moved
211        else if (!isset($_POST['b2f_move_verso']) AND array_key_exists($_GET['image_id'], $verso_cats) AND $verso_cats[$_GET['image_id']] != NULL)
212        {
213          $item['verso_id'] = $_GET['image_id'];
214          $item['categories'] = $verso_cats[$_GET['image_id']];
215          back2front_restaure_categories($item);
216         
217          $verso_categories = 'NULL';
218          $template->assign('B2F_MOVE_VERSO', '');
219        }
220        // leave the verso
221        else
222        {
223          $verso_categories = isset($verso_cats[$_GET['image_id']]) ? $verso_cats[$_GET['image_id']] : 'NULL';
224          $template->assign('B2F_MOVE_VERSO', isset($verso_cats[$_GET['image_id']]) ? 'checked="checked"' : '');
225        }
226       
227        // insert or update verso associations
228        pwg_query("INSERT INTO ".B2F_TABLE."
229          VALUES(".$_POST['b2f_front_id'].", ".$_GET['image_id'].", '".$verso_categories."')
230          ON DUPLICATE KEY UPDATE image_id = ".$_POST['b2f_front_id'].", categories = '".$verso_categories."';");
231     
232        $template->assign(array(
233          'B2F_IS_VERSO' => 'checked="checked"',
234          'B2F_FRONT_ID' => $_POST['b2f_front_id'],
235        ));
236       
237        $verso['id'] = $_POST['b2f_front_id'];
238        $verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;image_id='.$verso['id'];
239        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>')));
240      }
241    }
242    /* picture isn't verso */
243    else
244    {
245      /* search if it was a verso */
246      $query = "SELECT categories
247        FROM ".B2F_TABLE."
248        WHERE verso_id = ".$_GET['image_id'].";";
249      $result = pwg_query($query);
250     
251      /* it must be restored to its original categories (see criteria on maintain.inc) */
252      if (pwg_db_num_rows($result))
253      {
254        $item['verso_id'] = $_GET['image_id'];
255        list($item['categories']) = pwg_db_fetch_row($result);
256        back2front_restaure_categories($item);
257       
258        pwg_query("DELETE FROM ".B2F_TABLE."
259          WHERE verso_id = ".$_GET['image_id'].";");
260         
261        array_push($page['infos'], l10n('This picture is no longer a backside'));
262      }
263    }
264  }
265 
266/* GET SAVED VALUES */
267  if ($template->get_template_vars('B2F_IS_VERSO') == null)
268  {
269    $template->assign('B2F_MOVE_VERSO', 'checked="checked"');
270   
271    /* is the picture a verso ? */
272    $query = "
273      SELECT image_id, categories
274      FROM ".B2F_TABLE."
275      WHERE verso_id = ".$_GET['image_id']."
276    ;";
277    $result = pwg_query($query);
278   
279    if (pwg_db_num_rows($result))
280    {
281      list($recto_id, $cats) = pwg_db_fetch_row($result);
282      $template->assign(array(
283        'B2F_IS_VERSO' => 'checked="checked"',
284        'B2F_FRONT_ID' => $recto_id,
285        'B2F_MOVE_VERSO' => $cats != NULL ? 'checked="checked"' : '',
286      ));
287    }
288    /* is the picture a front ? */
289    else
290    {
291      $query = "SELECT verso_id
292        FROM ".B2F_TABLE."
293        WHERE image_id = ".$_GET['image_id'].";";
294      $result = pwg_query($query);
295     
296      if (pwg_db_num_rows($result))
297      {     
298        $item = pwg_db_fetch_assoc($result);
299
300        $template->assign(array(
301          'B2F_VERSO_ID' => $item['verso_id'],
302          'B2F_VERSO_URL' => get_root_url().'admin.php?page=picture_modify&amp;image_id='.$item['verso_id'],
303        ));
304      }
305    }
306  }
307 
308  $template->set_prefilter('picture_modify', 'Back2front_picture_modify_prefilter');
309}
310
311
312function Back2front_picture_modify_prefilter($content, &$smarty)
313{
314  $search = '<form id="associations"';
315  $replacement = file_get_contents(B2F_PATH.'template/picture_modify.tpl')."\n".$search;
316  return str_replace($search, $replacement, $content);
317}
318
319
320/*
321 * Add mark on thumbnails list
322 */
323function Back2Front_thumbnails($tpl_thumbnails_var, $pictures)
324{
325  global $conf;
326 
327  $conf['back2front'] = explode(',',$conf['back2front']);
328  if (!$conf['back2front'][5]) return $tpl_thumbnails_var;
329  if (empty($pictures)) return $tpl_thumbnails_var;
330 
331  $ids = array();
332  foreach ($pictures as $row)
333  {
334    array_push($ids, $row['id']);
335  }
336   
337  /* has the pictures a verso ? */
338  $query = "SELECT image_id, verso_id
339    FROM ".B2F_TABLE."
340    WHERE image_id IN(".implode(',', $ids).");";
341  $result = hash_from_query($query, 'image_id');
342 
343  $ids = array_keys($result);
344 
345  foreach($tpl_thumbnails_var as &$tpl_var)
346  {
347    if (in_array($tpl_var['ID'], $ids))
348    {
349      $tpl_var['NAME'].= ' <img class="has_verso" src="'.B2F_PATH.'template/rotate_1.png" title="'.l10n('This picture has a backside :').'"/>';
350    }
351  }
352 
353  return $tpl_thumbnails_var;
354}
355
356?>
Note: See TracBrowser for help on using the repository browser.