Changeset 38888

Show
Ignore:
Timestamp:
04/07/08 05:13:42 (3 months ago)
Author:
ketsugi
Message:

[ADD] Pagination and filtering (num of acronyms, order by field)
[FIX] Will now replace acronyms following a slash '/' character

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • acronyms/trunk/acronyms.php

    r38715 r38888  
    174174            add_option( 'acronym_content', 1 ); 
    175175            add_option( 'acronym_comments', 1 ); 
     176            add_option( 'default_num_of_acronyms', 15 ); 
    176177             
    177178      } 
     
    192193      { 
    193194            // Add the Manage page 
    194             add_management_page(__('Manage Acronyms'), __('Acronyms'), "manage_options", __FILE__, array( 'Acronyms', 'manage_acronyms') ); 
     195            add_management_page( __( 'Manage Acronyms' ), __( 'Acronyms' ), "manage_options", __FILE__, array( 'Acronyms', 'manage_acronyms' ) ); 
    195196      } 
    196197       
    197198      function management_handler () { 
    198             $title = __('Acronyms'); 
     199            $title = __( 'Acronyms' ); 
    199200            $parent_file = 'edit.php?page=acronyms.php'; 
    200201            $acronyms = get_option( 'acronym_acronyms' ); 
     
    202203            wp_reset_vars(array('action', 'acronym')); 
    203204 
    204             if ( isset($_GET['deleteit']) ) 
     205            if ( isset( $_GET['deleteit'] ) ) // Bulk delete array of acronyms 
    205206                  $action = 'delete'; 
     207            else if ( isset( $_GET['search'] ) ) // Search term for redirecting to less crufty HTTP GET URL 
     208              $action = 'search'; 
    206209            else $action = $_GET['action']; 
    207210 
     
    216219                  exit; 
    217220                        break; 
     221                   
    218222                  case 'edit-acronym': 
    219223                        $acronym = $_GET['acronym']; 
     
    225229                  exit; 
    226230                        break; 
    227                   case 'delete': 
     231               
     232                  case 'delete': 
    228233                        check_admin_referer('delete-acronym'); 
    229234 
    230                  if ( !current_user_can('manage_categories') ) 
     235            if ( !current_user_can('manage_categories') ) 
    231236                              wp_die(__('Cheatin’ uh?')); 
    232237 
    233                 $acronyms = $_GET['delete_acronyms']; 
     238                $acronyms = $_GET['delete_acronyms']; 
    234239                  foreach( (array) $acronyms as $acronym ) { 
    235                             Acronyms::delete ( $acronym ); 
    236                 } 
    237                 
    238                 if ( 1 < ( count( $acronyms ) ) ) 
    239                       $message = 6; 
     240                      Acronyms::delete ( $acronym ); 
     241                } 
     242           
     243                if ( 1 < ( count( $acronyms ) ) ) 
     244                      $message = 6; 
    240245                  else $message = 2; 
    241246                        wp_redirect('edit.php?page=acronyms.php&message=' . $message); 
    242247                  exit; 
    243                         break; 
     248                  break; 
     249               
     250                  case 'search': 
     251                        $s = '&s=' . urlencode( $_GET['search'] ); 
     252                        $p = isset( $_GET['p'] ) ? '&p=' . $_GET['p'] : ''; 
     253                        $n = isset( $_GET['n'] ) && get_option( 'default_num_of_acronyms' ) != $_GET['n'] ? '&n=' . $_GET['n'] : ''; 
     254                        $o = isset( $_GET['o'] ) ? '&o=' . $_GET['o'] : ''; 
     255                        wp_redirect( "edit.php?page=acronyms.php$s$p$n$o" ); 
    244256            } 
    245257      } 
     
    253265      function manage_acronyms () 
    254266      { 
     267            // Set any error/notice messages based on the 'message' GET value 
    255268            $message = $_GET['message']; 
    256269            $messages[1] = __('Acronym added.'); 
     
    260273            $messages[5] = __('Acronym not updated.'); 
    261274            $messages[6] = __('Acronyms deleted.'); 
    262  
     275             
    263276            if ( isset( $message ) ) { ?> 
    264277<div id="message" class="updated fade"><p><?php echo $messages[$message] ?></p></div> 
     
    268281                  echo '<br class="clear"/><br class="clear"/>'; 
    269282            } 
     283            // Retrieve and set pagination information 
     284            $s = isset( $_GET['s'] ) ? urldecode( $_GET['s'] ) : ''; // Number of acronyms per page 
     285            $o = isset( $_GET['o'] ) ? urldecode( $_GET['o'] ) : 'acronym'; // Sort acronyms by initalism or by full text (default is initialism) 
     286            $p = isset( $_GET['p'] ) && is_numeric( $_GET['p'] ) && 0 < $_GET['p'] ? $_GET['p'] : 1; // Which page to display 
     287            if ( isset( $_GET['n'] ) ) { // Number of acronyms per page 
     288                  if ( is_numeric( $_GET['n'] ) && 0 < $_GET['n'] ) $n = $_GET['n']; 
     289                  else if ( 'All' == $_GET['n'] ) $n = 0; 
     290            } 
     291            else $n = get_option( 'default_num_of_acronyms' ); 
     292            if ( 0 == $n ) $t = 1; 
     293            else $t = ceil( Acronyms::count_acronyms( $s ) / $n ); // Total number of pages, rounded up to nearest integer 
    270294?> 
    271295<div class="wrap"> 
    272296  <form id="posts-filter" action="" method="get"> 
    273297  <input type="hidden" name="page" value="acronyms.php"/> 
    274         <h2><?php 
    275                                           _e('Manage Acronyms'); 
    276                                           if( 'edit' != $_GET['action'] ) echo ' (<a href="#addacronym">' . __('add new') . '</a>)' 
    277                   ?></h2> 
    278             <p id="post-search"> 
    279                   <input type="text" id="post-search-input" name="s" value="<?php echo attribute_escape(stripslashes($_GET['s'])); ?>" /> 
    280                   <input type="submit" value="<?php _e( 'Search acronyms' ); ?>" class="button" /> 
    281             </p> 
    282       <br class="clear" /> 
    283  
    284   <div class="tablenav"> 
    285     <div class="alignleft"> 
    286       <input type="submit" value="<?php _e('Delete') ?>" name="deleteit" class="button-secondary delete" /> 
    287       <?php wp_nonce_field('delete-acronym'); ?> 
    288             </div> 
    289     <br class="clear" /> 
    290   </div> 
    291   <br class="clear" /> 
    292 <?php Acronyms::show_acronym_list($_GET['s']) ?> 
     298  <h2><?php 
     299                                    _e('Manage Acronyms'); 
     300                                    if( 'edit' != $_GET['action'] ) echo ' (<a href="#addacronym">' . __('add new') . '</a>)' 
     301            ?></h2> 
     302      <p id="post-search"> 
     303            <input type="text" id="post-search-input" name="search" value="<?php echo attribute_escape( stripslashes( $_GET['s'] ) ); ?>" /> 
     304            <input type="submit" value="<?php _e( 'Search acronyms' ); ?>" class="button" /> 
     305      </p> 
     306      <br class="clear" /> 
     307<?php  
     308Acronyms::show_pagination_bar( $n, $p, $t, $o, true); 
     309Acronyms::show_acronym_list( $s, $n, $p, $o ); 
     310Acronyms::show_pagination_bar( $n, $p, $t, $o); 
     311?> 
    293312  </form> 
    294313</div><?php 
    295314            if ( 'edit' != $_GET['action'] || empty( $_GET['acronym'] ) ) { 
    296315                  echo '<br class="clear"/><br class="clear"/>'; 
    297                   Acronyms::add_acronym_form ( '', '' ); 
     316                  Acronyms::add_acronym_form (); 
    298317            } 
    299318      } 
     
    309328            $text = " $text "; 
    310329            foreach ( $acronyms as $acronym => $fulltext ) 
    311                   $text = preg_replace( "|(?!<[^<>]*?)(?<![?./&])\b$acronym\b(?!:)(?![^<>]*?>)|msU", "<acronym title=\"$fulltext\">$acronym</acronym>" , $text ); 
     330                  $text = preg_replace( "|(?!<[^<>]*?)(?<![?.&])\b$acronym\b(?!:)(?![^<>]*?>)|msU", "<acronym title=\"$fulltext\">$acronym</acronym>" , $text ); 
    312331            $text = trim($text); 
    313332            return $text; 
     
    321340      } 
    322341 
    323   /* string show_acronym_list ( string $s, int $num_of_acronyms, int $page_num
     342  /* string show_acronym_list ( string $s, int $n, int $p
    324343       * 
    325        * Displays the list of acronyms, filtered by search term $s and showing page # $page_num based on $num_of_acronyms per page 
    326        */ 
    327       function show_acronym_list ( $s ) 
    328       { 
     344       * Displays the list of acronyms, filtered by search term $s and showing page # $p based on $n per page 
     345       */ 
     346      function show_acronym_list ( $s, $n, $p = '1', $o = 'acronym' ) 
     347      { 
     348            // Sort the acronyms appropriately 
     349            $acronyms = get_option( 'acronym_acronyms' ); 
     350            if ( 'full' == $o ) 
     351                  natcasesort( $acronyms ); 
     352            else 
     353                  uksort( $acronyms, "strnatcmp"); 
     354 
    329355?> 
    330356      <table class="widefat"> 
     
    338364      <tbody id="the-list" class="list:acronym"> 
    339365<?php 
    340             $acronyms = get_option( 'acronym_acronyms' ); 
     366            $index_start = $n * ( $p - 1 ); 
     367            $index_end = $n * $p; 
     368            $index = 0; 
    341369            foreach ( $acronyms as $acronym => $fulltext ) 
    342370            { 
    343                   if ( ( '' == $s ) || ( ( false !== strpos( strtolower( $acronym ), strtolower( $s ) ) ) || ( false !== strpos( strtolower( $fulltext ), strtolower( $s ) ) ) ) ) { 
    344 ?> 
    345             <tr<?php if (0 == $i++ % 2) echo ' class="alternate"' ?>> 
    346               <th scope="row" class="check-column"> <input type="checkbox" name="delete_acronyms[]" value="<?php echo $acronym ?>" /></th> 
    347                   <td><strong> 
    348                     <a class="row-title" href="edit.php?page=acronyms.php&amp;action=edit&amp;acronym=<?php echo urlencode( $acronym ) ?>&amp;fulltext=<?php echo urlencode( $fulltext ) ?>" title="Edit &quot;<?php echo $acronym ?>&quot;"> 
    349                       <?php echo $acronym ?> 
    350                     </a></strong></td> 
    351                   <td><?php echo $fulltext ?></td> 
    352             </tr> 
    353 <?php 
     371                  if ( ( '' == $s ) || ( ( false !== strpos( strtolower( $acronym ), strtolower( $s ) ) ) || ( false !== strpos( strtolower( $fulltext ), strtolower( $s ) ) ) ) ) 
     372                  { 
     373                        if ( 0 == $n || ( ++$index >= $index_start && $index <= $index_end ) ) { 
     374      ?> 
     375                  <tr<?php if (0 == $i++ % 2) echo ' class="alternate"' ?>> 
     376                    <th scope="row" class="check-column"> <input type="checkbox" name="delete_acronyms[]" value="<?php echo $acronym ?>" /></th> 
     377                        <td><strong> 
     378                          <a class="row-title" href="edit.php?page=acronyms.php&amp;action=edit&amp;acronym=<?php echo urlencode( $acronym ) ?>&amp;fulltext=<?php echo urlencode( $fulltext ) ?>" title="Edit &quot;<?php echo $acronym ?>&quot;"><?php echo $acronym ?></a></strong></td> 
     379                        <td><?php echo $fulltext ?></td> 
     380                  </tr> 
     381      <?php 
     382                        } 
    354383                  } 
    355384            } 
     
    360389      } 
    361390       
     391      function show_pagination_bar ( $n, $p, $t, $o, $filter = false) 
     392      { 
     393            echo '<div class="tablenav">'; 
     394            // Display pagination links 
     395            $page_links = paginate_links( array( 
     396                  'base' => add_query_arg( 'p', '%#%' ), 
     397                  'format' => '', 
     398                  'total' => $t, 
     399                  'current' => $p, 
     400                  'add_args' => $n 
     401            ) ); 
     402 
     403            if ( 0 < $n && $page_links ) 
     404                  echo "<div class='tablenav-pages'>$page_links</div>"; 
     405?> 
     406 
     407    <div class="alignleft"> 
     408      <input type="submit" value="<?php _e('Delete') ?>" name="deleteit" class="button-secondary delete" /> 
     409<?php 
     410            wp_nonce_field('delete-acronym');  
     411            if ( $filter ) {  
     412                  echo '<select name="n" id="n">'; 
     413                  $num_options = array ( 5, 15, 25, 50, 'All' ); 
     414                  foreach ( $num_options as $option ) { 
     415                        echo '<option value="' . $option . '"'; 
     416                        if ( $n == $option ) echo ' selected="selected" '; 
     417                        echo '>Show ' . $option . ' acronyms per page</option>'; 
     418                  } 
     419?> 
     420                  </select> 
     421                  <select name="o" id="o"> 
     422                        <option value="acronym"<?php echo 'acronym' == $o ? ' selected="selected"' : '' ?>>Order by acronym</option> 
     423                        <option value="full"<?php echo 'full' == $o ? ' selected="selected"' : '' ?>>Order by full text</option> 
     424                  </select> 
     425                  <input type="submit" value="Filter" class="button-secondary" /> 
     426<?php 
     427            } 
     428?> 
     429            </div> 
     430    <br class="clear" /> 
     431  </div> 
     432  <br class="clear" /> 
     433<?php 
     434      } 
     435 
    362436      /* void add_acronym_form ( string $acronym, string $fulltext )  
    363437       * 
    364438       * Display the form for adding or editing acronyms 
    365439       */ 
    366   function add_acronym_form ( $acronym, $fulltext
     440  function add_acronym_form ( $acronym = '', $fulltext = ''
    367441      { 
    368442            if ( ! empty($acronym) ) { 
     
    398472<?php } 
    399473 
    400       /* void update ( string $acronym, string $fulltext ) 
     474      /* boolean update ( string $acronym, string $fulltext ) 
    401475       * 
    402476       * Add a new acronym to the list, or edit an existing one 
     
    408482                  $acronyms = get_option( 'acronym_acronyms' ); 
    409483                  $acronyms[$acronym] = $fulltext; 
    410                   uksort( $acronyms, "strcmp"); 
    411484                  update_option( 'acronym_acronyms', $acronyms ); 
    412485                  return true; 
     
    414487      } 
    415488 
    416       /* void delete ( string $acronym ) 
     489      /* boolean delete ( string $acronym ) 
    417490       * 
    418491       * Delete an existing acronym 
     
    425498                  unset( $acronyms[$acronym] ); 
    426499                  update_option( 'acronym_acronyms', $acronyms ); 
     500                  return true; 
     501            } 
     502            else return false; 
     503      } 
     504       
     505      /* int count_acronyms ( string $s ) 
     506       * 
     507       * Get the number of acronyms based on the search term if provided 
     508       */ 
     509      function count_acronyms ( $s = '' ) 
     510      { 
     511            $acronyms = get_option( 'acronym_acronyms' ); 
     512            if ( empty( $s ) ) return count( $acronyms ); 
     513            else { 
     514                  $index = 0; 
     515                  foreach ( $acronyms as $acronym => $fulltext ) 
     516                  { 
     517                        if ( false !== strpos( strtolower( $acronym ), strtolower( $s ) ) || ( false !== strpos( strtolower( $fulltext ), strtolower( $s ) ) ) ) 
     518                        { 
     519                              $index++; 
     520                        } 
     521                  } 
     522                  return $index; 
    427523            } 
    428524      }