View source
<?php
define('NODE_GALLERY_DEFAULT_GALLERY_TYPE', 'node_gallery_gallery');
define('NODE_GALLERY_DEFAULT_ITEM_TYPE', 'node_gallery_item');
define('NODE_GALLERY_DEFAULT_MEDIA_FIELD', 'node_gallery_media');
function node_gallery_type_exists($type) {
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'node') . '/node.module';
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'user') . '/user.module';
$types = node_type_get_types();
$types = array_change_key_case($types, CASE_LOWER);
return array_key_exists($type, $types);
}
function node_gallery_setup_display_modes($item_type = NODE_GALLERY_DEFAULT_ITEM_TYPE) {
$bundle_settings = field_bundle_settings('file', 'image');
$bundle_settings['view_modes']['node_gallery_file_thumbnail']['custom_settings'] = TRUE;
$bundle_settings['view_modes']['node_gallery_file_thumbnail']['file_image']['image_style'] = 'node_gallery_thumbnail';
$bundle_settings['view_modes']['node_gallery_file_cover']['custom_settings'] = TRUE;
$bundle_settings['view_modes']['node_gallery_file_cover']['file_image']['image_style'] = 'node_gallery_thumbnail';
$bundle_settings['view_modes']['node_gallery_file_display']['custom_settings'] = TRUE;
$bundle_settings['view_modes']['node_gallery_file_display']['file_image']['image_style'] = 'node_gallery_display';
field_bundle_settings('file', 'image', $bundle_settings);
$file_displays = file_displays_load('image', 'node_gallery_file_thumbnail');
if (empty($file_displays['image__node_gallery_file_thumbnail__file_image'])) {
$file_displays['image__node_gallery_file_thumbnail__file_image'] = file_display_new('image', 'node_gallery_file_thumbnail', 'file_image');
}
$file_displays['image__node_gallery_file_thumbnail__file_image']->status = 1;
$file_displays['image__node_gallery_file_thumbnail__file_image']->settings['image_style'] = 'node_gallery_thumbnail';
file_display_save($file_displays['image__node_gallery_file_thumbnail__file_image']);
$file_displays = file_displays_load('image', 'node_gallery_file_cover');
if (empty($file_displays['image__node_gallery_file_cover__file_image'])) {
$file_displays['image__node_gallery_file_cover__file_image'] = file_display_new('image', 'node_gallery_file_cover', 'file_image');
}
$file_displays['image__node_gallery_file_cover__file_image']->status = 1;
$file_displays['image__node_gallery_file_cover__file_image']->settings['image_style'] = 'node_gallery_thumbnail';
file_display_save($file_displays['image__node_gallery_file_cover__file_image']);
$file_displays = file_displays_load('image', 'node_gallery_file_display');
if (empty($file_displays['image__node_gallery_file_display__file_image'])) {
$file_displays['image__node_gallery_file_display__file_image'] = file_display_new('image', 'node_gallery_file_display', 'file_image');
}
$file_displays['image__node_gallery_file_display__file_image']->status = 1;
$file_displays['image__node_gallery_file_display__file_image']->settings['image_style'] = 'node_gallery_display';
file_display_save($file_displays['image__node_gallery_file_display__file_image']);
$settings = variable_get('node_gallery_api_file_link_settings', array());
$settings['image']['node_gallery_file_thumbnail'] = 'gallery_item';
$settings['image']['node_gallery_file_cover'] = 'gallery';
variable_set('node_gallery_api_file_link_settings', $settings);
$bundle_settings = field_view_mode_settings('node', $item_type);
$bundle_settings['extra_fields']['display']['node_gallery_navigation']['node_gallery_node_thumbnail']['visible'] = FALSE;
$bundle_settings['extra_fields']['display']['node_gallery_navigation']['node_gallery_node_thumbnail']['weight'] = 1;
field_bundle_settings('node', $item_type, $bundle_settings);
variable_set('node_submitted_' . $item_type, FALSE);
}
function node_gallery_setup_content_types() {
$t = get_t();
$gallery_type = array(
'type' => NODE_GALLERY_DEFAULT_GALLERY_TYPE,
'name' => $t('Gallery'),
'base' => 'node_content',
'description' => $t('A gallery of nodes.'),
'title_label' => $t('Gallery Name'),
'custom' => TRUE,
);
$content_type = node_type_set_defaults($gallery_type);
node_add_body_field($content_type, $t('Description'));
node_type_save($content_type);
$item_type = array(
'type' => NODE_GALLERY_DEFAULT_ITEM_TYPE,
'name' => $t('Gallery Item'),
'base' => 'node_content',
'description' => $t('A gallery node item.'),
'title_label' => $t('Title'),
'custom' => TRUE,
);
$content_type = node_type_set_defaults($item_type);
node_add_body_field($content_type, $t('Caption'));
node_type_save($content_type);
_node_gallery_create_default_relationship();
node_gallery_create_thumbnail_node_view_mode();
menu_cache_clear_all();
}
function node_gallery_create_thumbnail_node_view_mode($item_type = NODE_GALLERY_DEFAULT_ITEM_TYPE, $field_name = NODE_GALLERY_DEFAULT_MEDIA_FIELD) {
$instance = field_info_instance('node', $field_name, $item_type);
$instance['display']['default']['label'] = 'hidden';
$instance['display']['default']['type'] = 'file_rendered';
$instance['display']['default']['settings']['file_view_mode'] = 'node_gallery_file_display';
$instance['display']['teaser']['label'] = 'hidden';
$instance['display']['teaser']['weight'] = 1;
$instance['display']['teaser']['type'] = 'file_rendered';
$instance['display']['teaser']['settings']['file_view_mode'] = 'node_gallery_file_display';
$instance['display']['node_gallery_node_thumbnail']['label'] = 'hidden';
$instance['display']['node_gallery_node_thumbnail']['type'] = 'file_rendered';
$instance['display']['node_gallery_node_thumbnail']['settings']['file_view_mode'] = 'node_gallery_file_thumbnail';
field_update_instance($instance);
}
function _node_gallery_create_default_relationship() {
$r = new NodeGalleryRelationshipType();
$r->settings = node_gallery_api_relationship_type_settings_defaults();
$r->settings['relationship_type']['gallery_types'] = array(
NODE_GALLERY_DEFAULT_GALLERY_TYPE,
);
$r->settings['relationship_type']['item_types'] = array(
NODE_GALLERY_DEFAULT_ITEM_TYPE,
);
$r->settings['view_modes']['full']['view'] = 'node_gallery_gallery_item_views:embed_1';
$r->settings['view_modes']['teaser']['view'] = 'node_gallery_gallery_item_views:embed_6';
$r->settings['item_view']['view_navigator_item_display'] = 'node_gallery_gallery_item_views:embed_5';
$r->filefield_name = NODE_GALLERY_DEFAULT_MEDIA_FIELD;
$r->label = $r->settings['relationship']['name'];
$r
->save();
variable_set('node_gallery_default_relationship_type_id', $r->id);
}
function node_gallery_install() {
node_gallery_setup_display_modes();
node_gallery_setup_content_types();
}
function node_gallery_uninstall() {
$default_relationship_type_id = variable_get('node_gallery_default_relationship_type_id');
entity_delete('node_gallery_relationship_type', $default_relationship_type_id);
variable_del('node_gallery_default_relationship_type_id');
cache_clear_all();
}
function _node_gallery_check_dependencies($module_list) {
$module_data = system_rebuild_module_data();
$module_list = array_flip(array_values($module_list));
foreach ($module_list as $module) {
if (!isset($module_data[$module])) {
$t = get_t();
throw new DrupalUpdateException($t('Node Gallery (node_gallery) requires that @module module is installed. Install this module and run the update again.', array(
'@module' => $module,
)));
}
if ($module_data[$module]->status) {
unset($module_list[$module]);
continue;
}
$module_list[$module] = $module_data[$module]->sort;
foreach (array_keys($module_data[$module]->requires) as $dependency) {
if (!isset($module_list[$dependency])) {
$module_list[$dependency] = 0;
}
}
}
}
function _node_gallery_check_field_upgrades() {
$result = db_query("SELECT * FROM {node_gallery_relationships_d6}");
$missing_fields = array();
foreach ($result as $row) {
$field = field_info_field($row->imagefield_name);
if (is_null($field)) {
$missing_fields[] = $row->imagefield_name;
}
}
if (!empty($missing_fields)) {
$t = get_t();
throw new DrupalUpdateException($t('The following fields need to be upgraded with CCK Content Migrate before Node Gallery can be upgraded: @fields.', array(
'@fields' => implode(', ', $missing_fields),
)));
}
}
function node_gallery_update_7000() {
if (db_table_exists('node_gallery_images')) {
db_rename_table('node_gallery_galleries', 'node_gallery_galleries_d6');
db_rename_table('node_gallery_images', 'node_gallery_images_d6');
db_rename_table('node_gallery_relationships', 'node_gallery_relationships_d6');
}
}
function node_gallery_update_7001() {
if (!db_table_exists('node_gallery_galleries')) {
$module_list = array(
'node_gallery_api',
'node_gallery',
);
_node_gallery_check_dependencies($module_list);
_node_gallery_check_field_upgrades();
module_enable($module_list);
}
}
function node_gallery_update_7002() {
if (!db_table_exists('node_gallery_relationships_d6')) {
return;
}
if (!module_exists('node_gallery_api')) {
throw new DrupalUpdateException('This update requires Node Gallery API (node_gallery_api) module is enabled. Enable this module and run the update again.');
}
$result = db_query("SELECT * FROM {node_gallery_relationships_d6}");
foreach ($result as $row) {
$old_settings = unserialize($row->settings);
$old_settings['view_gallery_full_image_display'] = unserialize($old_settings['view_gallery_full_image_display']);
$old_settings['view_gallery_teaser_view_image_display'] = unserialize($old_settings['view_gallery_teaser_view_image_display']);
$old_settings['view_navigator_image_display'] = unserialize($old_settings['view_navigator_image_display']);
$settings = node_gallery_api_relationship_type_settings_defaults();
$settings['relationship']['name'] = $old_settings['name'];
$settings['relationship_type']['gallery_types'] = array(
$row->gallery_type,
);
$settings['relationship_type']['item_types'] = array(
$row->image_type,
);
$settings['item_view']['display_navigator'] = $old_settings['display_navigator'];
$settings['item_view']['view_navigator_item_display'] = 'node_gallery_gallery_item_views:embed_5';
$settings['view_modes']['full']['view'] = 'node_gallery_gallery_item_views:embed_1';
$settings['view_modes']['teaser']['view'] = 'node_gallery_gallery_item_views:embed_6';
$settings['manage_items']['items_fields'] = $old_settings['manage_images_fields'];
$settings['manage_items']['items_per_page'] = (int) $old_settings['manage_images_per_page'];
if ((bool) $old_settings['manage_images_show_gallery_list']) {
$settings['manage_items']['item_fields']['node_gallery_ref_' . $row->rid] = 'node_gallery_ref_' . $row->rid;
}
$exists = db_query("SELECT COUNT(*) FROM {node_gallery_relationship_type} WHERE id = :id", array(
':id' => $row->rid,
))
->fetchField();
if (!$exists) {
$r = new NodeGalleryRelationshipType();
$r->id = $row->rid;
$r->is_new = TRUE;
$r->settings = $settings;
$r->label = $settings['relationship']['name'];
$r->filefield_name = $row->imagefield_name;
$r
->save();
}
node_gallery_setup_display_modes($row->image_type);
node_gallery_create_thumbnail_node_view_mode($row->image_type, $row->imagefield_name);
}
}
function node_gallery_update_7003(&$sandbox) {
if (!db_table_exists('node_gallery_images_d6')) {
return;
}
if (!isset($sandbox['total'])) {
$sandbox['total'] = db_query("SELECT COUNT(*) FROM {node_gallery_images_d6}")
->fetchField();
$sandbox['processed'] = 0;
}
$count = 1000;
$result = db_query_range("SELECT i.nid, i.gid as ngid, i.weight, r.rid as relationship_type FROM {node_gallery_images_d6} i LEFT JOIN {node} n ON i.nid = n.nid LEFT JOIN {node_gallery_relationships_d6} r ON n.type = r.image_type GROUP BY i.nid ORDER BY i.nid", $sandbox['processed'], $count);
foreach ($result as $row) {
db_insert('node_gallery_relationship')
->fields((array) $row)
->execute();
$sandbox['processed']++;
}
if (!empty($sandbox['total'])) {
$sandbox['#finished'] = $sandbox['processed'] / $sandbox['total'];
if ($sandbox['#finished'] === 1) {
field_cache_clear();
}
}
}
function node_gallery_update_7004(&$sandbox) {
if (!db_table_exists('node_gallery_galleries_d6')) {
return;
}
if (!isset($sandbox['total'])) {
$sandbox['total'] = db_query("SELECT COUNT(*) FROM {node_gallery_galleries_d6}")
->fetchField();
$sandbox['processed'] = 0;
}
$count = 500;
$result = db_query_range("SELECT g.gid as ngid, g.cover_image as cover_item, COUNT(n.nid) as item_count, SUM(n.status) as pub_item_count FROM {node_gallery_galleries_d6} g LEFT JOIN {node_gallery_images_d6} i ON g.gid = i.gid LEFT JOIN {node} n ON i.nid = n.nid GROUP BY g.gid ORDER BY g.gid", $sandbox['processed'], $count);
foreach ($result as $row) {
$row = (array) $row;
$row['pub_item_count'] = (int) $row['pub_item_count'];
$row['item_count'] = (int) $row['item_count'];
db_insert('node_gallery_galleries')
->fields($row)
->execute();
$sandbox['processed']++;
}
if (!empty($sandbox['total'])) {
$sandbox['#finished'] = $sandbox['processed'] / $sandbox['total'];
if ($sandbox['#finished'] === 1) {
field_cache_clear();
}
}
}
function node_gallery_update_7005(&$sandbox) {
if (db_table_exists('node_gallery_galleries_d6')) {
db_drop_table('node_gallery_galleries_d6');
db_drop_table('node_gallery_images_d6');
db_drop_table('node_gallery_relationships_d6');
}
}
function node_gallery_update_7006(&$sandbox) {
$settings = variable_get('file_entity_link_settings', array());
$settings['image']['node_gallery_file_thumbnail'] = FALSE;
variable_set('file_entity_link_settings', $settings);
$settings = variable_get('node_gallery_api_file_link_settings', array());
$settings['image']['node_gallery_file_thumbnail'] = 'gallery_item';
variable_set('node_gallery_api_file_link_settings', $settings);
$t = get_t();
return $t("File Entity Link module dependency removed from Node Gallery. You may disable this module if you aren't using it for other purposes.");
}
function node_gallery_update_7007(&$sandbox) {
$bundle_settings = field_bundle_settings('file', 'image');
$bundle_settings['view_modes']['node_gallery_file_cover']['custom_settings'] = TRUE;
$bundle_settings['view_modes']['node_gallery_file_cover']['file_image']['image_style'] = 'node_gallery_thumbnail';
field_bundle_settings('file', 'image', $bundle_settings);
$file_displays = file_displays_load('image', 'node_gallery_file_cover');
if (empty($file_displays['image__node_gallery_file_cover__file_image'])) {
$file_displays['image__node_gallery_file_cover__file_image'] = file_display_new('image', 'node_gallery_file_cover', 'file_image');
}
$file_displays['image__node_gallery_file_cover__file_image']->status = 1;
$file_displays['image__node_gallery_file_cover__file_image']->settings['image_style'] = 'node_gallery_thumbnail';
file_display_save($file_displays['image__node_gallery_file_cover__file_image']);
$settings = variable_get('node_gallery_api_file_link_settings', array());
$settings['image']['node_gallery_file_cover'] = 'gallery';
variable_set('node_gallery_api_file_link_settings', $settings);
}