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
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('VERSO_URL', $verso['path']);
36   
37    /* admin link */
38    if (is_admin())
39    {
40      $template->assign('VERSO_U_ADMIN', get_root_url().'admin.php?page=picture_modify&amp;image_id='.$verso['id']);
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    }
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    }
79
80    /* template & output */
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    ));
90   
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    }   
103  }
104 
105  return $content;
106}
107
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}
114
115
116/*
117 * Add field on picture modify page
118 */
119function Back2Front_picture_modify()
120{
121  global $page, $template, $conf;
122 
123  if ($page['page'] != 'picture_modify') return;
124  $conf['back2front'] = explode(',',$conf['back2front']);
125 
126/* SAVE VALUES */
127  if (isset($_POST['b2f_submit']))
128  {
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   
148    /* picture is verso */
149    if (isset($_POST['b2f_is_verso']))
150    {     
151      /* verso don't exists */
152      if (!picture_exists($_POST['b2f_front_id']))
153      {
154        array_push($page['errors'], l10n_args(get_l10n_args('Unknown id %d for frontside picture', $_POST['b2f_front_id'])));
155      }
156      /* verso same as recto  */
157      else if ($_POST['b2f_front_id'] == $_GET['image_id'])
158      {
159        array_push($page['errors'], l10n('Backside and frontside can\'t be the same picture'));
160      }
161      /* recto has already a verso */
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'])
163      {
164          $recto_current_verso['id'] = $all_recto_verso[$_POST['b2f_front_id']];
165          $recto_current_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;image_id='.$recto_current_verso['id'];
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          );
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'];
176          $recto_is_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;image_id='.$recto_is_verso['id'];
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>')));
178      }
179      /* everything is fine */
180      else
181      {
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))
184        {
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         
189          pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE."
190            WHERE image_id = ".$_GET['image_id'].";");
191          pwg_query("INSERT INTO ".IMAGE_CATEGORY_TABLE."(image_id, category_id)
192            VALUES(".$_GET['image_id'].", ".$conf['back2front'][0].");");
193           
194          // random representant for each categories
195          set_random_representant($verso_categories);
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"');
199        }
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.";");
221     
222        $template->assign(array(
223          'B2F_IS_VERSO' => 'checked="checked"',
224          'B2F_FRONT_ID' => $_POST['b2f_front_id'],
225        ));
226       
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>')));
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      {
244        $item['verso_id'] = $_GET['image_id'];
245        list($item['categories']) = pwg_db_fetch_row($result);
246        back2front_restaure_categories($item);
247       
248        pwg_query("DELETE FROM ".B2F_TABLE."
249          WHERE verso_id = ".$_GET['image_id'].";");
250         
251        array_push($page['infos'], l10n('This picture is no longer a backside'));
252      }
253    }
254  }
255 
256/* GET SAVED VALUES */
257  if ($template->get_template_vars('B2F_IS_VERSO') == null)
258  {
259    $template->assign('B2F_MOVE_VERSO', 'checked="checked"');
260   
261    /* is the picture a verso ? */
262    $query = "
263      SELECT image_id, categories
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    {
271      list($recto_id, $cats) = pwg_db_fetch_row($result);
272      $template->assign(array(
273        'B2F_IS_VERSO' => 'checked="checked"',
274        'B2F_FRONT_ID' => $recto_id,
275        'B2F_MOVE_VERSO' => $cats != NULL ? 'checked="checked"' : '',
276      ));
277    }
278    /* is the picture a front ? */
279    else
280    {
281      $query = "SELECT verso_id
282        FROM ".B2F_TABLE."
283        WHERE image_id = ".$_GET['image_id'].";";
284      $result = pwg_query($query);
285     
286      if (pwg_db_num_rows($result))
287      {     
288        $item = pwg_db_fetch_assoc($result);
289
290        $template->assign(array(
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'],
293        ));
294      }
295    }
296  }
297 
298  $template->set_prefilter('picture_modify', 'Back2front_picture_modify_prefilter');
299}
300
301
302function Back2front_picture_modify_prefilter($content, &$smarty)
303{
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);
307}
308
309?>
Note: See TracBrowser for help on using the repository browser.