source: extensions/Back2Front/Back2Front.php @ 10821

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

use a private album instead a privacy level

File size: 7.4 KB
RevLine 
[10819]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{
[10821]64  global $page, $template, $conf;
65  $conf['back2front'] = explode(',',$conf['back2front']);
[10819]66 
67  if ($page['page'] == 'picture_modify')
68  {
69    /* change values */
70    if (isset($_POST['b2f_submit']))
71    {
72      /* picture is verso */
73      if (isset($_POST['b2f_is_verso']))
74      {
75        /* frontside exists */
76        if (picture_exists($_POST['b2f_front_id']))
77        {
[10821]78          /* search if recto has already a verso */
79          $query = "SELECT verso_id
80            FROM ".B2F_TABLE."
81            WHERE image_id = ".$_POST['b2f_front_id'].";";
82          $result = pwg_query($query);
83          list($recto_current_verso['id']) = pwg_db_fetch_row($result);
[10819]84         
[10821]85          if (pwg_db_num_rows($result) AND $recto_current_verso['id'] != $_GET['image_id'])
86          {
87            $recto_current_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;cat_id=&amp;image_id='.$recto_current_verso['id'];
88            $template->append('errors', l10n('This picture has already a backside : ').'<a href="'.$recto_current_verso['link'].'">'.$recto_current_verso['id'].'</a>');
89          }
90          /* recto is clean */
91          else
92          {
93            $verso_categories = implode(',',array_keys($template->get_template_vars('associated_options')));
94            pwg_query("INSERT INTO ".B2F_TABLE."
95              VALUES(".$_POST['b2f_front_id'].", ".$_GET['image_id'].", '".$verso_categories."')
96              ON DUPLICATE KEY UPDATE image_id = ".$_POST['b2f_front_id'].";");
97           
98            /* move the verso ? */
99            if (isset($_POST['b2f_move_verso']))
100            {
101              pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE."
102                WHERE image_id = ".$_GET['image_id'].";");
103               
104              pwg_query("INSERT INTO ".IMAGE_CATEGORY_TABLE."
105                VALUES(".$_GET['image_id'].", ".$conf['back2front'][0].", NULL);");
106            }
[10819]107         
[10821]108            $template->assign(array(
109              'B2F_IS_VERSO' => 'checked="checked"',
110              'B2F_FRONT_ID' => $_POST['b2f_front_id'],
111            ));
112           
113            $template->append('infos', l10n('This picture is now the backside of the picture n° ').$_POST['b2f_front_id']);
114          }
[10819]115        }
116        else
117        {
[10821]118          $template->append('errors', l10n('Unknown id for frontside picture : ').$_POST['b2f_front_id']);
[10819]119        }
120      }
121      /* picture isn't verso */
122      else
123      {
[10821]124        /* search if it was a verso */
125        $query = "SELECT categories
126          FROM ".B2F_TABLE."
127          WHERE verso_id = ".$_GET['image_id'].";";
128        $result = pwg_query($query);
[10819]129       
[10821]130        /* it must be restored to its original categories (see criteria on maintain.inc) */
131        if (pwg_db_num_rows($result))
132        {
133          /* original categories */
134          list($item['categories']) = pwg_db_fetch_row($result);
135          /* catch current categories */
136          $versos_infos = pwg_query("SELECT category_id FROM ".IMAGE_CATEGORY_TABLE." WHERE image_id = ".$_GET['image_id'].";");
137          while (list($verso_cat) = pwg_db_fetch_row($versos_infos))
138          {
139            $current_verso_cats[] = $verso_cat;
140          }
141          /* if verso € 'versos' cat only */
142          if (count($current_verso_cats) == 1 AND $current_verso_cats[0] == $conf['back2front'][0])
143          {
144            foreach (explode(',',$item['categories']) as $cat)
145            {
146              $datas[] = array(
147                'image_id' => $_GET['image_id'],
148                'category_id' => $cat,
149                );
150            }
151            if (isset($datas))
152            {
153              mass_inserts(
154                IMAGE_CATEGORY_TABLE,
155                array('image_id', 'category_id'),
156                $datas
157                );
158            }
159          }
160         
161          pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE."
162            WHERE image_id = ".$_GET['image_id']." AND category_id = ".$conf['back2front'][0].";");
163         
164          pwg_query("DELETE FROM ".B2F_TABLE."
165            WHERE verso_id = ".$_GET['image_id'].";");
166           
167          $template->append('infos', l10n('This picture is no longer a backside'));
168        }
[10819]169      }
170    }
171    /* get saved values */
172    else
173    {
174      /* is the pisture a verso ? */
175      $query = "
[10821]176        SELECT image_id
[10819]177        FROM ".B2F_TABLE."
178        WHERE verso_id = ".$_GET['image_id']."
179      ;";
180      $result = pwg_query($query);
181     
182      if (pwg_db_num_rows($result))
183      {
[10821]184        list($recto_id) = pwg_db_fetch_row($result);
[10819]185        $template->assign(array(
186          'B2F_IS_VERSO' => 'checked="checked"',
[10821]187          'B2F_FRONT_ID' => $recto_id,
[10819]188        ));
189      }
190      /* is the picture a front ? */
191      else
192      {
[10821]193        $query = "SELECT verso_id
[10819]194          FROM ".B2F_TABLE."
[10821]195          WHERE image_id = ".$_GET['image_id'].";";
[10819]196        $result = pwg_query($query);
197       
198        if (pwg_db_num_rows($result))
199        {
200          include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
201         
202          $item = pwg_db_fetch_assoc($result);
[10821]203          $query = "SELECT id, name, file
[10819]204            FROM ".IMAGES_TABLE."
[10821]205            WHERE id = ".$item['verso_id'].";";
[10819]206          $item = pwg_db_fetch_assoc(pwg_query($query));
207         
208          $template->assign(array(
209            'B2F_VERSO_ID' => $item['id'],
210            'B2F_VERSO_URL' => get_root_url().'admin.php?page=picture_modify&amp;cat_id=&amp;image_id='.$item['id'],
211            'B2F_VERSO_NAME' => get_image_name($item['name'], $item['file']),
212          ));
213        }
214      }
215    }
216   
217    $template->set_filename('B2F_picture_modify', dirname(__FILE__).'/template/picture_modify.tpl');
218    $template->concat('ADMIN_CONTENT', $template->parse('B2F_picture_modify', true));
219  }
220}
221
222function picture_exists($id)
223{
224  if (!preg_match('#([0-9]{1,})#', $id) OR $id == '0') return false;
225 
226  $query = "SELECT id FROM ".IMAGES_TABLE." WHERE id = ".$id.";";
227  $result = pwg_query($query);
228 
229  if (pwg_db_num_rows($result)) return true;
230  else return false;
231}
232
233?>
Note: See TracBrowser for help on using the repository browser.