views_handler_field_upload_description.inc

File

modules/upload/views_handler_field_upload_description.inc
View source
  1. <?php
  2. /**
  3. * Field handler to provide a list of roles.
  4. */
  5. class views_handler_field_upload_description extends views_handler_field {
  6. function init(&$view, &$options) {
  7. parent::init($view, $options);
  8. if (!empty($options['link_to_file'])) {
  9. $this->additional_fields['fid'] = 'fid';
  10. }
  11. }
  12. function option_definition() {
  13. $options = parent::option_definition();
  14. $options['link_to_file'] = array('default' => FALSE);
  15. return $options;
  16. }
  17. function options_form(&$form, &$form_state) {
  18. parent::options_form($form, $form_state);
  19. $form['link_to_file'] = array(
  20. '#title' => t('Link this field to download the file'),
  21. '#type' => 'checkbox',
  22. '#default_value' => !empty($this->options['link_to_file']),
  23. );
  24. }
  25. function pre_render($values) {
  26. if (empty($this->options['link_to_file'])) {
  27. return;
  28. }
  29. $fids = array();
  30. $this->items = array();
  31. $data = array();
  32. foreach ($values as $result) {
  33. if ($result->{$this->aliases['fid']}) {
  34. $fids[] = $result->{$this->aliases['fid']};
  35. }
  36. }
  37. if ($fids) {
  38. $result = db_query("SELECT f.fid, f.filepath FROM {files} f WHERE f.fid IN (" . implode(', ', $fids) . ")");
  39. while ($file = db_fetch_object($result)) {
  40. $this->items[$file->fid] = $file;
  41. }
  42. }
  43. }
  44. function render($values) {
  45. return $this->render_link(check_plain($values->{$this->field_alias}), $values);
  46. }
  47. /**
  48. * Render whatever the data is as a link to the file.
  49. *
  50. * Data should be made XSS safe prior to calling this function.
  51. */
  52. function render_link($data, $value) {
  53. if (!empty($this->options['link_to_file']) && $value->{$this->aliases['fid']} && $data !== NULL && $data !== '') {
  54. $values = $this->items[$value->{$this->aliases['fid']}];
  55. $this->options['alter']['make_link'] = TRUE;
  56. $this->options['alter']['path'] = file_create_url($values->filepath);
  57. }
  58. else {
  59. $this->options['alter']['make_link'] = FALSE;
  60. }
  61. return $data;
  62. }
  63. }