You are here

function imagepicker_convert_to_alpha in Image Picker 5.2

Same name and namespace in other branches
  1. 6.2 imagepicker.admin.inc \imagepicker_convert_to_alpha()
1 call to imagepicker_convert_to_alpha()
imagepicker_convert_form_submit in ./imagepicker.module

File

./imagepicker.module, line 3524
Enables permitted roles to upload images for insertion into configured nodes.

Code

function imagepicker_convert_to_alpha($dir) {
  $result = TRUE;
  $errlist = array();
  $uiddirlist = file_scan_directory($dir, ".*", array(
    '.',
    '..',
    'CVS',
  ), 0, FALSE);
  if (!imagepicker_make_alphadirs($dir)) {
    drupal_set_message(t('Directories could not be created'), 'error');
    return FALSE;
  }
  if (count($uiddirlist)) {
    foreach ($uiddirlist as $uiddir => $uid) {
      if ($uiddirlist[$uiddir]->basename) {
        $useruid = $uiddirlist[$uiddir]->basename;

        // get the user name
        $result = db_query("SELECT uid, name FROM {users} WHERE uid=%d AND status=1", array(
          $useruid,
        ));
        $row = db_fetch_array($result);
        if ($row['name']) {
          $username = $row['name'];
        }
        if ($username) {
          $firstletter = drupal_strtolower(drupal_substr($username, 0, 1));
          $firstletter = preg_match('/^[a-z]$/', $firstletter) ? $firstletter : 'others';
          $newpath = $dir . DIRECTORY_SEPARATOR . $firstletter . DIRECTORY_SEPARATOR . $username;
          $thumbstarget = $newpath . DIRECTORY_SEPARATOR . IMAGEPICKER_THUMBS_DIR;
          $browsertarget = $newpath . DIRECTORY_SEPARATOR . IMAGEPICKER_BROWSER_DIR;

          // create dirs
          if (!file_check_directory($newpath, TRUE)) {
            $errlist[$newpath] = "Could not create dir";
          }
          if (!file_check_directory($thumbstarget, TRUE)) {
            $errlist[$thumbstarget] = "Could not create thumbs dir";
          }
          if (!file_check_directory($browsertarget, TRUE)) {
            $errlist[$browsertarget] = "Could not create browser dir";
          }
          if (!count($errlist)) {

            // clear
            drupal_get_messages();
            $oldpath = $uiddir;
            $newfilelist = file_scan_directory($oldpath, ".*", array(
              '.',
              '..',
              'CVS',
            ), 0, FALSE);
            if (count($newfilelist)) {
              foreach ($newfilelist as $k => $v) {
                if (!is_dir($k)) {
                  $file = $newfilelist[$k]->filename;
                  $thumbsfile = $oldpath . DIRECTORY_SEPARATOR . IMAGEPICKER_THUMBS_DIR . DIRECTORY_SEPARATOR . $newfilelist[$k]->basename;
                  $browserfile = $oldpath . DIRECTORY_SEPARATOR . IMAGEPICKER_BROWSER_DIR . DIRECTORY_SEPARATOR . $newfilelist[$k]->basename;
                  if (!file_move($file, $newpath, FILE_EXISTS_ERROR)) {
                    $errlist[$file] = "Could not move to {$newpath}";
                  }
                  if (!file_move($thumbsfile, $thumbstarget, FILE_EXISTS_ERROR)) {
                    $errlist[$thumbsfile] = "Could not move to {$thumbstarget}";
                  }
                  if (!file_move($browserfile, $browsertarget, FILE_EXISTS_ERROR)) {
                    $errlist[$browserfile] = "Could not move to {$browsertarget}";
                  }
                }
              }
            }
            if (!count($errlist)) {
              $delpath[] = $oldpath . DIRECTORY_SEPARATOR . IMAGEPICKER_THUMBS_DIR;
              $delpath[] = $oldpath . DIRECTORY_SEPARATOR . IMAGEPICKER_BROWSER_DIR;
              $delpath[] = $oldpath;
            }
          }
        }
        else {
          $errlist[$k] = "NO uid for user {$u}";
        }
      }
    }
  }
  if (!count($errlist)) {

    // tidy up
    if (count($delpath)) {
      foreach ($delpath as $p) {
        if (is_dir($p)) {
          if (!rmdir($p)) {
            $errlist[$p] = 'deletion failed';
          }
        }
        else {
          $errlist[$p] = 'not a dir';
        }
      }
    }
  }
  if (count($errlist)) {
    foreach ($errlist as $key => $val) {
      drupal_set_message("ERRORS: {$key}: {$val} ", 'error');
    }
    $result = FALSE;
  }
  return $result;
}