You are here

function imagepicker_convert_to_numeric in Image Picker 5.2

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

File

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

Code

function imagepicker_convert_to_numeric($dir) {
  $result = TRUE;
  $errlist = array();

  // work over the oldstyle dirs
  $dirlist = range('a', 'z');
  array_push($dirlist, 'others');
  foreach ($dirlist as $alphadir) {
    $newdir = $dir . DIRECTORY_SEPARATOR . $alphadir;
    if (is_dir($newdir)) {

      // old style dir exists, now look to see if it has any users
      $newdirlist = file_scan_directory($newdir, ".*", array(
        '.',
        '..',
        'CVS',
      ), 0, FALSE);
      if (count($newdirlist)) {
        foreach ($newdirlist as $k => $v) {
          if ($newdirlist[$k]->basename) {
            $username = $newdirlist[$k]->basename;

            // get the user uid
            $uid = FALSE;
            $result = db_query("SELECT uid FROM {users} WHERE name='%s' AND status=1", array(
              $username,
            ));
            $row = db_fetch_array($result);
            if ($row['uid']) {
              $uid = $row['uid'];
            }
            if ($uid) {
              $newpath = $dir . DIRECTORY_SEPARATOR . $uid;
              $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 = $newdirlist[$k]->filename;

                // do here: move oldpath/* to newpath and delete oldpath
                // needs to be done per-file and dir
                $oldfilelist = file_scan_directory($oldpath, ".*", array(
                  '.',
                  '..',
                  'CVS',
                ), 0, FALSE);
                if (count($oldfilelist)) {
                  foreach ($oldfilelist as $k1 => $v1) {
                    if (!is_dir($oldfilelist[$k1]->filename) && file_exists($oldfilelist[$k1]->filename)) {
                      $file = $oldfilelist[$k1]->filename;
                      $thumbsfile = $oldpath . DIRECTORY_SEPARATOR . IMAGEPICKER_THUMBS_DIR . DIRECTORY_SEPARATOR . $oldfilelist[$k1]->basename;
                      $browserfile = $oldpath . DIRECTORY_SEPARATOR . IMAGEPICKER_BROWSER_DIR . DIRECTORY_SEPARATOR . $oldfilelist[$k1]->basename;

                      # file_move(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME)  FILE_EXISTS_ERROR
                      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;
                  $delpath[] = $newdir;
                }
              }
            }
            else {
              $errlist[$k] = "NO uid for user {$username}";
            }
          }
        }
      }
      else {

        // newdir exists but is empty so delete it
        $delpath[] = $newdir;
      }
    }
  }
  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;
}