You are here

function asset_wizard_form_asset_selection in Asset 5

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

Form builder for step 1 of the asset wizard. This step is the most intensive so it has been separated out for clarity.

1 call to asset_wizard_form_asset_selection()
asset_wizard_form in ./asset_wizard.inc
Main form builder function for the asset wizard.

File

./asset_wizard.inc, line 387

Code

function asset_wizard_form_asset_selection(&$form, &$form_values, $current_asset = NULL) {
  global $user;
  static $types, $ops;
  if (!isset($types)) {
    foreach (module_implements('asset_type') as $module) {
      $types[$module] = module_invoke($module, 'asset_type', 'info');
      foreach ($types[$module] as $delta => $type) {
        $ops[$type['value']] = array(
          'module' => $module,
          'delta' => $delta,
        );
      }
    }
  }
  if ($_GET['op'] == t("Delete Folder")) {
    $dir = filter_xss($_GET['dir']);
    if (empty($dir) || strtolower($dir) == strtolower($user->name)) {
      drupal_set_message(t('You can not delete the main folder.'), 'error');
      drupal_goto(filter_xss($_GET['q']), 'dir=' . $dir);
    }
    else {

      // First check if this folder has sub-folders, these need to be deleted first
      $subdirs = db_query("SELECT aid FROM {asset} WHERE type='directory' AND dirname='%s' LIMIT 1", $dir);
      if (db_num_rows($subdirs)) {

        // There is at least one sub-folder
        drupal_set_message(t('This folder contains at least one sub-folder.<br/>You need to delete these first before continuing.'), 'error');
        drupal_goto(filter_xss($_GET['q']), 'dir=' . $dir);
      }
      else {

        // 	No subfolders, let's display the warning
        drupal_set_message('Warning: you are about to delete this folder and all assets within this folder.');
        $form['warning'] = array(
          '#value' => t('Click the Next button if you are sure you want to delete this folder and its content.<br/>Click the Back button if you want to go back.<br/><br/>This operation CAN NOT be undone.'),
        );
        $form['delete_confirm'] = array(
          '#type' => 'hidden',
          '#value' => 1,
        );
        return;
      }
    }
  }
  $op = false;
  if ($form_values['op'] == t('Back')) {

    // if a user got here by clicking back, then they are trying to get to the
    // main selection screen.
    $op = false;
  }
  elseif (isset($form_values['op']) && isset($ops[$form_values['op']])) {
    $op = $form_values['op'];
  }
  elseif ($ops[$_GET['op']]) {
    $op = $_GET['op'];

    // unset op, so that links that re-use the query string wont use it again.
    unset($_GET['op']);
  }
  if ($op) {
    $module = $ops[$op]['module'];
    $delta = $ops[$op]['delta'];
    $form = array_merge($form, asset_wizard_default_fields($form_values));
    $form = array_merge($form, module_invoke($module, 'asset_type', 'form', $delta, $form_values));
    $form['module'] = array(
      '#type' => 'value',
      '#value' => $module,
    );
    $form['delta'] = array(
      '#type' => 'value',
      '#value' => $delta,
    );
  }
  else {

    // normal asset selection form
    // set directory
    if ($current_asset && asset_check_directory($current_asset->dirname)) {
      $dir = $current_asset->dirname;
    }
    elseif (isset($_GET['dir']) && asset_check_directory($_GET['dir'])) {
      $dir = $_GET['dir'];
    }
    elseif (asset_check_directory($user->name, FILE_CREATE_DIRECTORY, NULL, array(
      'parent' => '',
      'title' => $user->name,
    ))) {
      $dir = $user->name;
    }
    else {
      $dir = '';
    }
    $form['parent'] = array(
      '#type' => 'hidden',
      '#value' => $form_values['parent'] ? $form_values['parent'] : $dir,
    );

    // copy querystring args for building links
    $query = $_GET;
    unset($query['q']);

    // create directory crumbs and '..' entry
    $crumbs = array();
    if (file_create_path($dir) != file_directory_path()) {
      $parts = explode('/', $dir);
      while ($current = array_pop($parts)) {
        $query['dir'] = $parts ? join('/', $parts) . '/' . $current : $current;
        $crumbs[] = l($current, $_GET['q'], array(), asset_build_query($query));
      }
      $query['dir'] = substr($dir, 0, strrpos($dir, '/'));
      $list = '<li class="parent">' . l('..', $_GET['q'], array(), asset_build_query($query)) . '</li>';
    }
    $query['dir'] = '';
    $crumbs[] = l('assets', $_GET['q'], array(), asset_build_query($query));

    // build directory list and filename options array
    $sql = 'SELECT DISTINCT(a.aid),a.filename FROM {asset} a ';
    $args = array();

    // for everyone but users with 'administer assets' access control,
    // only get assets with proper permissions.
    if (user_access('administer assets')) {
      $sql .= 'WHERE a.dirname=\'%s\' ';
      $args = array(
        $dir,
      );
    }
    else {

      // Permissions are not functioning correctly, as per #543 and #489
      $sql .= 'LEFT JOIN {asset_role} r ON a.aid=r.aid ' . 'WHERE (a.uid = %d ' . 'OR a.status = 1 ' . 'OR (r.rid IN (%s) AND r.status = 1) ) ' . 'AND a.dirname=\'%s\' ';
      $args = array(
        $user->uid,
        join(array_keys($user->roles), ','),
        $dir,
      );

      //$sql .= 'WHERE a.dirname="%s" ';

      //$args = array($dir);
    }
    $sql .= "ORDER BY a.filename";
    $result = db_query($sql, $args);
    $options = array();
    while ($asset = db_fetch_object($result)) {
      $asset = asset_load($asset->aid);

      // add to list if directory or add to options if file
      if ($asset->type == 'directory') {

        // if wizard was loaded with an aid, remove it
        if ($query['aid']) {
          unset($query['aid']);
        }
        $query['dir'] = ($asset->dirname ? $asset->dirname . '/' : '') . $asset->filename;
        $list .= '<li class="folder">' . l($asset->title, $_GET['q'], array(), asset_build_query($query)) . '</li>';
      }
      else {
        $options[$asset->aid] = $asset->filename;
      }
    }
    $form['dir_crumb'] = array(
      '#value' => '/ ' . join(' / ', array_reverse($crumbs)),
    );
    if ($list) {
      $form['folder_list'] = array(
        '#value' => '<ul class="folder-list">' . $list . '</ul>',
      );
    }
    $form['aid'] = array(
      '#type' => 'select',
      '#size' => 10,
      '#options' => $options,
    );
    if ($current_asset->aid) {
      $form['aid']['#default_value'] = $current_asset->aid;
    }
    $form['asset_preview'] = array(
      '#value' => '<div class="asset-preview"></div>',
    );
    $form['#tree'] = false;
    $form['#theme'] = 'asset_wizard_selection_form';

    //return $form;
  }
}