View source
<?php
define('MEDIAFRONT_PRESET_MAX_LENGTH', 64);
function mediafront_preset_perm() {
return array(
'administer mediafront presets',
);
}
function mediafront_get_presets_path() {
return 'admin/build/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_preset_theme() {
$theme = array();
foreach (mediafront_preset_get_presets() as $preset) {
$theme['mediafront_formatter_' . $preset['name'] . '_player'] = array(
'arguments' => array(
'element' => NULL,
),
'function' => 'theme_mediafront_formatter_player',
);
}
return $theme;
}
function theme_mediafront_formatter_player($element) {
$preset = substr($element['#formatter'], 0, strrpos($element['#formatter'], '_'));
return theme('mediafront_player', $element['#node'], $preset);
}
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', $header, $rows);
}
else {
return 'No presets. To add a new preset, click on the <strong>Add Preset</strong> button.';
}
}
function mediafront_preset_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 = $preset['settings'] ? array_merge($preset['settings'], $params) : $params;
$form['preview']['player'] = array(
'#type' => 'markup',
'#value' => mediafront_get_player($params),
);
$preset['settings'] = mediafront_get_settings($preset['player'], $params);
$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'] = array(
'#type' => 'fieldset',
'#title' => t('Player Settings'),
'#weight' => -8,
'#collapsible' => true,
'#collapsed' => true,
'#tree' => TRUE,
);
$form['settings']['player_settings'] = array_merge(mediafront_player_settings_form($preset), $form['settings']['player_settings']);
$form['settings']['connect'] = array(
'#type' => 'fieldset',
'#title' => t('Player to Player'),
'#collapsible' => true,
'#collapsed' => true,
);
$result = mediafront_preset_get_presets();
$presets = array();
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' => $preset['connect']['iscontroller'],
'#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' => $preset['connect']['isplaylist'],
'#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)) {
$return = mediafront_preset_array_flatten($value, $return);
}
else {
$return[$key] = $value;
}
}
return $return;
}
function mediafront_preset_submit($form, &$form_state) {
$preset = array();
if (isset($form_state['values']['pid'])) {
$preset['pid'] = $form_state['values']['pid'];
}
$preset['name'] = $form_state['values']['name'];
$preset['description'] = $form_state['values']['description'];
$preset['player'] = $form_state['values']['player'];
$preset['connect'] = array(
'iscontroller' => isset($form_state['values']['iscontroller']) ? $form_state['values']['iscontroller'] : array(),
'isplaylist' => isset($form_state['values']['isplaylist']) ? $form_state['values']['isplaylist'] : array(),
);
$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_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_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, hyphens, 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,
'#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_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_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);
$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);
}
drupal_rebuild_theme_registry();
}
function mediafront_preset_get_default_presets() {
$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_query("SELECT * FROM {mediafront_preset} ORDER BY name ASC");
while ($preset = db_fetch_array($result)) {
$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) {
$preset = db_fetch_array(db_query("SELECT * FROM {mediafront_preset} WHERE name='%s'", $preset_name));
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];
}
}
return $preset;
}
function mediafront_preset_delete($preset_name) {
db_query("DELETE FROM {mediafront_preset} WHERE name='%s'", $preset_name);
drupal_rebuild_theme_registry();
}
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_result(db_query("SELECT COUNT(*) FROM {mediafront_preset} WHERE name='%s'", $preset_name));
}
}
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,
));
}
}