View source
<?php
include_once 'commons_media.features.inc';
include_once 'commons_media.private_files.inc';
function commons_media_system_info_alter(&$info, $file, $type) {
if ($file->name == 'commons_media') {
$entity_integrations = commons_entity_integration_info();
if (!empty($entity_integrations)) {
foreach ($entity_integrations as $entity_type => $bundles) {
foreach ($bundles as $bundle => $integrations) {
if (!empty($integrations['media'])) {
$info['features']['field_instance'][] = "{$entity_type}-{$bundle}-field_media";
}
}
}
}
}
$commons_entity_integrations =& drupal_static(__FUNCTION__);
if (!isset($commons_entity_integrations)) {
foreach (module_implements('commons_entity_integration') as $module) {
$commons_entity_integrations[$module] = call_user_func($module . '_commons_entity_integration');
}
}
if (isset($commons_entity_integrations[$file->name])) {
foreach ($commons_entity_integrations[$file->name] as $entity_type => $integration) {
foreach ($integration as $bundle => $options) {
if (commons_media_has_media_integration($entity_type, $bundle)) {
$info['features_exclude']['dependencies']['commons_media'] = 'commons_media';
}
}
}
}
}
function commons_media_has_media_integration($entity_type, $bundle) {
$commons_media_entity_types = commons_entity_integration_info();
return isset($commons_media_entity_types[$entity_type][$bundle]['media']) ? $commons_media_entity_types[$entity_type][$bundle]['media'] : FALSE;
}
function commons_media_menu_alter(&$items) {
if (isset($items['file/ajax'])) {
$items['file/ajax']['page callback'] = 'commons_ajax_upload';
}
if (isset($items['media/ajax'])) {
$items['media/ajax']['page callback'] = 'commons_ajax_upload';
}
}
function commons_ajax_upload() {
$form_parents = func_get_args();
$form_build_id = (string) array_pop($form_parents);
$form_parents = array_filter($form_parents, 'element_child');
if (empty($_POST['form_build_id'])) {
drupal_set_message(t('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (@size) that this server supports.', array(
'@size' => format_size(file_upload_max_size()),
)), 'error');
$commands = array();
$commands[] = ajax_command_replace(NULL, theme('status_messages'));
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}
list($form, $form_state, $form_id, $form_build_id, $commands) = ajax_get_form();
if (!$form) {
drupal_set_message(t('An unrecoverable error occurred. Use of this form has expired. Try reloading the page and submitting again.'), 'error');
$commands = array();
$commands[] = ajax_command_replace(NULL, theme('status_messages'));
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}
$current_element = $form;
foreach ($form_parents as $parent) {
$current_element = $current_element[$parent];
}
$current_file_count = isset($current_element['#file_upload_delta']) ? $current_element['#file_upload_delta'] : 0;
drupal_process_form($form['#form_id'], $form, $form_state);
foreach ($form_parents as $parent) {
$form = $form[$parent];
}
if (isset($form['#file_upload_delta']) && $current_file_count < $form['#file_upload_delta']) {
$form[$current_file_count]['#attributes']['class'][] = 'ajax-new-content';
}
else {
$form['#suffix'] .= '<span class="ajax-new-content"></span>';
}
$output = theme('status_messages') . drupal_render($form);
$js = drupal_add_js();
$settings = call_user_func_array('array_merge_recursive', $js['settings']['data']);
$commands[] = ajax_command_replace(NULL, $output, $settings);
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}
function commons_media_element_info_alter(&$type) {
$type['media']['#process'][] = 'commons_media_simplify_media_buttons';
}
function commons_media_simplify_media_buttons($element, $edit, &$form_state) {
$element['#title_display'] = 'invisible';
return $element;
}
function commons_media_page_build(&$page) {
$page['page_bottom']['commons_media']['#attached']['css'] = array(
drupal_get_path('module', 'commons_media') . '/commons_media.css' => array(
'every_page' => TRUE,
),
);
}
function commons_media_filter_default_formats_alter(&$formats) {
if (!empty($formats['filtered_html'])) {
if (module_exists('entity_embed')) {
$formats['filtered_html']['cache'] = 0;
$formats['filtered_html']['filters']['filter_html']['settings']['allowed_html'] .= ' <drupal-entity>';
$formats['filtered_html']['filters'] += array(
'entity_embed' => array(
'weight' => 2,
'status' => 1,
'settings' => array(),
),
);
}
if (module_exists('media_wysiwyg')) {
$formats['filtered_html']['filters'] += array(
'media_filter' => array(
'status' => 1,
'weight' => 5,
),
);
}
}
}
function commons_media_ckeditor_profile_defaults_alter(&$profiles) {
if (module_exists('entity_embed') && !empty($profiles['Advanced'])) {
$profiles['Advanced']['settings']['allowed_content'] = 'f';
$profiles['Advanced']['settings']['toolbar'] = '[
[\'Format\'],
[\'Bold\',\'Italic\',\'Strike\'],
[\'NumberedList\',\'BulletedList\',\'Indent\',\'Outdent\',\'Blockquote\'],
[\'Link\',\'Unlink\',\'file\']
]';
$profiles['Advanced']['settings']['loadPlugins'] += array(
'drupalentity' => array(
'name' => 'drupalentity',
'desc' => 'Plugin for embedding entities',
'path' => '/profiles/commons/modules/contrib/entity_embed/js/plugins/drupalentity/',
'default' => 'f',
'buttons' => array(
'file' => array(
'label' => 'File',
'icon' => 'entity.png',
),
'node' => array(
'label' => 'Content',
'icon' => 'entity.png',
),
),
),
);
}
}
function commons_media_form_file_entity_add_upload_alter(&$form, &$form_state, $form_id) {
if ($form['#step'] == 1) {
$form['#attached']['js'][] = drupal_get_path('module', 'commons_media') . '/commons_media_autoupload.js';
}
}
function commons_media_form_media_internet_add_alter(&$form, &$form_state, $form_id) {
if (isset($form['embed_code'])) {
$form['embed_code']['#description'] = t('Enter a URL or link to the media you would like to embed, such as a video or image. You can upload video from a range of providers, such as YouTube, Flickr or Vimeo.');
}
}
function commons_media_media_wysiwyg_format_form_prepare_alter(&$form, &$form_state, $media) {
$form['options']['fields']['#access'] = FALSE;
$form['#attached']['js'][] = drupal_get_path('module', 'commons_media') . '/commons_media_autoembed.js';
}
function commons_media_entity_presave($entity, $type) {
if (!variable_get('file_private_path', FALSE)) {
return;
}
list($entity_id, $entity_vid, $entity_bundle) = entity_extract_ids($type, $entity);
if (!og_is_group_content_type($type, $entity_bundle)) {
return;
}
foreach (_commons_media_get_file_and_image_fields($entity, $type) as $field_name) {
foreach (array_keys($entity->{$field_name}) as $langcode) {
$items = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();
foreach ($items as $delta => $item) {
if (isset($entity->group_content_access[LANGUAGE_NONE][0]['value']) && $entity->group_content_access[LANGUAGE_NONE][0]['value'] == 2) {
$source_file = file_load($item['fid']);
if (file_uri_scheme($source_file->uri) == 'public') {
$usage = file_usage_list($source_file);
if (empty($usage)) {
$scheme = 'private://';
$uri = file_stream_wrapper_uri_normalize($scheme . $source_file->filename);
file_move($source_file, $uri, FILE_EXISTS_RENAME);
}
}
}
}
}
}
}
function _commons_media_get_file_and_image_fields($entity, $type) {
$collected_fields = array();
list(, , $bundle) = entity_extract_ids($type, $entity);
$instances = field_info_instances($type, $bundle);
$fields = field_info_fields();
foreach ($instances as $instance) {
$field_name = $instance['field_name'];
if ($fields[$field_name]['type'] == 'file' || $fields[$field_name]['type'] == 'image') {
if (isset($entity->{$field_name})) {
$collected_fields[] = $field_name;
}
}
}
return $collected_fields;
}