View source
<?php
define('DYNAMIC_BACKGROUND_BLOG_PATH', 'blog');
function dynamic_background_blog_perm() {
return array(
'configure blog dynamic background',
'user selection of blog background',
);
}
function dynamic_background_blog_menu() {
$items = array();
$items['admin/build/backgrounds/blog'] = array(
'title' => 'Blog',
'description' => t('Configure blog dynamic background'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'dynamic_background_blog_admin_form',
),
'access arguments' => array(
'configure blog dynamic background',
),
'type' => MENU_LOCAL_TASK,
'weight' => -10,
);
$items['user/%user/blog/background'] = array(
'title' => 'My blog background',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'dynamic_background_blog_form',
1,
),
'access arguments' => array(
'user selection of blog background',
),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function dynamic_background_blog_admin_form() {
$form = array(
'#tree' => TRUE,
);
$form += dynamic_background_image_presents_form('dynamic_background_blog_imagecache');
$form += dynamic_background_css_behaviour_form('dynamic_background_blog_css');
return system_settings_form($form);
}
function dynamic_background_blog_form($form_id, $user) {
$form = array();
$form['uid'] = array(
'#type' => 'hidden',
'#value' => $user->uid,
);
$form['dynamic_background'] = dynamic_background_image_selector_form(dynamic_background_blog_get_image_id($user->uid));
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Update background'),
);
return $form;
}
function dynamic_background_blog_form_submit($form, &$form_state) {
$image_id = NULL;
foreach ($form_state['values']['dynamic_background'] as $key => $value) {
if ($value['selected']) {
$image_id = $key;
break;
}
}
if (!is_null($image_id)) {
if (!is_null(dynamic_background_blog_get_image_id($form_state['values']['uid']))) {
db_query('UPDATE {dynamic_background_blog} SET image_id = %d WHERE uid = %d', $image_id, $form_state['values']['uid']);
}
else {
db_query('INSERT INTO {dynamic_background_blog} VALUES (%d, %d)', $form_state['values']['uid'], $image_id);
}
}
else {
db_query('DELETE FROM {dynamic_background_blog} WHERE uid=%d', $form_state['values']['uid']);
}
}
function dynamic_background_blog_get_image_id($uid) {
$result = db_query('SELECT image_id FROM {dynamic_background_blog} WHERE uid=%d', $uid);
$result = db_fetch_object($result)->image_id;
if ($result === FALSE) {
return NULL;
}
return $result;
}
function dynamic_background_blog_dynamic_background_css($vars) {
$uid = NULL;
if (isset($vars['node']) && $vars['node']->type == 'blog') {
$uid = $vars['node']->uid;
}
else {
$parts = split('/', $_GET['q']);
if (count($parts) >= 2 && $parts[0] == DYNAMIC_BACKGROUND_BLOG_PATH && is_numeric($parts[1])) {
$uid = (int) $parts[1];
}
}
$imagecache = variable_get('dynamic_background_blog_imagecache', FALSE);
if (!is_null($uid)) {
$image_id = dynamic_background_blog_get_image_id($uid);
if (!is_null($image_id)) {
$backgrounds = variable_get('dynamic_background_images', array());
if (isset($backgrounds[$image_id]['default'])) {
return array(
'image' => $backgrounds[$image_id]['default'],
'configuration' => variable_get('dynamic_background_blog_css', array()),
'imagecache' => $imagecache ? $imagecache['present'] : FALSE,
'weight' => 230,
);
}
}
}
}