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

Contents of /contributions/modules/media/media.module

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


Revision 1.11 - (show annotations) (download) (as text)
Tue Jul 21 04:34:45 2009 UTC (4 months, 1 week ago) by jmstacey
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-7--1
Changes since 1.10: +87 -74 lines
File MIME type: text/x-php
Some blind styling cleanup in preparation for work.
1 <?php
2 // $Id: media.module,v 1.10 2009/07/21 04:05:06 jmstacey Exp $
3
4 /**
5 * @file
6 * Media API
7 *
8 * The core Media API.
9 * The Media module provides a drop-in replacement for Drupal's Upload,
10 * FileField, Embedded Media Field, and other similar forms. It offers an API
11 * and hooks available for other modules to implement, allowing for customized
12 * file lists, tabs, drawers, and forms to the new Upload form.
13 */
14
15 /* ***************************************** */
16 /* CONSTANTS */
17 /* ***************************************** */
18
19 define('MEDIA_RESOURCE_URI_DEFAULT', 'public://');
20 define('MEDIA_TYPES_DEFAULT', '*');
21
22 /* ***************************************** */
23 /* DRUPAL API FUNCTIONS */
24 /* ***************************************** */
25
26 /**
27 * Implementation of hook_menu().
28 */
29 function media_menu() {
30 // AJAX formatter. This page is used to create the formatter form
31 // when adding a new file, after selecting a file and pressing 'Add'.
32 $items['media/js'] = array(
33 'page callback' => 'media_ahah_formatter_load',
34 'access arguments' => array('access content'),
35 );
36 $items['media/metadata/js'] = array(
37 'page callback' => 'media_ahah_metadata_ahah',
38 'access arguments' => array('access content'),
39 );
40
41 // Default settings, for content types that do not have their own.
42 $items['admin/settings/media'] = array(
43 'title' => 'Media settings',
44 'description' => 'Configure Global Media settings, including default content type settings.',
45 'page callback' => 'drupal_get_form',
46 'page arguments' => array('media_settings_global'),
47 'access arguments' => array('administer media'),
48 'weight' => 3,
49 );
50
51 return $items;
52 }
53
54 /**
55 * Implementation of hook_form_alter().
56 * @param $form
57 * @param $form_state
58 * @param $form_id
59 */
60 function media_form_alter(&$form, $form_state, $form_id) {
61 global $user;
62
63 // Load content-type settings
64 if ($form_id == 'node_type_form') {
65 include_once('media_settings.inc');
66 media_settings_content_type($form, $form['#node_type']->type);
67 }
68
69 // Add the media browser on the node add/edit screen.
70 if (strstr($form_id, 'node_form') ) {
71 // Is the media browser enabled on this node type? Type-specific options override the default.
72 if (variable_get('media_'. $form['type']['#value'] .'_override', NULL) !== NULL) {
73 $enabled = variable_get('media_'. $form['type']['#value'] .'_enabled', NULL);
74 }
75 else {
76 $enabled = variable_get('media_global_enabled', TRUE);
77 }
78 if ($enabled) {
79 // Get the fields we need to enable on this module.
80 $fields = media_active_fields_for_node_type($form['type']['#value']);
81
82 // Iterate through each field and add a browser form.
83 foreach ($fields as $field => $registration_ids) {
84
85 // Add the media browser form.
86 $form[$field]['media'] = media_build_browser_form($form_state, $registration_ids, $form['type']['#value'], $field, $user->uid);
87
88 // Add .media and .replace class to the field CSS class attributes.
89 $form[$field][0]['#attributes'] = array('class' => 'media replace');
90 }
91 }
92 }
93 }
94
95
96 /**
97 * Implements hook_theme().
98 * Register theming functions
99 *
100 * @return array
101 */
102 function media_theme() {
103 return array(
104 // The media file browser form.
105 'media_file_browser' => array(
106 'arguments' => array('element' => NULL),
107 ),
108
109 // The default media file list form element.
110 'media_file_list' => array(
111 'arguments' => array('element' => NULL),
112 ),
113
114 // The media browser pane.
115 'media_browser_pane' => array(
116 'arguments' => array('form' => array()),
117 ),
118 );
119 }
120
121
122 /* ***************************************** */
123 /* Media hook calls */
124 /* ***************************************** */
125
126 /**
127 * Gets all of the modules which register with Media.
128 *
129 * @param array $ids
130 * (Optional) If this contains an array of id strings, then return only the
131 * specified ids.
132 * @param boolean $reset
133 * (Optional) If TRUE, then reset the static cache.
134 * @return array
135 * An array of registrations, keyed by implementing function name, in the
136 * form of:
137 * 'module' => The module implementing the hook.
138 * 'uri' => The scheme the stream wrapper handles, default is 'public://'.
139 * 'types' => The mime types this module handles, defaults to * (all).
140 * 'name' => A human readable name, displayed on forms.
141 * 'kind' => Kind of functionality: 'resource' or 'format'.
142 * 'description' => A verbose description of functionality.
143 * 'callbacks' => An array of key => functions called for data.
144 * 'fields' => What fields does this functionality operate on?
145 * 'validation' => @todo
146 * 'display' => @todo
147 */
148 function media_get_registered_modules($ids = NULL, $reset = FALSE) {
149 static $registrations;
150
151 // Only build cache the first time the function is called, or if we reset it.
152 if (is_NULL($registrations) || $reset) {
153 $registrations = array();
154 // Get all the modules which implement hook_media_register().
155 foreach (module_implements('media_register') as $module) {
156 // Get all the registrations defined by the module.
157 $function = $module .'_media_register';
158 $registration = $function();
159
160 // TODO: Fix this. Why are we calling $function twice (jmstacey 6/2/09)?
161
162 // Iterate through each registration defined by the implementing module.
163 foreach (array_keys($function()) as $key) {
164 // Add the module name to each registration.
165 // Default the 'module' key of the registration to the implementing module.
166 $registration[$key]['module'] = $registration[$key]['module'] ? $registration[$key]['module'] : $module;
167
168 // translate strings now
169 $registration[$key]['name'] = t($registration[$key]['name']);
170 $registration[$key]['description'] = t($registration[$key]['description']);
171
172 // Default the URI to public://.
173 if (!$registration[$key]['uri']) {
174 $registration[$key]['uri'] = MEDIA_RESOURCE_URI_DEFAULT;
175 }
176
177 // Default 'types' to * (all).
178 if (!$registration[$key]['types']) {
179 $registration[$key]['types'] = MEDIA_TYPES_DEFAULT;
180 }
181 }
182
183 // Add the new registrations to our total.
184 $registrations = array_merge($registrations, $registration);
185 }
186 }
187
188 // Return requested registrations.
189 if ($ids) {
190 foreach ($ids as $id) {
191 $return[$id] = $registrations[$id];
192 }
193 return $return;
194 }
195
196 return $registrations;
197 }
198
199
200 /* ***************************************** */
201 /* Media API Functions */
202 /* ***************************************** */
203
204 /**
205 * Build a list of possible registration types.
206 * 'resource' handles building file resource lists.
207 * 'formatter' displays a list of file resources for the file browser.
208 * 'attach' passes an FID to the registered module for handling.
209 *
210 * @return array
211 */
212 function media_registration_kinds() {
213 return array('resource', 'formatter');
214 }
215
216 /**
217 * Get all fields that can be enabled on a field type.
218 *
219 * @param string $field_type
220 * The field type to get items for.
221 * @param string $function_type
222 * The kind of functionality being looked for.
223 * @return array
224 * Array of full registration objects.
225 */
226 function media_get_fields($field_type, $function_type = 'resource') {
227 static $data;
228
229 // Do we have cached version?
230 if ($data[$field_type][$function_type]) {
231 return $data[$field_type][$function_type];
232 }
233
234 $data = array();
235 // Get all the registered modules.
236 foreach (media_get_registered_modules() as $id => $registration) {
237 // Check to see if this registration supports this function type.
238 if ($registration['kind'] == $function_type) {
239 // Now look for the fields.
240 if ($registration['fields']) {
241 foreach ($registration['fields'] as $field) {
242 // If this registration supports this field type, add it to the
243 // returned array.
244 if ($field == $field_type) {
245 $data[$field_type][$function_type][$id] = $registration;
246 }
247 }
248 }
249 }
250 }
251
252 return $data[$field_type][$function_type];
253 }
254
255 /**
256 * Select registrations for use by matching against uri and file extension.
257 *
258 * @param array $registrations
259 * Array of extensions.
260 * @param string $uri
261 * The kind of uri being used.
262 * @param string $file_extension
263 * The current file extension.
264 * @return array
265 * An array of applicable formatters.
266 */
267 function media_get_applicable_formatters($registrations, $file_extension) {
268 // Do we have a file extension?
269 if ($file_extension) {
270 foreach ($registrations as $id => $formatter) {
271 // Does this formatter use any file type? If not, then we have to dig.
272 if (!$formatter['types'] == MEDIA_TYPES_DEFAULT) {
273 // We need to see if this file type is supported specifically.
274 if (!in_array($file_extension, $registration['types'])) {
275 // This registration is not useful.
276 unset($registrations[$id]);
277 }
278 }
279 }
280 }
281
282 return $registrations;
283 }
284
285
286 /* ***************************************** */
287 /* Media Internal Functions */
288 /* ***************************************** */
289
290 /**
291 * Parsing function for the registrations to hand back the kinds of modules
292 * registering. Used to select all formatters, resources, etc.
293 *
294 * @param string $kinds
295 * Return all the matching registrations of this kind.
296 * @return array
297 * An array of matching registrations.
298 */
299 function media_get_registration_kinds($kind = NULL) {
300 $kinds = array();
301 // Get the registered modules.
302 $registrations = media_get_registered_modules();
303 // Parse the registrations.
304 foreach ($registrations as $id => $registration) {
305 if ($kind) {
306 // Get the kind that is being looked for.
307 if ($registration['kind'][$kind]) {
308 $kinds[$id] = $registration;
309 }
310 }
311 else {
312 $kinds[$id] = $registration;
313 }
314 }
315
316 return $kinds;
317 }
318
319 /**
320 * Return a set of formatters which can format the specified item.
321 *
322 * If $description is NULL, all formatters will be returned. A set of
323 * registered modules can be passed in to narrow the formatter options.
324 *
325 * @param string $description
326 * File extension to return.
327 * @param array $registrations
328 * (Optional) If specified, then match only against these registrations.
329 * @return array
330 * An array of formatter ids keyed by module.
331 */
332 function media_registration_item_formatters($description = '*', $registrations = NULL) {
333 $formatters = array();
334 // Get all the registrations if we don't have any.
335 if (is_NULL($registrations)) {
336 $registrations = media_get_registered_modules();
337 }
338 // Iterate through each of the registered modules and find the formatters.
339 foreach ($registrations as $id => $registration) {
340 // Look for the formatter, or just take all (wildcard being *).
341 if ((is_array($registration['kind']['formatter']['types']) && in_array($registration['kind']['formatter']['types'], $description))
342 || $description == '*'
343 || $registration['kind']['formatter']['types'] == '*') {
344 $formatters[$registration['module']] = $id;
345 }
346 }
347
348 return $formatters;
349 }
350
351 /**
352 * Parsing function for the registrations to hand back the kinds of modules
353 * registering.
354 *
355 * @param string $type
356 * Only hand back data for the specified type.
357 * @return array
358 * @TODO: finish this function.
359 */
360 function media_registration_types($type = NULL) {
361 // get the registered modules
362 $registrations = media_get_registered_modules();
363 // parse the registrations
364 foreach ($registrations as $registration) {
365 // @TODO
366 }
367
368 // return
369 }
370
371 /**
372 * Parsing function for the registrations to hand back the kinds of modules
373 * registering.
374 *
375 * @param string $array
376 * Name of the element we want to get data from.
377 * @return array
378 */
379 function media_registration_data($name) {
380 $data = array();
381
382 // Get the registered modules.
383 $registrations = media_get_registered_modules();
384
385 // Parse the registrations.
386 foreach ($registrations as $id => $registration) {
387 // Do we have this data in this registration?
388 if ($registration[$name]) {
389 // Get the item that was requested.
390 $data[$registration['module']] = array(
391 $id => array(
392 $name => $registration[$name],
393 'description' => $registration['description']
394 )
395 );
396 }
397 }
398
399 return $data;
400 }
401
402
403 /**
404 * Fetch all resources registered and call the specified callback. I
405 *
406 * Also add item to the media browser as a horizontal tab.
407 *
408 * @TODO Implement admin weighting here somehow.
409 *
410 * @param array $registration_ids
411 * Array of registration ids to be loaded.
412 * @param string $node_type
413 * Drupal node type.
414 * @param field $field
415 * CCK field name.
416 * @param int $uid
417 * Drupal {user} id.
418 * @return array
419 */
420 function media_get_resources($registration_ids, $node_type, $field, $uid) {
421 // Get all the registrations that define the resources.
422 $registrations = media_get_registered_modules($registration_ids);
423
424 foreach ($registrations as $id => $registration) {
425 // Get the callback function.
426 $function = $registration['callbacks']['resource'];
427
428 if (function_exists($function)) {
429 // Get the results of the callback function.
430 $item = $function($node_type, $field, $uid);
431 $tab_name = check_plain(key($item));
432
433 // Add a resource_id to the item.
434 $item[$tab_name][key($item[$tab_name])]['resource_id'] = array(
435 '#type' => 'value',
436 '#value' => $id,
437 );
438
439 // Add a resource module to the item so that we don't have to figure it out later
440 $item[$tab_name][key($item[$tab_name])]['registered_module'] = array(
441 '#type' => 'value',
442 '#value' => $registration['module'],
443 );
444
445 // Add tabs under the tab name.
446 $items[$tab_name][key($item[$tab_name])] = $item[$tab_name][key($item[$tab_name])];
447 }
448
449 }
450
451 return $items;
452 }
453
454 /**
455 * Get a list of fields for the requested node type.
456 *
457 * @param string $type_name
458 * Drupal {node} type.
459 * @param string $function
460 * Either 'resource' or 'formatter'.
461 * @return array
462 * An array of field names.
463 */
464 function media_active_fields_for_node_type($type_name, $function = 'resource') {
465 // Need to know if the type-specific or global settings are going to be used.
466 // At least one of them is enabled at this point.
467 $type_override = variable_get('media_'. $type_name .'_override', FALSE);
468
469 $items = array();
470 $type = _media_content_field_types($type_name);
471
472 // extract the fields for this node type
473 foreach ((array)$type['fields'] as $field_name => $field) {
474 // Ignore the content-type specific per-field setting unless the override is set
475 $fields_enabled = FALSE;
476
477 if ($type_override) {
478 $fields_enabled = variable_get('media_'. $type_name .'_'. $field['field_name'] .'_'. $function, FALSE);
479 }
480 else {
481 //@TODO: Right now, if it's using the default it will be enabled for all fields.
482 // Eventually it will probably be better to allow per-field controls at a global level as well.
483 // However, this line will probably just work once the actual variables exist.
484 $fields_enabled = variable_get('media_global_'. $field['field_name'] .'_'. $function, 'default');
485
486 // Handle the default special, because we want the default to be on for all fields but don't know what the fields are yet.
487 if ($fields_enabled === 'default') {
488 $fields_enabled = array();
489 foreach (media_registration_kinds() as $kind) {
490 // get all the kinds that match this field
491 if ($registrations = media_get_fields($field['type'], $kind)) {
492 foreach ($registrations as $id => $registration) {
493 $compound_id = $field['field_name'] .'--'. $id;
494 $fields_enabled[$compound_id] = $compound_id;
495 }
496 }
497 }
498 }
499 }
500
501 if (!empty($fields_enabled)) {
502 $items[] = $fields_enabled;
503 }
504 }
505
506 $data = array();
507 foreach ($items as $item) {
508 foreach ($item as $id => $value) {
509 // we need to split the $id into $field_name and $media registration id
510 if ($value) {
511 $id = explode('--', $id);
512 $data[$id[0]][] = $id[1];
513 }
514 }
515 }
516
517 return $data;
518 }
519
520 /**
521 * Sanitize the incoming name to be used for a html #id.
522 *
523 * @param string $drawer_name
524 * @return string
525 */
526 function media_create_id($drawer_name) {
527 return str_replace(array(' ', "'", '"', "%", "<", ">"), '', $drawer_name);
528 }
529
530 /**
531 * Get a list of content field types.
532 *
533 * @param string $type_name
534 * A string representing the content type.
535 * @return array
536 * A form structured array is returned.
537 */
538 function _media_content_field_types($type_name) {
539 if (module_exists('content')) {
540 // Use CCK if available
541 $type = content_types($type_name);
542 }
543 else {
544 // Get the specific content type.
545 $type = (array)node_get_types('type', $type_name);
546 }
547
548 // Recognize the upload/attachment form and create an artificial field.
549 // This should be the only one we ever have to do this for.
550 if (module_exists('upload')) {
551 $type['fields']['attachments'] = array(
552 'field_name' => 'attachments',
553 'type' => 'attachments',
554 'widget' => array(
555 'label' => $type['extra']['attachments']['label'],
556 ),
557 );
558 }
559
560 return $type;
561 }
562
563
564 /* *************************************************** */
565 /* Media forms */
566 /* *************************************************** */
567
568 /**
569 * Build data for the media browser display.
570 *
571 * @TODO Clean this form up and use a form theme function.
572 *
573 * Note: The FAPI docs say a submit element event defaults to 'click',
574 * but as of d6.10, it defaults to 'mousedown', so we need to override.
575 * 'event' => 'click',
576 *
577 * @param array $registration_ids
578 * Array of registrations to call.
579 * @param string $node_type
580 * @param string $field
581 * @param uid $uid
582 * @return array
583 * Drupal FAPI form array.
584 */
585 function media_build_browser_form($form_state, $registration_ids, $node_type, $field, $uid) {
586 static $id;
587
588 // We need a static counter for our form element wrapper.
589 $id += 1;
590
591 // Load our css.
592 $path = drupal_get_path('module', 'media');
593 drupal_add_css($path .'/media.css');
594 // Load our specific js for the file selector
595 drupal_add_js($path .'/javascript/media.js');
596 // Load the md5 library so we can hash the upload filename for use in the meta form.
597 drupal_add_js($path .'/javascript/jquery.md5.js');
598
599 $items = array();
600 $form = array();
601
602 $form['media_browser_activate'] = array(
603 '#type' => 'markup',
604 '#value' => '<div class="media browser activation">'. t('Add files') .'</div>',
605 );
606
607 // We are using a tab form type.
608 $form['media_browser'] = array(
609 '#type' => 'tabset',
610 '#attributes' => array('class' => 'media browser wrapper'),
611 );
612
613 // Get all the active resources
614 $resources = media_get_resources($registration_ids, $node_type, $field, $uid);
615
616 // Store the tab & drawer names for the js form selector.
617 $drawer_options = array();
618
619 // loop through the form and start pulling out the data to
620 // create tabs -> panes -> drawers
621 foreach ($resources as $tab_name => $data) {
622 // create a tab id
623 $tab_id = strtolower(str_replace(' ', '_', $tab_name));
624 // create tab
625 $form['media_browser'][$tab_id] = array(
626 '#type' => 'tabpage',
627 '#title' => $tab_name,
628 '#theme' => 'media_browser_pane',
629 );
630
631 $drawer_options[$tab_id] = $tab_name;
632
633 // build the drawers for this tab
634 $drawer_list = array();
635 $active_drawer = TRUE;
636
637 // check to see if we do have children- we should, but just in case
638 if (is_array($data)) {
639 $drawers = array();
640
641 foreach ($data as $drawer_name => $drawer_data) {
642 // @TODO check drawer access permissions here to make sure we should present this to the user
643
644 // The drawer id needs to have additional data on it to prevent
645 // name space conflicts with ids
646 $drawer_id = strtolower(str_replace(' ', '_', $drawer_name)) .'_display';
647
648 // create a link with a specific id to call
649 $drawers_link = '<a onclick="javascript: return FALSE;" href="#'. $drawer_id .'">'. $drawer_name .'</a>';
650 $drawer_list[] = array('data' => $drawers_link, 'class' => ($active_drawer ? 'active' : '') );
651
652 // add the drawer form element
653 $form['media_browser'][$tab_id][$drawer_name] = $drawer_data;
654
655 // add classes to the drawer display item
656 $form['media_browser'][$tab_id][$drawer_name]['#prefix'] = '<div id="'. $drawer_id .'" class="drawer display '. ($active_drawer ? ' active' : NULL) .'">';
657 $form['media_browser'][$tab_id][$drawer_name]['#suffix'] = '</div>';
658
659 // no longer on the first drawer
660 $active_drawer = FALSE;
661
662 $drawer_options[$tab_id .'|'. $drawer_id] = '- '. $drawer_name;
663 }
664
665 // change the drawers to a list for easer display
666 $form['media_browser'][$tab_id]['drawers'] = array(
667 '#type' => 'markup',
668 '#value' => theme('item_list', $drawer_list, NULL, 'ul', array('class' => 'drawers'))
669 );
670
671 }
672 }
673
674 $form['media_browser']['drawer_select'] = array(
675 '#type' => 'select',
676 '#title' => t('Drawer select'),
677 '#description' => t('Oh bother, you really should have JavaScript enabled, you know...'),
678 '#options' => $drawer_options,
679 '#prefix' => '<div class="media-browser-drawer-select">',
680 '#suffix' => '</div>',
681 );
682
683 // Container for the progress indicator.
684 $form['media_browser']['media_browser_file_progress'] = array(
685 '#prefix' => '<div id="media-browser-file-progress-'. $id .'" class="media-browser-file-progress">',
686 '#suffix' => '</div>',
687 );
688 $form['media_browser']['media_browser_file_progress']['file_progress_message'] = array(
689 '#type' => 'item',
690 '#title' => theme('image', variable_get('media_file_progress_image', $path .'/images/uploading-gradient.gif')) . t('Please wait while your file is attached...'),
691 );
692
693 // AHAH enabled submit button.
694 $form['media_browser']['media_browser_submit'] = array(
695 '#type' => 'submit',
696 '#value' => t('Add file'),
697 '#description' => t("Add the selected file."),
698 '#submit' => array('media_browser_submit'), // If no javascript action.
699 '#validate' => array('media_browser_validate'),
700 '#attributes' => array('class' => 'media-browser-submit'),
701 '#ahah' => array(
702 'path' => 'media/js',
703 'wrapper' => 'media-browser-file-progress-'. $id,
704 'method' => 'replace',
705 'effect' => 'fade',
706 'event' => 'click',
707 ),
708 );
709
710 // Container for the metadata submission message.
711 $form['media_browser']['media_browser_metadata_message'] = array(
712 '#prefix' => '<div id="media-browser-metadata-message-'. $id .'" class="media-browser-metadata-message">',
713 '#suffix' => '</div>',
714 );
715 $form['media_browser']['media_browser_metadata_message']['message'] = array(
716 '#type' => 'item',
717 '#value' => '',
718 );
719
720 $form['media_browser']['media_browser_metadata'] = array(
721 '#type' => 'tabset',
722 '#attributes' => array('class' => 'media-browser-metadata-wrapper'),
723 );
724
725 // @TODO: This all goes in the form creation, actually,
726 // to create our metadata form...
727 // $uri = $form_state['values']['media_files'];
728 // $file_extension = pathinfo($uri, PATHINFO_EXTENSION);
729 //
730 // // Get the file creator for this item.
731 // $file_creator = media_get_registered_modules(array($registration_id));
732 //
733 // // Get the formaters for this node type.
734 // $formatters = media_active_fields_for_node_type($node_type, 'formatter');
735 //
736 // // Get the registrations.
737 // $registrations = media_get_registered_modules($formatters[$field]);
738 //
739 // // Remove any non-applying registrations.
740 // $registrations = media_get_applicable_formatters($registrations, $file_extension);
741 //
742 // // Get all the formatting forms.
743 // $formatter_options = array();
744 // $forms = array();
745 // foreach ($registrations as $id => $registration) {
746 // $formatter_options[$id] = $registration['name'];
747 // $function = $registration['callbacks']['form'];
748 // if (function_exists($function)) {
749 // $forms[$id] = $function($node_type, $field, $file_extension, $uri);
750 // }
751 // }
752
753 // @TODO: This is placeholder only.
754 foreach (array('Video', 'Image', 'Audio', 'PDF') as $mime_type) {
755 $form['media_browser']['media_browser_metadata'][$mime_type] = array(
756 '#type' => 'tabpage',
757 '#title' => $mime_type,
758 );
759 $form['media_browser']['media_browser_metadata'][$mime_type][$mime_type .'_title'] = array(
760 '#type' => 'textfield',
761 '#title' => t('Title'),
762 );
763 }
764
765 // Our AHAH enabled submit button for the metadata.
766 $form['media_browser']['media_browser_metadata_submit'] = array(
767 '#type' => 'submit',
768 '#value' => t('Add metadata'),
769 '#description' => t("Add the selected file."),
770 '#submit' => array('media_browser_metadata_submit'), // If no javascript action.
771 '#validate' => array('media_browser_metadata_validate'),
772 '#attributes' => array('class' => 'media-browser-metadata-submit'),
773 '#ahah' => array(
774 'path' => 'media/metadata/js',
775 'wrapper' => 'media-browser-metadata-message-'. $id,
776 'method' => 'replace',
777 'effect' => 'fade',
778 'event' => 'click',
779 ),
780 );
781
782 // build the tabs into a single form element
783 // @TODO make sure we have children for each tab and remove any that
784 // we don't have data for
785 $form['tabs'] = array(
786 '#type' => 'markup',
787 '#value' => theme('item_list', $tabs, NULL, 'ul', array('class' => 'tabs')),
788 );
789
790 return $form;
791 }
792
793 /**
794 * Display files in a form element.
795 *
796 * This is a generic for other modules to make use of.
797 *
798 * @param array $files
799 * array of (uri => uri, filename => filename, meta => array(key => value))
800 * @param string $title
801 * option title argument
802 * @return array
803 * Form array containing a select list populated with files.
804 */
805 function media_resource_display_user_files_form($files, $title = NULL) {
806 // Pass files into options array.
807 $options = array();
808
809 foreach ($files as $file) {
810 // TODO: Use FID rather than URI
811 $options[$file['uri']] = $file['filename'];
812 }
813
814 // Parse files into form element.
815 $form['media_files'] = array(
816 '#type' => 'select',
817 '#options' => $options,
818 '#title' => $title ? $title : '',
819 '#attributes' => array('class' => 'resource select'),
820 '#size' => variable_get('media_file_list_size', 10),
821 );
822
823 return $form;
824 }
825
826 /**
827 * Display the upload form for the tab.
828 *
829 * @return array
830 * Form array containing a file field.
831 */
832 function media_resource_display_upload_form() {
833 $form['media_upload']['upload'] = array(
834 '#type' => 'file',
835 '#title' => t('Upload your file'),
836 '#size' => 30,
837 '#attributes' => array('class' => 'resource select'),
838 );
839
840 return $form;
841 }
842
843
844 /* ***************************************** */
845 /* Media Hook Implementations */
846 /* ***************************************** */
847
848 /**
849 * Implementation of hook_media_elements().
850 *
851 * A Media File List element is created with the following FAPI:
852 * '#type' => 'media_file_list',
853 * '#options' => $options, // An associative array of filepaths, keyed by FID.
854 * '#title' => $title, // The translated title, displayed in the tab.
855 * '#description' => $description, // A translated description, to be displayed below the title.
856 */
857 function media_elements() {
858 $elements = array();
859
860 $elements['media_file_list'] = array(
861 '#input' => TRUE,
862 '#process' => array('media_file_list_element_process'),
863 '#element_validate' => array('media_file_list_element_validate'),
864 '#submit' => array('media_file_list_element_submit'),
865 );
866
867 return $elements;
868 }
869
870 /* ***************************************** */
871 /* Callbacks */
872 /* ***************************************** */
873
874 /**
875 * Process callback for the media_browser element.
876 *
877 * @param $element
878 * @param $edit
879 * @param $form_state
880 * @param $form
881 * @return array
882 */
883 function media_file_list_element_process($element, $edit, $form_state, $form) {
884 $element['list'] = array(
885 '#type' => 'select',
886 '#options' => $element['#options'],
887 '#size' => variable_get('media_file_list_size', 10),
888 );
889
890 return $element;
891 }
892
893 /**
894 * Submit callback for the media_browser element.
895 *
896 * @param array $form
897 * @param $form_state
898 */
899 function media_file_list_element_submit(&$form, $form_state) {
900 drupal_set_message('Successful submit of the Media File List...');
901 }
902
903 /**
904 * Validate callback for the media_browser element.
905 *
906 * @param $element
907 * @param $form_state
908 */
909 function media_file_list_element_validate($element, $form_state) {
910 drupal_set_message('Successful validation of the Media File List...');
911 return $element;
912 }
913
914 /**
915 * Validate callback for the media_browser metadata.
916 *
917 * @param array $form
918 * @param $form_state
919 */
920 function media_browser_metadata_validate(&$form, $form_state) {
921 drupal_set_message('validating metadata... media_browser_metadata_validate needs to pass it to the registered module.');
922 }
923
924 /**
925 * Submit callback for the media_browser metadata.
926 *
927 * @param array $form
928 * @param $form_state
929 */
930 function media_browser_metadata_submit(&$form, $form_state) {
931 drupal_set_message('media_browser_metadata_submit: last step... we now have metadata, and need to send that info to the registered module w/ the file info.');
932 }
933
934 /**
935 * Validate callback for the media_browser.
936 *
937 * @param array $form
938 * @param $form_state
939 */
940 function media_browser_validate(&$form, $form_state) {
941 drupal_set_message('media_browser_validate is validating... needs to go to registered module from here.');
942 }
943
944 /**
945 * Submit callback for the media_browser.
946 *
947 * @param array $form
948 * @param $form_state
949 */
950 function media_browser_submit(&$form, $form_state) {
951 drupal_set_message(theme('image', variable_get('media_file_progress_success', drupal_get_path('module', 'media') .'/images/check-green-blah.png')) . t('media_browser_submit File attachment: !file successful.', array('!file' => l($form_state['values']['media_files'], $form_state['values']['media_files']))));
952 drupal_set_message('(media_browser_submit will need to actually process the file here, sending it to the original registered module to store. we also need to ensure the file is kept in synch with the metadata & formatter coming up next.)');
953
954 // To consider: Use module_implements to call all modules which implement this hook.
955 // This might allow the modules to determine their own chaining, but at the expense of performance.
956 // Order of operations and state would be an issue...
957 //
958 // Another alternative: Create a mechanism for before and after requirements. Example: a thumbnail creator would register
959 // an action and with the registration provide a list of modules/functions that must run before the action it provides is run.
960 // This would mean that for every action fire we would have to pull all of this together and build an action chain.
961 // That could get horribly messy in the event of a race condition.
962 //
963 // For now just look for the specific module's hook and fire it off.
964 $hook_media_action = $form_state['values']['registered_module'] .'_media_action';
965
966 if (function_exists($hook_media_action)) {
967 $hook_media_action($form, $form_state, 'media_browser_submit');
968 }
969 else {
970 drupal_set_message(t('Media: No action handler found.'), 'error');
971 }
972
973 // dsm($form_state['values']);
974 }

  ViewVC Help
Powered by ViewVC 1.1.2