You are here

function imagepicker_admin_orphans_do in Image Picker 6.2

Same name and namespace in other branches
  1. 7 imagepicker.admin.inc \imagepicker_admin_orphans_do()
1 call to imagepicker_admin_orphans_do()
imagepicker_admin_orphans_form_submit in ./imagepicker.admin.inc

File

./imagepicker.admin.inc, line 1650
admin settings functions

Code

function imagepicker_admin_orphans_do($orphanid, $orphans_delete = FALSE, $reallocate_to_user = '') {
  $OK = FALSE;
  $message = '';
  $return = '';
  $fstype = variable_get('imagepicker_fstype', '');
  if ($fstype != 'alpha') {

    // check that the incoming user id is nonexistent
    $dir = imagepicker_get_files_directory();
    $orphanids = imagepicker_check_orphans($dir);

    // sanity check
    if (in_array($orphanid, $orphanids)) {

      // delete images
      if ($orphans_delete == 1) {

        // create 'phony' user object
        $user_obj = new stdClass();
        $user_obj->uid = $orphanid;

        // get all image ids belonging to orphanids
        $ct = 0;
        $result = db_query("SELECT img_id FROM {imagepicker} WHERE uid=%d", array(
          $user_obj->uid,
        ));

        // feed them into _imagepicker_image_delete()
        while ($row = db_fetch_array($result)) {
          _imagepicker_image_delete($row['img_id'], $user_obj, 'admin', TRUE);
          imagepicker_delete_group_image($row['img_id']);
          $ct++;
        }
        $OK = imagepicker_delete_olduser_dirs($user_obj->uid);
        if ($OK) {
          $message = t('!ct Files deleted.', array(
            '!ct' => $ct,
          ));
        }
        else {
          $message = t('There was an error in deleting files');
        }
      }
      elseif ($reallocate_to_user != '') {

        // reallocate to new user
        $result = db_query("SELECT uid FROM {users} WHERE name='%s'", array(
          trim($reallocate_to_user),
        ));
        if ($row = db_fetch_array($result)) {
          $new_uid = $row['uid'];
          $ct = 0;
          $result2 = db_query("SELECT img_id FROM {imagepicker} WHERE uid=%d", array(
            $orphanid,
          ));
          while ($row2 = db_fetch_array($result2)) {
            $newf = imagepicker_reallocate_image($row2['img_id'], $orphanid, $new_uid);
            if ($newf) {

              // move this entry in imagepicker to $new_uid
              $img_name = basename($newf);
              db_query("UPDATE {imagepicker} SET uid=%d, img_name='%s' WHERE img_id=%d", array(
                $new_uid,
                $img_name,
                $row2['img_id'],
              ));
              $ct++;
            }
          }

          // give the new user the groups if any
          db_query("UPDATE {imagepicker_user_groups} SET uid=%d WHERE uid=%d", array(
            $new_uid,
            $orphanid,
          ));
          $OK = imagepicker_delete_olduser_dirs($orphanid);
          if ($OK) {
            $message = t('!ct Files moved.', array(
              '!ct' => $ct,
            ));
          }
          else {
            $message = t('There was an error in moving files');
          }
        }
      }
      if ($message) {
        $return = array(
          $message,
          $OK,
        );
      }
    }
  }
  return $return;
}