source: extensions/Back2Front/Back2Front.php @ 12364

Last change on this file since 12364 was 12364, checked in by mistic100, 13 years ago

solve a prefilter issue on picture_modify.tpl

File size: 11.4 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 */
9function Back2Front_picture_content($content, $image)
[12361]10{
[10852]11  global $template, $user, $conf;
[10819]12
13  /* search for a verso picture */
14  $query = "
15    SELECT
16      i.id,
17      i.path,
[10852]18      i.has_high,
19      i.width,
20      i.height
[10819]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);
[10852]32    $conf['back2front'] = explode(',',$conf['back2front']);
33   
[10819]34    /* websize picture */
[12361]35    $template->assign(array(
36      'B2F_PATH' => B2F_PATH,
37      'VERSO_URL' => $verso['path'],
38      ));
[10819]39   
40    /* admin link */
41    if (is_admin())
42    {
[10828]43      $template->assign('VERSO_U_ADMIN', get_root_url().'admin.php?page=picture_modify&amp;image_id='.$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    }
47
48    /* high picture */
49    if ($verso['has_high'])
50    {
51      $template->assign('VERSO_HD', get_high_url($verso));
52    }
[11219]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    }
[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 
[11219]118  if ($page['page'] != 'picture_modify') return;
[10821]119  $conf['back2front'] = explode(',',$conf['back2front']);
[10819]120 
[10823]121/* SAVE VALUES */
122  if (isset($_POST['b2f_submit']))
123  {
[10852]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   
[10823]143    /* picture is verso */
144    if (isset($_POST['b2f_is_verso']))
[10852]145    {     
[10823]146      /* verso don't exists */
147      if (!picture_exists($_POST['b2f_front_id']))
[10819]148      {
[11599]149        array_push(
150          $page['errors'], 
151          sprintf(
152            l10n('Unknown id %d for frontside picture'), 
153            $_POST['b2f_front_id']
154            )
155          );
[10823]156      }
157      /* verso same as recto  */
158      else if ($_POST['b2f_front_id'] == $_GET['image_id'])
159      {
[10852]160        array_push($page['errors'], l10n('Backside and frontside can\'t be the same picture'));
[10823]161      }
162      /* recto has already a verso */
[10852]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'])
[10823]164      {
165          $recto_current_verso['id'] = $all_recto_verso[$_POST['b2f_front_id']];
[10828]166          $recto_current_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;image_id='.$recto_current_verso['id'];
[11599]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            );
[10823]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'];
[10828]180          $recto_is_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;image_id='.$recto_is_verso['id'];
[11599]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            );
[10823]188      }
189      /* everything is fine */
190      else
191      {
[10852]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))
[10819]194        {
[10852]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         
[10823]199          pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE."
200            WHERE image_id = ".$_GET['image_id'].";");
[10981]201          pwg_query("INSERT INTO ".IMAGE_CATEGORY_TABLE."(image_id, category_id)
202            VALUES(".$_GET['image_id'].", ".$conf['back2front'][0].");");
[10821]203           
[10823]204          // random representant for each categories
205          set_random_representant($verso_categories);
[10852]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"');
[10819]209        }
[10852]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.";");
[10823]231     
232        $template->assign(array(
233          'B2F_IS_VERSO' => 'checked="checked"',
234          'B2F_FRONT_ID' => $_POST['b2f_front_id'],
235        ));
236       
[10852]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>')));
[10823]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      {
[10852]254        $item['verso_id'] = $_GET['image_id'];
[10823]255        list($item['categories']) = pwg_db_fetch_row($result);
[10852]256        back2front_restaure_categories($item);
[10819]257       
[10823]258        pwg_query("DELETE FROM ".B2F_TABLE."
259          WHERE verso_id = ".$_GET['image_id'].";");
[10821]260         
[10852]261        array_push($page['infos'], l10n('This picture is no longer a backside'));
[10819]262      }
263    }
[10823]264  }
265 
266/* GET SAVED VALUES */
267  if ($template->get_template_vars('B2F_IS_VERSO') == null)
268  {
[10852]269    $template->assign('B2F_MOVE_VERSO', 'checked="checked"');
270   
[10823]271    /* is the picture a verso ? */
272    $query = "
[10852]273      SELECT image_id, categories
[10823]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    {
[10852]281      list($recto_id, $cats) = pwg_db_fetch_row($result);
[10823]282      $template->assign(array(
283        'B2F_IS_VERSO' => 'checked="checked"',
284        'B2F_FRONT_ID' => $recto_id,
[10852]285        'B2F_MOVE_VERSO' => $cats != NULL ? 'checked="checked"' : '',
[10823]286      ));
287    }
288    /* is the picture a front ? */
[10819]289    else
290    {
[10823]291      $query = "SELECT verso_id
[10819]292        FROM ".B2F_TABLE."
[10823]293        WHERE image_id = ".$_GET['image_id'].";";
[10819]294      $result = pwg_query($query);
295     
296      if (pwg_db_num_rows($result))
[10852]297      {     
[10823]298        $item = pwg_db_fetch_assoc($result);
[10852]299
[10819]300        $template->assign(array(
[10852]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'],
[10819]303        ));
304      }
305    }
306  }
[10823]307 
[10852]308  $template->set_prefilter('picture_modify', 'Back2front_picture_modify_prefilter');
[10819]309}
310
[10852]311
312function Back2front_picture_modify_prefilter($content, &$smarty)
[10819]313{
[12364]314  $search = '<form id="associations"';
[10852]315  $replacement = file_get_contents(B2F_PATH.'template/picture_modify.tpl')."\n".$search;
316  return str_replace($search, $replacement, $content);
[10819]317}
318
[12361]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 
330  $ids = array();
331  foreach ($pictures as $row)
332  {
333    array_push($ids, $row['id']);
334  }
335   
336  /* has the pictures a verso ? */
337  $query = "SELECT image_id, verso_id
338    FROM ".B2F_TABLE."
339    WHERE image_id IN(".implode(',', $ids).");";
340  $result = hash_from_query($query, 'image_id');
341 
342  $ids = array_keys($result);
343 
344  foreach($tpl_thumbnails_var as &$tpl_var)
345  {
346    if (in_array($tpl_var['ID'], $ids))
347    {
348      $tpl_var['NAME'].= ' <img class="has_verso" src="'.B2F_PATH.'template/rotate_1.png" title="'.l10n('This picture has a backside :').'"/>';
349    }
350  }
351 
352  return $tpl_thumbnails_var;
353}
354
[10819]355?>
Note: See TracBrowser for help on using the repository browser.