media_library.modal.inc in Media Library 6
Modal frame functions
File
media_library.modal.incView source
<?php
/**
* @file
* Modal frame functions
*/
/**
* Main modal frame
*/
function media_library_modal() {
ctools_include('modal');
ctools_include('ajax');
ctools_include('object-cache');
$object = media_library_get_cache('media_obj');
if (!is_object($object)) {
$object = new stdClass();
}
// Textarea name
$textarea = $_GET['textarea'];
if (!empty($textarea)) {
$object->textarea = $textarea;
}
foreach ($_POST as $attr => $value) {
$object->{$attr} = $value;
}
ctools_object_cache_set('media_library', 'media_obj', $object);
if (empty($object->type)) {
ctools_modal_render(t('Choose a media content to add'), media_library_types_choice());
}
else {
// If we are editing an existing object, lets jump to it
media_library_modal_add($object->type);
}
ctools_modal_render(t('Error'), t('Error processing request'));
}
/**
* Renders the list of Types to be selected prior to inserting media.
*/
function media_library_types_choice() {
$types = media_library_get_types();
$output = '';
// Only one type, let's jump
if (count($types) == 1) {
media_library_modal_add(key($types));
}
foreach ($types as $type => $info) {
$title = filter_xss_admin($info['title']);
$description = isset($info['description']) ? $info['description'] : $info['title'];
$icon = $info['icon'];
$output .= '<div class="content-type-button clear-block">';
$url = "media-library/main/add/{$type}";
$output .= ctools_ajax_image_button($icon, $url, $description, 'media-library-modal-add-config');
$output .= '<div>' . ctools_ajax_text_button($title, $url, $description, 'media-library-modal-add-config') . '</div></div>';
}
if (empty($output)) {
$output = t('There are no media types enabled. Please enable at least one Media Library submodule');
}
return $output;
}
/**
* Second step in the modal dialog, for generating the multi-step form.
* @param
* type - media type name
* update - TRUE if updating an object
*/
function media_library_modal_add($type) {
ctools_include('modal');
ctools_include('ajax');
ctools_include('wizard');
ctools_include('object-cache');
// Let's get our step
$step = arg(4);
// Loads our object from cache.
$object = media_library_get_cache('media_obj');
$object->type = $type;
ctools_object_cache_set('media_library', 'media_obj', $object);
// Get our type info
$types = media_library_get_types();
if (!isset($types[$type])) {
ctools_modal_render(t('Error'), t('Specified type does not exist: ' . $type));
}
$module = $types[$type]['module'];
// Call the hook to get the steps list.
$steps = module_invoke($module, 'media_forms', $type);
if (empty($step)) {
$step = key($steps);
}
$order = array();
foreach ($steps as $step_name => &$info) {
$order[$step_name] = $info['label'];
$info['title'] = $info['label'];
unset($steps[$step_name]['label']);
}
$forms = $steps;
/**
* This is the CTools Wizard form setting
* Check CTools Advanced Help on topic Wizard for documentation
*/
$form_info = array(
'id' => 'media-library',
'path' => "media-library/main/add/{$type}/%step",
'title' => t('Insert !media', array(
'!media' => $types[$type]['title'],
)),
'show trail' => FALSE,
'show back' => FALSE,
'show cancel' => TRUE,
'show return' => FALSE,
'next text' => t('Next'),
'next callback' => 'media_library_modal_next',
'finish callback' => 'media_library_modal_finish',
'return callback' => 'media_library_modal_cancel',
'cancel callback' => 'media_library_modal_cancel',
'order' => $order,
'forms' => $forms,
);
$form_state = array(
'cache name' => 'media_obj',
'ajax' => TRUE,
'modal' => TRUE,
);
// Our object is to be saved here...
$form_state['media_obj'] = $object;
// Set default values when editing
foreach ($form_state['media_obj'] as $attr => $value) {
if ($attr == 'action') {
$form_state['action'] = $value;
}
else {
$form_state['update'][$attr] = $value;
}
}
ctools_wizard_multistep_form($form_info, $step, $form_state);
}
function media_library_modal_cancel(&$form_state) {
media_library_clear_cache('media_obj');
$form_state['commands'][] = ctools_modal_command_dismiss();
}
function media_library_modal_next(&$form_state) {
ctools_object_cache_set('media_library', 'media_obj', $form_state['media_obj']);
}
function media_library_modal_finish(&$form_state) {
// Generates the tag to be saved
$tag = media_library_create_filter_tag($form_state['media_obj']);
// Handles common textarea inserts
if (isset($form_state['media_obj']->textarea)) {
$form_state['commands'][] = array(
'command' => 'ml_insert',
'selector' => '#' . $textarea,
'data' => $tag,
);
}
elseif ($form_state['media_obj']->tiny) {
// Generates the preview
drupal_alter('filter_media', $form_state['media_obj']->tag, TRUE);
$types = media_library_get_types();
$form_state['media_obj']->preview = module_invoke($types[$form_state['media_obj']->type]['module'], 'filter_media', (array) $form_state['media_obj'], TRUE);
// Save the tag
$form_state['media_obj']->tag = $tag;
// Check for insert vs update
if (isset($form_state['action']) && $form_state['action'] == 'update') {
$command = 'ml_update_tiny';
}
else {
$command = 'ml_insert_tiny';
}
$form_state['commands'][] = array(
'command' => $command,
'data' => $form_state['media_obj'],
);
}
media_library_clear_cache('media_obj');
}
/**
* Remove an item from the object cache.
*/
function media_library_clear_cache($name) {
ctools_object_cache_clear('media_library', $name);
}
/**
* Get the cached changes to a given task handler.
*/
function media_library_get_cache($name) {
$cache = ctools_object_cache_get('media_library', $name);
return $cache;
}
/**
* Upon generating a preview for an existing tag on the content,
* Javascript will call this from ajax with the type argument
* The data is on $_POST, and returns the preview content.
*/
function media_library_ajax_preview($type) {
$object = new stdClass();
// Get stuff from POST
foreach ($_POST as $property => $value) {
$object->{$property} = $value;
}
// Generates our preview
drupal_alter('filter_media', $object->tag, TRUE);
$types = media_library_get_types();
$object->preview = module_invoke($types[$type]['module'], 'filter_media', (array) $object, TRUE);
drupal_json($object);
}
/**
* Utility for jumping to next step in modal sequence
*/
function media_library_modal_skip_step($form_state) {
drupal_goto(ctools_wizard_get_path($form_state['form_info'], $form_state['next']));
}
/**
* Utility to sub modules. This generates a form structure and add
* a pager for browsing media. This should be used in a modal context
*
* @param
* form_state - the form_state of the form
* total - the total amount of results
* pager - boolean that indicates if the pager needs to be setup first
*/
function media_library_modal_browse_form($form_state, $total = 0, $pager = TRUE) {
global $pager_page_array, $pager_total, $pager_total_items;
// Get variables
$limit = variable_get('media_library_limit', MEDIA_LIBRARY_LIMIT);
// Get total from pager_total_items
if (!$total) {
$total = $pager_total_items[0];
}
// Setup pager
if ($pager) {
$page = isset($_GET['page']) ? $_GET['page'] : 0;
$pages = ceil($total / $limit);
$pager_page_array = explode(',', $page);
$pager_total_items[0] = (int) $total;
$pager_total[0] = $pages;
$pager_page_array[0] = max(0, min((int) $pager_page_array[0], (int) $pager_total[0] - 1));
}
// Theme function
$form['#theme'] = 'media_library_browse_form';
// URL: A workaround from hell. Never caused any chaos, at least.
$type = $form_state['media_obj']->type;
$step = $form_state['step'];
$_GET['q'] = "media-library/main/add/{$type}/{$step}";
// Pager
$pager = '<div class="media-library-pager">' . theme('pager', array(), $limit, 0) . '</div>';
// This is a workaround, without it pager won't work.
// We must remove this from links so ctools response is adequate.
$pager = preg_replace('/ctools_multipart=1/', '', $pager);
$footer = theme('media_library_browse_footer', $total, $pages);
$form['#suffix'] = $footer . $pager;
return $form;
}
Functions
Name | Description |
---|---|
media_library_ajax_preview | Upon generating a preview for an existing tag on the content, Javascript will call this from ajax with the type argument The data is on $_POST, and returns the preview content. |
media_library_clear_cache | Remove an item from the object cache. |
media_library_get_cache | Get the cached changes to a given task handler. |
media_library_modal | Main modal frame |
media_library_modal_add | Second step in the modal dialog, for generating the multi-step form. |
media_library_modal_browse_form | Utility to sub modules. This generates a form structure and add a pager for browsing media. This should be used in a modal context |
media_library_modal_cancel | |
media_library_modal_finish | |
media_library_modal_next | |
media_library_modal_skip_step | Utility for jumping to next step in modal sequence |
media_library_types_choice | Renders the list of Types to be selected prior to inserting media. |