Advertisement
benjamin_mcf

Untitled

Dec 2nd, 2011
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.46 KB | None | 0 0
  1. /**
  2.  * Submit the image upload form in the Avatar Selection administration page.
  3.  *
  4.  * @param $form
  5.  *   General variable used in drupal, defining the structure & the fields of a
  6.  *   form.
  7.  * @param &$form_state
  8.  *   General reference, used to control the processing of the form.
  9.  */
  10. function avatar_selection_upload_form_submit($form, &$form_state) {
  11.   $op = $form_state['values']['op'];
  12.  
  13.   if ($op == t('Upload')) {
  14.     // Get access settings.
  15.     $access = array_keys(array_filter($form_state['values']['access']));
  16. //    $og = array();
  17. //    if (module_exists('og')) {
  18. //      $og = array_keys(array_filter($form_state['values']['og_access']));
  19. //    }
  20.     $name = $form_state['values']['avatar_name'];
  21.     $weight = $form_state['values']['avatar_weight'];
  22.  
  23.     // Scan for new files.
  24.     if ($form_state['values']['scan_avatars'] == 1) {
  25.       $count = _avatar_selection_scan_images($name, $access, /*$og, */$weight);
  26.       drupal_set_message(t('Scan complete: %added new avatars found. %deleted avatars removed.', array('%added' => $count['add'], '%deleted' => $count['delete'])));
  27.     }
  28.  
  29.     // Save uploaded files.
  30.     $dir = file_build_uri('avatar_selection');
  31.     $is_writable = file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
  32.  
  33.     if ($is_writable) {
  34.       // If required, validate the uploaded picture.
  35.       $validators = array(
  36.         'file_validate_is_image' => array(),
  37.         'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
  38.         'file_validate_size' => array(variable_get('user_picture_file_size', 30) * 1024),
  39.       );
  40.  
  41.       if ($file = file_save_upload('picture_upload', $validators, $dir, FILE_EXISTS_RENAME)) {
  42.  
  43.         if (image_get_info($file->uri)) {
  44.           $info = image_get_info($file->uri);
  45.           global $user;
  46.           $file->status = FILE_STATUS_PERMANENT;
  47.           $file->filename = 'avatar' . '-' . $user->uid . '-' . REQUEST_TIME . '.' . $info['extension'];
  48.           $file = file_save($file);
  49.           _avatar_selection_save_avatar_info(0, $file->filename, $name, $access, /*$og, */$weight);
  50.           drupal_set_message(t('New image saved.'));
  51.         }
  52.         else {
  53.           file_delete($file->uri);
  54.           drupal_set_message(t('Uploaded file does not appear to be a valid image file. Please try again.'));
  55.         }
  56.       }
  57.     }
  58.     else {
  59.       form_set_error('picture_upload', t('Directory not writable: !dir', array('!dir' => $dir)));
  60.     }
  61.   }
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement