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

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

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


Revision 1.23 - (show annotations) (download) (as text)
Sat Jan 27 13:43:17 2007 UTC (2 years, 10 months ago) by ber
Branch: MAIN
CVS Tags: HEAD
Changes since 1.22: +4 -4 lines
File MIME type: text/x-php
Reverted a change in the validation code, wich caused the file to never be properly validated.

Also fixed three minor cosmetic code issues.
1 <?php
2 // $Id: field_image.inc,v 1.20 2006/04/26 11:43:34 ber Exp $
3
4 function flexinode_field_image_name($field) {
5 return t('image');
6 }
7
8 function flexinode_field_image_form($field, $node) {
9 $fieldname = 'flexinode_'. $field->field_id;
10
11 if ($node->$fieldname) {
12 $form[$fieldname .'_old'] = array(
13 '#type' => 'hidden',
14 '#value' => serialize($node->$fieldname),
15 );
16 }
17 $form[$fieldname] = array(
18 '#type' => 'file',
19 '#title' => t($field->label),
20 '#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) .' '. t('The file is limited to %kbKB and a resolution of %wxh pixels (width x height).', array('%wxh' => $field->options[1], '%kb' => $field->options[4])),
21 '#required' => $field->required,
22 '#weight' => $field->weight,
23 );
24
25 return $form;
26 }
27
28 function flexinode_field_image_db_select($field) {
29 $fieldname = 'flexinode_'. $field->field_id;
30 return $fieldname .'.serialized_data AS '. $fieldname;
31 }
32
33 function flexinode_field_image_db_sort_column($field) {
34 return 'flexinode_'. $field->field_id .'.textual_data';
35 }
36
37 function flexinode_field_image_insert($field, $node) {
38 $fieldname = 'flexinode_'. $field->field_id;
39 $node->$fieldname = file_save_upload($node->$fieldname, $node->$fieldname->filename);
40 if (is_object($node->$fieldname)) {
41 $node->$fieldname->smallpath = flexinode_field_image_make_smaller($node->$fieldname->filepath, '_sm', $field->options[2]);
42 $node->$fieldname->thumbpath = flexinode_field_image_make_smaller($node->$fieldname->filepath, '_th', $field->options[3]);
43 $serialized = serialize($node->$fieldname);
44 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);
45 }
46 return $node;
47 }
48
49 function flexinode_field_image_make_smaller($path, $add, $size) {
50 list($width, $height) = explode('x', $size);
51 $dest_path = preg_replace('!(\.[^/.]+?)?$!', "$add\\1", $path, 1); // Add $add before extension. Works if there's no extension and with other stupid cases ;)
52
53 if ($size = getimagesize($path)) {
54 if ($size[0] > $width or $size[1] > $height) {
55 image_scale($path, $dest_path, $width, $height);
56 return $dest_path;
57 }
58 return $path;
59 }
60 return NULL;
61 }
62
63 function flexinode_field_image_delete($field, $node, $unconditional = 0) {
64 $fieldname = 'flexinode_'. $field->field_id;
65 $result = db_fetch_object(db_query('SELECT serialized_data FROM {flexinode_data} WHERE nid = %d AND field_id = %d', $node->nid, $field->field_id));
66 $file = unserialize($result->serialized_data);
67 if ($unconditional || $node->$fieldname != $file) {
68 file_delete($file->filepath);
69 file_delete($file->thumbpath);
70 file_delete($file->smallpath);
71 }
72 }
73
74 function flexinode_field_image_validate($field, $node) {
75 $fieldname = 'flexinode_'. $field->field_id;
76 if($file = file_check_upload($fieldname)) {
77 // check that uploaded file is an image, with a maximum file size and maximum height/width
78 if ($size = @getimagesize($file->filepath)) {
79 list($maxwidth, $maxheight) = explode('x', $field->options[1]);
80 if ((!in_array($size[2], array(1, 2, 3)))) {
81 form_set_error($fieldname, t('The uploaded file was not a valid image.'));
82 }
83 elseif (@filesize($file->filepath) > ($field->options[4] * 1000)) {
84 form_set_error($fieldname, t('The uploaded image is too large; the maximum file size is %num kB.', array('%num' => $field->options[4])));
85 }
86 elseif ($size[0] > $maxwidth || $size[1] > $maxheight) {
87 form_set_error($fieldname, t('The uploaded image is too large; the maximum dimensions are %nxn pixels.', array('%nxn' => $field->options[1])));
88 }
89 }
90 else {
91 form_set_error($fieldname, t('The uploaded file was not a valid image.'));
92 }
93 }
94 elseif (!empty($node->$fieldname)) {
95 form_set_error($fieldname, t('The image upload was not successful.'));
96 }
97 }
98
99 function flexinode_field_image_execute($field, $node) {
100 $fieldname = 'flexinode_'. $field->field_id;
101 if ($file = file_save_upload($fieldname, variable_get('file_directory_path', NULL))) {
102 return $file;
103 }
104 elseif(empty($node->$fieldname)) {
105 return unserialize($node->{$fieldname .'_old'});
106 }
107 }
108
109 function flexinode_field_image_format($field, $node, $brief = 0) {
110 $fieldname = 'flexinode_'. $field->field_id;
111 $file = is_object($node->$fieldname) ? $node->$fieldname : unserialize($node->$fieldname);
112 if ($file) {
113 if ($brief) {
114 return '<a href="' . url('node/'. $node->nid) . '"><img alt="'. check_plain($node->title) .'" src="'. file_create_url($file->thumbpath) .'" /></a>';
115 }
116 else {
117 $output = '<img alt="'. check_plain($node->title) .'" src="'. file_create_url($file->smallpath) .'" />';
118 $output .= '<br /><a href="'. file_create_url($file->filepath) .'">'. t("Get original file (%fsKB)", array('%fs' => round(@filesize($file->filepath)/1024))) .'</a>';
119 return $output;
120 }
121 }
122 }
123
124 function flexinode_field_image_load($field, $node) {
125 $fieldname = 'flexinode_'. $field->field_id;
126 return unserialize($node->$fieldname);
127 }
128
129 function flexinode_field_image_config($field) {
130
131 if (!isset($field->options)) {
132 $field->options = array(
133 1 => '800x600',
134 2 => '512x384',
135 3 => '100x100',
136 4 => 100,
137 );
138 }
139
140 $form['options'] = array(
141 '#type' => 'fieldset',
142 '#title' => t('Options'),
143 '#description' => t('Options for the image upload.'),
144 '#tree' => TRUE,
145 );
146 $form['options'][1] = array(
147 '#type' => 'textfield',
148 '#title' => t('Maximum picture dimensions'),
149 '#default_value' => $field->options[1],
150 '#description' => t('Maximum dimensions for pictures. Format: WidthxHeight'),
151 '#required' => TRUE,
152 );
153 $form['options'][2] = array(
154 '#type' => 'textfield',
155 '#title' => t('Preview dimensions'),
156 '#default_value' => $field->options[2],
157 '#description' => t('Dimensions for auto-created preview (scale will be preserved). Format: WidthxHeight'),
158 '#required' => TRUE,
159 );
160 $form['options'][3] = array(
161 '#type' => 'textfield',
162 '#title' => t('Thumbnail dimensions'),
163 '#default_value' => $field->options[3],
164 '#description' => t('Dimensions for auto-created thumbnail (scale will be preserved). Format: WidthxHeight'),
165 '#required' => TRUE,
166 );
167 $form['options'][4] = array(
168 '#type' => 'textfield',
169 '#title' => t('Maximum picture size'),
170 '#default_value' => $field->options[4],
171 '#description' => t('Maximum picture file size, in kB.'),
172 '#required' => TRUE,
173 );
174
175 return $form;
176 }
177
178
179 /**
180 * @addtogroup themeable
181 * @{
182 */
183
184 /**
185 * Format an image for display in a node.
186 *
187 * @param field_id
188 * Which field is being displayed (useful when overriding this function
189 * if you want to style one particular field differently).
190 * @param label
191 * The label for the field as displayed on the node form.
192 * @param file
193 * The file that the user has uploaded. This is an object as provided
194 * by file.inc.
195 * @param formatted_value
196 * The image as an HTML tag.
197 */
198 function theme_flexinode_image($field_id, $label, $file, $formatted_value) {
199 $output = theme('form_element', $label, $formatted_value);
200 $output = '<div class="flexinode-image-'. $field_id .'">'. $output .'</div>';
201 return $output;
202 }
203
204 /** @} End of addtogroup themeable */
205

  ViewVC Help
Powered by ViewVC 1.1.2