View source
<?php
function designkit_theme() {
return array(
'designkit_image' => array(
'variables' => array(
'name' => '',
'uri' => '',
'process' => TRUE,
),
),
'designkit_colorpicker' => array(
'render element' => 'element',
'file' => 'designkit.admin.inc',
),
'designkit' => array(
'template' => 'designkit',
'variables' => array(
'color' => array(),
'image' => array(),
),
),
);
}
function designkit_preprocess_html(&$variables) {
_designkit_preprocess($variables);
}
function designkit_preprocess_page(&$variables) {
_designkit_preprocess($variables);
}
function _designkit_preprocess(&$variables) {
static $color;
static $image;
static $styles;
$info = designkit_get_info();
if (empty($styles)) {
$color = !empty($info['designkit']['color']) ? variable_get('designkit_color', array()) : array();
$image = designkit_get_image_info();
if ($image || array_filter($color, 'designkit_valid_color')) {
$styles = theme('designkit', array(
'color' => $color,
'image' => $image,
));
drupal_add_css($styles, array(
'type' => 'inline',
'group' => CSS_THEME,
'weight' => 200,
));
}
}
if (!empty($info['designkit']['image'])) {
foreach (array_keys($info['designkit']['image']) as $name) {
if (isset($variables[$name])) {
unset($variables[$name]);
}
}
}
$variables['classes_array'][] = 'designkit';
$variables['designkit'] = $styles;
foreach ($image as $name => $options) {
if ($name == 'logo') {
continue;
}
$variables[$name] = theme('designkit_image', $options);
}
}
function designkit_image_default_styles() {
$styles = array();
$info = designkit_get_info();
if (!empty($info['designkit']['image'])) {
foreach ($info['designkit']['image'] as $name => $image_info) {
if (isset($image_info['effect'])) {
list($effect, $dimensions) = explode(':', $image_info['effect']);
list($width, $height) = explode('x', $dimensions);
$valid_effects = image_effect_definitions();
if (isset($valid_effects[$effect]) && is_numeric($width) && is_numeric($height)) {
$styles["designkit-image-{$name}"] = array(
'effects' => array(
array(
'weight' => 0,
'name' => $effect,
'data' => array(
'fit' => 'inside',
'upscale' => 1,
'width' => $width,
'height' => $height,
),
),
),
);
}
}
}
}
return $styles;
}
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', 'bartik')) {
$info = $theme_info->info;
}
else {
$default = variable_get('theme_default', 'bartik');
$info = db_select('system', 's')
->fields('s', array(
'info',
))
->condition('type', 'theme')
->condition('name', $default)
->execute()
->fetchField();
$info = unserialize($info);
}
}
return isset($info) ? $info : FALSE;
}
function designkit_get_image_info() {
$info = designkit_get_info();
$image = !empty($info['designkit']['image']) ? variable_get('designkit_image', array()) : array();
foreach ($image as $name => $fid) {
$file = db_select('file_managed', 'f')
->fields('f')
->condition('fid', $fid)
->execute()
->fetchObject();
if ($file && $file->uri && file_exists($file->uri)) {
$image[$name] = array(
'name' => $name,
'uri' => $file->uri,
'process' => !empty($info['designkit']['image'][$name]['effect']),
);
}
else {
unset($image[$name]);
}
}
return $image;
}
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['logo']['#access'] = !isset($form['designkit_image']['logo']);
array_unshift($form['#validate'], '_designkit_system_theme_settings_validate_pre');
$form['#validate'][] = '_designkit_system_theme_settings_validate_post';
$form['#submit'][] = '_designkit_system_theme_settings_submit';
}
function template_preprocess_designkit(&$variables) {
foreach ($variables['color'] as $key => $color) {
$variables[$key] = $color;
}
foreach ($variables['image'] as $key => $options) {
$variables["{$key}_raw"] = $variables[$key] = file_create_url($options['uri']);
if (!empty($options['process'])) {
$variables[$key] = image_style_url("designkit-image-{$key}", $options['uri']);
}
}
}
function theme_designkit_image($variables) {
if (!empty($variables['process'])) {
return theme('image_style', array(
'style_name' => "designkit-image-{$variables['name']}",
'path' => $variables['uri'],
));
}
else {
return theme('image', array(
'path' => $variables['uri'],
));
}
}