source: extensions/Back2Front/Back2Front.php @ 11219

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

add some options, better intégration

File size: 10.7 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)
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 */
[11219]35    $template->assign('VERSO_URL', $verso['path']);
[10819]36   
37    /* admin link */
38    if (is_admin())
39    {
[10828]40      $template->assign('VERSO_U_ADMIN', get_root_url().'admin.php?page=picture_modify&amp;image_id='.$verso['id']);
[10819]41      $template->set_filename('B2F_admin_button', dirname(__FILE__).'/template/admin_button.tpl');
42      $template->concat('PLUGIN_PICTURE_ACTIONS', $template->parse('B2F_admin_button', true));
43    }
44
45    /* high picture */
46    if ($verso['has_high'])
47    {
48      $template->assign('VERSO_HD', get_high_url($verso));
49    }
[11219]50   
51    /* link name */
52    $conf['back2front'][4] = unserialize($conf['back2front'][4]);
53    if (!empty($conf['back2front'][4][$user['language']]))
54    {
55      if (strpos($conf['back2front'][4][$user['language']], '|') !== false)
56      {
57        $conf['back2front'][4] = explode('|', $conf['back2front'][4][$user['language']]);
58      }
59      else
60      {
61        $conf['back2front'][4] = array($conf['back2front'][4][$user['language']], $conf['back2front'][4][$user['language']]);
62      }
63    }
64    else if (!empty($conf['back2front'][4]['default']))
65    {
66      if (strpos($conf['back2front'][4]['default'], '|') != false)
67      {
68        $conf['back2front'][4] = explode('|', $conf['back2front'][4]['default']);
69      }
70      else
71      {
72        $conf['back2front'][4] = array($conf['back2front'][4]['default'], $conf['back2front'][4]['default']);
73      }
74    }
75    else
76    {
77      $conf['back2front'][4] = array(l10n('See back'), l10n('See front'));
78    }
[10819]79
80    /* template & output */
[11219]81    $template->set_filenames(array('B2F_picture_content' => dirname(__FILE__).'/template/picture_content.tpl') );   
82    $template->assign(array(
83      'B2F_PATH' => B2F_PATH,
84      'b2f_switch_mode' => $conf['back2front'][1],
85      'b2f_transition' => $conf['back2front'][2],
86      'b2f_position' => $conf['back2front'][3],
87      'b2f_see_back' => $conf['back2front'][4][0],
88      'b2f_see_front' => $conf['back2front'][4][1],
89    ));
[10819]90   
[11219]91    switch ($conf['back2front'][3])
92    {
93      case 'toolbar':
94        $template->set_prefilter('picture', 'Back2Front_toolbar_prefilter'); 
95        break;
96      case 'top':
97        return $template->parse('B2F_picture_content', true)."\n".$content;
98        break;
99      case 'bottom':
100        return $content."\n".$template->parse('B2F_picture_content', true);
101        break;
102    }   
[10819]103  }
[11219]104 
105  return $content;
[10819]106}
107
[11219]108function Back2Front_toolbar_prefilter($content, &$smarty)
109{
110  $search = '{/if}{/strip}{*caddie management END*}';
111  $replacement = $search."\n".file_get_contents(B2F_PATH.'template/picture_content.tpl');
112  return str_replace($search, $replacement, $content);
113}
[10819]114
[11219]115
[10819]116/*
117 * Add field on picture modify page
118 */
[11219]119function Back2Front_picture_modify()
[10819]120{
[10821]121  global $page, $template, $conf;
[10852]122 
[11219]123  if ($page['page'] != 'picture_modify') return;
[10821]124  $conf['back2front'] = explode(',',$conf['back2front']);
[10819]125 
[10823]126/* SAVE VALUES */
127  if (isset($_POST['b2f_submit']))
128  {
[10852]129    /* catch all verso and recto ids and original categories */
130    $query = "SELECT image_id, verso_id, categories
131      FROM ".B2F_TABLE.";";
132    $rectos = array_from_query($query, 'image_id');
133    $versos = array_from_query($query, 'verso_id');
134    $cats = array_from_query($query, 'categories');
135   
136    if (count($rectos) != 0)
137    {
138      $all_recto_verso = array_combine($rectos, $versos);
139      $verso_cats = array_combine($versos, $cats);
140    }
141    else
142    {
143      $all_recto_verso = array(0=>0);
144      $verso_cats = array(0=>NULL);
145    }
146    unset($rectos, $versos, $cats);
147   
[10823]148    /* picture is verso */
149    if (isset($_POST['b2f_is_verso']))
[10852]150    {     
[10823]151      /* verso don't exists */
152      if (!picture_exists($_POST['b2f_front_id']))
[10819]153      {
[10852]154        array_push($page['errors'], l10n_args(get_l10n_args('Unknown id %d for frontside picture', $_POST['b2f_front_id'])));
[10823]155      }
156      /* verso same as recto  */
157      else if ($_POST['b2f_front_id'] == $_GET['image_id'])
158      {
[10852]159        array_push($page['errors'], l10n('Backside and frontside can\'t be the same picture'));
[10823]160      }
161      /* recto has already a verso */
[10852]162      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]163      {
164          $recto_current_verso['id'] = $all_recto_verso[$_POST['b2f_front_id']];
[10828]165          $recto_current_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;image_id='.$recto_current_verso['id'];
[10852]166          array_push($page['errors'], 
167            l10n_args(get_l10n_args('The picture n°%d has already a backside : %s', 
168              array($_POST['b2f_front_id'], '<a href="'.$recto_current_verso['link'].'">'.$recto_current_verso['id'].'</a>')
169            ))
170          );
[10823]171      }
172      /* recto is already a verso */
173      else if (in_array($_POST['b2f_front_id'], array_values($all_recto_verso)))
174      {
175          $recto_is_verso['id'] = $_POST['b2f_front_id'];
[10828]176          $recto_is_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;image_id='.$recto_is_verso['id'];
[10852]177          array_push($page['errors'], l10n_args(get_l10n_args('The picture n°%s is already a backside', '<a href="'.$recto_is_verso['link'].'">'.$recto_is_verso['id'].'</a>')));
[10823]178      }
179      /* everything is fine */
180      else
181      {
[10852]182        // move the verso - if first save
183        if (isset($_POST['b2f_move_verso']) AND (!array_key_exists($_GET['image_id'], $verso_cats) OR $verso_cats[$_GET['image_id']] == NULL))
[10819]184        {
[10852]185          // get current categories
186          $query = "SELECT category_id FROM ".IMAGE_CATEGORY_TABLE." WHERE image_id = ".$_GET['image_id'].";";
187          $verso_categories = array_from_query($query, 'category_id');
188         
[10823]189          pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE."
190            WHERE image_id = ".$_GET['image_id'].";");
[10981]191          pwg_query("INSERT INTO ".IMAGE_CATEGORY_TABLE."(image_id, category_id)
192            VALUES(".$_GET['image_id'].", ".$conf['back2front'][0].");");
[10821]193           
[10823]194          // random representant for each categories
195          set_random_representant($verso_categories);
[10852]196         
197          $verso_categories = isset($verso_cats[$_GET['image_id']]) ? $verso_cats[$_GET['image_id']] : implode(',',$verso_categories);
198          $template->assign('B2F_MOVE_VERSO', 'checked="checked"');
[10819]199        }
[10852]200        // restore the verso - if precedently moved
201        else if (!isset($_POST['b2f_move_verso']) AND array_key_exists($_GET['image_id'], $verso_cats) AND $verso_cats[$_GET['image_id']] != NULL)
202        {
203          $item['verso_id'] = $_GET['image_id'];
204          $item['categories'] = $verso_cats[$_GET['image_id']];
205          back2front_restaure_categories($item);
206         
207          $verso_categories = 'NULL';
208          $template->assign('B2F_MOVE_VERSO', '');
209        }
210        // leave the verso
211        else
212        {
213          $verso_categories = isset($verso_cats[$_GET['image_id']]) ? $verso_cats[$_GET['image_id']] : 'NULL';
214          $template->assign('B2F_MOVE_VERSO', isset($verso_cats[$_GET['image_id']]) ? 'checked="checked"' : '');
215        }
216       
217        // insert or update verso associations
218        pwg_query("INSERT INTO ".B2F_TABLE."
219          VALUES(".$_POST['b2f_front_id'].", ".$_GET['image_id'].", '".$verso_categories."')
220          ON DUPLICATE KEY UPDATE image_id = ".$_POST['b2f_front_id'].", categories = ".$verso_categories.";");
[10823]221     
222        $template->assign(array(
223          'B2F_IS_VERSO' => 'checked="checked"',
224          'B2F_FRONT_ID' => $_POST['b2f_front_id'],
225        ));
226       
[10852]227        $verso['id'] = $_POST['b2f_front_id'];
228        $verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;image_id='.$verso['id'];
229        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]230      }
231    }
232    /* picture isn't verso */
233    else
234    {
235      /* search if it was a verso */
236      $query = "SELECT categories
237        FROM ".B2F_TABLE."
238        WHERE verso_id = ".$_GET['image_id'].";";
239      $result = pwg_query($query);
240     
241      /* it must be restored to its original categories (see criteria on maintain.inc) */
242      if (pwg_db_num_rows($result))
243      {
[10852]244        $item['verso_id'] = $_GET['image_id'];
[10823]245        list($item['categories']) = pwg_db_fetch_row($result);
[10852]246        back2front_restaure_categories($item);
[10819]247       
[10823]248        pwg_query("DELETE FROM ".B2F_TABLE."
249          WHERE verso_id = ".$_GET['image_id'].";");
[10821]250         
[10852]251        array_push($page['infos'], l10n('This picture is no longer a backside'));
[10819]252      }
253    }
[10823]254  }
255 
256/* GET SAVED VALUES */
257  if ($template->get_template_vars('B2F_IS_VERSO') == null)
258  {
[10852]259    $template->assign('B2F_MOVE_VERSO', 'checked="checked"');
260   
[10823]261    /* is the picture a verso ? */
262    $query = "
[10852]263      SELECT image_id, categories
[10823]264      FROM ".B2F_TABLE."
265      WHERE verso_id = ".$_GET['image_id']."
266    ;";
267    $result = pwg_query($query);
268   
269    if (pwg_db_num_rows($result))
270    {
[10852]271      list($recto_id, $cats) = pwg_db_fetch_row($result);
[10823]272      $template->assign(array(
273        'B2F_IS_VERSO' => 'checked="checked"',
274        'B2F_FRONT_ID' => $recto_id,
[10852]275        'B2F_MOVE_VERSO' => $cats != NULL ? 'checked="checked"' : '',
[10823]276      ));
277    }
278    /* is the picture a front ? */
[10819]279    else
280    {
[10823]281      $query = "SELECT verso_id
[10819]282        FROM ".B2F_TABLE."
[10823]283        WHERE image_id = ".$_GET['image_id'].";";
[10819]284      $result = pwg_query($query);
285     
286      if (pwg_db_num_rows($result))
[10852]287      {     
[10823]288        $item = pwg_db_fetch_assoc($result);
[10852]289
[10819]290        $template->assign(array(
[10852]291          'B2F_VERSO_ID' => $item['verso_id'],
292          'B2F_VERSO_URL' => get_root_url().'admin.php?page=picture_modify&amp;image_id='.$item['verso_id'],
[10819]293        ));
294      }
295    }
296  }
[10823]297 
[10852]298  $template->set_prefilter('picture_modify', 'Back2front_picture_modify_prefilter');
[10819]299}
300
[10852]301
302function Back2front_picture_modify_prefilter($content, &$smarty)
[10819]303{
[10852]304  $search = '<form id="associations" method="post" action="{$F_ACTION}#associations">';
305  $replacement = file_get_contents(B2F_PATH.'template/picture_modify.tpl')."\n".$search;
306  return str_replace($search, $replacement, $content);
[10819]307}
308
309?>
Note: See TracBrowser for help on using the repository browser.