source: extensions/Back2Front/include/Back2Front.php @ 23177

Last change on this file since 23177 was 23177, checked in by mistic100, 11 years ago

use serialized array for config + custom upgrade process

File size: 11.4 KB
RevLine 
[10819]1<?php 
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
[23177]4include_once(B2F_PATH.'include/functions.inc.php');
[10852]5
[10819]6/*
7 * Add verso link on picture page
8 */
[23177]9function back2front_picture_content($content, $element_info)
[12361]10{
[10852]11  global $template, $user, $conf;
[10819]12
13  /* search for a verso picture */
[23177]14  $query = '
15SELECT i.*
16  FROM '.IMAGES_TABLE.' as i
17    INNER JOIN '.B2F_TABLE.' as v
18    ON i.id = v.verso_id
19    AND v.image_id = '.$element_info['id'].'
20;';
[10819]21  $result = pwg_query($query);
22
23  if (pwg_db_num_rows($result)) 
24  {
25    $verso = pwg_db_fetch_assoc($result);
[21212]26    $deriv_type = pwg_get_session_var('picture_deriv', $conf['derivative_default_size']);
[10852]27   
[21212]28    $verso['src_image'] = new SrcImage($verso);
29    $verso['derivatives'] = DerivativeImage::get_all($verso['src_image']);
30    $verso['element_path'] = get_element_path($verso);
31    $verso['selected_derivative'] = $verso['derivatives'][$deriv_type];
32   
[10819]33    /* websize picture */
[12361]34    $template->assign(array(
35      'B2F_PATH' => B2F_PATH,
[21212]36      'verso' => $verso,
[12361]37      ));
[10819]38   
39    /* admin link */
40    if (is_admin())
41    {
[21212]42      $template->assign('VERSO_U_ADMIN', get_root_url().'admin.php?page=photo-'.$verso['id']);
[23177]43      $template->set_filename('B2F_admin_button', realpath(B2F_PATH.'template/admin_button.tpl'));
[10819]44      $template->concat('PLUGIN_PICTURE_ACTIONS', $template->parse('B2F_admin_button', true));
45    }
[11219]46   
47    /* link name */
[23177]48    if (!empty($conf['back2front']['link_name'][$user['language']]))
[11219]49    {
[23177]50      if (strpos($conf['back2front']['link_name'][$user['language']], '|') !== false)
[11219]51      {
[23177]52        $conf['back2front']['link_name'] = explode('|', $conf['back2front']['link_name'][$user['language']]);
[11219]53      }
54      else
55      {
[23177]56        $conf['back2front']['link_name'] = array_fill(0, 2, $conf['back2front']['link_name'][$user['language']]);
[11219]57      }
58    }
[23177]59    else if (!empty($conf['back2front']['link_name']['default']))
[11219]60    {
[23177]61      if (strpos($conf['back2front']['link_name']['default'], '|') != false)
[11219]62      {
[23177]63        $conf['back2front']['link_name'] = explode('|', $conf['back2front']['link_name']['default']);
[11219]64      }
65      else
66      {
[23177]67        $conf['back2front']['link_name'] = array_fill(0, 2, $conf['back2front']['link_name']['default']);
[11219]68      }
69    }
70    else
71    {
[23177]72      $conf['back2front']['link_name'] = array(l10n('See back'), l10n('See front'));
[11219]73    }
[21212]74   
[23177]75    if ($conf['back2front']['transition'] == 'fade' && $conf['back2front']['position'] == 'bottom')
[21212]76    {
[23177]77      $conf['back2front']['position'] = 'top';
[21212]78    }
79   
[10819]80
81    /* template & output */
[23177]82    $template->set_filename('B2F_picture_content', realpath(B2F_PATH.'template/picture_content.tpl'));   
[11219]83    $template->assign(array(
[23177]84      'b2f_switch_mode' => $conf['back2front']['switch_mode'],
85      'b2f_transition' => $conf['back2front']['transition'],
86      'b2f_position' => $conf['back2front']['position'],
87      'b2f_see_back' => $conf['back2front']['link_name'][0],
88      'b2f_see_front' => $conf['back2front']['link_name'][1],
[11219]89    ));
[10819]90   
[23177]91    switch ($conf['back2front']['position'])
[11219]92    {
93      case 'toolbar':
[12361]94        $template->concat('PLUGIN_PICTURE_ACTIONS', $template->parse('B2F_picture_content', true));
[11219]95        break;
96      case 'top':
[23177]97        $content = $template->parse('B2F_picture_content', true)."\n".$content;
[11219]98        break;
99      case 'bottom':
[23177]100        $content = $content."\n".$template->parse('B2F_picture_content', true);
[11219]101        break;
102    }   
[10819]103  }
[11219]104 
105  return $content;
[10819]106}
107
108
109/*
110 * Add field on picture modify page
111 */
[23177]112function back2front_picture_modify()
[10819]113{
[10821]114  global $page, $template, $conf;
[10852]115 
[21212]116  if ($page['page'] != 'photo') return;
117  if (isset($_GET['tab']) && $_GET['tab']!='properties') return;
118 
[10823]119/* SAVE VALUES */
120  if (isset($_POST['b2f_submit']))
121  {
[10852]122    /* catch all verso and recto ids and original categories */
[23177]123    $query = 'SELECT * FROM '.B2F_TABLE.';';
124    $result = pwg_query($query);
[10852]125   
[23177]126    $rectos = $versos = $cats = array();
127    while ($row = pwg_db_fetch_assoc($result))
128    {
129      $rectos[] = $row['image_id'];
130      $versos[] = $row['verso_id'];
131      $cats[] = $row['categories'];
132    }
133   
[10852]134    if (count($rectos) != 0)
135    {
136      $all_recto_verso = array_combine($rectos, $versos);
137      $verso_cats = array_combine($versos, $cats);
138    }
139    else
140    {
141      $all_recto_verso = array(0=>0);
[23177]142      $verso_cats = array(0=>null);
[10852]143    }
144    unset($rectos, $versos, $cats);
145   
[10823]146    /* picture is verso */
147    if (isset($_POST['b2f_is_verso']))
[10852]148    {     
[10823]149      /* verso don't exists */
150      if (!picture_exists($_POST['b2f_front_id']))
[10819]151      {
[23177]152        array_push($page['errors'], sprintf(
153          l10n('Unknown id %d for frontside picture'), 
154          $_POST['b2f_front_id']
155          ));
[10823]156      }
157      /* verso same as recto  */
158      else if ($_POST['b2f_front_id'] == $_GET['image_id'])
159      {
[10852]160        array_push($page['errors'], l10n('Backside and frontside can\'t be the same picture'));
[10823]161      }
162      /* recto has already a verso */
[23177]163      else if (in_array($_POST['b2f_front_id'], array_keys($all_recto_verso)) && $all_recto_verso[$_POST['b2f_front_id']] != $_GET['image_id'])
[10823]164      {
[23177]165        $recto_current_verso['id'] = $all_recto_verso[$_POST['b2f_front_id']];
166        $recto_current_verso['link'] = get_root_url().'admin.php?page=photo-'.$recto_current_verso['id'];
167       
168        array_push($page['errors'], sprintf(
169          l10n('The picture n°%d has already a backside : %s'), 
170          $_POST['b2f_front_id'], 
171          '<a href="'.$recto_current_verso['link'].'">'.$recto_current_verso['id'].'</a>'
172          ));
[10823]173      }
174      /* recto is already a verso */
175      else if (in_array($_POST['b2f_front_id'], array_values($all_recto_verso)))
176      {
[23177]177        $recto_is_verso['id'] = $_POST['b2f_front_id'];
178        $recto_is_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;image_id='.$recto_is_verso['id'];
179       
180        array_push($page['errors'],  sprintf(
181          l10n('The picture n°%s is already a backside'), 
182          '<a href="'.$recto_is_verso['link'].'">'.$recto_is_verso['id'].'</a>'
183          ));
[10823]184      }
185      /* everything is fine */
186      else
187      {
[10852]188        // move the verso - if first save
[23177]189        if (isset($_POST['b2f_move_verso']) && (!array_key_exists($_GET['image_id'], $verso_cats) || $verso_cats[$_GET['image_id']] == null))
[10819]190        {
[10852]191          // get current categories
[23177]192          $query = 'SELECT category_id FROM '.IMAGE_CATEGORY_TABLE.' WHERE image_id = '.$_GET['image_id'].';';
[10852]193          $verso_categories = array_from_query($query, 'category_id');
194         
[23177]195          pwg_query('DELETE FROM '.IMAGE_CATEGORY_TABLE.' WHERE image_id = '.$_GET['image_id'].';');
196          pwg_query('INSERT INTO '.IMAGE_CATEGORY_TABLE.'(image_id, category_id) VALUES('.$_GET['image_id'].', '.$conf['back2front']['versos_cat'].');');
[10821]197           
[10823]198          // random representant for each categories
199          set_random_representant($verso_categories);
[10852]200         
201          $verso_categories = isset($verso_cats[$_GET['image_id']]) ? $verso_cats[$_GET['image_id']] : implode(',',$verso_categories);
202          $template->assign('B2F_MOVE_VERSO', 'checked="checked"');
[10819]203        }
[10852]204        // restore the verso - if precedently moved
[23177]205        else if (!isset($_POST['b2f_move_verso']) && array_key_exists($_GET['image_id'], $verso_cats) && $verso_cats[$_GET['image_id']] != null)
[10852]206        {
207          $item['verso_id'] = $_GET['image_id'];
208          $item['categories'] = $verso_cats[$_GET['image_id']];
209          back2front_restaure_categories($item);
210         
211          $verso_categories = 'NULL';
212          $template->assign('B2F_MOVE_VERSO', '');
213        }
214        // leave the verso
215        else
216        {
217          $verso_categories = isset($verso_cats[$_GET['image_id']]) ? $verso_cats[$_GET['image_id']] : 'NULL';
218          $template->assign('B2F_MOVE_VERSO', isset($verso_cats[$_GET['image_id']]) ? 'checked="checked"' : '');
219        }
220       
221        // insert or update verso associations
[23177]222        $query = '
223INSERT INTO '.B2F_TABLE.'
224  VALUES(
225    '.$_POST['b2f_front_id'].',
226    '.$_GET['image_id'].',
227    "'.$verso_categories.'"
228  )
229  ON DUPLICATE KEY UPDATE
230    image_id = '.$_POST['b2f_front_id'].',
231    categories = "'.$verso_categories.'"
232;';
233        pwg_query($query);
234       
[10823]235        $template->assign(array(
236          'B2F_IS_VERSO' => 'checked="checked"',
237          'B2F_FRONT_ID' => $_POST['b2f_front_id'],
238        ));
239       
[10852]240        $verso['id'] = $_POST['b2f_front_id'];
[21212]241        $verso['link'] = get_root_url().'admin.php?page=photo-'.$verso['id'];
[23177]242       
243        array_push($page['infos'], sprintf(
244          l10n('This picture is now the backside of the picture n°%s'),
245          '<a href="'.$verso['link'].'">'.$verso['id'].'</a>'
246          ));
[10823]247      }
248    }
249    /* picture isn't verso */
250    else
251    {
252      /* search if it was a verso */
[23177]253      $query = '
254SELECT categories
255  FROM '.B2F_TABLE.'
256  WHERE verso_id = '.$_GET['image_id'].'
257;';
[10823]258      $result = pwg_query($query);
259     
[23177]260      /* it must be restored to its original categories */
[10823]261      if (pwg_db_num_rows($result))
262      {
[10852]263        $item['verso_id'] = $_GET['image_id'];
[10823]264        list($item['categories']) = pwg_db_fetch_row($result);
[23177]265       
[10852]266        back2front_restaure_categories($item);
[23177]267        pwg_query('DELETE FROM '.B2F_TABLE.' WHERE verso_id = '.$_GET['image_id'].';');
[10852]268        array_push($page['infos'], l10n('This picture is no longer a backside'));
[10819]269      }
270    }
[10823]271  }
272 
273/* GET SAVED VALUES */
274  if ($template->get_template_vars('B2F_IS_VERSO') == null)
275  {
[10852]276    $template->assign('B2F_MOVE_VERSO', 'checked="checked"');
277   
[10823]278    /* is the picture a verso ? */
[23177]279    $query = '
280SELECT image_id, categories
281  FROM '.B2F_TABLE.'
282  WHERE verso_id = '.$_GET['image_id'].'
283;';
[10823]284    $result = pwg_query($query);
285   
286    if (pwg_db_num_rows($result))
287    {
[10852]288      list($recto_id, $cats) = pwg_db_fetch_row($result);
[23177]289     
[10823]290      $template->assign(array(
291        'B2F_IS_VERSO' => 'checked="checked"',
292        'B2F_FRONT_ID' => $recto_id,
[10852]293        'B2F_MOVE_VERSO' => $cats != NULL ? 'checked="checked"' : '',
[10823]294      ));
295    }
296    /* is the picture a front ? */
[10819]297    else
298    {
[23177]299      $query = '
300SELECT verso_id
301  FROM '.B2F_TABLE.'
302  WHERE image_id = '.$_GET['image_id'].'
303;';
[10819]304      $result = pwg_query($query);
305     
306      if (pwg_db_num_rows($result))
[10852]307      {     
[10823]308        $item = pwg_db_fetch_assoc($result);
[10852]309
[10819]310        $template->assign(array(
[10852]311          'B2F_VERSO_ID' => $item['verso_id'],
[21212]312          'B2F_VERSO_URL' => get_root_url().'admin.php?page=photo-'.$item['verso_id'],
[10819]313        ));
314      }
315    }
316  }
[10823]317 
[23177]318  $template->set_prefilter('picture_modify', 'back2front_picture_modify_prefilter');
[10819]319}
320
[10852]321
[23177]322function back2front_picture_modify_prefilter($content, &$smarty)
[10819]323{
[21212]324  $search = '</form>';
325  $replacement = $search."\n\n".file_get_contents(B2F_PATH.'template/picture_modify.tpl');
[10852]326  return str_replace($search, $replacement, $content);
[10819]327}
328
[12361]329
330/*
331 * Add mark on thumbnails list
332 */
[23177]333function back2front_thumbnails($tpl_thumbnails_var)
[12361]334{
[21212]335  global $conf, $selection;
[12361]336 
[23177]337  if (!$conf['back2front']['show_thumbnail']) return $tpl_thumbnails_var;
[21212]338  if (empty($tpl_thumbnails_var)) return $tpl_thumbnails_var;
[12361]339   
340  /* has the pictures a verso ? */
[23177]341  $query = '
342SELECT image_id
343  FROM '.B2F_TABLE.'
344  WHERE image_id IN('.implode(',', $selection).')
345;';
[21212]346  $ids = array_from_query($query, 'image_id');
[12361]347 
[21212]348  $root_path = get_absolute_root_url();
[12361]349 
350  foreach($tpl_thumbnails_var as &$tpl_var)
351  {
[21212]352    if (in_array($tpl_var['id'], $ids))
[12361]353    {
[21212]354      $tpl_var['NAME'].= ' <img class="has_verso" src="'.$root_path.B2F_PATH.'template/rotate_1.png" title="'.l10n('This picture has a backside :').'"/>';
[12361]355    }
356  }
357 
358  return $tpl_thumbnails_var;
359}
360
[10819]361?>
Note: See TracBrowser for help on using the repository browser.