/[drupal]/contributions/modules/AudioRecordingField/AudioRecordingField.module
ViewVC logotype

Contents of /contributions/modules/AudioRecordingField/AudioRecordingField.module

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


Revision 1.1 - (show annotations) (download) (as text)
Thu Aug 7 13:43:01 2008 UTC (15 months, 2 weeks ago) by alphaxp
Branch: MAIN
CVS Tags: DRUPAL-6--1-0-BETA1, HEAD
Branch point for: DRUPAL-6--1
File MIME type: text/x-php
First AudioRecordingField commit
1 <?php
2 // $Id$
3
4 /*
5 * Implementation of hook menu
6 * defines the callback the listenUp module would use
7 */
8 function AudioRecordingField_menu() {
9 $items = array();
10 $items['listenUp_file_receive'] = array(
11 'title' => 'listenUp_file_receive',
12 'description' => 'listenUp_file_receive',
13 'page callback' => 'AudioRecordingField_file_receive',
14 'type' => MENU_CALLBACK,
15 'access callback' => true,
16 );
17
18 return $items;
19 }
20
21 /*
22 * listenUp recorder applet callback. This is what happens when
23 * 'send' is pressed in the applet
24 */
25 function AudioRecordingField_file_receive(){
26 header("Cache-control: private");
27 header("Content-Type: text/plain");
28
29 $field_id = $_GET['field_id'];
30
31 //TODO save duration in field
32 $duration = strip_tags($_POST['duration']);
33 $tmp_name = $_FILES['userfile']['tmp_name'];
34 $upload_dir = file_directory_path();
35
36 $c = (file_check_directory($upload_dir,0))? 1:0;
37 print("file folder writeable? $c");
38
39 // TODO: get file extension from real file type
40 $new_name = file_create_filename(time().'.wav');
41 $new_path = $upload_dir.$new_name;
42
43 if($_FILES['usefiles']['error']>0){
44 print("ERROR - error code: ".$_FILES['userfile']['error']."\n");
45 $fid = "NULL";
46 }else{
47 $_FILES['files'] = $_FILES['userfile'];
48
49 $_FILES['files']['name'] = $new_name;
50 foreach($_FILES['files'] as $key=>$value){
51 $_FILES['files'][$key] = array(0 => $value);
52 }
53
54 var_export($_FILES);
55
56 if(!$file = file_save_upload(0,NULL,$upload_dir)){
57 print("ERROR - file_save_upload failed\n");
58 $fid = "NULL";
59 } else{
60 var_export($file);
61 $fid = $file->fid;
62
63 //convert uploaded wav files to mp3 - does'nt work yet
64 /*
65 $ffmpeg_format = 'libmp3lame';
66 $ffmpeg_extension = 'mp3';
67 $ffmpeg_frequency = '44100';
68 $ffmpeg_bitrate = '64k';
69 $ffmpeg_channels = '2';
70 $ffmpeg_mime = 'audio/mpeg';
71 $ffmpeg_path = $field['widget']['ffmpeg_path'] ?
72 $field['widget']['ffmpeg_path'] : '/usr/bin/ffmpeg';
73
74 $ffmpeg_arguments = '-y -i ' . $file->filepath . ' ' .
75 '-f ' . $ffmpeg_extension . ' ' .
76 '-acodec ' . $ffmpeg_format . ' ' .
77 '-ar ' . $ffmpeg_frequency . ' ' .
78 '-ab ' . $ffmpeg_bitrate . ' ' .
79 '-ac ' . $ffmpeg_channels . ' ' .
80 $file->filepath.".mp3";
81
82 var_export($ffmpeg_arguments);
83 // Convert the file to one with the fresh file name.
84 $ffmpeg_results = shell_exec("ffmpeg" . " " . $ffmpeg_arguments);
85
86 */
87
88 print("SUCCESS - File havs been uploaded,\n");
89 }
90 }
91 $js = "document.getElementById('$field_id').value='$fid';";
92 print("CALLJS $js");
93 }
94
95 /**
96 * Implementation of hook_theme().
97 */
98 function AudioRecordingField_theme() {
99 return array(
100 'AudioRecordingField_listenUp' => array(
101 'arguments' => array('element' => NULL),),
102 'AudioRecordingField_formatter_listenup_player' =>
103 array('arguments' => array('element' => NULL)),
104 'AudioRecordingField_formatter_link_to_file' =>
105 array('arguments' => array('element' => NULL)),
106 );
107 }
108
109 /**
110 * Declare information about a field type.
111 *
112 * @return
113 * An array keyed by field type name. Each element of the array is an associative
114 * array with these keys and values:
115 * - "label": The human-readable label for the field type.
116 */
117 function AudioRecordingField_field_info() {
118
119 return array(
120 'AudioRecordingField' => array('label' => 'Audio recorder'),
121 );
122 }
123
124 /**
125 * Implementation of hook_field_settings().
126 */
127 function AudioRecordingField_field_settings($op, $field) {
128 switch ($op) {
129 case 'database columns':
130 // value is audio file fid
131 $columns['value'] = array(
132 'type' => 'int',
133 'length' => 12,
134 'sortable' => FALSE,
135 'not null' => FALSE);
136 return $columns;
137 }
138 }
139
140 /**
141 * Implementation of hook_field().
142 *
143 */
144 function AudioRecordingField_field($op, &$node, $field, &$items, $teaser, $page) {
145
146 switch ($op) {
147 case 'presave':
148 foreach($items as $key=>$value){
149 if(!empty($value['value'])){
150 $file = AudioRecordingField_get_file_object($value['value']);
151
152 // since the applet, and the file receive scripts saved the
153 // new recording for an anonymous user, we need to
154 // update the real uploading user here.
155 if(file_set_status($file,1)){
156 global $user;
157 $file->uid = $user->uid;
158 //update user columb in file table
159 drupal_write_record('files', &$file, array('fid'));
160 }
161 }
162 }
163 break;
164 case 'load':
165 // load file data to field
166 $field_name = $field['field_name'];
167 foreach($items as $key=>$value){
168 $n[$field_name][$key]['value'] = $value['value'];
169 $n[$field_name][$key]['file'] = AudioRecordingField_get_file_object($value['value']);
170 }
171 return $n;
172
173 break;
174 }
175 }
176
177 /**
178 * Implementation of hook_content_is_empty().
179 *
180 *
181 * This function tells the content module whether or not to consider
182 * the $item to be empty. This is used by the content module
183 * to remove empty, non-required values before saving them.
184 */
185 function AudioRecordingField_content_is_empty($item, $field) {
186
187 if (empty($item['value'])) {
188 return TRUE;
189 }
190 return FALSE;
191 }
192
193
194 /**
195 * Implementation of hook_widget_info().
196 */
197 function AudioRecordingField_widget_info() {
198 return array(
199 'AudioRecordingField_listenUp' => array(
200 'label' => t('ListenUp audio recorder'),
201 'field types' => array('AudioRecordingField'),
202 ),
203 );
204 }
205
206 /**
207 * Implementation of hook_widget_settings().
208 *
209 * Handle the parameters for a widget.
210 */
211 function AudioRecordingField_widget_settings($op, $widget) {
212
213 switch ($op) {
214 case 'form':
215 $form = array();
216 $form['recorder'] = array(
217 '#type' => 'fieldset',
218 '#title' => t('ListenUp recorder settings'),
219 );
220 $form['recorder']['recorder_width'] = array(
221 '#type' => 'textfield',
222 '#title' => t('recorder width'),
223 '#default_value' => isset($widget['recorder_width']) ? $widget['recorder_width']:'',
224 '#size' => 8,
225 '#description' => t('ListenUp recorder applet width'),
226 '#required' => true,
227 );
228 $form['recorder']['recorder_height'] = array(
229 '#type' => 'textfield',
230 '#title' => t('recorder height'),
231 '#default_value' => isset($widget['recorder_height']) ? $widget['recorder_height']:'',
232 '#size' => 8,
233 '#description' => t('ListenUp recorder applet width'),
234 '#required' => true,
235 );
236 $form['recorder']['recorder_params'] = array(
237 '#type' => 'textarea',
238 '#title' => t('recorder params'),
239 '#default_value' => isset($widget['recorder_params']) ? $widget['recorder_params']:'',
240 '#size' => 8,
241 '#description' => t('<div>ListenUp recorder applet parameters. Add list of params'.
242 ' in the form <i>parameter_name|value,</i></div>'.
243 '<div> full list of optional parameter can be found '.l('here','http://www.javasonics.com/listenup/docs/player_parameters.html').
244 ' and '.l('here','http://www.javasonics.com/listenup/docs/recorder_parameters.html').'.</div>'),
245 );
246
247 $form['player'] = array(
248 '#type' => 'fieldset',
249 '#title' => t('ListenUp player settings'),
250 );
251 $form['player']['player_width'] = array(
252 '#type' => 'textfield',
253 '#title' => t('player width'),
254 '#default_value' => isset($widget['player_width']) ? $widget['player_width']:'',
255 '#size' => 8,
256 '#description' => t('ListenUp player applet width'),
257 '#required' => true,
258 );
259 $form['player']['player_height'] = array(
260 '#type' => 'textfield',
261 '#title' => t('player height'),
262 '#default_value' => isset($widget['player_height']) ? $widget['player_height']:'',
263 '#size' => 8,
264 '#description' => t('ListenUp player applet width'),
265 '#required' => true,
266 );
267 $form['player']['player_params'] = array(
268 '#type' => 'textarea',
269 '#title' => t('player params'),
270 '#default_value' => isset($widget['player_params']) ? $widget['player_params']:'',
271 '#size' => 8,
272 '#description' => t('<div>ListenUp player applet parameters. Add list of params'.
273 ' in the form <i>parameter_name|value,</i></div>'.
274 '<div> full list of optional parameter can be found '.l('here','http://www.javasonics.com/listenup/docs/player_parameters.html').
275 '.</div>'),
276 );
277
278 return $form;
279 break;
280 case 'validate':
281 if(!is_numeric($widget['recorder_height']) || !is_numeric($widget['recorder_width']) ||
282 $widget['recorder_height']<=0 || $widget['recorder_width']<=0 ||
283 !is_numeric($widget['player_height']) || !is_numeric($widget['player_width']) ||
284 $widget['player_height']<=0 || $widget['player_width']<=0)
285 form_set_error('recorder_dim',t('ListenUp applets height and widths setting must be numeric integers'));
286
287
288 $m = array();
289 preg_match('/((.+\|.+)\n)*(.+\|.+)*/',$widget['recorder_params'],$m);
290 if(strlen($m[0])!=strlen($widget['recorder_params']))
291 form_set_error('recorder_params',t('illegal parameters structure in !name',array('!name' => 'recorder params')));
292
293
294 $m = array();
295 preg_match('/((.+\|.+)\n)*(.+\|.+)*/',$widget['player_params'],$m);
296 if(strlen($m[0])!=strlen($widget['player_params']))
297 form_set_error('player_params',t('illegal parameters structure in !name',array('!name' => 'player params')));
298
299
300 case 'save':
301 $save = array('recorder_width','recorder_height','player_width','player_height','recorder_params','player_params');
302
303 return $save;
304 }
305 }
306
307 /**
308 * Implementation of hook_widget().
309 */
310 function AudioRecordingField_widget(&$form, &$form_state, $field, $items, $delta = 0) {
311 $element = array(
312 '#type' => $field['widget']['type'],
313 '#default_value' => isset($items[$delta]) ? $items[$delta] : '',
314 );
315 return $element;
316 }
317
318
319 /**
320 * Implementation of FAPI hook_elements().
321 *
322 * Any FAPI callbacks needed for individual widgets can be declared here,
323 * and the element will be passed to those callbacks for processing.
324 *
325 * Drupal will automatically theme the element using a theme with
326 * the same name as the hook_elements key.
327 *
328 */
329 function AudioRecordingField_elements() {
330 return array(
331 'AudioRecordingField_listenUp' => array(
332 '#input' => TRUE,
333 '#process' => array('AudioRecordingField_listenUp_process'),
334 ),
335 );
336 }
337
338 /**
339 * Process an individual element.
340 *
341 * Build the form element. When creating a form using FAPI #process,
342 * note that $element['#value'] is already set.
343 *
344 * The $fields array is in $form['#field_info'][$element['#field_name']].
345 */
346 function AudioRecordingField_listenUp_process($element, $edit, $form_state, $form) {
347 $field = $form['#field_info'][$element['#field_name']];
348 $field_key = $element['#columns'][0];
349
350 $element['applet'] = array(
351 '#required' => $element['#required'],
352 '#value' => AudioRecordingField_recorder_applet_marup($element,$field['widget'])
353 );
354
355 // this would be the hidden field the applet would insert the newly
356 // uploaded file fid to.
357 $element[$field_key] = array(
358 '#type' => 'hidden',
359 '#title' => t($field['widget']['label']),
360 '#required' => $element['#required'],
361 '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : "",
362 );
363
364 return $element;
365 }
366
367
368 /**
369 * Implementation of hook_field_formatter_info().
370 */
371 function AudioRecordingField_field_formatter_info() {
372 return array(
373 'listenup_player' => array(
374 'label' => t('ListenUp player'),
375 'field types' => array('AudioRecordingField'),
376 'multiple values' => CONTENT_HANDLE_CORE,
377 ),
378 'link_to_file' => array(
379 'label' => t('Link to file'),
380 'field types' => array('AudioRecordingField'),
381 'multiple values' => CONTENT_HANDLE_CORE,
382 ),
383 );
384 }
385
386 /*
387 * listenUp player formatter theme
388 */
389 function theme_AudioRecordingField_formatter_listenup_player($element){
390 $field = content_fields($element['#field_name']);
391 return AudioRecordingField_player_applet_markup($element,$field['widget']);
392 }
393
394 /*
395 * link to file formatter theme
396 */
397 function theme_AudioRecordingField_formatter_link_to_file($element){
398 $file = $element['#item']['file'];
399
400 $output = "";
401 if(!empty($file)){
402 $url = file_create_url($file->filename);
403 $output = l($file->name,$url,array('attributes' => array('class' => 'AudioRecordingField_item')));
404 }
405 return $output;
406 }
407
408 /*
409 * player applet html markup
410 */
411 function AudioRecordingField_player_applet_markup($element,$widget){
412 $value = $element['#item']['value'];
413 $file = $element['#item']['file'];
414 $url = file_create_url($file->filename);
415
416 $width = isset($widget['player_width'])? $widget['player_width']:300;
417 $height = isset($widget['player_height'])? $widget['player_height']:100 ;
418 $params = $widget['player_params'];
419 $params_str = AudioRecordingField_build_params($params);
420
421 $output = "";
422
423 if(!empty($value)){
424 $output = '<applet '.
425 ' code="com.softsynth.javasonics.recplay.PlayerApplet"'.
426 ' codebase="'.base_path().drupal_get_path('module','AudioRecordingField').'/codebase"'.
427 ' archive="JavaSonicsListenUp.jar"'.
428 ' name="ListenUpRecorder"'.
429 ' width="'.$width.'"'.
430 ' height="'.$height.'">'.
431 ' <param name="sampleURL" value="'.$url.'">'.$params_str.
432 ' </applet>';
433 }
434 return $output;
435 }
436
437 /*
438 * listenUp recorder applet
439 */
440 function AudioRecordingField_recorder_applet_marup($element,$widget){
441 $field_id = $element['#id']."-value";
442 $url = url('listenUp_file_receive',array('query'=>array('field_id'=>$field_id)));
443 $width = isset($widget['player_width'])?$widget['recorder_width']:300;
444 $height = isset($widget['player_height'])? $widget['recorder_height']:100;
445 $params = $widget['recorder_params'];
446 $params_str = AudioRecordingField_build_params($params);
447
448
449 $sampleURL = "";
450 if(!empty($element['#value']['value'])){
451 $file = $element['#value']['file'];
452 $surl = file_create_url($file->filename);
453 $sampleURL = "<param name='sampleURL' value='".$surl."'>";
454 }
455
456 $output = '<applet '.
457 ' code="com.softsynth.javasonics.recplay.RecorderUploadApplet"'.
458 ' codebase="'.base_path().drupal_get_path('module','AudioRecordingField').'/codebase"'.
459 ' archive="JavaSonicsListenUp.jar,OggXiphSpeexJS.jar"'.
460 ' name="ListenUpRecorder"'.
461 ' mayscript="true"'.
462 ' width="'.$width.'"'.
463 ' height="'.$height.'">'.
464 $sampleURL.
465 ' <param name="uploadURL" value="'.$url.'">'.
466 ' <param name="uploadFileName" value="whatever.wav">'.$params_str.
467 ' </applet>';
468
469 return $output;
470 }
471
472 /**
473 * FAPI theme for an individual elements.
474 *
475 * $element['#field_name'] contains the field name
476 * $element['#delta] is the position of this element in the group
477 */
478 function theme_AudioRecordingField_listenUp($element) {
479 return $element['#children'];
480 }
481
482 /*
483 * retrives a file object from the db.
484 */
485 function AudioRecordingField_get_file_object($fid){
486 $sql = "SELECT * FROM {files} f WHERE f.fid='".$fid."'";
487 $res = db_query($sql);
488 return db_fetch_object($res);
489 }
490
491 /*
492 * parse a "name|value" string to a string of applet parameters tags
493 */
494 function AudioRecordingField_build_params($params){
495 $params_arr = array();
496 $params_str = "";
497 if(preg_match_all('/[\w\d_]+\|[\w\d_]+/',$params,$params_arr)){
498
499 foreach($params_arr[0] as $val){
500 $pair = explode('|',$val);
501 if(count($pair) ==2)
502 $params_str.= '<param name="'.$pair[0].'" value="'.$pair[1].'">';
503 }
504 }
505
506 return $params_str;
507 }

  ViewVC Help
Powered by ViewVC 1.1.2