=== modified file 'prog_gallery.module' --- prog_gallery.module 2009-03-31 07:04:06 +0000 +++ prog_gallery.module 2009-03-31 10:00:44 +0000 @@ -210,6 +210,16 @@ 'type' => MENU_CALLBACK ); + $items['node/%prog_gallery/import'] = array( + 'title' => 'Import', + 'page callback' => 'prog_gallery_import_images', + 'page arguments' => array(1), + 'access callback' => 'prog_gallery_access', + 'access arguments' => array('update', 1), + 'type' => MENU_LOCAL_TASK, + 'weight' => 200, + ); + // galleries and galleries/my path $items['galleries'] = array( 'title' => 'Galleries', @@ -239,6 +249,22 @@ 'type' => MENU_NORMAL_ITEM, ); + $items['admin/settings/prog_gallery/settings'] = array( + 'title' => 'Settings', + 'type' => MENU_DEFAULT_LOCAL_TASK, + 'weight' => -1, + ); + + $items['admin/settings/prog_gallery/import'] = array( + 'title' => 'Import Location', + 'description' => t('Import images from a folder'), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('prog_gallery_admin_settings_import_images'), + 'access callback' => 'user_access', + 'access arguments' => array('administer site configuration'), + 'type' => MENU_LOCAL_TASK, + ); + // this menu is remove`able - if (variable_get('prog_gallery_image_view_alter_edit_tab', 1) == 1) // rewrite edit tab for image nodes that are in gallery @@ -537,8 +563,34 @@ } } - - +/** + * Administration page for Importing images + */ +function prog_gallery_admin_settings_import_images() { + $form['prog_gallery']['import_location'] = array( + '#title' => t('Import folder'), + '#description' => t("Directory inside inside ".variable_get('file_directory_path', 'sites/default/files/').' to import images from'), + '#default_value' => variable_get('prog_gallery_import_location', 'import'), + '#type' => 'textfield', + '#size' => 60, + '#required' => TRUE, + ); + + $form['#submit'] = array('prog_gallery_admin_settings_import_images_submit'); + + return system_settings_form($form); +} +function prog_gallery_admin_settings_import_images_validate($form, $form_state) +{ + if ( strstr( $form_state['values']['import_location'], '../') !== FALSE || + strstr( $form_state['values']['import_location'], '..\\') !== FALSE ) + { + form_set_error('import_location', t('Invalid directory name')); + } +} +function prog_gallery_admin_settings_import_images_submit($form_id, $form_values) { + variable_set('prog_gallery_import_location', $form_values['values']['import_location']); +} /* * gallery node content tab @@ -787,7 +839,113 @@ return $node; } - +/** + * non-javascript import of images from directory + */ +function prog_gallery_import_images($node) { + if ( !isset( $_SESSION['prog_gallery_imported_images'] ) ) + return drupal_get_form('prog_gallery_import_imagesform', $node); + else { + unset($_SESSION['prog_gallery_imported_images']); + return ''; + } +} +function prog_gallery_import_imagesform($node) { + $file_directory_path = variable_get('file_directory_path','sites/default/files/'); + $options = array('1' => t('Yes'),'0' => t('No')); + $form['prog_gallery']['import_location'] = array( + '#title' => t('Import folder'), + '#description' => t('Directory inside inside "'.variable_get('file_directory_path', 'sites/default/files/').'" to import images from.'), + '#default_value' => variable_get('prog_gallery_import_location', 'import'), + '#type' => 'textfield', + '#size' => 60, + '#required' => TRUE, + ); + $form['prog_gallery']['import_images'] = array( + '#type' => 'radios', + '#title' => t('Import images now?'), + '#description' => t('Choose your option.'), + '#default_value' => 1, + '#options' => $options, + '#required' => TRUE, + ); + $form['hidden'] = array( + '#type' => 'import', + '#value' => 'just_passed' + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Proceed') + ); + return $form; +} +function prog_gallery_import_imagesform_validate($form, $form_state) +{ + if ( strstr( $form_state['values']['import_location'], '../') !== FALSE || + strstr( $form_state['values']['import_location'], '..\\') !== FALSE ) + { + form_set_error('import_location', t('Invalid directory name')); + } +} +function prog_gallery_import_imagesform_submit($form_id, $formvalues) { + if ( $formvalues['values']['import_images'] == '0' ) { + + drupal_set_message(t('Did not import images.')); + + } else if ( $formvalues['values']['import_images'] == '1' ) { + + $path = variable_get('file_directory_path', 'sites/default/files/'). + $formvalues['values']['import_location']; + $images = file_scan_directory($path,'.*\..*$'); + $files = array(); + $file_count = 0; + foreach( $images as $key => $image ) { + $files['files']['name'][$file_count] = $image->basename; + $files['files']['tmp_name'][$file_count] = $image->filename; + $files['files']['error'][$file_count] == 0; + ++$file_count; + } + $nid=arg(1); + for ($count = 0; $count < $file_count; ++$count) { + if (isset($files['files']['name'][$count]) && drupal_strlen($files['files']['name'][$count]) > 0) { + + if ($files['files']['error'][$count] == 0) { + + $type = prog_gallery_gettype($files['files']['name'][$count]); + + if ($type != 'bad') { + if ($type == 'image') $new_node = prog_gallery_create_node_from_imagefile($files['files'], $count); + + if ($new_node->images['thumbnail']) { + $gallery_content_entry = Array( + 'nid' => rawurlencode($new_node->nid), + 'title' => rawurlencode($new_node->title), + 'fails' => rawurlencode($new_node->images['thumbnail']), + 'tips' => rawurlencode($new_node->type), + 'description' => rawurlencode($new_node->body) + ); + + db_query("INSERT INTO {prog_gallery_images} (i_nid, g_nid, weight, exif_date) VALUES (%d, %d, 0, 0)", $new_node->nid, $nid); + + // update exif date to images table + prog_gallery_image_setexifdate($new_node->nid); + + // if gallery's default pic not set yet set it to this one + if (prog_gallery_album_thumb($nid) == 0) db_query("UPDATE {prog_gallery} SET thumb='%s' WHERE g_nid= '%s'", $new_node->nid, $nid); + + drupal_set_message(t('File ') . $files['files']['name'][$count] . t(' imported successfully.')); + } + else drupal_set_message(t('File ') . $files['files']['name'][$count] . t(' import error: ') . t('error creating file'), 'error'); + } + else drupal_set_message(t('File ') . $files['files']['name'][$count] . t(' import error: ') . t('bad file type'), 'error'); + } + else drupal_set_message(t('File ') . $files['files']['name'][$count] . t(' import error: ') . $files['files']['error'][$count], 'error'); + } + } // end of for loop + drupal_set_message(t("Don't forget to delete the imported images from ".$path)); + } + $_SESSION['prog_gallery_imported_images'] = TRUE; +} /* * non-javascript browser tasks