Ignore:
Timestamp:
Jun 7, 2011, 9:44:37 PM (13 years ago)
Author:
mistic100
Message:

big code cleaning

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/Comments_on_Albums/include/coa_albums.php

    r11251 r11267  
    44
    55// +-----------------------------------------------------------------------+
    6 //        Category's infos
     6// |                            category infos                             |
    77// +-----------------------------------------------------------------------+
    88$category = $page['category'];
     
    1010$url_self = duplicate_index_url(array(
    1111  'category' => array(
    12     'id'=>$category['id'],
    13     'name'=>$category['name'],
    14     'permalink'=>$category['permalink']
    15   ),
     12    'id' => $category['id'],
     13    'name' => $category['name'],
     14    'permalink' => $category['permalink']
     15    ),
    1616  array('start')
    17 ));
    18 
    19 
    20 // +-----------------------------------------------------------------------+
    21 //        Actions
    22 // +-----------------------------------------------------------------------+
    23 if (isset($_GET['action'])) {
    24   switch ($_GET['action']) {
    25     case 'edit_comment' : {
     17  ));
     18
     19
     20// +-----------------------------------------------------------------------+
     21// |                                actions                                |
     22// +-----------------------------------------------------------------------+
     23if (isset($_GET['action']))
     24{
     25  switch ($_GET['action'])
     26  {
     27    case 'edit_comment' :
     28    {
    2629      check_pwg_token();
     30     
    2731      include_once(COA_PATH.'include/functions_comment.inc.php'); // custom fonctions
     32     
    2833      check_input_parameter('comment_to_edit', $_GET, false, PATTERN_ID);
     34     
    2935      $author_id = get_comment_author_id_albums($_GET['comment_to_edit']);
    3036
    31       if (can_manage_comment('edit', $author_id)) {
    32         if (!empty($_POST['content'])) {
     37      if (can_manage_comment('edit', $author_id))
     38      {
     39        if (!empty($_POST['content']))
     40        {
    3341          $comment_action = update_user_comment_albums(
    3442            array(
     
    3644              'image_id' => $category['id'],
    3745              'content' => $_POST['content']
    38             ),
     46              ),
    3947            $_POST['key']
    40           );
     48            );
    4149         
    4250          $infos = array();
     
    5866
    5967          $template->assign(
    60             ($comment_action=='reject') ? 'errors' : 'infos',
    61             $infos
    62           );
     68              ($comment_action=='reject') ? 'errors' : 'infos',
     69              $infos
     70            );
    6371
    6472          unset($_POST['content']);
    6573          break;
    66         } else {
     74        }
     75        else
     76        {
    6777          $edit_comment = $_GET['comment_to_edit'];
    6878          break;
     
    7080      }
    7181    }
    72     case 'delete_comment' : {
     82    case 'delete_comment' :
     83    {
    7384      check_pwg_token();
     85     
    7486      include_once(COA_PATH.'include/functions_comment.inc.php');
     87     
    7588      check_input_parameter('comment_to_delete', $_GET, false, PATTERN_ID);
     89     
    7690      $author_id = get_comment_author_id_albums($_GET['comment_to_delete']);
    7791
    78       if (can_manage_comment('delete', $author_id)) {
     92      if (can_manage_comment('delete', $author_id))
     93      {
    7994        delete_user_comment_albums($_GET['comment_to_delete']);
    8095      }
     
    8297      redirect($url_self);
    8398    }
    84     case 'validate_comment' : {
     99    case 'validate_comment' :
     100    {
    85101      check_pwg_token();
     102     
    86103      include_once(COA_PATH.'include/functions_comment.inc.php');
     104     
    87105      check_input_parameter('comment_to_validate', $_GET, false, PATTERN_ID);
     106     
    88107      $author_id = get_comment_author_id_albums($_GET['comment_to_validate']);
    89108
    90       if (can_manage_comment('validate', $author_id)) {
     109      if (can_manage_comment('validate', $author_id))
     110      {
    91111        validate_user_comment_albums($_GET['comment_to_validate']);
    92112      }
     
    97117}
    98118
    99 
    100 // +-----------------------------------------------------------------------+
    101 //        Insert comment
    102 // +-----------------------------------------------------------------------+
    103 if ($category['commentable'] and isset($_POST['content'])) {
    104   if (is_a_guest() and !$conf['comments_forall']) {
     119// +-----------------------------------------------------------------------+
     120// |                            insert comment                             |
     121// +-----------------------------------------------------------------------+
     122if ($category['commentable'] and isset($_POST['content']))
     123{
     124  if (is_a_guest() and !$conf['comments_forall'])
     125  {
    105126    die('Session expired');
    106127  }
     
    113134
    114135  include_once(COA_PATH.'include/functions_comment.inc.php');
     136 
    115137  $comment_action = insert_user_comment_albums($comm, @$_POST['key'], $infos);
    116138
    117   switch ($comment_action) {
     139  switch ($comment_action)
     140  {
    118141    case 'moderate':
    119142      array_push($infos, l10n('An administrator must authorize your comment before it is visible.'));
     
    129152  }
    130153
    131   $template->assign(($comment_action=='reject') ? 'errors' : 'infos', $infos);
    132   trigger_action('user_comment_insertion', array_merge($comm, array('action'=>$comment_action)));
    133  
    134 } elseif (isset($_POST['content'])) {
     154  $template->assign(
     155      ($comment_action=='reject') ? 'errors' : 'infos',
     156      $infos
     157    );
     158   
     159  // allow plugins to notify what's going on
     160  trigger_action( 'user_comment_insertion',
     161      array_merge($comm, array('action'=>$comment_action) )
     162    );
     163 
     164}
     165else if (isset($_POST['content']))
     166{
    135167  set_status_header(403);
    136168  die('ugly spammer');
     
    139171
    140172// +-----------------------------------------------------------------------+
    141 //        Display comments
    142 // +-----------------------------------------------------------------------+
    143 if ($category['commentable']) {
    144   if (!is_admin()) {
     173// |                           display comments                            |
     174// +-----------------------------------------------------------------------+
     175if ($category['commentable'])
     176{
     177  if (!is_admin())
     178  {
    145179    $validated_clause = " AND validated = 'true'";
    146   } else {
     180  }
     181  else
     182  {
    147183    $validated_clause = null;
    148184  }
    149185
    150186  // number of comments for this category
    151   $query = 'SELECT COUNT(*) AS nb_comments
    152     FROM '.COA_TABLE.'
    153     WHERE category_id = '.$category['id']
    154     .$validated_clause.'
    155   ;';
     187  $query = '
     188SELECT
     189    COUNT(*) AS nb_comments
     190  FROM '.COA_TABLE.'
     191  WHERE category_id = '.$category['id']
     192  .$validated_clause.'
     193;';
    156194  $row = pwg_db_fetch_assoc(pwg_query($query));
    157195
    158196  // navigation bar creation, custom again
    159   if (isset($_GET['start_comments'])) {
     197  if (isset($_GET['start_comments']))
     198  {
    160199    $page['start_comments'] = $_GET['start_comments'];
    161   } else {
     200  }
     201  else
     202  {
    162203    $page['start_comments'] = 0;
    163204  }
     
    169210    $page['start_comments'],
    170211    $conf['nb_comment_page']
    171   );
    172 
    173   $template->assign(array(
    174     'COMMENT_COUNT' => $row['nb_comments'],
    175     'comment_navbar' => $navigation_bar,
    176   ));
    177 
    178   if ($row['nb_comments'] > 0) {
     212    );
     213
     214  $template->assign(
     215    array(
     216      'COMMENT_COUNT' => $row['nb_comments'],
     217      'comment_navbar' => $navigation_bar,
     218      )
     219    );
     220
     221  if ($row['nb_comments'] > 0)
     222  {
    179223    // get comments
    180     $query = 'SELECT
    181         com.id,
    182         com.author,
    183         com.author_id,
    184         '.$conf['user_fields']['username'].' AS username,
    185         com.date,
    186         com.category_id,
    187         com.content,
    188         com.validated
    189       FROM '.COA_TABLE.' AS com
    190       LEFT JOIN '.USERS_TABLE.' AS u
    191       ON u.'.$conf['user_fields']['id'].' = author_id
    192       WHERE category_id = '.$category['id'].'
    193       '.$validated_clause.'
    194       ORDER BY date ASC
    195       LIMIT '.$conf['nb_comment_page'].' OFFSET '.$page['start_comments'].'
    196     ;';
     224    $query = '
     225SELECT
     226    com.id,
     227    com.author,
     228    com.author_id,
     229    '.$conf['user_fields']['username'].' AS username,
     230    com.date,
     231    com.category_id,
     232    com.content,
     233    com.validated
     234  FROM '.COA_TABLE.' AS com
     235  LEFT JOIN '.USERS_TABLE.' AS u
     236    ON u.'.$conf['user_fields']['id'].' = author_id
     237  WHERE category_id = '.$category['id'].'
     238    '.$validated_clause.'
     239  ORDER BY date ASC
     240  LIMIT '.$conf['nb_comment_page'].' OFFSET '.$page['start_comments'].'
     241;';
    197242    $result = pwg_query($query);
    198243
    199     while ($row = pwg_db_fetch_assoc($result)) {
     244    while ($row = pwg_db_fetch_assoc($result))
     245    {
    200246      // author
    201       if (!empty($row['author'])) {
     247      if (!empty($row['author']))
     248      {
    202249        $author = $row['author'];
    203         if ($author == 'guest') {
     250        if ($author == 'guest')
     251        {
    204252          $author = l10n('guest');
    205253        }
    206       } else {
     254      }
     255      else
     256      {
    207257        $author = stripslashes($row['username']);
    208258      }
     
    214264        'DATE' => format_date($row['date'], true),
    215265        'CONTENT' => trigger_event('render_comment_content', $row['content']),
    216       );
     266        );
    217267     
    218268      // rights
    219       if (can_manage_comment('delete', $row['author_id'])) {
    220         $tpl_comment['U_DELETE'] = add_url_params($url_self, array(
    221           'action' => 'delete_comment',
    222           'comment_to_delete' => $row['id'],
    223           'pwg_token' => get_pwg_token(),
    224         ));
    225       }
    226       if (can_manage_comment('edit', $row['author_id'])) {
    227         $tpl_comment['U_EDIT'] = add_url_params($url_self, array(
    228           'action' => 'edit_comment',
    229           'comment_to_edit' => $row['id'],
    230           'pwg_token' => get_pwg_token(),
    231         ));
    232         if (isset($edit_comment) and ($row['id'] == $edit_comment)) {
     269      if (can_manage_comment('delete', $row['author_id']))
     270      {
     271        $tpl_comment['U_DELETE'] = add_url_params(
     272          $url_self,
     273          array(
     274            'action' => 'delete_comment',
     275            'comment_to_delete' => $row['id'],
     276            'pwg_token' => get_pwg_token(),
     277            )
     278          );
     279      }
     280      if (can_manage_comment('edit', $row['author_id']))
     281      {
     282        $tpl_comment['U_EDIT'] = add_url_params(
     283          $url_self,
     284          array(
     285            'action' => 'edit_comment',
     286            'comment_to_edit' => $row['id'],
     287            'pwg_token' => get_pwg_token(),
     288            )
     289          );
     290        if (isset($edit_comment) and ($row['id'] == $edit_comment))
     291        {
    233292          $key = get_ephemeral_key(2, $category['id']);
    234293          $tpl_comment['IN_EDIT'] = true;
     
    237296        }
    238297      }
    239       if (is_admin() AND $row['validated'] != 'true') {
    240         $tpl_comment['U_VALIDATE'] = add_url_params($url_self, array(
    241           'action' => 'validate_comment',
    242           'comment_to_validate' => $row['id'],
    243           'pwg_token' => get_pwg_token(),
    244         ));
    245       }
    246      
    247       // template
     298      if (is_admin() AND $row['validated'] != 'true')
     299      {
     300        $tpl_comment['U_VALIDATE'] = add_url_params(
     301          $url_self,
     302          array(
     303            'action' => 'validate_comment',
     304            'comment_to_validate' => $row['id'],
     305            'pwg_token' => get_pwg_token(),
     306            )
     307          );
     308      }
     309     
    248310      $template->append('comments', $tpl_comment);
    249311    }
     
    252314  // comment form
    253315  $show_add_comment_form = true;
    254   if (isset($edit_comment)) {
     316  if (isset($edit_comment))
     317  {
    255318    $show_add_comment_form = false;
    256319  }
    257   if (is_a_guest() and !$conf['comments_forall']) {
     320  if (is_a_guest() and !$conf['comments_forall'])
     321  {
    258322    $show_add_comment_form = false;
    259323  }
    260324
    261   if ($show_add_comment_form) {
     325  if ($show_add_comment_form)
     326  {
    262327    $key = get_ephemeral_key(3, $category['id']);
    263328    $content = null;
    264     if ('reject'===@$comment_action) {
     329    if ('reject'===@$comment_action)
     330    {
    265331      $content = htmlspecialchars(stripslashes($comm['content']));
    266332    }
    267     $template->assign('comment_add', array(
    268       'F_ACTION' => $url_self,
    269       'KEY' => $key,
    270       'CONTENT' => $content,
    271       'SHOW_AUTHOR' => !is_classic_user(),
    272     ));
     333    $template->assign(
     334      'comment_add',
     335      array(
     336        'F_ACTION' => $url_self,
     337        'KEY' => $key,
     338        'CONTENT' => $content,
     339        'SHOW_AUTHOR' => !is_classic_user(),
     340        )
     341      );
    273342  }
    274343 
    275344  // template
    276   $template->assign(array(
    277     'COA_PATH' => COA_PATH, // for css
    278     'COA_ABSOLUTE_PATH' => dirname(__FILE__) .'/../', // for template
    279   ));
    280  
    281   $template->set_filename('comments_on_albums', dirname(__FILE__) .'/../template/coa_albums.tpl');
     345  $template->assign(
     346    array(
     347      'COA_PATH' => COA_PATH, // for css
     348      'COA_ABSOLUTE_PATH' => dirname(__FILE__) .'/../', // for template
     349      )
     350    );
     351 
     352  $template->set_filename('comments_on_albums', dirname(__FILE__) .'/../template/albums.tpl');
    282353  $template->concat('PLUGIN_INDEX_CONTENT_END', $template->parse('comments_on_albums', true));
    283354 
    284   $template->set_filename('comments_on_albums_messages', dirname(__FILE__) .'/../template/coa_messages.tpl');
    285   $template->concat('PLUGIN_INDEX_CONTENT_BEFORE', $template->parse('comments_on_albums_messages', true));
     355  if (isset($infos))
     356  {
     357    $template->set_prefilter('index', 'coa_messages');
     358   
     359    function coa_messages($content, &$smarty)
     360    {
     361      $search = '{if !empty($PLUGIN_INDEX_CONTENT_BEFORE)}{$PLUGIN_INDEX_CONTENT_BEFORE}{/if}';
     362
     363  $replacement = $search.'
     364{if isset($errors)}
     365<div class="errors">
     366  <ul>
     367    {foreach from=$errors item=error}
     368    <li>{$error}</li>
     369    {/foreach}
     370  </ul>
     371</div>
     372{/if}
     373{if isset($infos)}
     374<div class="infos">
     375  <ul>
     376    {foreach from=$infos item=info}
     377    <li>{$info}</li>
     378    {/foreach}
     379  </ul>
     380</div>
     381{/if}';
     382
     383      return str_replace($search, $replacement, $content);
     384    }
     385  }
    286386}
    287387
Note: See TracChangeset for help on using the changeset viewer.