You are here

function theme_dir_listing_list_view in Filebrowser 6.2

Same name and namespace in other branches
  1. 8 filebrowser.theme.inc \theme_dir_listing_list_view()
  2. 7.4 filebrowser.theme.inc \theme_dir_listing_list_view()
  3. 7.2 filebrowser.theme.inc \theme_dir_listing_list_view()
  4. 7.3 filebrowser.theme.inc \theme_dir_listing_list_view()

Theming function for list view.

Parameters

Node $node the node to transform to list view.:

Return value

string A string containing list view XHTML.

File

./filebrowser.theme.inc, line 87

Code

function theme_dir_listing_list_view($node) {
  $output = '';
  $files =& $node->file_listing;

  // Deal with files.
  $unsorted_rows = array();

  // add optional columns
  $node->folder_presentation->visible_columns['description'] = TRUE;

  // Visible columns
  $visible_columns = array();
  $column_names = _filebrowser_externals('metadata_info');

  // Record if directory listing is empty or not.
  $directory_empty = TRUE;

  // Available actions
  $actions = module_invoke_all('filebrowser_actions_info', $node);
  $has_actions = count($actions) > 0;

  // Header building
  $header = array();
  if ($has_actions) {
    $header[] = array(
      '',
    );
  }

  // Considered each file (or directory)
  foreach ($files as $file_name => $data) {

    // Skip dot folder
    if ($file_name == ".") {
      continue;
    }

    // At least one file
    $directory_empty = FALSE;

    // File extension case
    if ($file_name != ".." && $data['kind'] == 0 && $node->folder_presentation->hide_extension) {
      $pos = strrpos($data[FILEBROWSER_DATA_NAME_DISPLAY_NAME], ".");
      $data[FILEBROWSER_DATA_NAME_DISPLAY_NAME] = substr($data[FILEBROWSER_DATA_NAME_DISPLAY_NAME], 0, $pos);
    }
    if ($has_actions) {
      $unsorted_rows[$file_name]['checkbox'] = $data['mime-type'] != 'folder/parent' ? "<input type='checkbox' name='select_{$data['fid']}'/>" : '';
    }
    foreach ($column_names as $column_name => $column_spec) {
      if (isset($node->folder_presentation->visible_columns[$column_name]) && $node->folder_presentation->visible_columns[$column_name]) {
        switch ($column_name) {
          case FILEBROWSER_DATA_NAME_ICON:
            $visible_columns[$column_name] = TRUE;
            $unsorted_rows[$file_name][FILEBROWSER_DATA_NAME_ICON] = array(
              'data' => _filebrowser_thumbnails_generate($node, $data),
              'style' => 'width:1%;',
            );
            break;
          case FILEBROWSER_DATA_NAME_DISPLAY_NAME:
            $unsorted_rows[$file_name][FILEBROWSER_DATA_NAME_DISPLAY_NAME] = '<a href="' . $data['url'] . '">' . ($data['display-name'] == '..' ? t('Go up') : $data['display-name']) . '</a>' . theme('mark', $data['status']);
            $visible_columns[$column_name] = TRUE;
            break;
          case FILEBROWSER_DATA_NAME_SIZE:
            $used = $data['size'] != 0 && $data['kind'] == 0;
            $unsorted_rows[$file_name][FILEBROWSER_DATA_NAME_SIZE] = $used ? format_size($data['size']) : "";
            if ($used) {
              $visible_columns[$column_name] = TRUE;
            }
            break;
          case FILEBROWSER_DATA_NAME_CREATED:
            if ($data['created'] != 0) {
              $visible_columns[$column_name] = TRUE;
            }
            $unsorted_rows[$file_name][FILEBROWSER_DATA_NAME_CREATED] = format_date($data['created'], "small");
            break;
          case FILEBROWSER_DATA_NAME_MODIFIED:
            if ($data['modified'] != 0) {
              $visible_columns[$column_name] = TRUE;
            }
            $unsorted_rows[$file_name][FILEBROWSER_DATA_NAME_MODIFIED] = format_date($data['modified'], "small");
            break;
          case FILEBROWSER_DATA_NAME_TYPE:
            $used = $data['mime-type'] != 'folder' && $data['mime-type'] != 'folder/parent';
            if ($used) {
              $visible_columns[$column_name] = TRUE;
            }
            $unsorted_rows[$file_name][FILEBROWSER_DATA_NAME_TYPE] = $used ? t($data['mime-type']) : "";
            break;
          case FILEBROWSER_DATA_NAME_DESCRIPTION:
            if (!empty($data['description'])) {
              $visible_columns[$column_name] = TRUE;
            }
            $unsorted_rows[$file_name]['description'] = str_replace("\n", "<br/>", $data['description']);
            break;
          default:
            $visible_columns[$column_name] = isset($data[$column_name]);
            if ($visible_columns[$column_name]) {
              $unsorted_rows[$file_name][$column_name] = $data[$column_name];
            }
            break;
        }
      }
    }
  }
  if (!$directory_empty) {

    // Builder header and clean up unused columns
    foreach ($column_names as $column_name => $column_spec) {
      if (isset($visible_columns[$column_name]) && $visible_columns[$column_name]) {
        if ($column_name == FILEBROWSER_DATA_NAME_ICON) {
          $header[$column_name] = array();
        }
        else {
          $header[$column_name] = array(
            'data' => $column_spec['title'],
            'field' => $column_name,
          );
        }
      }
      else {

        // If unused data then clean up !
        foreach ($unsorted_rows as &$row) {
          unset($row[$column_name]);
        }
      }
    }
    $header[$node->folder_presentation->default_sort]['sort'] = $node->folder_presentation->default_sort_order;

    // Handle any and all sorting.
    $table_sort = tablesort_init($header);

    // Split files in two heaps to preserve folders and files
    $just_folders = array();
    $just_files = array();

    // TODO : take data from $unsorted_rows and not $data
    foreach ($files as $name => $data) {
      if ($name != '.') {
        if ($data['kind'] == 0) {
          $just_files[] = $data;
        }
        else {

          // Do not retain the '.' folder
          $just_folders[] = $data;
        }
      }
    }

    // Sort files according to correct column.
    if (isset($table_sort['sql'])) {
      $field = $table_sort['sql'];
      $columns = _filebrowser_externals('metadata_info');
      if (isset($columns[$table_sort['sql']]) && isset($columns[$table_sort['sql']]['sortable']) && $columns[$table_sort['sql']]['sortable']) {
        switch ($columns[$table_sort['sql']]['type']) {
          case 'integer':
            $code = '$a=isset($a["' . $field . '"])?$a["' . $field . '"]:0;';
            $code .= '$b=isset($b["' . $field . '"])?$b["' . $field . '"]:0;';
            $code .= 'return $a-$b;';
            break;
          case 'string':
            $code = '$a=isset($a["' . $field . '"])?$a["' . $field . '"]:"";';
            $code .= '$b=isset($b["' . $field . '"])?$b["' . $field . '"]:"";';
            $code .= 'return -strcmp(strtolower($a),strtolower($b));';
            break;
        }
        $sorter = create_function('$a,$b', $code);
        usort($just_folders, $sorter);
        if ($table_sort['sort'] == 'asc') {
          $just_folders = array_reverse($just_folders, TRUE);
        }
        usort($just_files, create_function('$a,$b', $code));
        if ($table_sort['sort'] == 'asc') {
          $just_files = array_reverse($just_files, TRUE);
        }
      }
    }

    // Combine folders and files array and setup correct order.

    //$rows = array_merge($just_folders, $just_files);
    $rows = array();
    foreach ($just_folders as $data) {
      $rows[] = $unsorted_rows[$data['display-name']];
    }
    foreach ($just_files as $data) {
      $rows[] = $unsorted_rows[$data['display-name']];
    }
    $output .= theme('table', $header, $rows);
    if ($has_actions) {
      module_load_include("form.actions.inc", "filebrowser");
      $output = drupal_get_form('filebrowser_form_actions', $output, $actions, $node);
    }
    return $output;
  }

  // Directory is not empty
}