You are here

function dynamic_background_create_css in Dynamic Background 7

Same name and namespace in other branches
  1. 6 dynamic_background.module \dynamic_background_create_css()
  2. 7.2 dynamic_background.module \dynamic_background_create_css()

Helper function that creates a CSS based on user supplied css.

Parameters

array $images_conf:

boolean $reset optional:

Return value

string $css or FALSE if custom CSS have not been defined

2 calls to dynamic_background_create_css()
dynamic_background_css in ./dynamic_background.module
Menu callback function used to generate an body style css with the selected background image. The callback is /background.css.
dynamic_background_preprocess_html in ./dynamic_background.module
Page preprocess function used to create the $background variable, so it can be used in html.tpl.php. If selected have selected to use custom CSS, the image will automatically be added to the page header.

File

./dynamic_background.module, line 273
This module enables administrators to upload images used as background on the site. The selected background image link is exposed as either $background in the page.tpl file or as /background.css.

Code

function dynamic_background_create_css($images_conf, $reset = FALSE) {
  static $css;
  if (!isset($css) || $reset) {

    // Build style array based on weight, this will allow weight base override
    // at the same time allowing different selectors.
    $style_array = array();
    foreach ($images_conf as $image_conf) {

      // Only use image if css behaviour have be set.
      if (!empty($image_conf['configuration'])) {

        // Add image style, if one have been defined.
        $image = $image_conf['image'];
        if (isset($image_conf['image_style']) && $image_conf['image_style']) {

          // Image style found, so update the image path with an image style
          // based one.
          $image = image_style_url($image_conf['image_style'], $image);
        }
        else {
          $image = file_create_url($image);
        }

        // Check if selector have been used, if it have and has a higher weight
        // override it.
        if (isset($style_array[$image_conf['configuration']['selector']])) {
          if ($style_array[$image_conf['configuration']['selector']]['weight'] > $image_conf['weight']) {

            // This css selector have been used before and the new whan was
            // fater, so override the select in the style array.
            $style_array[$image_conf['configuration']['selector']] = array(
              'css' => $image_conf['configuration']['css'],
              'image' => $image,
              'weight' => $image_conf['weight'],
            );
          }
        }
        else {

          // This selector have not been used before, so add it to the array.
          $style_array[$image_conf['configuration']['selector']] = array(
            'css' => $image_conf['configuration']['css'],
            'image' => $image,
            'weight' => $image_conf['weight'],
          );
        }
      }
    }

    // Build css based on weighted style array.
    $css = '';
    foreach ($style_array as $selector => $style) {
      $css .= $selector . " {\n        background-image: url('" . $style['image'] . "');\n        " . $style['css'] . "\n      }\n";
    }
  }
  return $css;
}