You are here

function asset_asset_type in Asset 6

Same name and namespace in other branches
  1. 5.2 asset.module \asset_asset_type()
  2. 5 asset.module \asset_asset_type()

Implementation of hook_asset_type().

File

inc/asset.api.inc, line 101

Code

function asset_asset_type($op = 'info', $delta = 0, $form_values = array()) {
  switch ($op) {
    case 'info':
      $info['upload'] = array(
        'value' => t('Upload'),
        'title' => t('Upload a new file.'),
        'src' => drupal_get_path('module', 'asset') . '/misc/lullacons/doc-option-add.png',
      );
      $info['directory'] = array(
        'value' => t('New Folder'),
        'title' => t('Create a new folder.'),
        'src' => drupal_get_path('module', 'asset') . '/misc/lullacons/folder-option-add.png',
      );
      $info['directory-del'] = array(
        'value' => t('Delete Folder'),
        'title' => t('Delete the current folder.'),
        'src' => drupal_get_path('module', 'asset') . '/misc/lullacons/folder-option-remove.png',
      );
      return $info;
    case 'form':
      $form['module'] = array(
        '#type' => 'value',
        '#value' => 'asset',
      );
      if ($delta == 'upload') {
        $form['upload'] = array(
          '#type' => 'file',
          '#title' => t('Upload a File'),
          '#size' => 35,
          '#weight' => -1,
        );
      }
      if (empty($_SESSION['asset_op'])) {

        // Don't allow the asset cck to select a width/size or imagecache preset
        $form['#redirect'] = "node/1";
      }
      $form['#attributes']['enctype'] = 'multipart/form-data';
      return $form;
    case 'validate':

      // must return a valid asset aid
      if ($delta == 'upload') {
        if ($file = file_save_upload('upload')) {
          $parent_tmp = $form_values['parent'] ? $form_values['parent'] . '/' : $form_values['parent'];
          $path = file_create_path($parent_tmp . $file->filename);
          if (file_copy($file, $path, FILE_EXISTS_REPLACE)) {
            $asset = asset_save($file, $form_values);
            drupal_set_message(t("The file {$file->filename} has been successfully uploaded."));
            if (isset($_SESSION['asset_fieldname'])) {
              drupal_goto(filter_xss($_GET['q']), 'dir=' . filter_xss($_GET['dir']));
            }
            else {
              return $asset->aid;
            }
          }
          else {
            form_set_error('upload', 'Error saving file to <em>' . $path . '</em>');
          }
        }
        else {
          form_set_error('upload', 'Error uploading file</em>');
        }
      }
      else {
        $parent_tmp = $form_values['parent'] ? $form_values['parent'] . '/' : $form_values['parent'];
        $dir = $parent_tmp . $form_values['title'];
        if (asset_check_directory($dir, FILE_CREATE_DIRECTORY)) {
          $query = $_GET;
          unset($query['q']);
          $query['dir'] = trim(str_replace(file_create_path(), '', $dir), '/');
          drupal_goto($_GET['q'], asset_build_query($query));
        }
        else {
          form_set_error('title', t('Error creating directory.'));
        }
      }
      break;
    case 'submit':
      break;
  }
}