modules/upload/views_handler_field_upload_description.inc

00001 <?php
00002 // $Id: views_handler_field_upload_description.inc,v 1.5 2009/09/15 22:15:31 merlinofchaos Exp $
00003 
00007 class views_handler_field_upload_description extends views_handler_field {
00008   function init(&$view, &$options) {
00009     parent::init($view, $options);
00010     if (!empty($options['link_to_file'])) {
00011       $this->additional_fields['fid'] = 'fid';
00012     }
00013   }
00014 
00015   function option_definition() {
00016     $options = parent::option_definition();
00017     $options['link_to_file'] = array('default' => FALSE);
00018     return $options;
00019   }
00020 
00021   function options_form(&$form, &$form_state) {
00022     parent::options_form($form, $form_state);
00023     $form['link_to_file'] = array(
00024       '#title' => t('Link this field to download the file'),
00025       '#type' => 'checkbox',
00026       '#default_value' => !empty($this->options['link_to_file']),
00027     );
00028   }
00029 
00030   function pre_render($values) {
00031     if (empty($this->options['link_to_file'])) {
00032       return;
00033     }
00034 
00035     $fids = array();
00036     $this->items = array();
00037 
00038     $data = array();
00039     foreach ($values as $result) {
00040       if ($result->{$this->aliases['fid']}) {
00041         $fids[] = $result->{$this->aliases['fid']};
00042       }
00043     }
00044 
00045     if ($fids) {
00046       $result = db_query("SELECT f.fid, f.filepath FROM {files} f WHERE f.fid IN (" . implode(', ', $fids) . ")");
00047       while ($file = db_fetch_object($result)) {
00048         $this->items[$file->fid] = $file;
00049       }
00050     }
00051   }
00052 
00053   function render($values) {
00054     return $this->render_link($values->{$this->field_alias}, $values);
00055   }
00056 
00062   function render_link($data, $value) {
00063     if (!empty($this->options['link_to_file']) && $value->{$this->aliases['fid']} && $data !== NULL && $data !== '') {
00064       $values = $this->items[$value->{$this->aliases['fid']}];
00065       $this->options['alter']['make_link'] = TRUE;
00066       $this->options['alter']['path'] = file_create_url($values->filepath);
00067     }
00068     else {
00069       $this->options['alter']['make_link'] = FALSE;
00070     }
00071     return $data;
00072   }
00073 }

Generated on Mon Nov 30 15:06:00 2009 for Views by  doxygen 1.4.7