View source
<?php
require_once dirname(__FILE__) . '/media_gallery.fields.inc';
require_once dirname(__FILE__) . '/fields_rsi_prevention.inc';
define('MEDIA_GALLERY_PAGER_ELEMENT', 1);
function media_gallery_file_view_modes() {
return array(
'media_gallery_thumbnail' => t('Gallery thumbnail'),
'media_gallery_lightbox' => t('Gallery lightbox'),
'media_gallery_detail' => t('Gallery detail'),
'media_gallery_block_thumbnail' => t('Gallery block thumbnail'),
'media_gallery_collection_thumbnail' => t('Gallery collection thumbnail'),
);
}
function media_gallery_menu() {
$items['admin/config/media/galleries'] = array(
'title' => 'Gallery settings',
'description' => 'Configure settings for the "All galleries" page.',
'access arguments' => array(
'administer media galleries',
),
'page callback' => 'media_gallery_admin_settings',
'file' => 'media_gallery.admin.inc',
);
$items['media-gallery/sort/collection/%taxonomy_term/%'] = array(
'title' => 'Gallery sort callback',
'access callback' => 'media_gallery_edit_access_ajax',
'access arguments' => array(
'collection',
3,
4,
),
'page callback' => 'media_gallery_ajax_sort',
'page arguments' => array(
'collection',
3,
),
'file' => 'media_gallery.pages.inc',
);
$items['media-gallery/sort/gallery/%node/%'] = array(
'title' => 'Gallery sort callback',
'access callback' => 'media_gallery_edit_access_ajax',
'access arguments' => array(
'gallery',
3,
4,
),
'page callback' => 'media_gallery_ajax_sort',
'page arguments' => array(
'gallery',
3,
),
'file' => 'media_gallery.pages.inc',
);
$items['media-gallery/detail/%media_gallery_mg_node/%media_gallery_mg_item'] = array(
'page callback' => 'media_gallery_detail_page',
'page arguments' => array(
2,
3,
),
'access callback' => 'media_gallery_view_item_access',
'access arguments' => array(
2,
3,
),
'load arguments' => array(
2,
),
'file' => 'media_gallery.pages.inc',
);
$items['media-gallery/detail/%media_gallery_mg_node/%media_gallery_mg_item/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
'load arguments' => array(
2,
),
);
$items['media-gallery/detail/%media_gallery_mg_node/%media_gallery_mg_item/edit'] = array(
'title' => 'Edit',
'page callback' => 'media_gallery_media_page_edit',
'page arguments' => array(
2,
3,
),
'access callback' => 'media_gallery_edit_item_access',
'access arguments' => array(
2,
3,
),
'load arguments' => array(
2,
),
'weight' => 0,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'file' => 'media_gallery.pages.inc',
);
$items['media-gallery/detail/%media_gallery_mg_node/%media_gallery_mg_item/remove'] = array(
'title' => 'Remove',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'media_gallery_remove_item_form',
2,
3,
),
'access callback' => 'media_gallery_remove_item_access',
'access arguments' => array(
2,
3,
),
'load arguments' => array(
2,
),
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'file' => 'media_gallery.pages.inc',
);
$items['media-gallery/lightbox/%media_gallery_mg_node/%media_gallery_mg_item'] = array(
'page callback' => 'media_gallery_lightbox_page',
'page arguments' => array(
2,
3,
),
'access callback' => 'media_gallery_view_item_access',
'access arguments' => array(
2,
3,
),
'load arguments' => array(
2,
),
'file' => 'media_gallery.pages.inc',
'delivery callback' => 'media_gallery_lightbox_delivery_callback',
);
$items['media-gallery/add-images/%node/%'] = array(
'access callback' => 'media_gallery_edit_access_ajax',
'access arguments' => array(
'gallery',
2,
3,
),
'page callback' => 'media_gallery_add_images',
'page arguments' => array(
2,
),
'file' => 'media_gallery.pages.inc',
);
$items['node/%node/multiedit'] = array(
'title' => 'Edit media',
'page callback' => 'media_gallery_media_page_multiedit',
'page arguments' => array(
1,
),
'access callback' => 'media_gallery_multiedit_access',
'access arguments' => array(
1,
),
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'type' => MENU_LOCAL_TASK,
'file' => 'media_gallery.pages.inc',
);
$items['media/%file/download'] = array(
'title' => 'Download',
'page callback' => 'media_download',
'page arguments' => array(
1,
),
'access callback' => 'media_access',
'access arguments' => array(
'view',
),
'type' => MENU_CALLBACK,
'file' => 'media.pages.inc',
);
return $items;
}
function media_gallery_mg_node_load($nid, $nid2 = NULL) {
return node_load($nid);
}
function media_gallery_mg_item_load($fid, $nid) {
$node = node_load($nid);
if ($node && $node->type == 'media_gallery' && in_array($fid, media_gallery_get_file_ids($node))) {
return file_load($fid);
}
return FALSE;
}
function media_gallery_menu_alter(&$items) {
$items['taxonomy/term/%taxonomy_term']['page callback'] = 'media_gallery_list_galleries';
$items['taxonomy/term/%taxonomy_term']['file'] = 'media_gallery.pages.inc';
$items['taxonomy/term/%taxonomy_term']['module'] = 'media_gallery';
$items['media/%file/delete']['context'] = MENU_CONTEXT_PAGE;
}
function media_gallery_admin_paths() {
$paths = array(
'media-gallery/detail/*/*/edit' => TRUE,
'media-gallery/detail/*/*/remove' => TRUE,
'node/*/multiedit' => TRUE,
);
return $paths;
}
function media_gallery_menu_local_tasks_alter(&$data, $router_item, $root_path) {
if (($node = menu_get_object()) && isset($node->type) && $node->type == 'media_gallery' && !empty($data['tabs'])) {
$tabs =& $data['tabs'][0]['output'];
foreach ($tabs as &$tab) {
if (isset($tab['#link']['path']) && $tab['#link']['path'] == 'node/%/edit') {
$tab['#link']['title'] = t('Edit gallery');
}
}
}
if (($term = menu_get_object('taxonomy_term', 2)) && isset($term->vid) && $term->vid == variable_get('media_gallery_collection_vid')) {
$tabs =& $data['tabs'][0]['output'];
foreach ($tabs as &$tab) {
if (isset($tab['#link']['path']) && $tab['#link']['path'] == 'taxonomy/term/%/edit') {
$tab['#link']['href'] = 'admin/config/media/galleries';
$tab['#link']['title'] = t('Edit all galleries');
}
}
}
}
function media_gallery_node_load($nodes, $types) {
if (in_array('media_gallery', $types)) {
foreach ($nodes as $node) {
if ($node->type == 'media_gallery') {
$node->media_gallery_media_original = $node->media_gallery_media;
}
}
}
}
function media_gallery_view($node, $view_mode) {
if (node_access('update', $node)) {
$node->content['add_media_link'] = array(
'#theme' => 'menu_local_action',
'#link' => array(
'title' => t('Add media'),
'href' => 'media/browser',
'localized_options' => array(
'query' => array(
'render' => 'media-popup',
),
'attributes' => array(
'class' => array(
'media-gallery-add',
'launcher',
),
),
),
),
'#prefix' => '<ul class="field action-links">',
'#suffix' => '</ul>',
);
if (module_exists('overlay')) {
$node->content['add_media_link']['#link']['localized_options']['attributes']['class'][] = 'overlay-exclude';
}
module_load_include('inc', 'media', 'includes/media.browser');
media_attach_browser_js($node->content['add_media_link']);
$node->content['add_media_link']['#attached']['js'][] = drupal_get_path('module', 'media_gallery') . '/media_gallery.addimage.js';
$instance = field_info_instance('node', 'media_gallery_media', $node->type);
$token = drupal_get_token('media_gallery');
$gallery_js_settings = array(
'mediaGalleryAddImagesUrl' => url('media-gallery/add-images/' . $node->nid . '/' . $token),
'mediaGallerySortGalleryUrl' => url('media-gallery/sort/gallery/' . $node->nid . '/' . $token),
'mediaGalleryAllowedMediaTypes' => array_filter($instance['widget']['settings']['allowed_types']),
);
if ($view_mode == 'full') {
drupal_add_css(drupal_get_path('module', 'media_gallery') . '/media_gallery.dragdrop.css');
drupal_add_library('system', 'ui.sortable');
drupal_add_library('system', 'jquery.bbq');
drupal_add_js(drupal_get_path('module', 'media_gallery') . '/media_gallery.dragdrop.js');
drupal_add_js($gallery_js_settings, array(
'type' => 'setting',
));
}
else {
$node->content['add_media_link']['#attached']['js'][] = array(
'type' => 'setting',
'data' => $gallery_js_settings,
);
}
}
if ($view_mode == 'teaser') {
if (!empty($node->media_gallery_media[LANGUAGE_NONE])) {
if (media_access('view')) {
$first_item = array_shift($node->media_gallery_media[LANGUAGE_NONE]);
$node->media_gallery_media[LANGUAGE_NONE] = array(
$first_item,
);
}
else {
$node->media_gallery_media[LANGUAGE_NONE] = array();
}
}
}
elseif ($view_mode == 'full' || $view_mode == 'media_gallery_block') {
$full = $view_mode == 'full' ? TRUE : FALSE;
if (!empty($node->media_gallery_media) && media_access('view')) {
$media = $node->media_gallery_media[LANGUAGE_NONE];
}
else {
$media = array();
}
$columns = $full ? $node->media_gallery_columns[LANGUAGE_NONE][0]['value'] : $node->media_gallery_block_columns[LANGUAGE_NONE][0]['value'];
$rows = $full ? $node->media_gallery_rows[LANGUAGE_NONE][0]['value'] : $node->media_gallery_block_rows[LANGUAGE_NONE][0]['value'];
$number_per_page = $columns * $rows;
$deltas = array_keys($media);
$total = count($deltas);
$page = $full ? pager_default_initialize($total, $number_per_page, MEDIA_GALLERY_PAGER_ELEMENT) : 0;
$min_on_page = $number_per_page * $page;
$max_on_page = $number_per_page * ($page + 1) - 1;
$pre_links = array();
$post_links = array();
foreach ($deltas as $key => $delta) {
$item = $media[$delta];
$fid = _media_gallery_get_media_fid($item);
if ($key < $min_on_page) {
$pre_links[$key] = array(
'title' => '',
'href' => 'media-gallery/detail/' . $node->nid . '/' . $fid,
'attributes' => array(
'class' => array(
'colorbox-supplemental-link pre',
),
),
);
unset($media[$delta]);
}
elseif ($key > $max_on_page) {
$post_links[$key] = array(
'title' => '',
'href' => 'media-gallery/detail/' . $node->nid . '/' . $fid,
'attributes' => array(
'class' => array(
'colorbox-supplemental-link post',
),
),
);
unset($media[$delta]);
}
}
if ($media) {
$node->media_gallery_media[LANGUAGE_NONE] = array_values($media);
}
else {
$node->media_gallery_media[LANGUAGE_NONE] = array();
}
$node->content['colorbox_links_pre'] = array(
'#theme' => 'links',
'#links' => $pre_links,
'#weight' => -20,
'#attributes' => array(
'class' => array(
'colorbox-supplemental-links',
),
),
);
$node->content['colorbox_links_post'] = array(
'#theme' => 'links',
'#links' => $post_links,
'#weight' => 20,
'#attributes' => array(
'class' => array(
'colorbox-supplemental-links',
),
),
);
if ($full) {
$node->content['media_gallery_pager'] = array(
'#theme' => 'pager',
'#element' => MEDIA_GALLERY_PAGER_ELEMENT,
'#weight' => 2000,
);
}
}
return $node;
}
function media_gallery_field_extra_fields() {
$extra['node']['media_gallery'] = array(
'display' => array(
'add_media_link' => array(
'label' => t('"Add media" link'),
'weight' => 1,
),
),
);
return $extra;
}
function media_gallery_edit_access_ajax($type, $item, $token) {
if (!drupal_valid_token($token, 'media_gallery')) {
return FALSE;
}
switch ($type) {
case 'gallery':
return node_access('update', $item);
break;
case 'collection':
return user_access('administer media galleries');
break;
default:
return FALSE;
}
}
function media_gallery_theme() {
return array(
'media_gallery_collection' => array(
'render element' => 'element',
'file' => 'media_gallery.theme.inc',
),
'media_gallery_teaser' => array(
'render element' => 'element',
'file' => 'media_gallery.theme.inc',
),
'media_gallery_media_item_thumbnail' => array(
'render element' => 'element',
'file' => 'media_gallery.theme.inc',
'template' => 'media-gallery-media-item-thumbnail',
),
'media_gallery_media_item_lightbox' => array(
'render element' => 'element',
'file' => 'media_gallery.theme.inc',
),
'media_gallery_media_item_detail' => array(
'render element' => 'element',
'file' => 'media_gallery.theme.inc',
),
'media_gallery_block_thumbnail' => array(
'render element' => 'element',
'file' => 'media_gallery.theme.inc',
),
'media_gallery_collection_thumbnail' => array(
'render element' => 'element',
'file' => 'media_gallery.theme.inc',
),
'media_gallery_download_link' => array(
'variables' => array(
'file' => NULL,
'text' => NULL,
'options' => array(),
),
'file' => 'media_gallery.theme.inc',
),
'media_gallery_meta' => array(
'variables' => array(
'display' => NULL,
'location' => NULL,
'title' => NULL,
'license' => NULL,
'description' => NULL,
),
'file' => 'media_gallery.theme.inc',
),
'media_gallery_item' => array(
'variables' => array(
'image' => NULL,
'link_path' => NULL,
'classes' => NULL,
),
'file' => 'media_gallery.theme.inc',
),
'media_gallery_license' => array(
'variables' => array(
'element' => NULL,
'color' => 'dark',
),
'file' => 'media_gallery.theme.inc',
),
'media_gallery_file_field_inline' => array(
'render element' => 'element',
'file' => 'media_gallery.theme.inc',
),
);
}
function media_gallery_permission() {
return array(
'administer media galleries' => array(
'title' => t('Administer media galleries'),
),
);
}
function media_gallery_node_info() {
return array(
'media_gallery' => array(
'name' => t('Gallery'),
'base' => 'media_gallery',
'description' => t('A flexible gallery of media.'),
'help' => t('Create a gallery of thumbnails including custom display settings. Once your gallery is saved, your media can be added.'),
),
);
}
function media_gallery_update($node) {
if (empty($node->media_gallery_expose_block[LANGUAGE_NONE][0]['value']) && db_table_exists('block')) {
db_delete('block')
->condition('module', 'media_gallery')
->condition('delta', $node->nid)
->execute();
}
}
function media_gallery_block_info() {
$blocks = array();
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node');
$query
->entityCondition('bundle', 'media_gallery');
$query
->fieldCondition('media_gallery_expose_block', 'value', 1);
$result = $query
->execute();
if (!empty($result['node'])) {
$nids = array_keys($result['node']);
$node_titles = db_query("SELECT nid, title FROM {node} WHERE nid IN (:nids)", array(
':nids' => $nids,
))
->fetchAllKeyed();
foreach ($node_titles as $nid => $title) {
$blocks[$nid]['info'] = t('Recent gallery items: !title', array(
'!title' => $title,
));
$blocks[$nid]['visibility'] = 0;
$blocks[$nid]['pages'] = 'node/' . $nid . "\ngalleries";
}
}
return $blocks;
}
function media_gallery_block_view($delta = '') {
$node = node_load($delta);
if (empty($node->media_gallery_expose_block[LANGUAGE_NONE][0]['value'])) {
$block['subject'] = NULL;
$block['content'] = '';
}
elseif (empty($node->media_gallery_media_original) || !media_access('view')) {
$block['subject'] = check_plain($node->title);
$block['content'] = t('No content available.');
}
else {
$node = clone $node;
$node->media_gallery_media = $node->media_gallery_media_original;
$files =& $node->media_gallery_media[LANGUAGE_NONE];
$gallery_fids = array();
foreach ($files as $file) {
$gallery_fids[] = _media_gallery_get_media_fid($file);
}
$columns = !empty($node->media_gallery_block_columns[LANGUAGE_NONE][0]['value']) ? $node->media_gallery_block_columns[LANGUAGE_NONE][0]['value'] : 1;
$rows = !empty($node->media_gallery_block_rows[LANGUAGE_NONE][0]['value']) ? $node->media_gallery_block_rows[LANGUAGE_NONE][0]['value'] : 1;
$number_to_show = $columns * $rows;
$block_fids = db_select('file_managed', 'f')
->fields('f', array(
'fid',
))
->condition('fid', $gallery_fids, 'IN')
->orderBy('timestamp', 'DESC')
->range(0, $number_to_show)
->execute()
->fetchCol();
$fid_order = array_flip($block_fids);
if ($number_to_show < count($files)) {
foreach ($files as $key => $file) {
if (!isset($fid_order[_media_gallery_get_media_fid($file)])) {
unset($files[$key]);
}
}
}
_media_gallery_sort_by_recent(NULL, NULL, $fid_order);
usort($files, '_media_gallery_sort_by_recent');
$block['subject'] = check_plain($node->title);
$block['content']['gallery'] = node_view($node, 'media_gallery_block');
if (isset($block['content']['gallery']['#contextual_links'])) {
$block['content']['#contextual_links'] = $block['content']['gallery']['#contextual_links'];
unset($block['content']['gallery']['#contextual_links']);
}
$block['content']['more_link'] = array(
'#theme' => 'more_link',
'#url' => 'node/' . $node->nid,
'#title' => t('Show the complete gallery'),
'#weight' => 1000,
);
}
return $block;
}
function media_gallery_block_configure($delta = '') {
$node = node_load($delta);
$form = array();
$form_state = array();
field_attach_form('node', $node, $form, $form_state, $node->language);
media_gallery_form_media_gallery_node_form_alter($form, $form_state);
$block_settings = array(
'block' => $form['block'],
);
$block_settings['block']['media_gallery_block_fields'] = array(
'#type' => 'value',
'#value' => element_children($block_settings['block']),
);
$block_settings['block']['media_gallery_expose_block']['#access'] = FALSE;
$block_settings['block']['#collapsible'] = FALSE;
$block_settings['block']['#title'] = t('Block settings');
unset($block_settings['block']['#weight']);
_media_gallery_attach_form_resources($block_settings['block']);
return $block_settings;
}
function media_gallery_block_save($delta = '', $edit = array()) {
$node = node_load($delta);
foreach ($edit['media_gallery_block_fields'] as $field) {
$node->{$field} = $edit[$field];
}
node_save($node);
}
function media_gallery_library() {
$colorbox_path = variable_get('media_gallery_library_path', FALSE);
if ($colorbox_path === FALSE) {
$colorbox_path = module_exists('libraries') ? libraries_get_path('colorbox') : 'sites/all/libraries/colorbox';
}
else {
$colorbox_path .= '/colorbox';
}
$stylesheet = variable_get('media_gallery_colorbox_stylesheet', 'example1');
$libraries['colorbox'] = array(
'title' => 'Colorbox',
'website' => 'http://colorpowered.com/colorbox/',
'version' => '1.3.25',
'js' => array(
$colorbox_path . '/jquery.colorbox-min.js' => array(),
),
'css' => array(
$colorbox_path . '/' . $stylesheet . '/colorbox.css' => array(
'type' => 'file',
'media' => 'screen',
),
),
);
return $libraries;
}
function _media_gallery_sort_by_recent($a, $b, $set_fid_order = NULL) {
$fid_order =& drupal_static(__FUNCTION__, array());
if (isset($set_fid_order)) {
$fid_order = $set_fid_order;
return;
}
$a_order = $fid_order[_media_gallery_get_media_fid($a)];
$b_order = $fid_order[_media_gallery_get_media_fid($b)];
if ($a_order < $b_order) {
return -1;
}
elseif ($b_order < $a_order) {
return 1;
}
else {
return 0;
}
}
function media_gallery_get_media_type_count($node, $media_field = 'media_gallery_media') {
$fids = media_gallery_get_file_ids($node, $media_field);
if (empty($fids)) {
return array();
}
$query = db_select('file_managed', 'f');
$type = $query
->addField('f', 'type');
$query
->addExpression('COUNT(*)');
$type_count = $query
->condition('f.fid', $fids, 'IN')
->groupBy($type)
->execute()
->fetchAllKeyed();
return $type_count;
}
function media_gallery_get_file_ids($node, $media_field = 'media_gallery_media') {
$fids = array();
$media_items = _media_gallery_field_get_items('node', $node, $media_field);
if ($media_items !== FALSE) {
foreach ($media_items as $item) {
$fids[] = _media_gallery_get_media_fid($item);
}
}
return drupal_map_assoc($fids);
}
function _media_gallery_field_get_items($entity_type, $entity, $field_name, $langcode = NULL) {
if ($entity_type == 'node' && $field_name == 'media_gallery_media_original') {
$media = $entity->media_gallery_media;
$entity->media_gallery_media = $entity->media_gallery_media_original;
$field_name = 'media_gallery_media';
}
$items = field_get_items($entity_type, $entity, $field_name, $langcode);
if (isset($media)) {
$entity->media_gallery_media = $media;
}
return $items;
}
function _media_gallery_get_media_fid($file) {
return is_object($file) ? $file->fid : $file['fid'];
}
function media_gallery_remove_item_from_gallery($node, $file) {
$items =& $node->media_gallery_media[LANGUAGE_NONE];
foreach ($items as $key => $item) {
if ($file->fid == _media_gallery_get_media_fid($item)) {
unset($items[$key]);
}
}
node_save($node);
return $node;
}
function media_gallery_entity_info_alter(&$entity_info) {
foreach (media_gallery_file_view_modes() as $view_mode => $label) {
$entity_info['file']['view modes'][$view_mode] = array(
'label' => $label,
'custom settings' => FALSE,
);
}
$entity_info['node']['view modes']['media_gallery_block'] = array(
'label' => t('Media gallery block'),
'custom settings' => FALSE,
);
}
function media_gallery_form($node, $form_state) {
$form = node_content_form($node, $form_state);
return $form;
}
function media_gallery_form_alter(&$form, &$form_state, $form_id) {
if (strpos($form_id, 'media_edit') === 0) {
if ($form_id === 'media_edit' || preg_match('/^media_edit_[0-9]+$/', $form_id)) {
if (!empty($form['media_title']) && empty($form['media_title'][LANGUAGE_NONE][0]['value']['#default_value'])) {
$fid = $form['fid']['#value'];
$file = file_load($fid);
if ($file->type === 'image') {
$form['media_title'][LANGUAGE_NONE][0]['value']['#default_value'] = _media_gallery_get_media_title($file);
}
}
if ($form['field_license'][LANGUAGE_NONE]['#default_value'] == '_none') {
$form['field_license'][LANGUAGE_NONE]['#default_value'] = 'none';
}
unset($form['field_license'][LANGUAGE_NONE]['#options']['_none']);
}
_media_gallery_attach_edit_resources($form);
}
}
function media_gallery_form_media_gallery_node_form_alter(&$form, &$form_state) {
_media_gallery_attach_form_resources($form);
$form['media_gallery_media']['#access'] = FALSE;
$form['media_gallery_weight']['#access'] = FALSE;
$form['media_gallery_collection']['#access'] = FALSE;
$form['settings_wrapper'] = array(
'#type' => 'fieldset',
'#title' => t('Gallery settings'),
'#weight' => 10,
);
unset($form['media_gallery_lightbox_extras'][LANGUAGE_NONE]['#description']);
$form['media_gallery_lightbox_extras'][LANGUAGE_NONE]['#label'] = 'Show title and description';
$fieldset = array(
'media_gallery_columns' => 'gallery',
'media_gallery_rows' => 'gallery',
'media_gallery_image_info_where' => 'gallery',
'media_gallery_allow_download' => 'presentation',
'media_gallery_format' => 'presentation',
'media_gallery_lightbox_extras' => 'presentation',
);
foreach ($fieldset as $id => $subgroup) {
$form['settings_wrapper'][$subgroup][$id] = $form[$id];
$form[$id] = array(
'#language' => $form[$id]['#language'],
);
}
$form['block'] = array(
'#type' => 'fieldset',
'#title' => 'Blocks',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#group' => 'additional_settings',
'#attached' => array(),
'#weight' => -100,
);
$fieldset = array(
'media_gallery_expose_block' => 'block',
'media_gallery_block_columns' => 'block',
'media_gallery_block_rows' => 'block',
);
foreach ($fieldset as $id => $subgroup) {
$form[$subgroup][$id] = $form[$id];
$form[$id] = array(
'#language' => $form[$id]['#language'],
);
}
$form['block']['#attributes']['class'] = array(
'block-form',
);
$form['settings_wrapper']['gallery']['media_gallery_image_info_where']['#attributes']['class'][] = 'form-inline label';
$form['settings_wrapper']['gallery']['media_gallery_image_info']['#attributes']['class'][] = 'form-inline';
$form['settings_wrapper']['presentation']['media_gallery_format']['#attributes']['class'][] = 'media-gallery-show no-group-label';
$form['settings_wrapper']['gallery']['#prefix'] = '<div class="gallery-settings settings-group hidden clearfix"><div class="setting-icon"></div><div class="no-overflow">';
$form['settings_wrapper']['gallery']['#suffix'] = '</div></div>';
$form['settings_wrapper']['presentation']['#prefix'] = '<div class="presentation-settings settings-group hidden clearfix"><div class="group-label">' . t('Presentation settings') . '</div><div class="setting-icon"></div><div class="no-overflow">';
$form['settings_wrapper']['presentation']['#suffix'] = '</div></div>';
$form['settings_wrapper']['gallery']['media_gallery_rows']['#process'][] = 'media_gallery_process_dropdown';
$form['settings_wrapper']['gallery']['media_gallery_rows']['#media_gallery_dropdown_options'] = array(
'1',
'3',
'5',
'10',
'other',
);
$form['block']['media_gallery_block_rows']['#process'][] = 'media_gallery_process_dropdown';
$form['block']['media_gallery_block_rows']['#media_gallery_dropdown_options'] = array(
'1',
'2',
'3',
'4',
'other',
);
$form['settings_wrapper']['presentation']['media_gallery_allow_download']['#weight'] = 0;
$form['actions']['preview']['#access'] = FALSE;
}
function media_gallery_form_media_edit_alter(&$form, &$form_state) {
if (isset($form_state['media_gallery']['gallery'])) {
$form['actions']['delete']['#access'] = FALSE;
_media_gallery_add_remove_checkbox($form, $form_state, $form_state['media_gallery']['gallery']);
$form['#submit'][] = 'media_gallery_media_edit_submit';
if (isset($form['actions']['submit']['#submit'])) {
$form['actions']['submit']['#submit'][] = 'media_gallery_media_edit_submit';
}
}
elseif (($node = menu_get_object()) && arg(2) === 'multiedit' && $node->type === 'media_gallery') {
_media_gallery_add_remove_checkbox($form, $form_state, $node);
}
}
function _media_gallery_add_remove_checkbox(&$form, &$form_state, $node) {
$form['#gallery'] = $node;
$form['remove'] = array(
'#type' => 'checkbox',
'#title' => t('Remove from gallery'),
'#description' => t('The original file remains in your <a href="@library">media library</a>.', array(
'@library' => url('admin/content/media'),
)),
'#access' => node_access('update', $node),
);
$form['#submit'][] = 'media_gallery_multiedit_remove_item';
if (isset($form['actions']['submit']['#submit'])) {
$form['actions']['submit']['#submit'][] = 'media_gallery_multiedit_remove_item';
}
}
function media_gallery_multiedit_remove_item($form, &$form_state) {
if (isset($form['#gallery'])) {
$gallery = $form['#gallery'];
if (!empty($form_state['values']['remove'])) {
$file = file_load($form_state['values']['fid']);
media_gallery_remove_item_from_gallery($gallery, $file);
}
$uri = entity_uri('node', $gallery);
$form_state['redirect'] = $uri;
}
}
function media_gallery_media_edit_submit($form, &$form_state) {
$form_state['redirect'] = 'media-gallery/detail/' . $form_state['media_gallery']['gallery']->nid . '/' . $form_state['values']['fid'];
}
function media_gallery_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
if ($entity_type == 'file') {
$scheme = file_uri_scheme($entity->uri);
if ($scheme == 'youtube') {
$form['field_license']['#access'] = FALSE;
}
}
}
function _media_gallery_attach_form_resources(&$element) {
$element['#attached']['js'][] = drupal_get_path('module', 'media_gallery') . '/media_gallery.form.js';
_media_gallery_attach_css_resources($element);
}
function _media_gallery_attach_css_resources(&$element) {
$path = drupal_get_path('module', 'media_gallery');
$element['#attached']['css'][] = $path . '/media_gallery.css';
$element['#attached']['css'][] = array(
'data' => $path . '/media_gallery.ie7.css',
'browsers' => array(
'IE' => 'lt IE 8',
'!IE' => FALSE,
),
);
}
function _media_gallery_attach_edit_resources(&$element) {
$path = drupal_get_path('module', 'media_gallery');
$element['#attached']['js'][] = $path . '/js/media_gallery.edit.js';
$element['#attached']['css'][] = $path . '/css/media_gallery.edit.css';
}
function media_gallery_node_view_alter(&$build) {
if ($build['#bundle'] == 'media_gallery' && $build['#view_mode'] == 'teaser') {
$build['links']['#access'] = FALSE;
unset($build['#contextual_links']);
_media_gallery_attach_css_resources($build);
}
elseif ($build['#view_mode'] == 'media_gallery_block') {
$build['links']['#access'] = FALSE;
_media_gallery_attach_css_resources($build);
}
elseif ($build['#bundle'] == 'media_gallery' && $build['#view_mode'] == 'full') {
if (!empty($build['media_gallery_media'])) {
foreach (element_children($build['media_gallery_media']) as $delta) {
$fid = $build['media_gallery_media'][$delta]['#file']->fid;
$build['media_gallery_media'][$delta]['#contextual_links']['media_gallery'] = array(
'media-gallery/detail',
array(
$build['#node']->nid,
$fid,
),
);
}
}
_media_gallery_attach_css_resources($build);
}
}
function media_gallery_preprocess_node(&$variables) {
if ($variables['view_mode'] == 'media_gallery_block') {
$variables['title'] = '';
}
if ($variables['node']->type == 'media_gallery' && $variables['view_mode'] == 'teaser') {
$variables['content']['#theme'] = 'media_gallery_teaser';
$variables['content']['#node'] = $variables['node'];
$variables['classes_array'][] = 'mg-gallery';
$variables['classes_array'][] = 'mg-teaser';
}
}
function media_gallery_preprocess_menu_local_task(&$variables) {
if (isset($_GET['page']) && !isset($variables['element']['#link']['localized_options']['query']['page']) && strpos($variables['element']['#link']['href'], 'node/') == 0) {
$nid = arg(1, $variables['element']['#link']['href']);
if (is_numeric($nid) && arg(2, $variables['element']['#link']['href']) == 'multiedit') {
$node = node_load($nid);
if ($node->type == 'media_gallery') {
$page = pager_find_page(MEDIA_GALLERY_PAGER_ELEMENT);
if (is_int($page)) {
$variables['element']['#link']['localized_options']['query']['page'] = "{$page}";
}
}
}
}
}
function media_gallery_contextual_links_view_alter(&$element, $items) {
if (isset($element['#element']['#block']->module) && $element['#element']['#block']->module == 'media_gallery' && !empty($element['#links'])) {
$links =& $element['#links'];
foreach ($links as $key => &$link) {
if ($key != 'node-edit' && $key != 'block-configure') {
unset($links[$key]);
}
elseif ($key == 'node-edit') {
$link['title'] = t('Edit gallery');
}
}
}
}
function media_gallery_preprocess_field(&$variables, $hook) {
if ($variables['element']['#field_name'] === 'media_gallery_media') {
$columns = 1;
switch ($variables['element']['#view_mode']) {
case 'media_gallery_block':
$columns = $variables['element']['#object']->media_gallery_block_columns[LANGUAGE_NONE][0]['value'];
break;
case 'full':
$columns = $variables['element']['#object']->media_gallery_columns[LANGUAGE_NONE][0]['value'];
$media_gallery_view_mode_css = 'media-gallery-view-full';
break;
}
$variables['classes_array'][] = $variables['element']['#view_mode'] != 'teaser' ? 'clearfix media-gallery-media mg-col mg-col-' . $columns : 'clearfix media-gallery-media';
if (!empty($media_gallery_view_mode_css)) {
$variables['classes_array'][] = $media_gallery_view_mode_css;
}
foreach ($variables['items'] as $delta => $item) {
$variables['item_attributes_array'][$delta] = array(
'id' => 'media-gallery-media-' . $delta,
);
}
}
}
function media_gallery_select_galleries($tid, $pager = TRUE, $limit = FALSE) {
if (!variable_get('taxonomy_maintain_index_table', TRUE)) {
return array();
}
$query = db_select('taxonomy_index', 't');
$query
->leftJoin('media_gallery_weight', 'mgw', 'mgw.nid = t.nid AND mgw.tid = :tid', array(
':tid' => $tid,
));
$query
->addTag('node_access');
$query
->condition('t.tid', $tid);
if ($pager) {
$count_query = clone $query;
$count_query
->addExpression('COUNT(t.nid)');
$query = $query
->extend('PagerDefault');
if ($limit !== FALSE) {
$query = $query
->limit($limit);
}
$query
->setCountQuery($count_query);
}
else {
if ($limit !== FALSE) {
$query
->range(0, $limit);
}
}
$query
->addField('t', 'nid');
$query
->addField('t', 'tid');
$query
->addField('mgw', 'weight');
$query
->orderBy('mgw.weight', 'ASC');
$query
->orderBy('t.nid', 'ASC');
$result = $query
->execute();
return $result
->fetchCol();
}
function media_gallery_form_taxonomy_form_term_alter(&$form, &$form_state) {
if (isset($form_state['confirm_delete']) || !isset($form['#vocabulary']) || $form['#vocabulary']->vid != variable_get('media_gallery_collection_vid')) {
return;
}
$form['introduction'] = array(
'#weight' => -10,
'#markup' => check_plain(t('The following settings affect the "All galleries" page, which shows a thumbnail of every gallery created.')),
);
$form['#attributes']['class'][] = 'form-media-gallery-collection';
$form['name']['#title'] = t('Title');
$form['path']['alias']['#title'] = check_plain(t('"All galleries" URL'));
$form['path']['alias']['#weight'] = -1;
$form['path']['alias']['#field_prefix'] = url(NULL, array(
'absolute' => TRUE,
)) . (variable_get('clean_url', 0) ? '' : '?q=');
unset($form['path']['alias']['#description']);
_media_gallery_attach_form_resources($form);
$fieldset = array(
'media_gallery_columns' => 'gallery',
'media_gallery_rows' => 'gallery',
'media_gallery_image_info_where' => 'gallery',
);
foreach ($fieldset as $id => $subgroup) {
$form['settings_wrapper'][$subgroup][$id] = $form[$id];
unset($form[$id]);
}
$form['settings_wrapper']['gallery']['media_gallery_image_info_where']['#attributes']['class'][] = 'form-inline label';
$form['settings_wrapper']['gallery']['#prefix'] = '<div class="galleries-settings settings-group hidden"><div class="group-label">' . check_plain(t('"All galleries" layout settings')) . '</div><div class="setting-icon"></div><div class="no-overflow">';
$form['settings_wrapper']['gallery']['#suffix'] = '</div></div>';
$form['settings_wrapper']['gallery']['media_gallery_rows']['#process'][] = 'media_gallery_process_dropdown';
$form['settings_wrapper']['gallery']['media_gallery_rows']['#media_gallery_dropdown_options'] = array(
'1',
'3',
'5',
'10',
'other',
);
$form['relations']['#access'] = FALSE;
$form['actions']['delete']['#access'] = FALSE;
$form['field_license']['#access'] = FALSE;
$form['#submit'][] = 'media_gallery_taxonomy_form_term_submit';
}
function media_gallery_taxonomy_form_term_submit($form, &$form_state) {
$term_name = $form_state['values']['name'];
$messages = $_SESSION['messages']['status'];
$updated_message = t('Updated term %term.', array(
'%term' => $term_name,
));
foreach ($messages as $key => $message) {
if ($message === $updated_message) {
$_SESSION['messages']['status'][$key] = t('The gallery settings have been saved.');
}
}
}
function media_gallery_form_taxonomy_overview_vocabularies_alter(&$form, &$form_state) {
$gallery_collection_vid = variable_get('media_gallery_collection_vid');
unset($form[$gallery_collection_vid]);
}
function media_gallery_module_implements_alter(&$implementations, $hook) {
switch ($hook) {
case 'form_alter':
if (!isset($implementations['media_gallery'])) {
break;
}
$group = $implementations['media_gallery'];
unset($implementations['media_gallery']);
$implementations['media_gallery'] = $group;
break;
case 'taxonomy_term_insert':
case 'taxonomy_term_update':
if (isset($implementations['media_gallery'])) {
$implementations = array(
'media_gallery' => $implementations['media_gallery'],
) + $implementations;
}
break;
}
}
function media_gallery_get_default_gallery_collection() {
$gallery_collection_vid = variable_get('media_gallery_collection_vid');
$tid = db_select('taxonomy_term_data', 'ttd')
->fields('ttd', array(
'tid',
))
->condition('vid', $gallery_collection_vid)
->range(0, 1)
->execute()
->fetchField();
return taxonomy_term_load($tid);
}
function media_gallery_view_access($node) {
if (!node_access('view', $node)) {
return FALSE;
}
if ($node->type == 'media_gallery') {
return TRUE;
}
}
function media_gallery_edit_access($node) {
if (!node_access('update', $node)) {
return FALSE;
}
if ($node->type == 'media_gallery') {
return TRUE;
}
}
function media_gallery_multiedit_access($node) {
if (media_gallery_edit_access($node) && media_access('edit')) {
$media_items = field_get_items('node', $node, 'media_gallery_media');
if ($media_items !== FALSE && count($media_items) > 0) {
return TRUE;
}
}
}
function media_gallery_view_item_access($node, $file) {
return media_gallery_view_access($node) && media_access('view') && in_array($file->fid, media_gallery_get_file_ids($node));
}
function media_gallery_edit_item_access($node, $file) {
return media_access('edit') && in_array($file->fid, media_gallery_get_file_ids($node));
}
function media_gallery_remove_item_access($node, $file) {
return media_gallery_edit_access($node) && in_array($file->fid, media_gallery_get_file_ids($node));
}
function media_gallery_image_default_styles() {
$styles = array();
$styles['media_gallery_thumbnail'] = array(
'effects' => array(
array(
'name' => 'image_scale_and_crop',
'data' => array(
'width' => 450,
'height' => 450,
'upscale' => FALSE,
),
'weight' => 0,
),
),
);
$styles['media_gallery_large'] = array(
'effects' => array(
array(
'name' => 'image_scale',
'data' => array(
'width' => 900,
'height' => 900,
'upscale' => FALSE,
),
'weight' => 0,
),
),
);
return $styles;
}
function media_gallery_styles_default_styles() {
return array(
'file' => array(
'styles' => array(
'media_gallery_thumbnail' => array(
'label' => 'Media gallery thumbnail',
'description' => 'A square thumbnail for display within a Media Gallery.',
),
'media_gallery_large' => array(
'label' => 'Media gallery large',
'description' => 'A large format for display within a Media Gallery.',
),
),
),
);
}
function media_gallery_styles_default_presets_alter(&$styles) {
foreach (array_keys(media_gallery_image_default_styles()) as $image_style) {
foreach (array(
'image',
'media_youtube',
) as $container) {
$styles['file']['containers'][$container]['styles'][$image_style]['default preset'] = $image_style;
$styles['file']['containers'][$container]['presets'][$image_style] = array(
array(
'name' => class_exists('FileStyles') && method_exists('FileStyles', 'imageStyle') ? 'imageStyle' : 'image_style',
'settings' => array(
'image_style' => $image_style,
),
),
array(
'name' => 'thumbnail',
'settings' => array(),
),
);
}
}
$styles['file']['containers']['media_youtube']['styles']['media_gallery_large']['default preset'] = 'video';
}
function media_gallery_styles_presets() {
$presets = array(
'file' => array(
'media_gallery_thumbnail' => array(
'media_youtube' => array(
'youtube_thumbnail_media_gallery_thumbnail',
),
),
'media_gallery_large' => array(
'media_youtube' => array(
'youtube_full',
),
),
),
);
return $presets;
}
function media_gallery_media_wysiwyg_allowed_view_modes_alter(&$view_modes) {
$view_modes = array_diff_key($view_modes, media_gallery_file_view_modes());
}
function media_gallery_process_dropdown($element, &$form_state) {
$element['#attributes']['class'][] = 'media-gallery-dropdown';
$element['#attached']['js'][] = array(
'type' => 'setting',
'data' => array(
'media_gallery_dropdown_options' => array(
$element['#id'] => $element['#media_gallery_dropdown_options'],
),
),
);
return $element;
}
function media_gallery_taxonomy_term_insert($term) {
_media_gallery_prevent_unwanted_pathauto_aliases($term);
}
function media_gallery_taxonomy_term_update($term) {
_media_gallery_prevent_unwanted_pathauto_aliases($term);
}
function media_gallery_entity_insert($entity, $type) {
if ($type == 'taxonomy_term') {
_media_gallery_allow_all_pathauto_aliases();
}
}
function media_gallery_entity_update($entity, $type) {
if ($type == 'taxonomy_term') {
_media_gallery_allow_all_pathauto_aliases();
}
}
function _media_gallery_prevent_unwanted_pathauto_aliases($term = NULL) {
if (!isset($term) || $term->tid == variable_get('media_gallery_default_collection_tid')) {
if (isset($GLOBALS['conf']['pathauto_taxonomy_pattern'])) {
$GLOBALS['conf']['media_gallery_original_pathauto_taxonomy_pattern'] = $GLOBALS['conf']['pathauto_taxonomy_pattern'];
}
$GLOBALS['conf']['pathauto_taxonomy_pattern'] = '';
}
}
function _media_gallery_allow_all_pathauto_aliases() {
if (isset($GLOBALS['conf']['media_gallery_original_pathauto_taxonomy_pattern'])) {
$GLOBALS['conf']['pathauto_taxonomy_pattern'] = $GLOBALS['conf']['media_gallery_original_pathauto_taxonomy_pattern'];
}
}
function media_gallery_ctools_plugin_api($owner, $api) {
static $api_versions = array(
'file_entity' => array(
'file_default_displays' => 1,
),
);
if (isset($api_versions[$owner][$api])) {
return array(
'version' => $api_versions[$owner][$api],
);
}
}
function media_gallery_file_default_displays() {
$default_displays = array();
$default_image_styles = array(
'media_gallery_thumbnail' => 'media_gallery_thumbnail',
'media_gallery_lightbox' => 'media_gallery_large',
'media_gallery_detail' => 'media_gallery_large',
'media_gallery_block_thumbnail' => 'media_gallery_thumbnail',
'media_gallery_collection_thumbnail' => 'media_gallery_thumbnail',
);
$default_weight = -1;
foreach ($default_image_styles as $view_mode => $image_style) {
$display_name = 'image__' . $view_mode . '__file_image';
$default_displays[$display_name] = (object) array(
'api_version' => 1,
'name' => $display_name,
'status' => 1,
'settings' => array(
'image_style' => $image_style,
),
'weight' => $default_weight,
);
if (module_exists('media_youtube')) {
if (in_array($view_mode, array(
'media_gallery_lightbox',
'media_gallery_detail',
))) {
$display_name = 'video__' . $view_mode . '__media_youtube_video';
$default_displays[$display_name] = (object) array(
'api_version' => 1,
'name' => $display_name,
'status' => 1,
'weight' => $default_weight,
);
}
else {
$display_name = 'video__' . $view_mode . '__media_youtube_image';
$default_displays[$display_name] = (object) array(
'api_version' => 1,
'name' => $display_name,
'status' => 1,
'settings' => array(
'image_style' => $image_style,
),
'weight' => $default_weight,
);
}
}
}
return $default_displays;
}
function media_gallery_lightbox_delivery_callback($page_content) {
if (!function_exists('media_gallery_lightbox_page_deliver')) {
module_load_include('inc', 'media_gallery', 'media_gallery.pages');
}
media_gallery_lightbox_page_deliver($page_content);
}