You are here

function _file_table_action in Ubercart 5

Perform a specified action on the uc_files table

Parameters

$op: The action to perform on uc_files table

  • empty: truncate uc_files table
  • insert: scan the uc_file base dir and enter new files into the table
  • remove: remove the file specified by arguments
  • refresh: scan the uc_file base dir and remove files/dir from the table that don't exist

$arg1: Specified by op argument

$arg2: Specified by op argument

$arg3: Specified by op argument

4 calls to _file_table_action()
uc_file_feature_form in uc_file/uc_file.module
Form builder for hook_product_feature
uc_file_feature_settings_submit in uc_file/uc_file.module
uc_file_files_admin in uc_file/uc_file.module
Page builder for file products admin
uc_file_files_form_submit in uc_file/uc_file.module

File

uc_file/uc_file.module, line 1291
Allows products to be associated with downloadable files.

Code

function _file_table_action($op, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL) {
  switch ($op) {
    case 'empty':

      //Clear out file table (args not used)
      db_query("TRUNCATE TABLE {uc_files}");
      break;
    case 'insert':

      //Add new items into table (args not used)
      if (!is_null($dir = variable_get('uc_file_base_dir', NULL))) {
        $files = file_scan_directory($dir, variable_get('uc_file_file_mask', '.*'));
        $dir = substr($dir, -1) != '/' || substr($dir, -1) != '\\' ? $dir . '/' : $dir;
        foreach ($files as $file) {
          $filename = str_replace($dir, '', $file->filename);
          $file_dir = dirname($filename);
          if (!db_result(db_query("SELECT fid FROM {uc_files} WHERE filename = '%s'", $file_dir . '/')) && $file_dir != '.') {
            $fid = db_next_id('{uc_files}_fid');
            db_query("INSERT INTO {uc_files} (fid, filename) VALUES (%d, '%s')", $fid, $file_dir . '/');
          }
          if (!db_result(db_query("SELECT fid FROM {uc_files} WHERE filename = '%s'", $filename))) {
            $fid = db_next_id('{uc_files}_fid');
            db_query("INSERT INTO {uc_files} (fid, filename) VALUES (%d, '%s')", $fid, $filename);
          }
          if (!is_null($fid)) {
            $file_object = db_fetch_object(db_query("SELECT * FROM {uc_files} WHERE fid = %d", $fid));

            //Check any if any hook_file_action('insert', $args) are implemented
            foreach (module_implements('file_action') as $module) {
              $name = $module . '_file_action';
              $result = $name('insert', array(
                'file_object' => $file_object,
              ));
            }
            unset($fid);
          }
        }
      }
      break;
    case 'remove':

      //Remove a specific file id (arg1 = file id to delete, arg2 = TRUE = recursively delete directories, arg 3 = TRUE = delete associated rows/files)
      if (!is_null($arg1) && ($filename = db_result(db_query("SELECT filename FROM {uc_files} WHERE fid = %d", $arg1)))) {
        $dir = variable_get('uc_file_base_dir', NULL);
        $sub_fids = $arg2 ? _get_dir_file_ids($arg1, TRUE) : _get_dir_file_ids($arg1);
        $selected_fid = is_dir($dir . '/' . $filename) && !$arg2 ? array() : array(
          $arg1,
        );
        $fids = !$sub_fids ? $selected_fid : array_merge($sub_fids, $selected_fid);
        $fids = _sort_fids($fids);
        foreach ($fids as $fid) {
          if ($arg3) {
            $filename = db_result(db_query("SELECT filename FROM {uc_files} WHERE fid = %d", $fid));
            $pfids = db_query("SELECT pfid FROM {uc_file_products} WHERE fid = %d", $fid);
            while ($pfid = db_fetch_object($pfids)) {
              db_query("DELETE FROM {uc_product_features} WHERE pfid = %d AND fid = 'file'", $pfid->pfid);
              db_query("DELETE FROM {uc_file_products} WHERE pfid = %d", $pfid->pfid);
            }
            if (is_dir($dir . '/' . $filename)) {
              rmdir($dir . '/' . $filename);
            }
            else {
              unlink($dir . '/' . $filename);
            }
            _user_table_action('remove', $fid);
          }
          db_query("DELETE FROM {uc_files} WHERE fid = %d", $fid);
        }
      }
      break;
    case 'refresh':

      //Remove non-existing items from table (args not used)
      $files = db_query("SELECT * FROM {uc_files}");
      while ($file = db_fetch_object($files)) {
        if (is_dir(variable_get('uc_file_base_dir', NULL) . '/' . $file->filename)) {
          continue;
        }
        if (is_file(variable_get('uc_file_base_dir', NULL) . '/' . $file->filename)) {
          continue;
        }
        db_query("DELETE FROM {uc_files} WHERE fid = %d", $file->fid);
      }
      break;
    default:
      break;
  }
}