/[drupal]/contributions/modules/flexinode/field_file.inc
ViewVC logotype

Contents of /contributions/modules/flexinode/field_file.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.14 - (show annotations) (download) (as text)
Sat Jan 27 15:02:23 2007 UTC (2 years, 10 months ago) by ber
Branch: MAIN
CVS Tags: DRUPAL-4-7--0-1, HEAD
Changes since 1.13: +11 -3 lines
File MIME type: text/x-php
Added nessecary file validation.
Fixed file storage on _insert
1 <?php
2 // $Id: field_file.inc,v 1.13 2005/11/10 09:35:16 crunchywelch Exp $
3
4 function flexinode_field_file_name($field) {
5 return t('file');
6 }
7
8 function flexinode_field_file_form($field, $node) {
9 $fieldname = 'flexinode_'. $field->field_id;
10
11 $form[$fieldname] = array(
12 '#type' => 'file',
13 '#title' => t($field->label),
14 '#description' => ($node->$fieldname ? t('"%filename" has been uploaded. If you upload another file, the current file will be replaced.', array('%filename' => $node->$fieldname->filename)) : '') . t($field->description),
15 '#required' => $field->required,
16 '#weight' => $field->weight,
17 );
18 $form[$fieldname .'_old'] = array(
19 '#type' => 'hidden',
20 '#value' => serialize($node->$fieldname),
21 );
22 return $form;
23 }
24
25 function flexinode_field_file_db_select($field) {
26 $fieldname = 'flexinode_'. $field->field_id;
27 return $fieldname .'.serialized_data AS '. $fieldname;
28 }
29
30 function flexinode_field_file_db_sort_column($field) {
31 return 'flexinode_'. $field->field_id .'.textual_data';
32 }
33
34 function flexinode_field_file_insert($field, $node) {
35 $fieldname = 'flexinode_'. $field->field_id;
36 $node->$fieldname = file_save_upload($node->$fieldname, $node->$fieldname->filename);
37 if (is_object($node->$fieldname)) {
38 $serialized = serialize($node->$fieldname);
39 db_query("INSERT INTO {flexinode_data} (nid, field_id, textual_data, serialized_data) VALUES (%d, %d, '%s', '%s')", $node->nid, $field->field_id, $node->$fieldname->filename, $serialized);
40 }
41 }
42
43 function flexinode_field_file_delete($field, $node, $unconditional = 0) {
44 $fieldname = 'flexinode_'. $field->field_id;
45 $result = db_fetch_object(db_query('SELECT serialized_data FROM {flexinode_data} WHERE nid = %d AND field_id = %d', $node->nid, $field->field_id));
46 $file = unserialize($result->serialized_data);
47 if ($unconditional || $node->$fieldname != $file) {
48 file_delete($file->filepath);
49 }
50 }
51
52 function flexinode_field_file_validate($field, $node) {
53 $fieldname = 'flexinode_'. $field->field_id;
54 return file_check_upload($fieldname);
55 }
56
57 function flexinode_field_file_execute($field, $node) {
58 $fieldname = 'flexinode_'. $field->field_id;
59 if ($file = file_save_upload($fieldname, variable_get('file_directory_path', NULL))) {
60 return $file;
61 }
62 elseif (empty($node->$fieldname)) {
63 return unserialize($node->{$fieldname .'_old'});
64 }
65 }
66
67 function flexinode_field_file_format($field, $node, $brief = 0) {
68 $fieldname = 'flexinode_'. $field->field_id;
69 $file = is_object($node->$fieldname) ? $node->$fieldname : unserialize($node->$fieldname);
70
71 if ($file) {
72 return '<a href="'. file_create_url($file->filepath) .'">'. check_plain($file->filename) .'</a> ('. format_size($file->filesize) .')';
73 }
74 }
75
76 function flexinode_field_file_load($field, $node) {
77 $fieldname = 'flexinode_'. $field->field_id;
78 return unserialize($node->$fieldname);
79 }
80
81
82 /**
83 * @addtogroup themeable
84 * @{
85 */
86
87 /**
88 * Format a file download for display in a node.
89 *
90 * @param field_id
91 * Which field is being displayed (useful when overriding this function
92 * if you want to style one particular field differently).
93 * @param label
94 * The label for the field as displayed on the node form.
95 * @param file
96 * The file that the user has uploaded. This is an object as provided
97 * by file.inc.
98 * @param formatted_value
99 * A download link to the file.
100 */
101 function theme_flexinode_file($field_id, $label, $file, $formatted_value) {
102 $output = theme('form_element', $label, $formatted_value);
103 $output = '<div class="flexinode-file-'. $field_id .'">'. $output .'</div>';
104 return $output;
105 }
106
107 /** @} End of addtogroup themeable */
108
109 ?>

  ViewVC Help
Powered by ViewVC 1.1.2