Title: Iltud's Replies | WordPress.org

---

# Iltud

  [  ](https://wordpress.org/support/users/iltud/)

 *   [Profile](https://wordpress.org/support/users/iltud/)
 *   [Topics Started](https://wordpress.org/support/users/iltud/topics/)
 *   [Replies Created](https://wordpress.org/support/users/iltud/replies/)
 *   [Reviews Written](https://wordpress.org/support/users/iltud/reviews/)
 *   [Topics Replied To](https://wordpress.org/support/users/iltud/replied-to/)
 *   [Engagements](https://wordpress.org/support/users/iltud/engagements/)
 *   [Favorites](https://wordpress.org/support/users/iltud/favorites/)

 Search replies:

## Forum Replies Created

Viewing 1 replies (of 1 total)

 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [[NextCellent Gallery - NextGEN Legacy] Import multiple image folders](https://wordpress.org/support/topic/import-multiple-image-folders/)
 *  [Iltud](https://wordpress.org/support/users/iltud/)
 * (@iltud)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/import-multiple-image-folders/#post-4671106)
 * Hi all,
 * I coded this hack today but slightly differently : in the “Import Folder” tab,
   I added a checkbox in front of each folder in the folder tree. So, you can easily
   choose the folders instead of typing a comma separated list.
 * My hack involves 3 files :
    - /wp-content/plugins/nextcellent-gallery-nextgen-legacy/admin/ajax.php –> adding
      of checkbox (and some dirty inline CSS to align checkbox)
    - /wp-content/plugins/nextcellent-gallery-nextgen-legacy/admin/addgallery.php–
      > managing of the array of checkbox (and some typos)
    - /wp-content/plugins/nextcellent-gallery-nextgen-legacy/admin/functions.php–
      > adding of function import_galleries($folders_list) which is mainly a copy
      of import_gallery($galleryfolder) with a “foreach” and hack for the thumbnails
      creation
 * The first hack in [the old post](http://wordpress.org/support/topic/plugin-nextgen-gallery-mass-batch-import-or-upload?replies=15)
   had a bug : the thumbnails are created for only one gallery of the list.
 * My hack works better : I’ve imported almost 100 galleries and created +3250 thumbnails
   in only one shot. The thumbnails creation is currently running and will take 
   4 hours 🙂
 * In NextCellent, the thumbnails are created via Ajax : there is no timeout problem.
   The max execution time of PHP could be perhaps problematic for the creation of
   galleries in database, so I recommand to import folders by chunks of 10 or 20.
 * Here are the diffs for NextCellent **1.9.16** :
 *     ```
       --- ajax_original.php
       +++ ajax.php
       @@ -270,7 +270,7 @@
                            continue;
   
                                       if ( file_exists($root . $dir . $file) && $file != '.' && $file != '..' && is_dir($root . $dir . $file) ) {
       -                                       echo "<li class=\"directory collapsed\"><a href=\"#\" rel=\"" . esc_html($dir . $file) . "/\">" . esc_html($file) . "</a></li>";
       +                                       echo "<li class=\"directory collapsed\" style=\"background-position:1px;display: block;padding-left:20px;text-indent:0px\"><input style=\"position:relative;top:2px;padding: 0;margin:0;float:left;vertical-align:bottom;*overflow: hidden;\" type=\"checkbox\" name=\"multiplefolder[]\" value=\"" . esc_html($dir . $file) . "\"><a href=\"#\" style=\"\" rel=\"" . esc_html($dir . $file) . "/\">" . esc_html($file) . "</a></li>";
                                       }
                               }
       ```
   
 *     ```
       --- addgallery_original.php
       +++ addgallery.php
       @@ -57,9 +57,12 @@
                       if ( !nggGallery::current_user_can( 'NextGEN Import image folder' ))
                               wp_die(__('Cheatin’ uh?'));
   
       -               $galleryfolder = $_POST['galleryfolder'];
       -               if ( ( !empty($galleryfolder) ) AND ($defaultpath != $galleryfolder) )
       -                       nggAdmin::import_gallery($galleryfolder);
       +                       $folders_list = $_POST['multiplefolder'];
       +                       if(empty($folders_list)) {
       +                               $folders_list[] = $_POST['galleryfolder'];
       +                       }
       +
       +                       nggAdmin::import_galleries($folders_list);
               }
   
               if ( isset($_POST['uploadimage']) ){
       @@ -377,7 +380,7 @@
                               $tabs['zipupload'] = __('ZIP file', 'nggallery');
   
                if ( wpmu_enable_function('wpmuImportFolder') && nggGallery::current_user_can( 'NextGEN Import image folder' ) )
       -                       $tabs['importfolder'] = __('Import folder', 'nggallery');
       +                       $tabs['importfolder'] = __('Import folder(s)', 'nggallery');
   
               $tabs = apply_filters('ngg_addgallery_tabs', $tabs);
   
       @@ -452,7 +455,7 @@
            function tab_importfolder() {
            ?>
               <!-- import folder -->
       -       <h3><?php _e('Import an image folder', 'nggallery') ;?></h3>
       +       <h3><?php _e('Import one or more image folders', 'nggallery') ;?></h3>
                       <form name="importfolder" id="importfolder_form" method="POST" action="<?php echo $this->filepath.'#importfolder'; ?>" accept-charset="utf-8" >
                       <?php wp_nonce_field('ngg_addgallery') ?>
                               <table class="form-table">
       ```
   
 *     ```
       --- functions_original.php
       +++ functions.php
       @@ -223,6 +223,117 @@
   
               }
   
       +
       +       /**
       +        * nggAdmin::import_galleries()
       +        * TODO: Check permission of existing thumb folder & images
       +        *
       +        * @class nggAdmin
       +        * @param array $folders_list contains an array of relatives paths to the galleries themselves
       +        * @return void
       +        */
       +       static function import_galleries($folders_list) {
       +
       +               global $wpdb, $ngg, $user_ID;
       +
       +               // get the current user ID
       +               get_currentuserinfo();
       +
       +               $defaultpath = $ngg->options['gallerypath'];
       +               $imageslistforthumbnails = array();
       +               foreach ($folders_list as $galleryfolder) {
       +                       if ( ( !empty($galleryfolder) ) AND ($defaultpath != $galleryfolder) ) {
       +
       +                               $created_msg = '';
       +
       +                               // remove trailing slash at the end, if somebody use it
       +                               $galleryfolder = untrailingslashit($galleryfolder);
       +                               $gallerypath = WINABSPATH . $galleryfolder;
       +
       +                               if (!is_dir($gallerypath)) {
       +                                       nggGallery::show_error(__('Directory', 'nggallery').' <strong>' . esc_html( $gallerypath ) .'</strong> '.__('doesn`t exist!', 'nggallery'));
       +                                       return ;
       +                               }
       +
       +                               // read list of images
       +                               $new_imageslist = nggAdmin::scandir($gallerypath);
       +
       +                               if (empty($new_imageslist)) {
       +                                       nggGallery::show_message(__('Directory', 'nggallery').' <strong>' . esc_html( $gallerypath ) . '</strong> '.__('contains no pictures', 'nggallery'));
       +                                       return;
       +                               }
       +
       +                               // check & create thumbnail folder
       +                               if ( !nggGallery::get_thumbnail_folder($gallerypath) )
       +                                       return;
       +
       +                               // take folder name as gallery name
       +                               $galleryname = basename($galleryfolder);
       +                               $galleryname = apply_filters('ngg_gallery_name', $galleryname);
       +
       +                               // check for existing gallery folder
       +                               $gallery_id = $wpdb->get_var("SELECT gid FROM $wpdb->nggallery WHERE path = '$galleryfolder' ");
       +
       +                               if (!$gallery_id) {
       +                                       // now add the gallery to the database
       +                                       $gallery_id = nggdb::add_gallery( $galleryname, $galleryfolder, '', 0, 0, $user_ID );
       +                                       if (!$gallery_id) {
       +                                               nggGallery::show_error(__('Database error. Could not add gallery!','nggallery'));
       +                                               return;
       +                                       }
       +                                       $created_msg = _n( 'Gallery', 'Galleries', 1, 'nggallery' ) . ' <strong>' . esc_html( $galleryname ) . '</strong> ' . __('successfully created!','nggallery') . '<br />';
       +                               }
       +
       +                               // Look for existing image list
       +                               $old_imageslist = $wpdb->get_col("SELECT filename FROM $wpdb->nggpictures WHERE galleryid = '$gallery_id' ");
       +
       +                               // if no images are there, create empty array
       +                               if ($old_imageslist == NULL)
       +                                       $old_imageslist = array();
       +
       +                               // check difference
       +                               $new_images = array_diff($new_imageslist, $old_imageslist);
       +
       +                               // all images must be valid files
       +                               foreach($new_images as $key => $picture) {
       +
       +                                       // filter function to rename/change/modify image before
       +                                       $picture = apply_filters('ngg_pre_add_new_image', $picture, $gallery_id);
       +                                       $new_images[$key] = $picture;
       +
       +                                       if (!@getimagesize($gallerypath . '/' . $picture) ) {
       +                                               unset($new_images[$key]);
       +                                               @unlink($gallerypath . '/' . $picture);
       +                                       }
       +                               }
       +
       +                               // add images to database
       +                               $image_ids = nggAdmin::add_Images($gallery_id, $new_images);
       +
       +                               //add the preview image if needed
       +                               nggAdmin::set_gallery_preview ( $gallery_id );
       +
       +                               // Add images IDs to $imageslistforthumbnails
       +                               $imageslistforthumbnails = array_merge($imageslistforthumbnails, $image_ids);
       +                               //nggAdmin::do_ajax_operation( 'create_thumbnail' , $image_ids, __('Create new thumbnails','nggallery') );
       +
       +                               //TODO:Message will not shown, because AJAX routine require more time, message should be passed to AJAX
       +                               $message  = $created_msg . count($image_ids) .__(' picture(s) successfully added','nggallery');
       +                               $message .= ' [<a href="' . admin_url() . 'admin.php?page=nggallery-manage-gallery&mode=edit&gid=' . $gallery_id . '" >';
       +                               $message .=  __('Edit gallery','nggallery');
       +                               $message .= '</a>]';
       +
       +                               nggGallery::show_message($message);
       +                       }
       +               }
       +
       +               // now create ALL thumbnails
       +               nggAdmin::do_ajax_operation( 'create_thumbnail' , $imageslistforthumbnails, __('Create new thumbnails','nggallery') );
       +
       +               return;
       +       }
       +
       +
               /**
                * Scan folder for new images
                *
       ```
   

Viewing 1 replies (of 1 total)