You are here

function asset_wizard_form in Asset 5

Same name and namespace in other branches
  1. 6 asset_wizard.inc \asset_wizard_form()
  2. 6 inc/asset_wizard.inc \asset_wizard_form()

Main form builder function for the asset wizard.

1 string reference to 'asset_wizard_form'
asset_wizard in ./asset.module
Menu callback to load the asset popup window.

File

./asset_wizard.inc, line 19

Code

function asset_wizard_form($form_values = null) {

  // We need some values passed around:
  //   $op for the "delete" button on the asset selection screen
  //   $fieldname for the direct "select" button
  if (!isset($_GET['noreset'])) {
    $_SESSION['asset_op'] = $_GET['op'];
  }
  if (isset($_GET['textfield'])) {
    list($fieldname, $lost) = split("\\]\\[", $_GET['textfield']);
    $fieldname = str_replace(array(
      "_",
      "[",
    ), "-", $fieldname);
    $fieldname = "edit-" . $fieldname . "-";
    $_SESSION['asset_fieldname'] = $fieldname;

    // drupal_set_message("op2: ". $_SESSION['asset_op']);
  }
  elseif (isset($_GET['textarea']) || $_GET['q'] == "asset/wizard/tinymce") {
    unset($_SESSION['asset_fieldname']);
    $_SESSION['asset_op'] = "tinymce";
  }

  // Allow the aid to be passed in the querystring
  if ($form_values == null && is_numeric($_GET['aid'])) {
    $form_values['step'] = 2;
    $form_values['aid'] = $_GET['aid'];
  }

  // Step Handling
  if (!isset($form_values)) {
    $step = 1;
  }
  else {
    list($lost, $lost, $field) = split("-", $fieldname);
    switch ($form_values['op']) {
      case t('Back'):
        if ($field == ASSET_THUMBNAIL) {
          $step = 1;
        }
        elseif (isset($form_values['delete_cancel'])) {
          unset($_SESSION['messages']);
          drupal_goto(filter_xss($_GET['q']), 'dir=' . $form_values['dir']);
        }
        else {
          $step = $form_values['step'] > 1 ? $form_values['step'] - 1 : 1;
        }
        break;
      case t('Next'):
      case t('Finish'):
        if ($step == 1 && !$form_values['aid']) {

          // dont proceed without a valid aid.
          break;
        }
        if ($field == ASSET_THUMBNAIL && $form_values['step'] == 1) {

          // When inserting an image preview, don't offer the option to select a size
          $step = $form_values['step'] + 2;
        }
        else {
          $step = $form_values['step'] + 1;
        }
        break;
      case t('Edit'):
        $asset = asset_load(array(
          'aid' => $form_values['aid'],
        ));
        $form['edit-asset'] = array(
          '#type' => 'hidden',
          '#value' => '1',
        );
        $form['aid'] = array(
          '#type' => 'hidden',
          '#value' => $form_values['aid'],
        );
        $fileinfo = pathinfo($asset->filename);
        if (strlen($fileinfo['extension']) == 3) {

          // This is a regular file
          $form['filename'] = array(
            '#title' => t('Filename'),
            '#type' => 'textfield',
            '#default_value' => $asset->filename,
            '#description' => t("Warning: you are changing the actual filename. If you're not sure what you're doing, you should leave this unchanged."),
          );
        }
        else {

          // Not a regular file, could be a youtube movie for instance (filename $instance.youtube)
          $form['filename'] = array(
            '#title' => t('Filename'),
            '#type' => 'item',
            '#value' => $asset->filename,
            '#description' => t("You can only change the filename of regular files, not of YouTube movies for instance."),
          );
        }
        $form['old-filename'] = array(
          '#type' => 'hidden',
          '#value' => $asset->filename,
        );
        $form['old-parent'] = array(
          '#type' => 'hidden',
          '#value' => $asset->dirname,
        );
        $form = array_merge($form, asset_wizard_default_fields($form_values));
        $form['title']['#default_value'] = $asset->title;
        $form['author']['#default_value'] = $asset->author;
        $form['description']['#default_value'] = $asset->description;
        break;
      case t('Delete'):
        if (!$form_values['deleteme']) {
          $form_values['step'] = 1;
          $form['delete_warning'] = array(
            '#value' => " " . t("You are about to delete the selected asset. Press the delete button to confirm deletion."),
          );
          $form['deleteme'] = array(
            '#type' => 'hidden',
            '#value' => 1,
          );
        }
        elseif ($form_values['deleteme'] == 1) {
          $form['deleteme'] = array(
            '#type' => 'hidden',
            '#value' => 0,
          );
        }
      default:

        // only back,next,finish buttons should effect the step
        $step = $form_values['step'];
        break;
    }
  }
  $form['step'] = array(
    '#type' => 'hidden',
    '#value' => $step,
  );
  drupal_set_title("Asset Wizard - Step {$step}");

  // Extract asset info from the macro or aid
  if ($form_values['macro']) {
    $macros = asset_get_macros(' ' . $form_values['macro'] . ' ');
    $macro = array_pop($macros);
    if ($macro['aid']) {
      $asset = asset_load($macro['aid']);
    }
  }
  elseif ($form_values['aid']) {
    $asset = asset_load($form_values['aid']);
    $macro['aid'] = $form_values['aid'];
  }
  if (is_object($asset) && $step > 1) {
    $form[] = array(
      '#value' => '<div class="asset-selected-preview">' . asset_js_preview($asset->aid, false, true) . '</div>',
    );
  }

  // Build the form based on what step we are on
  switch ($step) {
    case 1:
      asset_wizard_form_asset_selection($form, $form_values, $asset);
      break;
    case 2:
      $form['macro'] = array(
        '#type' => 'hidden',
        '#value' => asset_build_macro($macro),
      );
      $form['help'] = array(
        '#value' => t('Select a formatting option, or <em>Choose</em> or <em>Upload</em> a new asset.'),
      );
      $formatters = asset_get_formatters();
      $ext = strtolower($asset->extension);
      $default = variable_get('asset_default_formatter_' . $asset->type . '_' . $ext, 'asset:link');
      if (is_array($formatters[$asset->type][$ext])) {
        foreach ($formatters[$asset->type][$ext] as $formatter) {
          $form['formatter'][] = array(
            '#type' => 'radio',
            '#title' => $formatter['name'],
            '#return_value' => $formatter['module'] . ':' . $formatter['format'],
            '#default_value' => !isset($_GET['format']) ? $default : $_GET['format'],
            '#description' => $formatter['description'] ? $formatter['description'] : NULL,
            '#parents' => array(
              'formatter',
            ),
          );
        }
      }
      if (is_array($formatters[$asset->type]['*'])) {
        foreach ($formatters[$asset->type]['*'] as $formatter) {
          $form['formatter'][] = array(
            '#type' => 'radio',
            '#title' => $formatter['name'],
            '#return_value' => $formatter['module'] . ':' . $formatter['format'],
            '#default_value' => !isset($_GET['format']) ? $default : $_GET['format'],
            '#description' => $formatter['description'] ? $formatter['description'] : NULL,
            '#parents' => array(
              'formatter',
            ),
          );
        }
      }
      if (is_array($formatters['*']['*'])) {
        foreach ($formatters['*']['*'] as $formatter) {
          $form['formatter'][] = array(
            '#type' => 'radio',
            '#title' => $formatter['name'],
            '#return_value' => $formatter['module'] . ':' . $formatter['format'],
            '#default_value' => !isset($_GET['format']) ? $default : $_GET['format'],
            '#description' => $formatter['description'] ? $formatter['description'] : NULL,
            '#parents' => array(
              'formatter',
            ),
          );
        }
      }
      break;
    case 3:
      list($macro['formatter'], $macro['format']) = explode(':', $form_values['formatter']);
      $form['macro'] = array(
        '#type' => 'hidden',
        '#value' => asset_build_macro($macro),
      );
      $default_options['title'] = array(
        '#type' => 'textfield',
        '#title' => t('Title'),
        '#default_value' => $asset->title,
        '#size' => 40,
      );
      list($lost, $lost, $field) = split("-", $fieldname);
      if (!preg_match('@link@i', $form_values['formatter'])) {
        if ($field != ASSET_THUMBNAIL) {

          // Don't allow the option to select a width/height when inserting a node thumbnail
          $default_options['width'] = array(
            '#type' => 'textfield',
            '#title' => t('Width'),
            '#default_value' => !isset($_GET['width']) ? $asset->width : filter_xss($_GET['width']),
          );
          $default_options['height'] = array(
            '#type' => 'textfield',
            '#title' => t('Height'),
            '#default_value' => !isset($_GET['height']) ? $asset->height : filter_xss($_GET['height']),
          );
          $default_options['resizable'] = array(
            '#type' => 'hidden',
            '#value' => 'true',
          );
        }
        else {
          $default_options['width'] = array(
            '#type' => 'hidden',
            '#title' => t('Width'),
            '#default_value' => $asset->height,
          );
          $default_options['height'] = array(
            '#type' => 'hidden',
            '#title' => t('Height'),
            '#default_value' => $asset->width,
          );
        }
        $default_options['align'] = array(
          '#type' => 'select',
          '#title' => t('Alignment'),
          '#options' => array(
            'none' => t('None'),
            'left' => t('Left'),
            'center' => t('Center'),
            'right' => t('Right'),
          ),
          '#default_value' => !isset($_GET['align']) ? 'none' : filter_xss($_GET['align']),
        );
      }
      $module_options = (array) module_invoke($macro['formatter'], 'asset_formatter', 'options', $asset, $macro);
      $form['options'] = array_merge($default_options, $module_options);
      $form['options']['#tree'] = true;
      break;
    case 4:
      if (is_array($form_values['options'])) {
        foreach ($form_values['options'] as $k => $v) {
          if (!in_array($k, array(
            'aid',
            'formatter',
            'format',
          )) && $v) {
            $macro[$k] = $v;
          }
        }
      }
      $form['macro'] = array(
        '#type' => 'textarea',
        '#title' => 'Paste the following code into your text.',
        '#value' => asset_build_macro($macro),
      );

      // Create the preview code for the CCK field
      $is_image = array(
        'jpg',
        'jpeg',
        'png',
        'gif',
        'tif',
        'tiff',
        'bmp',
      );
      $a = asset_load(array(
        'aid' => $macro['aid'],
      ));

      // Preview the image
      if (in_array($a->extension, $is_image)) {
        if (variable_get('asset_imagecache', 0)) {
          $presets = imagecache_presets();
          $preset = $presets[variable_get('asset_imagecache', 0)]['presetname'];
          $preview = theme('imagecache', $preset, str_replace("//", "/", $a->filepath));
        }
        else {
          $asset = array(
            'aid' => $macro['aid'],
            'format' => 'image',
            'formatter' => 'asset',
            'width' => '120',
            'height' => '80',
          );
          $preview = asset_render_macro($asset);
        }
      }
      else {
        $preview = asset_preview($macro['aid']);
      }
      $form['preview'] = array(
        '#type' => 'hidden',
        '#value' => $preview,
      );
      $form['author'] = array(
        '#type' => 'hidden',
        '#value' => $a->author,
      );
      $form[] = array(
        '#value' => asset_wizard_finish(),
      );
      break;
  }

  // This is important. If we're on the final step, we tell drupal to use the
  // normal redirect functionality. That means the browser goes to whatever page
  // after the final submit. All previous steps don't redirect, so just post
  // back to this form.
  $form['#redirect'] = $step == ASSET_WIZARD_FINAL_STEP ? NULL : FALSE;
  if ($form['#redirect'] == FALSE) {
    if ($step == 1) {
      $form['#redirect'] = NULL;
    }
  }
  $form['#multistep'] = TRUE;
  $form['#tree'] = TRUE;
  $form['footer']['#tree'] = false;

  // We only want to display the delete button on the "Choose" page
  // if ($step == 1 && empty($_SESSION['asset_op'])) {
  if ($step != 1 || !empty($_SESSION['asset_op']) || $_GET['op'] == t("Delete Folder")) {
    if ($_GET['op'] == t("Delete Folder")) {
      $form['delete_cancel'] = array(
        '#type' => 'hidden',
        '#value' => 1,
      );
      $form['dir'] = array(
        '#type' => 'hidden',
        '#value' => filter_xss($_GET['dir']),
      );
    }
    $form['footer']['previous'] = array(
      '#type' => 'submit',
      '#value' => t(ASSET_WIZARD_PREVIOUS),
      '#attributes' => isset($form_values['op']) || $step > 1 || $_GET['op'] == t("Delete Folder") ? array() : array(
        'disabled' => 'disabled',
      ),
    );
  }
  if ($step == ASSET_WIZARD_FINAL_STEP - 1) {
    $form['footer']['finish'] = array(
      '#type' => 'submit',
      '#value' => t('Finish'),
    );
  }
  elseif ($step != 1 || !empty($_SESSION['asset_op'])) {
    $form['footer']['next'] = array(
      '#type' => 'submit',
      '#value' => t(ASSET_WIZARD_NEXT),
    );
  }
  if ($step == 1 && $_SESSION['asset_op'] != "Upload" && $_SESSION['asset_op'] != "New Folder") {
    if ($_SESSION['asset_op'] != "tinymce") {
      $form['footer']['select'] = array(
        '#type' => 'button',
        '#value' => t('Select'),
      );
    }
    $form['footer']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
    );
    $form['footer']['edit'] = array(
      '#type' => 'submit',
      '#value' => t('Edit'),
    );
  }
  $form['footer']['cancel'] = array(
    '#type' => 'submit',
    '#value' => t(ASSET_WIZARD_CANCEL),
    '#attributes' => array(
      'alt' => t('Cancel and Return'),
    ),
  );
  return $form;
}