View source
<?php
define('DYNAMIC_BACKGROUND_BLOG_PATH', 'blog');
function dynamic_background_blog_permission() {
return array(
'configure blog dynamic background' => array(
'title' => t('Configure blog dynamic background'),
),
'user selection of blog background' => array(
'title' => t('Allow selection of background on user blog'),
),
);
}
function dynamic_background_blog_menu() {
$items = array();
$items['admin/config/user-interface/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_style_form('dynamic_background_blog_image_style');
$form += dynamic_background_css_behaviour_form('dynamic_background_blog_css');
return system_settings_form($form);
}
function dynamic_background_blog_form($form, $form_state, $user) {
$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']))) {
$res = db_update('dynamic_background_blog')
->fields(array(
'image_id' => $image_id,
))
->condition('uid', $form_state['values']['uid'])
->execute();
}
else {
db_insert('dynamic_background_blog')
->fields(array(
'uid' => $form_state['values']['uid'],
'image_id' => $image_id,
))
->execute();
}
}
else {
db_delete('dynamic_background_blog')
->condition('uid', $form_state['values']['uid'])
->execute();
}
}
function dynamic_background_blog_get_image_id($uid) {
$query = 'SELECT image_id FROM {dynamic_background_blog} WHERE uid=:uid';
$values = array(
':uid' => $uid,
);
$image_id = db_query($query, $values)
->fetchField();
if ($image_id === FALSE) {
return NULL;
}
return $image_id;
}
function dynamic_background_blog_dynamic_background_css($vars) {
$uid = NULL;
if (isset($vars['node']) && $vars['node']->type == 'blog') {
$uid = $vars['node']->uid;
}
else {
$parts = explode('/', drupal_get_path_alias());
if (count($parts) >= 2 && $parts[0] == DYNAMIC_BACKGROUND_BLOG_PATH && is_numeric($parts[1])) {
$uid = (int) $parts[1];
}
}
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]['picture'])) {
$image_style = variable_get('dynamic_background_blog_image_style', FALSE);
return array(
'image' => $backgrounds[$image_id]['picture'],
'configuration' => variable_get('dynamic_background_blog_css', array()),
'image_style' => $image_style ? $image_style['style'] : FALSE,
);
}
}
}
}
function dynamic_background_blog_dynamic_background_weight() {
return array(
'weight' => -35,
);
}