Ignore:
Timestamp:
Jun 13, 2013, 2:01:34 PM (11 years ago)
Author:
mistic100
Message:

use serialized array for config + custom upgrade process

Location:
extensions/Back2Front/include
Files:
1 added
1 moved

Legend:

Unmodified
Added
Removed
  • extensions/Back2Front/include/Back2Front.php

    r23167 r23177  
    22if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
    33
    4 include_once(B2F_PATH.'functions.inc.php');
     4include_once(B2F_PATH.'include/functions.inc.php');
    55
    66/*
    77 * Add verso link on picture page
    88 */
    9 function Back2Front_picture_content($content, $element_info)
     9function back2front_picture_content($content, $element_info)
    1010{
    1111  global $template, $user, $conf;
    1212
    1313  /* search for a verso picture */
    14   $query = "
    15     SELECT 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   ;";
     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;';
    2121  $result = pwg_query($query);
    2222
     
    2424  {
    2525    $verso = pwg_db_fetch_assoc($result);
    26     $conf['back2front'] = explode(',',$conf['back2front']);
    2726    $deriv_type = pwg_get_session_var('picture_deriv', $conf['derivative_default_size']);
    2827   
     
    4241    {
    4342      $template->assign('VERSO_U_ADMIN', get_root_url().'admin.php?page=photo-'.$verso['id']);
    44       $template->set_filename('B2F_admin_button', dirname(__FILE__).'/template/admin_button.tpl');
     43      $template->set_filename('B2F_admin_button', realpath(B2F_PATH.'template/admin_button.tpl'));
    4544      $template->concat('PLUGIN_PICTURE_ACTIONS', $template->parse('B2F_admin_button', true));
    4645    }
    4746   
    4847    /* link name */
    49     $conf['back2front'][4] = unserialize($conf['back2front'][4]);
    50     if (!empty($conf['back2front'][4][$user['language']]))
    51     {
    52       if (strpos($conf['back2front'][4][$user['language']], '|') !== false)
    53       {
    54         $conf['back2front'][4] = explode('|', $conf['back2front'][4][$user['language']]);
     48    if (!empty($conf['back2front']['link_name'][$user['language']]))
     49    {
     50      if (strpos($conf['back2front']['link_name'][$user['language']], '|') !== false)
     51      {
     52        $conf['back2front']['link_name'] = explode('|', $conf['back2front']['link_name'][$user['language']]);
    5553      }
    5654      else
    5755      {
    58         $conf['back2front'][4] = array($conf['back2front'][4][$user['language']], $conf['back2front'][4][$user['language']]);
    59       }
    60     }
    61     else if (!empty($conf['back2front'][4]['default']))
    62     {
    63       if (strpos($conf['back2front'][4]['default'], '|') != false)
    64       {
    65         $conf['back2front'][4] = explode('|', $conf['back2front'][4]['default']);
     56        $conf['back2front']['link_name'] = array_fill(0, 2, $conf['back2front']['link_name'][$user['language']]);
     57      }
     58    }
     59    else if (!empty($conf['back2front']['link_name']['default']))
     60    {
     61      if (strpos($conf['back2front']['link_name']['default'], '|') != false)
     62      {
     63        $conf['back2front']['link_name'] = explode('|', $conf['back2front']['link_name']['default']);
    6664      }
    6765      else
    6866      {
    69         $conf['back2front'][4] = array($conf['back2front'][4]['default'], $conf['back2front'][4]['default']);
     67        $conf['back2front']['link_name'] = array_fill(0, 2, $conf['back2front']['link_name']['default']);
    7068      }
    7169    }
    7270    else
    7371    {
    74       $conf['back2front'][4] = array(l10n('See back'), l10n('See front'));
    75     }
    76    
    77     if ($conf['back2front'][2] == 'fade' and $conf['back2front'][3] == 'bottom')
    78     {
    79       $conf['back2front'][3] = 'top';
     72      $conf['back2front']['link_name'] = array(l10n('See back'), l10n('See front'));
     73    }
     74   
     75    if ($conf['back2front']['transition'] == 'fade' && $conf['back2front']['position'] == 'bottom')
     76    {
     77      $conf['back2front']['position'] = 'top';
    8078    }
    8179   
    8280
    8381    /* template & output */
    84     $template->set_filename('B2F_picture_content', dirname(__FILE__).'/template/picture_content.tpl');   
     82    $template->set_filename('B2F_picture_content', realpath(B2F_PATH.'template/picture_content.tpl'));   
    8583    $template->assign(array(
    86       'b2f_switch_mode' => $conf['back2front'][1],
    87       'b2f_transition' => $conf['back2front'][2],
    88       'b2f_position' => $conf['back2front'][3],
    89       'b2f_see_back' => $conf['back2front'][4][0],
    90       'b2f_see_front' => $conf['back2front'][4][1],
     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],
    9189    ));
    9290   
    93     switch ($conf['back2front'][3])
     91    switch ($conf['back2front']['position'])
    9492    {
    9593      case 'toolbar':
     
    9795        break;
    9896      case 'top':
    99         return $template->parse('B2F_picture_content', true)."\n".$content;
     97        $content = $template->parse('B2F_picture_content', true)."\n".$content;
    10098        break;
    10199      case 'bottom':
    102         return $content."\n".$template->parse('B2F_picture_content', true);
     100        $content = $content."\n".$template->parse('B2F_picture_content', true);
    103101        break;
    104102    }   
     
    112110 * Add field on picture modify page
    113111 */
    114 function Back2Front_picture_modify()
     112function back2front_picture_modify()
    115113{
    116114  global $page, $template, $conf;
     
    118116  if ($page['page'] != 'photo') return;
    119117  if (isset($_GET['tab']) && $_GET['tab']!='properties') return;
    120  
    121  
    122   $conf['back2front'] = explode(',',$conf['back2front']);
    123118 
    124119/* SAVE VALUES */
     
    126121  {
    127122    /* catch all verso and recto ids and original categories */
    128     $query = "SELECT image_id, verso_id, categories
    129       FROM ".B2F_TABLE.";";
    130     $rectos = array_from_query($query, 'image_id');
    131     $versos = array_from_query($query, 'verso_id');
    132     $cats = array_from_query($query, 'categories');
     123    $query = 'SELECT * FROM '.B2F_TABLE.';';
     124    $result = pwg_query($query);
     125   
     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    }
    133133   
    134134    if (count($rectos) != 0)
     
    140140    {
    141141      $all_recto_verso = array(0=>0);
    142       $verso_cats = array(0=>NULL);
     142      $verso_cats = array(0=>null);
    143143    }
    144144    unset($rectos, $versos, $cats);
     
    150150      if (!picture_exists($_POST['b2f_front_id']))
    151151      {
    152         array_push(
    153           $page['errors'],
    154           sprintf(
    155             l10n('Unknown id %d for frontside picture'),
    156             $_POST['b2f_front_id']
    157             )
    158           );
     152        array_push($page['errors'], sprintf(
     153          l10n('Unknown id %d for frontside picture'),
     154          $_POST['b2f_front_id']
     155          ));
    159156      }
    160157      /* verso same as recto  */
     
    164161      }
    165162      /* recto has already a verso */
    166       else if (in_array($_POST['b2f_front_id'], array_keys($all_recto_verso)) AND $all_recto_verso[$_POST['b2f_front_id']] != $_GET['image_id'])
    167       {
    168           $recto_current_verso['id'] = $all_recto_verso[$_POST['b2f_front_id']];
    169           $recto_current_verso['link'] = get_root_url().'admin.php?page=photo-'.$recto_current_verso['id'];
    170           array_push(
    171             $page['errors'],
    172             sprintf(
    173               l10n('The picture n°%d has already a backside : %s'),
    174               $_POST['b2f_front_id'],
    175               '<a href="'.$recto_current_verso['link'].'">'.$recto_current_verso['id'].'</a>'
    176               )
    177             );
     163      else if (in_array($_POST['b2f_front_id'], array_keys($all_recto_verso)) && $all_recto_verso[$_POST['b2f_front_id']] != $_GET['image_id'])
     164      {
     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          ));
    178173      }
    179174      /* recto is already a verso */
    180175      else if (in_array($_POST['b2f_front_id'], array_values($all_recto_verso)))
    181176      {
    182           $recto_is_verso['id'] = $_POST['b2f_front_id'];
    183           $recto_is_verso['link'] = get_root_url().'admin.php?page=picture_modify&amp;image_id='.$recto_is_verso['id'];
    184           array_push(
    185             $page['errors'],
    186             sprintf(
    187               l10n('The picture n°%s is already a backside'),
    188               '<a href="'.$recto_is_verso['link'].'">'.$recto_is_verso['id'].'</a>'
    189               )
    190             );
     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          ));
    191184      }
    192185      /* everything is fine */
     
    194187      {
    195188        // move the verso - if first save
    196         if (isset($_POST['b2f_move_verso']) AND (!array_key_exists($_GET['image_id'], $verso_cats) OR $verso_cats[$_GET['image_id']] == NULL))
     189        if (isset($_POST['b2f_move_verso']) && (!array_key_exists($_GET['image_id'], $verso_cats) || $verso_cats[$_GET['image_id']] == null))
    197190        {
    198191          // get current categories
    199           $query = "SELECT category_id FROM ".IMAGE_CATEGORY_TABLE." WHERE image_id = ".$_GET['image_id'].";";
     192          $query = 'SELECT category_id FROM '.IMAGE_CATEGORY_TABLE.' WHERE image_id = '.$_GET['image_id'].';';
    200193          $verso_categories = array_from_query($query, 'category_id');
    201194         
    202           pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE."
    203             WHERE image_id = ".$_GET['image_id'].";");
    204           pwg_query("INSERT INTO ".IMAGE_CATEGORY_TABLE."(image_id, category_id)
    205             VALUES(".$_GET['image_id'].", ".$conf['back2front'][0].");");
     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'].');');
    206197           
    207198          // random representant for each categories
     
    212203        }
    213204        // restore the verso - if precedently moved
    214         else if (!isset($_POST['b2f_move_verso']) AND array_key_exists($_GET['image_id'], $verso_cats) AND $verso_cats[$_GET['image_id']] != NULL)
     205        else if (!isset($_POST['b2f_move_verso']) && array_key_exists($_GET['image_id'], $verso_cats) && $verso_cats[$_GET['image_id']] != null)
    215206        {
    216207          $item['verso_id'] = $_GET['image_id'];
     
    229220       
    230221        // insert or update verso associations
    231         pwg_query("INSERT INTO ".B2F_TABLE."
    232           VALUES(".$_POST['b2f_front_id'].", ".$_GET['image_id'].", '".$verso_categories."')
    233           ON DUPLICATE KEY UPDATE image_id = ".$_POST['b2f_front_id'].", categories = '".$verso_categories."';");
    234      
     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       
    235235        $template->assign(array(
    236236          'B2F_IS_VERSO' => 'checked="checked"',
     
    240240        $verso['id'] = $_POST['b2f_front_id'];
    241241        $verso['link'] = get_root_url().'admin.php?page=photo-'.$verso['id'];
    242         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>')));
     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          ));
    243247      }
    244248    }
     
    247251    {
    248252      /* search if it was a verso */
    249       $query = "SELECT categories
    250         FROM ".B2F_TABLE."
    251         WHERE verso_id = ".$_GET['image_id'].";";
     253      $query = '
     254SELECT categories
     255  FROM '.B2F_TABLE.'
     256  WHERE verso_id = '.$_GET['image_id'].'
     257;';
    252258      $result = pwg_query($query);
    253259     
    254       /* it must be restored to its original categories (see criteria on maintain.inc) */
     260      /* it must be restored to its original categories */
    255261      if (pwg_db_num_rows($result))
    256262      {
    257263        $item['verso_id'] = $_GET['image_id'];
    258264        list($item['categories']) = pwg_db_fetch_row($result);
     265       
    259266        back2front_restaure_categories($item);
    260        
    261         pwg_query("DELETE FROM ".B2F_TABLE."
    262           WHERE verso_id = ".$_GET['image_id'].";");
    263          
     267        pwg_query('DELETE FROM '.B2F_TABLE.' WHERE verso_id = '.$_GET['image_id'].';');
    264268        array_push($page['infos'], l10n('This picture is no longer a backside'));
    265269      }
     
    273277   
    274278    /* is the picture a verso ? */
    275     $query = "
    276       SELECT image_id, categories
    277       FROM ".B2F_TABLE."
    278       WHERE verso_id = ".$_GET['image_id']."
    279     ;";
     279    $query = '
     280SELECT image_id, categories
     281  FROM '.B2F_TABLE.'
     282  WHERE verso_id = '.$_GET['image_id'].'
     283;';
    280284    $result = pwg_query($query);
    281285   
     
    283287    {
    284288      list($recto_id, $cats) = pwg_db_fetch_row($result);
     289     
    285290      $template->assign(array(
    286291        'B2F_IS_VERSO' => 'checked="checked"',
     
    292297    else
    293298    {
    294       $query = "SELECT verso_id
    295         FROM ".B2F_TABLE."
    296         WHERE image_id = ".$_GET['image_id'].";";
     299      $query = '
     300SELECT verso_id
     301  FROM '.B2F_TABLE.'
     302  WHERE image_id = '.$_GET['image_id'].'
     303;';
    297304      $result = pwg_query($query);
    298305     
     
    309316  }
    310317 
    311   $template->set_prefilter('picture_modify', 'Back2front_picture_modify_prefilter');
     318  $template->set_prefilter('picture_modify', 'back2front_picture_modify_prefilter');
    312319}
    313320
    314321
    315 function Back2front_picture_modify_prefilter($content, &$smarty)
     322function back2front_picture_modify_prefilter($content, &$smarty)
    316323{
    317324  $search = '</form>';
     
    324331 * Add mark on thumbnails list
    325332 */
    326 function Back2Front_thumbnails($tpl_thumbnails_var)
     333function back2front_thumbnails($tpl_thumbnails_var)
    327334{
    328335  global $conf, $selection;
    329336 
    330   $conf['back2front'] = explode(',',$conf['back2front']);
    331   if (!$conf['back2front'][5]) return $tpl_thumbnails_var;
     337  if (!$conf['back2front']['show_thumbnail']) return $tpl_thumbnails_var;
    332338  if (empty($tpl_thumbnails_var)) return $tpl_thumbnails_var;
    333339   
    334340  /* has the pictures a verso ? */
    335   $query = "SELECT image_id
    336     FROM ".B2F_TABLE."
    337     WHERE image_id IN(".implode(',', $selection).");";
     341  $query = '
     342SELECT image_id
     343  FROM '.B2F_TABLE.'
     344  WHERE image_id IN('.implode(',', $selection).')
     345;';
    338346  $ids = array_from_query($query, 'image_id');
    339347 
Note: See TracChangeset for help on using the changeset viewer.