source: extensions/Back2Front/Back2Front.php @ 10823

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

code cleanup, correct errors with physical pictures

File size: 8.1 KB
Line 
1<?php 
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
4/*
5 * Add verso link on picture page
6 */
7function Back2Front_picture_content($content, $image)
8 {
9  global $template, $conf;
10
11  /* search for a verso picture */
12  $query = "
13    SELECT
14      i.id,
15      i.path,
16      i.has_high
17    FROM ".IMAGES_TABLE." as i
18      INNER JOIN ".B2F_TABLE." as v
19      ON i.id = v.verso_id
20    WHERE
21      v.image_id = ".$image['id']."
22  ;";
23  $result = pwg_query($query);
24
25  if (pwg_db_num_rows($result)) 
26  {
27    $verso = pwg_db_fetch_assoc($result);
28
29    /* websize picture */
30    $template->assign('VERSO_URL', $verso['path']);
31   
32    /* admin link */
33    if (is_admin())
34    {
35      $template->assign('VERSO_U_ADMIN', get_root_url().'admin.php?page=picture_modify&amp;cat_id=&amp;image_id='.$verso['id']);
36      $template->set_filename('B2F_admin_button', dirname(__FILE__).'/template/admin_button.tpl');
37      $template->concat('PLUGIN_PICTURE_ACTIONS', $template->parse('B2F_admin_button', true));
38    }
39
40    /* high picture */
41    if ($verso['has_high'])
42    {
43      $template->assign('VERSO_HD', get_high_url($verso));
44    }
45
46    /* template & output */
47    $template->set_filenames(array('B2F_picture_content' => dirname(__FILE__).'/template/picture_content.tpl') );
48    $template->assign('B2F_PATH', B2F_PATH);
49   
50    return $content . $template->parse('B2F_picture_content', true); 
51  }
52  else 
53  {
54    return $content;
55  }
56}
57
58
59/*
60 * Add field on picture modify page
61 */
62function Back2Front_picture_modify()
63{
64  global $page, $template, $conf;
65  $conf['back2front'] = explode(',',$conf['back2front']);
66 
67  if ($page['page'] != 'picture_modify')
68  {
69    return;
70  }
71 
72/* SAVE VALUES */
73  if (isset($_POST['b2f_submit']))
74  {
75    /* picture is verso */
76    if (isset($_POST['b2f_is_verso']))
77    {
78      /* catch all verso and recto ids */
79      $query = "SELECT image_id, verso_id
80        FROM ".B2F_TABLE.";";
81      $all_recto_verso = array_combine(array_from_query($query, 'image_id'), array_from_query($query, 'verso_id'));
82     
83      /* verso don't exists */
84      if (!picture_exists($_POST['b2f_front_id']))
85      {
86        $template->append('errors', l10n('Unknown id for frontside picture : ').$_POST['b2f_front_id']);
87      }
88      /* verso same as recto  */
89      else if ($_POST['b2f_front_id'] == $_GET['image_id'])
90      {
91        $template->append('errors', l10n('Backside and frontside can\'t be the same picture'));
92      }
93      /* recto has already a verso */
94      else if (in_array($_POST['b2f_front_id'], array_keys($all_recto_verso)))
95      {
96          $recto_current_verso['id'] = $all_recto_verso[$_POST['b2f_front_id']];
97          $recto_current_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;cat_id=&amp;image_id='.$recto_current_verso['id'];
98          $template->append('errors', l10n('This picture has already a backside : ').'<a href="'.$recto_current_verso['link'].'">'.$recto_current_verso['id'].'</a>');
99      }
100      /* recto is already a verso */
101      else if (in_array($_POST['b2f_front_id'], array_values($all_recto_verso)))
102      {
103          $recto_is_verso['id'] = $_POST['b2f_front_id'];
104          $recto_is_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;cat_id=&amp;image_id='.$recto_is_verso['id'];
105          $template->append('errors', l10n('This picture is already a backside : ').'<a href="'.$recto_is_verso['link'].'">'.$recto_is_verso['id'].'</a>');
106      }
107      /* everything is fine */
108      else
109      {
110        // get current categories
111        $query = "SELECT category_id FROM ".IMAGE_CATEGORY_TABLE." WHERE image_id = ".$_GET['image_id'].";";
112        $verso_categories = array_from_query($query, 'category_id');
113       
114        // insert or update verso associations
115        pwg_query("INSERT INTO ".B2F_TABLE."
116          VALUES(".$_POST['b2f_front_id'].", ".$_GET['image_id'].", '".implode(',',$verso_categories)."')
117          ON DUPLICATE KEY UPDATE image_id = ".$_POST['b2f_front_id'].";");
118       
119        // move the verso ?
120        if (isset($_POST['b2f_move_verso']))
121        {
122          pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE."
123            WHERE image_id = ".$_GET['image_id'].";");
124           
125          pwg_query("INSERT INTO ".IMAGE_CATEGORY_TABLE."
126            VALUES(".$_GET['image_id'].", ".$conf['back2front'][0].", NULL);");
127           
128          // random representant for each categories
129          set_random_representant($verso_categories);
130        }
131     
132        $template->assign(array(
133          'B2F_IS_VERSO' => 'checked="checked"',
134          'B2F_FRONT_ID' => $_POST['b2f_front_id'],
135        ));
136       
137        $template->append('infos', l10n('This picture is now the backside of the picture n° ').$_POST['b2f_front_id']);
138      }
139    }
140    /* picture isn't verso */
141    else
142    {
143      /* search if it was a verso */
144      $query = "SELECT categories
145        FROM ".B2F_TABLE."
146        WHERE verso_id = ".$_GET['image_id'].";";
147      $result = pwg_query($query);
148     
149      /* it must be restored to its original categories (see criteria on maintain.inc) */
150      if (pwg_db_num_rows($result))
151      {
152        // original categories
153        list($item['categories']) = pwg_db_fetch_row($result);
154        // catch current categories
155        $versos_infos = pwg_query("SELECT category_id FROM ".IMAGE_CATEGORY_TABLE." WHERE image_id = ".$_GET['image_id'].";");
156        while (list($verso_cat) = pwg_db_fetch_row($versos_infos))
157        {
158          $current_verso_cats[] = $verso_cat;
159        }
160       
161        /* if verso € 'versos' cat only */
162        if (count($current_verso_cats) == 1 AND $current_verso_cats[0] == $conf['back2front'][0])
163        {
164          foreach (explode(',',$item['categories']) as $cat)
165          {
166            $datas[] = array(
167              'image_id' => $_GET['image_id'],
168              'category_id' => $cat,
169              );
170          }
171          if (isset($datas))
172          {
173            mass_inserts(
174              IMAGE_CATEGORY_TABLE,
175              array('image_id', 'category_id'),
176              $datas
177              );
178          }
179        }
180       
181        pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE."
182          WHERE image_id = ".$_GET['image_id']." AND category_id = ".$conf['back2front'][0].";");
183       
184        pwg_query("DELETE FROM ".B2F_TABLE."
185          WHERE verso_id = ".$_GET['image_id'].";");
186         
187        $template->append('infos', l10n('This picture is no longer a backside'));
188      }
189    }
190  }
191 
192/* GET SAVED VALUES */
193  if ($template->get_template_vars('B2F_IS_VERSO') == null)
194  {
195    /* is the picture a verso ? */
196    $query = "
197      SELECT image_id
198      FROM ".B2F_TABLE."
199      WHERE verso_id = ".$_GET['image_id']."
200    ;";
201    $result = pwg_query($query);
202   
203    if (pwg_db_num_rows($result))
204    {
205      list($recto_id) = pwg_db_fetch_row($result);
206      $template->assign(array(
207        'B2F_IS_VERSO' => 'checked="checked"',
208        'B2F_FRONT_ID' => $recto_id,
209      ));
210    }
211    /* is the picture a front ? */
212    else
213    {
214      $query = "SELECT verso_id
215        FROM ".B2F_TABLE."
216        WHERE image_id = ".$_GET['image_id'].";";
217      $result = pwg_query($query);
218     
219      if (pwg_db_num_rows($result))
220      {
221        include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
222       
223        $item = pwg_db_fetch_assoc($result);
224        $query = "SELECT id, name, file
225          FROM ".IMAGES_TABLE."
226          WHERE id = ".$item['verso_id'].";";
227        $item = pwg_db_fetch_assoc(pwg_query($query));
228       
229        $template->assign(array(
230          'B2F_VERSO_ID' => $item['id'],
231          'B2F_VERSO_URL' => get_root_url().'admin.php?page=picture_modify&amp;cat_id=&amp;image_id='.$item['id'],
232          'B2F_VERSO_NAME' => get_image_name($item['name'], $item['file']),
233        ));
234      }
235    }
236  }
237 
238  $template->set_filename('B2F_picture_modify', dirname(__FILE__).'/template/picture_modify.tpl');
239  $template->concat('ADMIN_CONTENT', $template->parse('B2F_picture_modify', true));
240}
241
242function picture_exists($id)
243{
244  if (!preg_match('#([0-9]{1,})#', $id) OR $id == '0') return false;
245 
246  $query = "SELECT id FROM ".IMAGES_TABLE." WHERE id = ".$id.";";
247  $result = pwg_query($query);
248 
249  if (pwg_db_num_rows($result)) return true;
250  else return false;
251}
252
253?>
Note: See TracBrowser for help on using the repository browser.