| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: image_import.pages.inc,v 1.7.2.2 2010/08/03 17:43:00 sun Exp $ 3 4 /** 5 * Verify a candidate file for import. 6 * 7 * @param $file 8 * A file object to check, of the format returned by file_scan_directory(). 9 */ 10 function image_import_validate_file($file) { 11 $errors = array(); 12 $info = image_get_info($file->filename); 13 if ($info && !empty($info['extension'])) { 14 $file->height = $info['height']; 15 $file->width = $info['width']; 16 } 17 else { 18 $errors[] = t('Not a JPG, GIF or PNG file.'); 19 $file->height = $file->width = 0; 20 } 21 22 // Check files are not too large to import. 23 $file->filesize = filesize($file->filename); 24 if ($file->filesize > variable_get('image_max_upload_size', 800) * 1024) { 25 $errors[] = t('File too big.'); 26 } 27 28 return $errors; 29 } 30 31 /** 32 * Form generating function for the image import page. 33 */ 34 function image_import_form() { 35 $form = array(); 36 37 $dirpath = variable_get('image_import_path', ''); 38 if (!file_check_directory($dirpath)) { 39 drupal_set_message(t("You need to configure the import directory on the image import module's <a href='!admin-settings-import'>settings page</a>.", array('!admin-settings-import' => url('admin/settings/image/image_import'))), 'error'); 40 return $form; 41 } 42 43 $form['#dirpath'] = $dirpath; 44 $form['#node_type'] = 'image'; 45 46 $files = file_scan_directory($dirpath, '.*'); 47 ksort($files); 48 49 // When the form gets too large we end up running out of memory submitting it. 50 // To avoid this we use a pager and rather than setting up all the variables 51 // ourself we just send in a fake query and then select the desired part of 52 // the files array. 53 $page_size = variable_get('image_import_page_size', 50); 54 pager_query('SELECT %d', $page_size, 0, 'SELECT %d', array(count($files))); 55 $files = array_slice($files, $GLOBALS['pager_page_array'][0] * $page_size, $page_size); 56 57 $file_metadata_modules = module_implements('file_metadata'); 58 59 if ($files) { 60 if (module_exists('taxonomy')) { 61 // here's a little hack to get the taxonomy controls onto our form 62 $form['type'] = array('#type' => 'value', '#value' => $form['#node_type']); 63 $form['#node'] = new stdClass(); 64 $form['#node']->type = $form['#node_type']; 65 taxonomy_form_alter($form, array(), $form['#node_type'] . '_node_form'); 66 unset($form['type']); 67 unset($form['#node']); 68 } 69 if (module_exists('image_gallery')) { 70 $form['tree'] = array( 71 '#type' => 'checkbox', 72 '#title' => t('Reproduce directories structure with subgalleries'), 73 '#description' => t('A subgallery will be created only if the folder contains at least an image.'), 74 ); 75 } 76 77 $form['delete_directories'] = array( 78 '#type' => 'checkbox', 79 '#title' => t('Delete empty directories after import'), 80 '#description' => '', 81 ); 82 83 // Put the image files into an array for the checkboxes and gather 84 // additional information like dimensions and filesizes. Make sure that 85 // there's no 0th element, because a checkbox with a zero value is seen as 86 // unchecked and won't be imported. 87 $index = 0; 88 foreach ($files as $file) { 89 $index++; 90 $filelist[$index] = substr($file->filename, strlen($dirpath) + 1); 91 92 $problems = image_import_validate_file($file); 93 // Allow other modules to supply metadata about the images being imported. 94 // hook_file_metadata() may populate the $file properties 'title' and 95 // 'description'. 96 foreach ($file_metadata_modules as $module) { 97 $function = $module . '_file_metadata'; 98 $function($file); 99 } 100 101 // Spit out the import form elements. 102 $form['files']['import'][$index] = array( 103 '#type' => 'checkbox', 104 '#title' => substr($file->filename, strlen($dirpath) + 1), 105 ); 106 $form['files']['filesize'][$index] = array( 107 '#type' => 'item', 108 '#value' => format_size(filesize($file->filename)), 109 ); 110 $form['files']['dimensions'][$index] = array( 111 '#type' => 'item', 112 '#value' => $file->width . 'x' . $file->height, 113 ); 114 $form['files']['title'][$index] = array( 115 '#type' => 'textfield', 116 '#size' => 20, 117 '#default_value' => isset($file->title) ? $file->title : basename($file->name), 118 ); 119 $form['files']['body'][$index] = array( 120 '#type' => 'textfield', 121 '#size' => 20, 122 '#default_value' => isset($file->body) ? $file->body : basename($file->name), 123 ); 124 125 // If there were problems don't let them import it 126 if (count($problems)) { 127 $form['files']['import'][$index]['#type'] = 'item'; 128 $form['files']['errors'][$index] = array( 129 '#type' => 'markup', 130 '#value' => '<em>' . implode(' ', $problems) . '</em>', 131 ); 132 unset($form['files']['title'][$index]); 133 unset($form['files']['body'][$index]); 134 } 135 } 136 137 $form['pager'] = array('#value' => theme('pager', NULL, $page_size, 0)); 138 139 // Put the titles into an array. 140 $form['files']['import']['#tree'] = TRUE; 141 $form['files']['title']['#tree'] = TRUE; 142 $form['files']['body']['#tree'] = TRUE; 143 144 // Store a copy of the list into a form value so we can compare it to what 145 // they submit and not have to worry about files being added or removed 146 // from the filesystem. 147 $form['file_list'] = array( 148 '#type' => 'value', 149 '#value' => $filelist, 150 ); 151 152 $form['buttons']['submit'] = array( 153 '#type' => 'submit', 154 '#value' => t('Import'), 155 ); 156 } 157 else { 158 $form['none_found'] = array( 159 '#type' => 'item', 160 '#value' => '<em>' . t('No files were found.') . '</em>', 161 ); 162 } 163 164 return $form; 165 } 166 167 /** 168 * Theme function for the image import form. 169 */ 170 function theme_image_import_form($form) { 171 $output = drupal_render($form['token_help']); 172 173 if (!empty($form['file_list']['#value'])) { 174 $type = node_get_types('type', $form['#node_type']); 175 $header = array(theme('table_select_header_cell'), t('Image'), t('Name'), t('Size'), t('Dimensions'), check_plain($type->title_label), check_plain($type->body_label)); 176 $rows = array(); 177 foreach (element_children($form['files']['import']) as $key) { 178 $filename = $form['files']['import'][$key]['#title']; 179 unset($form['files']['import'][$key]['#title']); 180 $row = array( 181 array('data' => drupal_render($form['files']['import'][$key])), 182 array('data' => '<img width="40" src="' . url($form['#dirpath'] . '/' . $filename) . '">'), 183 array('data' => $filename), 184 array('data' => drupal_render($form['files']['filesize'][$key])), 185 array('data' => drupal_render($form['files']['dimensions'][$key])), 186 ); 187 if (!isset($form['files']['errors'][$key])) { 188 $row[] = array('data' => drupal_render($form['files']['title'][$key])); 189 $row[] = array('data' => drupal_render($form['files']['body'][$key])); 190 } 191 else { 192 $row[] = array('colspan' => 2, 'data' => drupal_render($form['files']['errors'][$key])); 193 } 194 195 $rows[] = $row; 196 } 197 $output .= theme('table', $header, $rows); 198 } 199 return $output . drupal_render($form); 200 } 201 202 /** 203 * Submit handler for the image import form. 204 */ 205 function image_import_form_submit($form, &$form_state) { 206 $batch = array( 207 'title' => t('Importing image'), 208 'progress_message' => 'Importing @current of @total.', 209 'operations' => array(), 210 'finished' => '_image_import_batch_finished', 211 'file' => drupal_get_path('module', 'image_import') . '/image_import.pages.inc', 212 ); 213 214 foreach (array_filter($form_state['values']['import']) as $index => $true) { 215 $origname = $form_state['values']['file_list'][$index]; 216 if ($filepath = file_check_location($form['#dirpath'] . '/' . $origname, $form['#dirpath'])) { 217 $args = array( 218 'node_type' => $form['#node_type'], 219 'title' => isset($form_state['values']['title'][$index]) ? $form_state['values']['title'][$index] : NULL, 220 'body' => isset($form_state['values']['body'][$index]) ? $form_state['values']['body'][$index] : NULL, 221 'taxonomy' => isset($form_state['values']['taxonomy']) ? $form_state['values']['taxonomy'] : array(), 222 'filepath' => $filepath, 223 'origname' => $origname, 224 'subgallery' => $form_state['values']['tree'], 225 ); 226 $batch['operations'][] = array('_image_import_batch_op', array($args)); 227 } 228 } 229 if ($form_state['values']['delete_directories']) { 230 $batch['operations'][] = array('_image_import_recursive_delete_empty_directories_batch_op', array(array('basepath' => $form['#dirpath']))); 231 } 232 233 batch_set($batch); 234 } 235 236 /** 237 * Batch operation callback for image import. 238 */ 239 function _image_import_batch_op($args, &$context) { 240 $error = FALSE; 241 // if user choses to reproduce directories structure, save the chosen gallery, 242 // then create, if needed, the nested galleries where the image goes 243 if ($args['subgallery']) { 244 static $subgalleries = array(); 245 $image_galleries_vid = _image_gallery_get_vid(); 246 $subgalleries_parent_tid = $args['taxonomy'][$image_galleries_vid]; 247 $args['taxonomy'][$image_galleries_vid] = _image_import_create_subgalleries( 248 $args['origname'], 249 $image_galleries_vid, 250 $subgalleries_parent_tid, 251 $subgalleries, 252 $context 253 ); 254 if (!$args['taxonomy'][$image_galleries_vid]) { 255 $error = TRUE; 256 } 257 } 258 259 // Create the node object. 260 if (!$error && $node = image_create_node_from($args['filepath'], $args['title'], $args['body'], $args['taxonomy'])) { 261 // Remove the original image now that the import has completed. 262 file_delete($args['filepath']); 263 264 $context['results']['good'][] = t('Imported %origname as !link @status [!edit-link].', array( 265 '%origname' => $args['origname'], 266 '!link' => l($node->title, 'node/' . $node->nid), 267 '@status' => $node->status ? '' : t('(Unpublished)'), 268 '!edit-link' => l(t('edit'), 'node/' . $node->nid . '/edit'), 269 )); 270 } 271 else { 272 watchdog('image_import', 'There was an error that prevented %filename from being imported.', array('%filename' => $args['filepath']), WATCHDOG_ERROR); 273 $context['results']['bad'][] = t('Error importing %filename.', array('%filename' => $args['filepath'])); 274 } 275 276 $context['finished'] = 1; 277 } 278 279 /** 280 * Batch finished callback for image import. 281 */ 282 function _image_import_batch_finished($success, $results, $operations) { 283 if (!$success) { 284 if (count($results['bad'])) { 285 drupal_set_message(t('There was a problem importing files: !bad-list', array('!bad-list' => theme('item_list', $results['bad']))), 'error'); 286 } 287 else { 288 drupal_set_message(t('There was a problem importing the files.'), 'error'); 289 } 290 } 291 if (count($results['good'])) { 292 drupal_set_message(t('Successfully imported: !good-list', array('!good-list' => theme('item_list', $results['good'])))); 293 } 294 watchdog('image_import', 'Completed image import.'); 295 } 296 297 /** 298 * Creates subgalleries. 299 * 300 * @param $origname 301 * The original name (with its path) of the image which subcategory has to be created. 302 * @param $image_galleries_vid 303 * The vocabulary id of corresponding to image galleries. 304 * @param $subgalleries_parent_tid 305 * The term id of the parent gallery, which will contain the subgallery. 306 * @param $subgalleries 307 * An array of the tids of each subgallery already created, indexed by subgallery path. 308 * @param $context 309 * The execution log. 310 * 311 * @return 312 * The tid of the subgallery in which the image goes to. 313 */ 314 function _image_import_create_subgalleries($origname, $image_galleries_vid, $subgalleries_parent_tid, &$subgalleries, &$context) { 315 // Get a term id from its parent (if any), its vocabulary, and its name. 316 $tid_query = 'SELECT td.tid FROM {term_data} td, {term_hierarchy} th WHERE td.tid = th.tid AND vid = %d AND name = \'%s\' AND parent = %d'; 317 // Explode path to image. 318 $requested_subgalleries = explode('/', $origname); 319 // Remove the filename at the end. 320 array_pop($requested_subgalleries); 321 // Parent gallery. 322 $parent_tid = (int) $subgalleries_parent_tid; 323 // Current image's gallery. 324 $subgallery_tid = (int) $subgalleries_parent_tid; 325 // Create each gallery in the path, if necessary. 326 foreach ($requested_subgalleries as $key => $subgallery_name) { 327 // The current subgallery's relative path. 328 $subgallery_path = implode('/', array_slice($requested_subgalleries, 0, $key + 1)); 329 // Was this subgallery already created by the current batch? 330 $subgallery_tid = $subgalleries[$subgallery_path]; 331 if (!$subgallery_tid) { 332 // First, look in DB. 333 $subgallery_tid = db_result(db_query($tid_query, $image_galleries_vid, $subgallery_name, $parent_tid)); 334 // Didn't find? Create it. 335 if (!$subgallery_tid) { 336 $subgallery_term = array( 337 'name' => $subgallery_name, 338 'parent' => $parent_tid, 339 'vid' => $image_galleries_vid, 340 ); 341 $status = taxonomy_save_term($subgallery_term); 342 if ($status) { 343 // Get its tid back. 344 $subgallery_tid = db_result(db_query($tid_query, $image_galleries_vid, $subgallery_name, $parent_tid)); 345 $term = taxonomy_get_term($subgallery_tid); 346 $context['results']['good'][] = t('Created gallery !link [!edit-link].', array( 347 '!link' => l($subgallery_name, taxonomy_term_path($term)), 348 '!edit-link' => l(t('edit'), 'admin/content/taxonomy/edit/term/' . $subgallery_tid), 349 )); 350 } 351 else { 352 watchdog('image_import', 'There was an error that prevented gallery %gallery_path from being created.', array('%gallery_path' => $subgallery_path), WATCHDOG_ERROR); 353 $context['results']['bad'][] = t('Error creating gallery %gallery_path.', array('%gallery_path' => $subgallery_path)); 354 // Return an error. 355 return 0; 356 } 357 } 358 $subgalleries[$subgallery_path] = $subgallery_tid; 359 } 360 // For the next subgallery to be created, last created will be its parent. 361 $parent_tid = (int)$subgallery_tid; 362 } 363 return $subgallery_tid; 364 } 365 366 /** 367 * Delete recursively all directories inside $basepath. 368 * 369 * @param $basepath 370 * The base directory where to find empty directories. 371 * @param $dir 372 * A directory inside $basepath, used for recursive purpose. 373 * Should not be used when calling this method. 374 * 375 * @return 376 * An array of the deleted directories paths, relative to $basepath. 377 */ 378 function _image_import_recursive_delete_empty_directories($basepath, $dir = '') { 379 $deleted = array(); 380 // Append $basepath to $dir only if $dir is not empty (to avoid trailing 381 // slash). 382 if ($dir != '') { 383 $crnt_dir = $basepath . '/' . $dir; 384 } 385 else { 386 $crnt_dir = $basepath; 387 } 388 // Get each file in $crnt_dir. 389 $ls = file_scan_directory($crnt_dir, '.*', array('.', '..', 'CVS'), 0, FALSE); 390 // For each directory inside, recursively delete empty directories inside. 391 foreach ($ls as $file) { 392 if (is_dir($file->filename)) { 393 $next_dir = $dir ? $dir . '/' . $file->basename : $file->basename; 394 $deleted = array_merge($deleted, _image_import_recursive_delete_empty_directories($basepath, $next_dir)); 395 if (sizeof(file_scan_directory($file->filename, '.*', array('.', '..'), 0, FALSE)) == 0) { 396 rmdir($file->filename); 397 $deleted[] = '<em>' . $next_dir . '</em>'; 398 } 399 } 400 } 401 return $deleted; 402 } 403 404 /** 405 * Batch operation callback for deletion of empty directories after the import. 406 */ 407 function _image_import_recursive_delete_empty_directories_batch_op($args, &$context) { 408 $deleted = _image_import_recursive_delete_empty_directories($args['basepath']); 409 if (!empty($deleted)) { 410 $context['results']['good'][] = t('Deleted directories: !dir-list.', array( 411 '!dir-list' => theme('item_list', $deleted), 412 )); 413 } 414 else { 415 $context['results']['good'][] = t('No directories were deleted.'); 416 } 417 $context['finished'] = 1; 418 } 419
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |