[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/project/release/views/handlers/ -> project_release_handler_field_files.inc (source)

   1  <?php
   2  // $Id: project_release_handler_field_files.inc,v 1.2 2009/11/29 07:30:41 dww Exp $
   3  
   4  /**
   5   * Field handler to provide a list of files associated with a release node.
   6   *
   7   * Based heavily on views_handler_field_upload_fid.inc from
   8   * contributions/modules/views/modules/upload (forked from revision 1.7).
   9   * However, we're using separate tables ({project_release_file}, not
  10   * {upload}), we support a slightly different set of tokens, we need different
  11   * download links, different handler options, etc. So, it wasn't really
  12   * possible to extend views_handler_field_upload_fid.inc...
  13   */
  14  class project_release_handler_field_files extends views_handler_field_prerender_list {
  15    function construct() {
  16      parent::construct();
  17    }
  18  
  19    function option_definition() {
  20      $options = parent::option_definition();
  21      $options['link_to_file'] = array('default' => FALSE);
  22      $options['max_files'] = array('default' => 0);
  23      $options['file_sort'] = array('default' => 'filename');
  24      $options['file_sort_order'] = array('default' => 'DESC');
  25      return $options;
  26    }
  27  
  28    function options_form(&$form, &$form_state) {
  29      parent::options_form($form, $form_state);
  30      $form['link_to_file'] = array(
  31        '#title' => t('Link this field to download the file'),
  32        '#type' => 'checkbox',
  33        '#default_value' => !empty($this->options['link_to_file']),
  34      );
  35      $max_options[0] = t('Show all');
  36      for ($n = 1; $n <= 10; $n++) {
  37        $max_options[$n] = $n;
  38      }
  39      $form['max_files'] = array(
  40        '#title' => t('Maximum number of files to display'),
  41        '#type' => 'select',
  42        '#options' => $max_options,
  43        '#default_value' => $this->options['max_files'],
  44      );
  45      $form['file_sort'] = array(
  46        '#title' => t('Sort files by'),
  47        '#type' => 'select',
  48        '#options' => array(
  49          'filename' => t('File name'),
  50          'filesize' => t('File size'),
  51          'timestamp' => t('File timestamp'),
  52          'fid' => t('File ID'),
  53        ),
  54        '#default_value' => $this->options['file_sort'],
  55      );
  56      $form['file_sort_order'] = array(
  57        '#title' => t('Sort file direction'),
  58        '#type' => 'select',
  59        '#options' => array(
  60          'ASC' => t('Ascending'),
  61          'DESC' => t('Descending'),
  62        ),
  63        '#default_value' => $this->options['file_sort_order'],
  64      );
  65    }
  66  
  67    function pre_render($values) {
  68      $nids = array();
  69      $this->items = array();
  70      foreach ($values as $result) {
  71        $nids[] = $result->{$this->field_alias};
  72      }
  73      if (!empty($nids)) {
  74        $order_by = 'f.' . $this->options['file_sort'];
  75        $order_by .= ' ' . $this->options['file_sort_order'];
  76        $result = db_query("SELECT prf.nid, prf.fid, prf.filehash, f.uid, f.filename, f.filepath, f.filesize, f.filemime, f.timestamp FROM {project_release_file} prf LEFT JOIN {files} f ON prf.fid = f.fid WHERE prf.nid IN (" . implode(', ', $nids) . ") ORDER BY $order_by");
  77        while ($file = db_fetch_array($result)) {
  78          $file['filename'] = check_plain($file['filename']);
  79          $file['filemime'] = check_plain($file['filemime']);
  80          $file['filesize'] = format_size($file['filesize']);
  81          $file['timestamp'] = format_date($file['timestamp']);
  82          $file_link = theme('project_release_download_link', $file['filepath'], NULL, TRUE);
  83          $file['filepath'] = $file_link['href'];
  84          if (!empty($this->options['link_to_file']) ) {
  85            $file['make_link'] = TRUE;
  86            $file['path'] = $file['filepath'];
  87          }
  88          // Enforce the maximum # of files per node as configured in the field.
  89          if (!empty($this->options['max_files']) && !empty($this->items[$file['nid']]) && (count($this->items[$file['nid']]) >= $this->options['max_files'])) {
  90            continue;
  91          }
  92          $this->items[$file['nid']][$file['fid']] = $file;
  93        }
  94      }
  95    }
  96  
  97    function render_item($count, $item) {
  98      return $item['filename'];
  99    }
 100  
 101    function document_self_tokens(&$tokens) {
 102      $tokens['[' . $this->options['id'] . '-fid' . ']'] = t('The file ID for the file.');
 103      $tokens['[' . $this->options['id'] . '-name' . ']'] = t('The name of the attached file.');
 104      $tokens['[' . $this->options['id'] . '-ext' . ']'] = t("The filename extenstion (e.g. 'zip' or 'tar.gz') of the attached file.");
 105      $tokens['[' . $this->options['id'] . '-type' . ']'] = t('The MIME type of the attached file.');
 106      $tokens['[' . $this->options['id'] . '-path' . ']'] = t('The path of the attached file.');
 107      $tokens['[' . $this->options['id'] . '-size' . ']'] = t('The size of the attached file.');
 108      $tokens['[' . $this->options['id'] . '-hash' . ']'] = t('The MD5 hash of the attached file.');
 109      $tokens['[' . $this->options['id'] . '-time' . ']'] = t('The date and time when the file was created or uploaded.');
 110    }
 111  
 112    function add_self_tokens(&$tokens, $item) {
 113      $tokens['[' . $this->options['id'] . '-fid' . ']'] = $item['fid'];
 114      $tokens['[' . $this->options['id'] . '-name' . ']'] = $item['filename'];
 115      $tokens['[' . $this->options['id'] . '-type' . ']'] = $item['filemime'];
 116      $tokens['[' . $this->options['id'] . '-path' . ']'] = $item['filepath'];
 117      $tokens['[' . $this->options['id'] . '-size' . ']'] = $item['filesize'];
 118      $tokens['[' . $this->options['id'] . '-hash' . ']'] = $item['filehash'];
 119      $tokens['[' . $this->options['id'] . '-time' . ']'] = $item['timestamp'];
 120      $file_parts = explode('.', basename($item['filename']));
 121      $ext = array_pop($file_parts);
 122      // Special cases for 'gz' and 'bz2', since you really need to know the
 123      // thing before that, too (e.g. '.tar.gz').
 124      if ($ext == 'gz' || $ext == 'bz2') {
 125        $ext = array_pop($file_parts) . '.' . $ext;
 126      }
 127      $tokens['[' . $this->options['id'] . '-ext' . ']'] = $ext;
 128    }
 129  }
 130  


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7