View source
<?php
define('MEDIAFRONT_PRESET_MAX_LENGTH', 64);
function mediafront_preset_permission() {
$perms = array(
'administer mediafront presets' => array(
'title' => t('Administer MediaFront Presets'),
'description' => t('Perform administration tasks for the MediaFront Presets.'),
),
);
return $perms;
}
function mediafront_get_presets_path() {
return 'admin/structure/mediafront';
}
function mediafront_preset_menu() {
$items = array();
$presets_path = mediafront_get_presets_path();
$items[$presets_path] = array(
'title' => 'MediaFront Presets',
'description' => 'Manage and configure the presets for MediaFront.',
'page callback' => 'mediafront_presets_overview',
'access arguments' => array(
'administer mediafront presets',
),
);
$items[$presets_path . '/list'] = array(
'title' => 'List',
'access arguments' => array(
'administer mediafront presets',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[$presets_path . '/add'] = array(
'title' => 'Add preset',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'mediafront_preset_create_form',
),
'access arguments' => array(
'administer mediafront presets',
),
'type' => MENU_LOCAL_TASK,
);
$items[$presets_path . '/import'] = array(
'title' => t('Import preset'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'mediafront_preset_import_form',
),
'access arguments' => array(
'administer mediafront presets',
),
'type' => MENU_LOCAL_TASK,
);
$file = 'includes/mediafront.preset.inc';
$items[$presets_path . '/preset/%mediafront_preset'] = array(
'title' => 'Edit MediaFront Preset',
'title callback' => 'mediafront_preset_page_title',
'title arguments' => array(
4,
),
'file' => $file,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'mediafront_preset_form',
4,
),
'access arguments' => array(
'administer mediafront presets',
),
);
$items[$presets_path . '/preset/%mediafront_preset/edit'] = array(
'title' => 'Edit',
'file' => $file,
'page arguments' => array(
4,
),
'access arguments' => array(
'administer mediafront presets',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items[$presets_path . '/preset/%mediafront_preset/delete'] = array(
'title' => 'Delete',
'file' => $file,
'page arguments' => array(
'mediafront_preset_delete_confirm',
4,
),
'access arguments' => array(
'administer mediafront presets',
),
'type' => MENU_CALLBACK,
);
$items[$presets_path . '/preset/%mediafront_preset/export'] = array(
'title' => t('Export'),
'file' => $file,
'title callback' => 'mediafront_preset_page_title',
'title arguments' => array(
4,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'mediafront_preset_export_form',
4,
),
'access arguments' => array(
'administer mediafront presets',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function mediafront_presets_overview() {
$presets = mediafront_preset_get_presets();
$presets_path = mediafront_get_presets_path();
if (count($presets) > 0) {
$header = array(
t('Preset Name'),
t('Description'),
array(
'data' => t('Operations'),
'colspan' => '3',
),
);
$rows = array();
foreach ($presets as $pid => $preset) {
$row = array();
$row[] = check_plain($preset['name']);
$row[] = array(
'data' => filter_xss_admin($preset['description']),
'class' => 'description',
);
$row[] = array(
'data' => l(t('edit'), $presets_path . '/preset/' . $preset['name']),
);
$row[] = array(
'data' => isset($preset['pid']) && is_numeric($preset['pid']) ? l(t('delete'), $presets_path . '/preset/' . $preset['name'] . '/delete') : '',
);
$row[] = array(
'data' => l(t('export'), $presets_path . '/preset/' . $preset['name'] . '/export'),
);
$rows[] = $row;
}
return theme('table', array(
'header' => $header,
'rows' => $rows,
));
}
else {
return 'No presets. To add a new preset, click on the <strong>Add Preset</strong> button.';
}
}
function mediafront_preset_form($form, &$form_state, $preset) {
$form['preview'] = array(
'#type' => 'fieldset',
'#title' => t('Player Preview'),
'#attributes' => array(
'id' => 'mediafront_admin_preview',
),
'#weight' => -10,
);
$preset = (array) $preset;
$params = array(
'admin' => true,
'player' => $preset['player'],
'preset' => $preset['name'],
);
$params = !empty($preset['settings']) ? array_merge($preset['settings'], $params) : $params;
$form['preview']['player'] = array(
'#type' => 'item',
'#markup' => mediafront_get_player($params),
);
$preset['settings'] = mediafront_get_settings($preset['player'], $params);
$form['#preset'] = $preset;
$form['settings'] = array(
'#type' => 'fieldset',
'#title' => t('Preset Settings'),
'#attributes' => array(
'id' => 'mediafront_admin_settings',
),
'#weight' => -9,
'#collapsible' => false,
'#collapsed' => false,
);
$form['settings']['preset'] = array(
'#type' => 'fieldset',
'#title' => t('Preset Information'),
'#weight' => -9,
'#collapsible' => true,
'#collapsed' => true,
);
if (isset($preset['pid'])) {
$form['settings']['preset']['pid'] = array(
'#type' => 'value',
'#value' => $preset['pid'],
);
}
$form['settings']['preset']['name'] = array(
'#type' => 'value',
'#value' => $preset['name'],
);
$form['settings']['preset']['description'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#description' => t('Add a brief description to your preset name.'),
'#default_value' => $preset['description'],
'#weight' => -9,
);
$form['settings']['preset']['player'] = array(
'#type' => 'value',
'#value' => $preset['player'],
);
$form['settings']['player_settings_wrapper'] = array(
'#type' => 'fieldset',
'#title' => t('Player Settings'),
'#weight' => -8,
'#collapsible' => true,
'#collapsed' => true,
);
$form['settings']['player_settings_wrapper']['player_settings'] = array(
'#type' => 'vertical_tabs',
'#tree' => TRUE,
);
$player_settings =& $form['settings']['player_settings_wrapper']['player_settings'];
$player_settings = array_merge(mediafront_player_settings_form($preset), $player_settings);
$form['settings']['connect'] = array(
'#type' => 'fieldset',
'#title' => t('Player to Player'),
'#collapsible' => true,
'#collapsed' => true,
);
$result = mediafront_preset_get_presets();
$presets = array(
0 => '-- None --',
);
foreach ($result as $pid => $otherPreset) {
if ($otherPreset['name'] != $preset['name']) {
$presets[$otherPreset['name']] = $otherPreset['name'];
}
}
if (isset($preset['connect']) && $preset['connect']) {
$form['settings']['connect']['iscontroller'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#title' => t('Make this preset the Control Bar for'),
'#description' => t('Select which preset you would like to control using this preset.'),
'#default_value' => !empty($preset['connect']['iscontroller']) ? $preset['connect']['iscontroller'] : 0,
'#options' => $presets,
);
$form['settings']['connect']['isplaylist'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#title' => t('Make this preset the Playlist for'),
'#description' => t('Select which preset you would like to play this playlist.'),
'#default_value' => !empty($preset['connect']['isplaylist']) ? $preset['connect']['isplaylist'] : 0,
'#options' => $presets,
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Preset'),
'#weight' => 30,
);
$form['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#weight' => 40,
'#submit' => array(
'mediafront_preset_delete_submit',
),
);
$form['#submit'][] = 'mediafront_preset_submit';
$form['#validate'][] = 'mediafront_preset_validate';
return $form;
}
function mediafront_preset_validate($form, &$form_state) {
}
function mediafront_preset_array_flatten($array, $return = array()) {
foreach ($array as $key => $value) {
if (is_array($value) && $key != 'plugins') {
$return = mediafront_preset_array_flatten($value, $return);
}
else {
$return[$key] = is_array($value) ? array_map('check_plain', $value) : $value;
}
}
return $return;
}
function mediafront_preset_submit($form, &$form_state) {
$preset = array();
if (isset($form_state['values']['pid'])) {
$preset['pid'] = check_plain($form_state['values']['pid']);
}
$preset['name'] = check_plain($form_state['values']['name']);
$preset['description'] = check_plain($form_state['values']['description']);
$preset['player'] = check_plain($form_state['values']['player']);
$is_controller = array();
if (isset($form_state['values']['iscontroller'])) {
foreach ($form_state['values']['iscontroller'] as $id) {
$id = check_plain($id);
$is_controller[$id] = $id;
}
}
$is_playlist = array();
if (isset($form_state['values']['isplaylist'])) {
foreach ($form_state['values']['isplaylist'] as $id) {
$id = check_plain($id);
$is_playlist[$id] = $id;
}
}
$preset['connect'] = array(
'iscontroller' => $is_controller,
'isplaylist' => $is_playlist,
);
$preset['settings'] = mediafront_get_settings($preset['player'], mediafront_preset_array_flatten($form_state['values']['player_settings']));
mediafront_preset_save($preset);
$form_state['redirect'] = mediafront_get_presets_path() . '/preset/' . $preset['name'];
}
function mediafront_preset_delete_submit($form, &$form_state) {
$preset['name'] = $form_state['values']['name'];
$form_state['redirect'] = mediafront_get_presets_path() . '/preset/' . $preset['name'] . '/delete';
}
function mediafront_preset_delete_confirm($form, &$form_state, $preset) {
$form['name'] = array(
'#type' => 'value',
'#value' => $preset['name'],
);
$message = t('Are you sure you want to delete the preset %name?', array(
'%name' => $preset['name'],
));
$caption = '<p>' . t('This action cannot be undone.') . '</p>';
return confirm_form($form, $message, mediafront_get_presets_path(), $caption, t('Delete'));
}
function mediafront_preset_delete_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
mediafront_preset_delete($form_state['values']['name']);
}
$form_state['redirect'] = mediafront_get_presets_path();
}
function mediafront_preset_create_form($form, &$form_state) {
$form['preset'] = array(
'#type' => 'fieldset',
'#title' => t('Preset Information'),
'#weight' => -10,
);
$form['preset']['name'] = array(
'#type' => 'textfield',
'#title' => t('Preset Name'),
'#maxlength' => MEDIAFRONT_PRESET_MAX_LENGTH,
'#description' => t('Spaces are NOT allowed; punctuation is not allowed except for periods, apostrophes, and underscores.'),
'#required' => TRUE,
'#weight' => -10,
);
$form['preset']['description'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#description' => t('Add a brief description to your preset name.'),
'#weight' => -9,
);
$players = mediafront_get_players();
$options = array();
foreach ($players as $name => $player) {
$options[$name] = $player['title'];
}
$form['preset']['player'] = array(
'#type' => 'select',
'#title' => t('Media Player'),
'#description' => t('Select which player you would like to use for this preset.'),
'#options' => $options,
'#default_value' => 'osmplayer',
'#weight' => -8,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Next'),
'#weight' => 30,
);
$form['#submit'][] = 'mediafront_create_preset_submit';
$form['#validate'][] = 'mediafront_create_preset_validate';
return $form;
}
function mediafront_create_preset_validate($form, &$form_state) {
if ($error = mediafront_validate_preset_name($form_state['values']['name'])) {
form_set_error('name', $error);
}
}
function mediafront_create_preset_submit($form, &$form_state) {
$preset = $form_state['values'];
$preset['name'] = strtolower($preset['name']);
mediafront_preset_save($preset);
$form_state['redirect'] = mediafront_get_presets_path() . '/preset/' . $preset['name'];
}
function mediafront_preset_import_form($form, &$form_state) {
$form['preset'] = array(
'#title' => t('Preset Code'),
'#type' => 'textarea',
'#rows' => 10,
'#description' => t('Copy the export text and paste it into this text field to import a new preset.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Import Preset'),
'#weight' => 30,
);
$form['#submit'][] = 'mediafront_preset_import_submit';
$form['#validate'][] = 'mediafront_preset_import_validate';
return $form;
}
function mediafront_preset_import_validate($form, &$form_state) {
$preset = '';
ob_start();
eval($form_state['values']['preset']);
ob_end_clean();
if (is_array($preset)) {
$name = isset($preset['name']) ? $preset['name'] : '';
if ($error = mediafront_validate_preset_name($name)) {
form_set_error('name', $error);
}
}
else {
form_set_error('name', 'Invalid preset import.');
}
$form_state['preset'] =& $preset;
}
function mediafront_preset_import_submit($form, &$form_state) {
$preset = $form_state['preset'];
mediafront_preset_save($preset);
$form_state['redirect'] = mediafront_get_presets_path() . '/preset/' . $preset['name'];
}
function mediafront_preset_export_form($form, &$form_state, $preset) {
unset($preset['pid']);
unset($preset['default']);
$code = var_export($preset, true);
$code = str_replace("=> \n ", '=> ', $code);
$code = '$preset = ' . $code . ';';
$lines = substr_count($code, "\n");
$form['export'] = array(
'#title' => t('Export Preset'),
'#type' => 'textarea',
'#value' => $code,
'#rows' => $lines,
'#description' => t('Copy the export text and paste it into another preset using the import function.'),
);
return $form;
}
function mediafront_player_settings_form($preset) {
$settings_form = $preset['player'] . '_player_settings_form';
if (function_exists($settings_form)) {
return $settings_form($preset);
}
return array();
}
function mediafront_preset_page_title($preset) {
return $preset['name'];
}
function mediafront_preset_save($preset) {
$pid = isset($preset['pid']) && $preset['pid'] ? array(
'pid',
) : array();
drupal_write_record('mediafront_preset', $preset, $pid);
cache_set('mediafront_preset_' . $preset['name'], $preset);
drupal_set_message('Preset (' . $preset['name'] . ') settings have been saved.');
$player_save = $preset['player'] . '_mediafront_preset_save';
if (function_exists($player_save)) {
$preset['settings'] = isset($preset['settings']) ? $preset['settings'] : array();
$preset['settings'] = mediafront_get_settings($preset['player'], $preset['settings']);
$player_save($preset);
}
}
function mediafront_preset_get_default_presets() {
$default_presets =& drupal_static(__FUNCTION__);
if ($default_presets) {
return $default_presets;
}
else {
$default_presets = module_invoke_all('mediafront_default_presets');
drupal_alter('mediafront_default_presets', $default_presets);
return $default_presets;
}
}
function mediafront_preset_get_presets() {
$presets = array();
$normal_presets = array();
$result = db_select('mediafront_preset', 'p')
->fields('p')
->orderBy('p.name', 'ASC')
->execute();
foreach ($result as $preset) {
$preset = (array) $preset;
$preset['connect'] = $preset['connect'] ? unserialize($preset['connect']) : array();
$preset['settings'] = $preset['settings'] ? unserialize($preset['settings']) : array();
$presets[$preset['pid']] = $preset;
$normal_presets[$preset['name']] = $preset['pid'];
}
foreach (mediafront_preset_get_default_presets() as $preset) {
if (!empty($preset['name']) && !isset($normal_presets[$preset['name']])) {
$presets[$preset['name']] = $preset;
}
}
return $presets;
}
function mediafront_preset_load($preset_name) {
return mediafront_get_preset($preset_name);
}
function mediafront_get_preset($preset_name, $reset = FALSE) {
$cache = cache_get('mediafront_preset_' . $preset_name);
if (!$reset && $cache) {
if (!empty($cache->data['settings']['id'])) {
unset($cache->data['settings']['id']);
}
return $cache->data;
}
else {
$preset = db_select('mediafront_preset', 'p')
->fields('p')
->condition('p.name', $preset_name)
->execute()
->fetchAssoc();
if ($preset) {
$preset['settings'] = $preset['settings'] ? unserialize($preset['settings']) : array();
$preset['connect'] = $preset['connect'] ? unserialize($preset['connect']) : array();
}
else {
$default_presets = mediafront_preset_get_default_presets();
if (isset($default_presets[$preset_name])) {
$preset = $default_presets[$preset_name];
}
}
if (!empty($preset['settings']['id'])) {
unset($preset['settings']['id']);
}
cache_set('mediafront_preset_' . $preset_name, $preset);
return $preset;
}
}
function mediafront_preset_delete($preset_name) {
db_delete('mediafront_preset')
->condition('name', $preset_name)
->execute();
drupal_theme_rebuild();
}
function mediafront_preset_name_exists($preset_name) {
$default_presets = mediafront_preset_get_default_presets();
if ($default_presets && isset($default_presets[$preset_name])) {
return true;
}
else {
return (bool) db_select('mediafront_preset', 'p')
->fields('p')
->condition('p.name', $preset_name)
->range(0, 1)
->execute()
->fetchField();
}
}
function mediafront_validate_preset_name($name) {
if (!$name) {
return t('You must enter a preset.');
}
if (mediafront_preset_name_exists($name)) {
return t('The preset name %name is already taken.', array(
'%name' => $name,
));
}
if (strpos($name, ' ') !== FALSE) {
return t('The preset name cannot contain a space.');
}
if (is_numeric($name[0])) {
return t('The preset name cannot begin with a number.');
}
if (preg_match('/[^a-z0-9_]/i', $name)) {
return t('The preset name contains an illegal character.');
}
if (drupal_strlen($name) > MEDIAFRONT_PRESET_MAX_LENGTH) {
return t('The preset name %name is too long: it must be %max characters or less.', array(
'%name' => $name,
'%max' => MEDIAFRONT_PRESET_MAX_LENGTH,
));
}
}