You are here

asset.api.inc in Asset 6

File

inc/asset.api.inc
View source
<?php

/**
 * Implementation of hook_asset_formatter().
 */
function asset_asset_formatter($op = 'info', $asset = NULL, $attr = array()) {
  switch ($op) {
    case 'info':
      $formats['link'] = array(
        'name' => t('Link'),
        'types' => array(
          '*' => array(
            '*',
          ),
        ),
        'description' => t('A simple download link.'),
      );
      $formats['image'] = array(
        'name' => t('Image'),
        'types' => array(
          'local' => array(
            'jpg',
            'gif',
            'png',
          ),
        ),
        'description' => t('The full-size image.'),
      );
      return $formats;
    case 'options':
      switch ($attr['format']) {
        case 'image':
          $info = image_get_info(file_create_path($asset->filepath));
          $form['height'] = array(
            '#type' => 'textfield',
            '#title' => t('Height'),
            '#size' => '10',
            '#default_value' => !isset($_GET['height']) ? $info['height'] : filter_xss($_GET['height']),
          );
          $form['width'] = array(
            '#type' => 'textfield',
            '#title' => t('Width'),
            '#size' => '10',
            '#default_value' => !isset($_GET['width']) ? $info['width'] : filter_xss($_GET['width']),
          );
          $form['resizable'] = array(
            '#type' => 'hidden',
            '#value' => 'true',
          );
          return $form;
        default:
          return array();
      }
      break;
    case 'render':
      switch ($attr['format']) {
        case 'image':
          $img_attributes = array(
            'title' => $attr['title'],
            'alt' => $attr['title'],
          );
          if ($attr['height']) {
            $img_attributes['height'] = $attr['height'];
          }
          if ($attr['width']) {
            $img_attributes['width'] = $attr['width'];
          }
          return '<img src="' . file_create_url($asset->filepath) . '" ' . drupal_attributes($img_attributes) . ' />';
        default:
          return theme('asset_render_default', $asset);
      }
      break;
    case 'preview':
      switch ($attr['format']) {
        case 'image':
          return theme('image', file_create_path($asset->filepath), '', '', array(
            'width' => '100',
          ), false);
        case 'link':
          return theme('asset_render_default', $asset);
      }
      break;
    case 'details':
      switch ($attr['format']) {
        case 'image':
          $info = image_get_info(file_create_path($asset->filepath));
          return array(
            t('Width') => $info['width'] . 'px',
            t('Height') => $info['height'] . 'px',
          );
      }
      return array();
    case 'img':
      switch ($attr['format']) {
        case 'image':
          return file_create_url($asset->filepath);
        case 'link':
        default:

          // when we get around to building icons.
          $icon = drupal_get_path('module', 'asset') . '/misc/icons/' . $asset->extension . '.png';
          if (file_exists($icon)) {
            return $icon;
          }

          // if all else fails send back a transparent gif so the default bg image shows
          return drupal_get_path('module', 'asset') . '/misc/transparent.gif';
      }
      break;
  }
}

/**
 * Implementation of hook_asset_type().
 */
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;
  }
}

Functions

Namesort descending Description
asset_asset_formatter Implementation of hook_asset_formatter().
asset_asset_type Implementation of hook_asset_type().