source: extensions/Back2Front/Back2Front.php @ 10828

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

warnings if image_verso table empty

File size: 8.3 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;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      $rectos = array_from_query($query, 'image_id');
82      $versos = array_from_query($query, 'verso_id');
83      if (count($rectos) != 0)
84      {
85        $all_recto_verso = array_combine($rectos, $versos);
86      }
87      else
88      {
89        $all_recto_verso = array(0=>0);
90      }
91      unset($rectos, $versos);
92     
93      /* verso don't exists */
94      if (!picture_exists($_POST['b2f_front_id']))
95      {
96        $template->append('errors', l10n('Unknown id for frontside picture : ').$_POST['b2f_front_id']);
97      }
98      /* verso same as recto  */
99      else if ($_POST['b2f_front_id'] == $_GET['image_id'])
100      {
101        $template->append('errors', l10n('Backside and frontside can\'t be the same picture'));
102      }
103      /* recto has already a verso */
104      else if (in_array($_POST['b2f_front_id'], array_keys($all_recto_verso)))
105      {
106          $recto_current_verso['id'] = $all_recto_verso[$_POST['b2f_front_id']];
107          $recto_current_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;image_id='.$recto_current_verso['id'];
108          $template->append('errors', l10n('This picture has already a backside : ').'<a href="'.$recto_current_verso['link'].'">'.$recto_current_verso['id'].'</a>');
109      }
110      /* recto is already a verso */
111      else if (in_array($_POST['b2f_front_id'], array_values($all_recto_verso)))
112      {
113          $recto_is_verso['id'] = $_POST['b2f_front_id'];
114          $recto_is_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;image_id='.$recto_is_verso['id'];
115          $template->append('errors', l10n('This picture is already a backside : ').'<a href="'.$recto_is_verso['link'].'">'.$recto_is_verso['id'].'</a>');
116      }
117      /* everything is fine */
118      else
119      {
120        // get current categories
121        $query = "SELECT category_id FROM ".IMAGE_CATEGORY_TABLE." WHERE image_id = ".$_GET['image_id'].";";
122        $verso_categories = array_from_query($query, 'category_id');
123       
124        // insert or update verso associations
125        pwg_query("INSERT INTO ".B2F_TABLE."
126          VALUES(".$_POST['b2f_front_id'].", ".$_GET['image_id'].", '".implode(',',$verso_categories)."')
127          ON DUPLICATE KEY UPDATE image_id = ".$_POST['b2f_front_id'].";");
128       
129        // move the verso ?
130        if (isset($_POST['b2f_move_verso']))
131        {
132          pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE."
133            WHERE image_id = ".$_GET['image_id'].";");
134           
135          pwg_query("INSERT INTO ".IMAGE_CATEGORY_TABLE."
136            VALUES(".$_GET['image_id'].", ".$conf['back2front'][0].", NULL);");
137           
138          // random representant for each categories
139          set_random_representant($verso_categories);
140        }
141     
142        $template->assign(array(
143          'B2F_IS_VERSO' => 'checked="checked"',
144          'B2F_FRONT_ID' => $_POST['b2f_front_id'],
145        ));
146       
147        $template->append('infos', l10n('This picture is now the backside of the picture n° ').$_POST['b2f_front_id']);
148      }
149    }
150    /* picture isn't verso */
151    else
152    {
153      /* search if it was a verso */
154      $query = "SELECT categories
155        FROM ".B2F_TABLE."
156        WHERE verso_id = ".$_GET['image_id'].";";
157      $result = pwg_query($query);
158     
159      /* it must be restored to its original categories (see criteria on maintain.inc) */
160      if (pwg_db_num_rows($result))
161      {
162        // original categories
163        list($item['categories']) = pwg_db_fetch_row($result);
164        // catch current categories
165        $versos_infos = pwg_query("SELECT category_id FROM ".IMAGE_CATEGORY_TABLE." WHERE image_id = ".$_GET['image_id'].";");
166        while (list($verso_cat) = pwg_db_fetch_row($versos_infos))
167        {
168          $current_verso_cats[] = $verso_cat;
169        }
170       
171        /* if verso € 'versos' cat only */
172        if (count($current_verso_cats) == 1 AND $current_verso_cats[0] == $conf['back2front'][0])
173        {
174          foreach (explode(',',$item['categories']) as $cat)
175          {
176            $datas[] = array(
177              'image_id' => $_GET['image_id'],
178              'category_id' => $cat,
179              );
180          }
181          if (isset($datas))
182          {
183            mass_inserts(
184              IMAGE_CATEGORY_TABLE,
185              array('image_id', 'category_id'),
186              $datas
187              );
188          }
189        }
190       
191        pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE."
192          WHERE image_id = ".$_GET['image_id']." AND category_id = ".$conf['back2front'][0].";");
193       
194        pwg_query("DELETE FROM ".B2F_TABLE."
195          WHERE verso_id = ".$_GET['image_id'].";");
196         
197        $template->append('infos', l10n('This picture is no longer a backside'));
198      }
199    }
200  }
201 
202/* GET SAVED VALUES */
203  if ($template->get_template_vars('B2F_IS_VERSO') == null)
204  {
205    /* is the picture a verso ? */
206    $query = "
207      SELECT image_id
208      FROM ".B2F_TABLE."
209      WHERE verso_id = ".$_GET['image_id']."
210    ;";
211    $result = pwg_query($query);
212   
213    if (pwg_db_num_rows($result))
214    {
215      list($recto_id) = pwg_db_fetch_row($result);
216      $template->assign(array(
217        'B2F_IS_VERSO' => 'checked="checked"',
218        'B2F_FRONT_ID' => $recto_id,
219      ));
220    }
221    /* is the picture a front ? */
222    else
223    {
224      $query = "SELECT verso_id
225        FROM ".B2F_TABLE."
226        WHERE image_id = ".$_GET['image_id'].";";
227      $result = pwg_query($query);
228     
229      if (pwg_db_num_rows($result))
230      {
231        include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
232       
233        $item = pwg_db_fetch_assoc($result);
234        $query = "SELECT id, name, file
235          FROM ".IMAGES_TABLE."
236          WHERE id = ".$item['verso_id'].";";
237        $item = pwg_db_fetch_assoc(pwg_query($query));
238       
239        $template->assign(array(
240          'B2F_VERSO_ID' => $item['id'],
241          'B2F_VERSO_URL' => get_root_url().'admin.php?page=picture_modify&amp;image_id='.$item['id'],
242          'B2F_VERSO_NAME' => get_image_name($item['name'], $item['file']),
243        ));
244      }
245    }
246  }
247 
248  $template->set_filename('B2F_picture_modify', dirname(__FILE__).'/template/picture_modify.tpl');
249  $template->concat('ADMIN_CONTENT', $template->parse('B2F_picture_modify', true));
250}
251
252function picture_exists($id)
253{
254  if (!preg_match('#([0-9]{1,})#', $id) OR $id == '0') return false;
255 
256  $query = "SELECT id FROM ".IMAGES_TABLE." WHERE id = ".$id.";";
257  $result = pwg_query($query);
258 
259  if (pwg_db_num_rows($result)) return true;
260  else return false;
261}
262
263?>
Note: See TracBrowser for help on using the repository browser.