NGG_Sort_Manager

Class NGG_Sort_Manager.

Defined (1)

The class is defined in the following location(s).

/admin/manage/class-ngg-sort-manager.php  
  1. class NGG_Sort_Manager implements NGG_Displayable { 
  2.  
  3. /** 
  4. * @todo We also use this in NGG_Manager. Should we make another superclass or a trait for one line? 
  5. */ 
  6. const BASE = 'admin.php?page=nggallery-manage'; 
  7.  
  8. private $id; 
  9.  
  10. public function __construct() { 
  11. $this->id = (int) $_GET['gid']; 
  12.  
  13. function display() { 
  14.  
  15. /** 
  16. * Check if sorting is actually allowed. 
  17. */ 
  18. $options = get_option( 'ngg_options' ); 
  19.  
  20. if ( $options['galSort'] != "sortorder" ) { 
  21. //Disable sort button and provide feedback why is disabled 
  22. nggGallery::show_error( __( 'To enable manual Sort set Custom Order Sort. See Settings->Gallery Settings->Sort Options',  
  23. 'nggallery' ) ); 
  24. echo '<a href="' . self::BASE . '&mode=image&gid=' . $this->id . '">' . __( 'Go back',  
  25. 'nggallery' ) . '</a>'; 
  26.  
  27. return; 
  28.  
  29. if ( isset( $_POST['update_sort_order'] ) ) { 
  30. $this->handle_update_sort_order(); 
  31.  
  32. /** 
  33. * @global $nggdb nggdb 
  34. */ 
  35. global $nggdb; 
  36.  
  37. // look for presort args 
  38. if ( isset( $_GET['presort'] ) ) { 
  39. $presort = $_GET['presort']; 
  40. } else { 
  41. $presort = false; 
  42. if ( ( isset( $_GET['dir'] ) && $_GET['dir'] == 'DESC' ) ) { 
  43. $dir = 'DESC'; 
  44. } else { 
  45. $dir = 'ASC'; 
  46. $sort_items = array( 'pid', 'filename', 'alttext', 'imagedate' ); 
  47. // ensure that nobody added some evil sorting :-) 
  48. if ( in_array( $presort, $sort_items ) ) { 
  49. $picturelist = $nggdb->get_gallery( $this->id, $presort, $dir, false ); 
  50. } else { 
  51. $picturelist = $nggdb->get_gallery( $this->id, 'sortorder', $dir, false ); 
  52.  
  53. //This is the url without any presort variable 
  54. $clean_url = self::BASE . '&mode=sort&gid=' . $this->id; 
  55. //If we go back, then the mode should be edit 
  56. $back_url = self::BASE . '&mode=image&gid=' . $this->id; 
  57.  
  58. //In case of presort, then we take this url. 
  59. if ( isset( $_GET['dir'] ) || isset( $_GET['presort'] ) ) { 
  60. $base_url = $_SERVER['REQUEST_URI']; 
  61. } else { 
  62. $base_url = $clean_url; 
  63.  
  64. ?> 
  65. <div class="wrap"> 
  66. <h2><?php _e( 'Sort Gallery', 'nggallery' ) ?></h2> 
  67.  
  68. <form id="sort_gallery" method="POST" action="<?php echo $clean_url ?>" accept-charset="utf-8"> 
  69. <div class="tablenav"> 
  70. <div class="alignleft actions"> 
  71. <?php wp_nonce_field( 'ngg-update-sort' ) ?> 
  72. <a href="<?php echo esc_url( $back_url ); ?>" class="button"><?php _e( 'Back to gallery',  
  73. 'nggallery' ); ?></a> 
  74. <input class="button-primary action" type="submit" name="update_sort_order" onclick="saveImageOrder()" value="<?php _e( 'Update Sort Order',  
  75. 'nggallery' ) ?>"> 
  76. </div> 
  77. </div> 
  78. <input name="sort_order" type="hidden" id="sort_order"> 
  79. <ul class="subsubsub"> 
  80. <li><?php _e( 'Presort', 'nggallery' ) ?> :</li> 
  81. <li><a href="<?php echo esc_attr( remove_query_arg( 'presort',  
  82. $base_url ) ); ?>" <?php if ( $presort == '' ) { 
  83. echo 'class="current"'; 
  84. } ?>><?php _e( 'Unsorted', 'nggallery' ) ?></a> | 
  85. </li> 
  86. <li><a href="<?php echo esc_attr( add_query_arg( 'presort', 'pid',  
  87. $base_url ) ); ?>" <?php if ( $presort == 'pid' ) { 
  88. echo 'class="current"'; 
  89. } ?>><?php _e( 'Image ID', 'nggallery' ) ?></a> | 
  90. </li> 
  91. <li><a href="<?php echo esc_attr( add_query_arg( 'presort', 'filename',  
  92. $base_url ) ); ?>" <?php if ( $presort == 'filename' ) { 
  93. echo 'class="current"'; 
  94. } ?>><?php _e( 'Filename', 'nggallery' ) ?></a> | 
  95. </li> 
  96. <li><a href="<?php echo esc_attr( add_query_arg( 'presort', 'alttext',  
  97. $base_url ) ); ?>" <?php if ( $presort == 'alttext' ) { 
  98. echo 'class="current"'; 
  99. } ?>><?php _e( 'Alt/Title text', 'nggallery' ) ?></a> | 
  100. </li> 
  101. <li><a href="<?php echo esc_attr( add_query_arg( 'presort', 'imagedate',  
  102. $base_url ) ); ?>" <?php if ( $presort == 'imagedate' ) { 
  103. echo 'class="current"'; 
  104. } ?>><?php _e( 'Date/Time', 'nggallery' ) ?></a> | 
  105. </li> 
  106. <li><a href="<?php echo esc_attr( add_query_arg( 'dir', 'ASC',  
  107. $base_url ) ); ?>" <?php if ( $dir == 'ASC' ) { 
  108. echo 'class="current"'; 
  109. } ?>><?php _e( 'Ascending', 'nggallery' ) ?></a> | 
  110. </li> 
  111. <li><a href="<?php echo esc_attr( add_query_arg( 'dir', 'DESC',  
  112. $base_url ) ); ?>" <?php if ( $dir == 'DESC' ) { 
  113. echo 'class="current"'; 
  114. } ?>><?php _e( 'Descending', 'nggallery' ) ?></a></li> 
  115. </ul> 
  116. </form> 
  117. <br style="clear:both"> 
  118.  
  119. <div class='sortable'> 
  120. <?php 
  121. if ( $picturelist ) { 
  122. foreach ( $picturelist as $picture ) { 
  123. ?> 
  124. <div class="image-box" id="pid-<?php echo $picture->pid ?>" data-id="<?php echo $picture->pid ?>"> 
  125. <img src="<?php echo esc_url( $picture->thumbURL ); ?>"> 
  126.  
  127. <p><?php echo esc_html( stripslashes( $picture->alttext ) ); ?></p> 
  128. </div> 
  129. <?php 
  130. ?> 
  131. </div> 
  132. </div> 
  133. <script type="text/javascript"> 
  134. jQuery(document).ready(function() { 
  135. jQuery(".sortable").sortable({items: 'div.image-box'}); 
  136. jQuery("#sort_gallery").submit(function() { 
  137. var ids = []; 
  138.  
  139. jQuery(".image-box").each(function() { 
  140. ids.push(jQuery(this).data('id')); 
  141. }); 
  142.  
  143. jQuery('#sort_order').val(ids.join(', ')); 
  144. }) 
  145. }); 
  146. </script> 
  147. <?php 
  148.  
  149. /** 
  150. * Update the sort order if the user wants to. 
  151. * @access private 
  152. */ 
  153. private function handle_update_sort_order() { 
  154.  
  155. //Check the nonce. 
  156. if ( wp_verify_nonce( $_POST['_wpnonce'], 'ngg-update-sort' ) === false ) { 
  157. nggGallery::show_error( __( 'You waited too long, or you cheated.', 'nggallery' ) ); 
  158.  
  159. return; 
  160.  
  161. global $wpdb; 
  162.  
  163. $ids = $integerIDs = array_map( 'intval', explode( ', ', $_POST['sort_order'] ) ); 
  164.  
  165. if ( is_array( $ids ) ) { 
  166.  
  167. /** 
  168. * Prepare the SQL statement. Preparing isn't necessary here: we are sure we have int: 
  169. * PHP's internal counter and an array we have run intval over. Preparing would cost too much for no gain. 
  170. */ 
  171. $sql = "UPDATE $wpdb->nggpictures SET sortorder = CASE"; 
  172.  
  173. foreach ( $ids as $key => $pic_id ) { 
  174. $sql .= " WHEN pid = $pic_id THEN $key"; 
  175.  
  176. $string = join( ', ', $ids ); 
  177.  
  178. $sql .= " END WHERE pid IN ($string)"; 
  179.  
  180. $wpdb->query( $sql ); 
  181.  
  182. do_action( 'ngg_gallery_sort', $this->id ); 
  183.  
  184. nggGallery::show_message( __( 'Sort order changed', 'nggallery' ) );