View source
<?php
function designkit_theme() {
return array(
'designkit_image' => array(
'arguments' => array(
'name' => '',
'filepath' => '',
),
),
'designkit_colorpicker' => array(
'arguments' => array(
'element' => array(),
),
),
'designkit' => array(
'template' => 'designkit',
'arguments' => array(
'color' => array(),
'image' => array(),
),
),
);
}
function designkit_preprocess_page(&$vars) {
$info = designkit_get_info();
$color = !empty($info['designkit']['color']) ? variable_get('designkit_color', array()) : array();
$image = !empty($info['designkit']['image']) ? variable_get('designkit_image', array()) : array();
if (!empty($info['designkit']['image'])) {
foreach (array_keys($info['designkit']['image']) as $name) {
if (isset($vars[$name])) {
unset($vars[$name]);
}
}
}
foreach ($image as $name => $fid) {
$file = db_fetch_object(db_query('SELECT * FROM {files} f WHERE f.fid = %d', $fid));
if ($file && $file->filepath && file_exists($file->filepath)) {
$image[$name] = $file->filepath;
$vars[$name] = theme('designkit_image', $name, $file->filepath);
}
else {
unset($image[$name]);
}
}
if ($image || array_filter($color, 'designkit_valid_color')) {
$vars['body_classes'] .= " designkit";
$styles = theme('designkit', $color, $image);
$vars['styles'] .= $styles;
$vars['designkit'] = $styles;
}
}
function designkit_imagecache_default_presets() {
$presets = array();
$info = designkit_get_info();
if (!empty($info['designkit']['image'])) {
foreach ($info['designkit']['image'] as $name => $image_info) {
if (isset($image_info['imagecache'])) {
list($action, $dimensions) = explode(':', $image_info['imagecache']);
list($width, $height) = explode('x', $dimensions);
$valid_actions = imagecache_action_definitions();
if (isset($valid_actions[$action]) && is_numeric($width) && is_numeric($height)) {
$presets["designkit-image-{$name}"] = array(
'presetname' => "designkit-image-{$name}",
'actions' => array(
0 => array(
'weight' => '0',
'module' => 'imagecache',
'action' => $action,
'data' => array(
'fit' => 'inside',
'width' => $width,
'height' => $height,
),
),
),
);
}
}
}
}
return $presets;
}
function designkit_valid_color($color) {
$matches = array();
preg_match('/(#[0-9a-f]{6}|#[0-9a-f]{3})/i', $color, $matches);
if ($matches && (strlen($color) === 7 || strlen($color) === 3)) {
return TRUE;
}
return FALSE;
}
function designkit_get_info($reset = FALSE) {
static $info;
if (!isset($info) || $reset) {
global $theme_key, $theme_info;
if (isset($theme_info, $theme_key) && $theme_key == variable_get('theme_default', 'garland')) {
$info = $theme_info->info;
}
else {
$default = variable_get('theme_default', 'garland');
$result = db_query("SELECT name,type,info FROM {system} WHERE type = 'theme' AND name = '%s'", $default);
while ($row = db_fetch_object($result)) {
$info = unserialize($row->info);
}
}
}
return isset($info) ? $info : FALSE;
}
function designkit_colorshift($source, $shift, $opacity = 0.5) {
if (designkit_valid_color($source) && designkit_valid_color($shift)) {
$source = _color_unpack($source, TRUE);
$shift = _color_unpack($shift, TRUE);
$shifted = array();
foreach (array_keys($source) as $key) {
$shifted[$key] = $source[$key] + ($shift[$key] - $source[$key]) * $opacity;
}
return _color_pack($shifted, TRUE);
}
return $source;
}
function designkit_colorhsl($source, $key = NULL) {
if (designkit_valid_color($source)) {
$source = _color_unpack($source, TRUE);
$hsl = _color_rgb2hsl($source);
$keys = array(
'h' => 0,
's' => 1,
'l' => 2,
);
if (isset($key, $keys[$key])) {
return isset($hsl[$keys[$key]]) ? $hsl[$keys[$key]] : NULL;
}
return $hsl;
}
return NULL;
}
function designkit_form_spaces_features_form_alter(&$form, &$form_state) {
module_load_include('inc', 'designkit', 'designkit.admin');
_designkit_form_alter($form, $form_state);
}
function designkit_form_system_theme_settings_alter(&$form, &$form_state) {
module_load_include('inc', 'designkit', 'designkit.admin');
_designkit_form_alter($form, $form_state);
$form['buttons']['#weight'] = !isset($form['buttons']['#weight']) ? 10 : $form['buttons']['#weight'];
$form['logo']['#access'] = !isset($form['designkit_image']['logo']);
$form['#submit'][] = '_designkit_system_theme_settings_submit';
}
function template_preprocess_designkit(&$vars) {
foreach ($vars['color'] as $key => $color) {
$vars[$key] = $color;
}
foreach ($vars['image'] as $key => $filepath) {
$vars[$key] = imagecache_create_url("designkit-image-{$key}", $filepath);
$vars["{$key}_raw"] = file_create_url($filepath);
}
}
function theme_designkit_image($name, $filepath) {
return theme('imagecache', "designkit-image-{$name}", $filepath);
}
function theme_designkit_colorpicker($element) {
drupal_add_css('misc/farbtastic/farbtastic.css', 'module', 'all', FALSE);
drupal_add_js('misc/farbtastic/farbtastic.js');
drupal_add_js(drupal_get_path('module', 'designkit') . '/designkit.js');
$output = theme('textfield', $element);
$output .= "<div id='{$element['#id']}-colorpicker' style='display:none;'></div>";
return $output;
}