source: extensions/Back2Front/Back2Front.php @ 10981

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

last commit for first public version

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