Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
249 lines (213 sloc) 8.01 KB
<?php
/**
* @file
* Code for the Fishers wordpress migration feature.
*/
include_once 'fishers_wordpress_migration.features.inc';
/**
* Implements hook_filter_info().
*
* The Nextgen gallery from Wordpress leaves behind tags like:
* [nggallery id=18] to represent a whole gallery, and
* [singlepic id=422 w=320 h=240 float=] to represent a single picture from a
* gallery. This filter translates that into markup.
*/
function fishers_wordpress_migration_filter_info() {
$filters['nggallery_filter'] = array(
'title' => t('Convert NextGen gallery tags into markup'),
'description' => t('This filter will convert [nggallery id=18] and [singlepic id=422 w=320 h=240 float=] tags into markup.'),
'process callback' => 'fishers_wordpress_migration_filter',
'weight' => 2,
);
return $filters;
}
/**
* Filter callback for fishers_wordpress_migration markup filter.
*/
function fishers_wordpress_migration_filter($text) {
$text = ' ' . $text . ' ';
$text = preg_replace_callback("/\[.*?\]/s", 'fishers_wordpress_migration_token_to_markup', $text);
return $text;
}
/**
* Replace callback to convert a fishers_wordpress_migration file tag into HTML markup.
*
* @param string $match
* Takes a match of tag code
* @param boolean $wysiwyg
* Set to TRUE if called from within the WYSIWYG text area editor.
* @return
* The HTML markup representation of the tag, or an empty string on failure.
*
* @see fishers_wordpress_migration_get_file_without_label()
*/
function fishers_wordpress_migration_token_to_markup($match, $wysiwyg = FALSE) {
$settings = array();
$match = str_replace("[", "", $match);
$match = str_replace("]", "", $match);
$tag = $match[0];
try {
if (!is_string($tag)) {
throw new Exception('Unable to find matching tag');
}
$nextgen_tag = str_replace(" ", "&", $tag);
parse_str($nextgen_tag, $vars);
if (isset($vars['nggallery'])) {
$gid = $vars['id'];
$query = new EntityFieldQuery();
$gallery = $query->entityCondition('entity_type', 'node')
->propertyCondition('status', 1)
->fieldCondition('field_nextgen_gid', 'value', $gid, '=')
->execute();
$element = node_view(node_load(current($gallery['node'])->nid), "teaser");
$element['#prefix'] = '<div class="mg-col-4">';
$element['#suffix'] = '</div>';
}
elseif (isset($vars['singlepic'])) {
// query looks lik id=422 w=320 h=240 float=left/right/''
$fid = $vars['id'];
$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'file')
->propertyCondition('status', 1)
->fieldCondition('field_nextgen_id', 'value', $fid, '=')
->execute();
if(is_array($entities['file'])) {
$files = file_load_multiple(array_keys($entities['file']));
$file = current($files);
}
if (!$file) {
throw new Exception('Could not load fishers_wordpress_migration object');
}
$tag_info['file'] = $file;
$tag_info['view_mode'] = 'teaser';
if(isset($vars['w'])) {
$settings['attributes']['width'] = $vars['w'];
}
if(isset($vars['h'])) {
$settings['attributes']['height'] = $vars['h'];
}
// Display the field elements.
$element = array();
$element['content']['file'] = media_get_file_without_label($file, $tag_info['view_mode'], $settings);
field_attach_prepare_view('file', array($file->fid => $file), $tag_info['view_mode']);
entity_prepare_view('file', array($file->fid => $file));
$element['content'] += field_attach_view('file', $file, $tag_info['view_mode']);
if (count(element_children($element['content'])) > 1) {
// Add surrounding divs to group them together.
// We dont want divs when there are no additional fields to allow them
// to display inline.
$element['content']['#prefix'] = '<div>';
$element['content']['#suffix'] = '</div>';
}
}
}
catch (Exception $e) {
watchdog('fishers_wordpress_migration', 'Unable to render fishers_wordpress_migration from %tag. Error: %error', array('%tag' => $tag, '%error' => $e->getMessage()));
return '';
}
return drupal_render($element);
}
/**
* Helper function to assign orphaned images to the correct gallery.
*/
function _fishers_wordpress_migration_images_to_galleries() {
require_once drupal_get_path('module', 'media_gallery') . '/media_gallery.pages.inc';
// Get all unique galleryids.
$gids = db_select('field_data_field_nextgen_gid', 'fgid')
->fields('fgid', array('field_nextgen_gid_value'))
->groupBy('field_nextgen_gid_value')
->execute()
->fetchCol('field_nextgen_gid_value');
foreach($gids as $gid) {
// Pull in all files with a matching galleryid.
$query = new EntityFieldQuery;
// Get all files with this gid.
$files = $query
->entityCondition('entity_type', 'file')
->fieldCondition('field_nextgen_gid', 'value', $gid, '=')
->execute();
$_POST['files'] = array_keys($files['file']);
$query = new EntityFieldQuery;
// Get first gallery with this gid.
$node_bare = $query
->entityCondition('entity_type', 'node')
->fieldCondition('field_nextgen_gid', 'value', $gid, '=')
->range(0,1)
->execute();
// Add the files to the gallery.
media_gallery_add_images(node_load(current($node_bare['node'])->nid));
}
}
/**
* Implements hook_library().
*/
function fishers_wordpress_migration_library() {
$libraries = array();
$magnific_library_path = libraries_get_path('magnific-popup', FALSE);
$module_path = drupal_get_path('module', 'fishers_wordpress_migration');
if (empty($magnific_library_path)) {
drupal_set_message(t('Please install the Magnific Popup JS library.'), 'error');
return array();
}
$libraries['magnific_popup'] = array(
'title' => 'Magnific Popup jQuery Plugin',
'website' => 'http://dimsemenov.com/plugins/magnific-popup/',
'version' => '1',
'js' => array(
"{$magnific_library_path}/dist/jquery.magnific-popup.js" => array('type' => 'file'),
),
'css' => array(
"{$magnific_library_path}/dist/magnific-popup.css" => array('type' => 'file'),
),
'dependencies' => array(
array('system', 'jquery'),
),
);
return $libraries;
}
/**
* Internal utility function to check required library status.
*/
function _fishers_wordpress_migration_check_status() {
$jquery = drupal_get_library('system', 'jquery');
if (empty($jquery) || empty($jquery['version'])) {
// jQuery not available? Totally an error condition.
// @todo We should watchdog this, and/or report it on site status screen.
return FALSE;
}
// Check if the available jQuery version is 1.7.2+.
// @see http://dimsemenov.com/plugins/magnific-popup/documentation.html
if (version_compare($jquery['version'], '1.7.2') < 0) {
// $jquery['version'] is lower than 1.7.2.
// @todo We should watchdog this, and/or report it on site status screen.
//return FALSE;
}
return TRUE;
}
/**
* Implements hook_view().
*
* Adds magnific to galleries.
*/
function fishers_wordpress_migration_node_view($node, $view_mode, $langcode) {
// Add Magnific Popup library to page.
if ($node->type == 'media_gallery' && $view_mode == 'full') {
drupal_add_library('fishers_wordpress_migration', 'magnific_popup', FALSE);
drupal_add_js(drupal_get_path('module', 'fishers_wordpress_migration') .'/js/magnificPrep.js', 'file');
}
drupal_add_css(drupal_get_path('module', 'fishers_wordpress_migration') .'/css/magnificPrep.css', 'file');
}
/**
* Implements hook_init().
*
* Adds css to pages with pulled in node galleries.
*
* @see _media_gallery_attach_css_resources().
*/
function fishers_wordpress_migration_init(){
$path = drupal_get_path('module', 'media_gallery');
drupal_add_css($path . '/media_gallery.css');
drupal_add_css($path . '/media_gallery.ie7.css', array(
'browsers' => array('IE' => 'lt IE 8', '!IE' => FALSE))
);
}