You are here

function override_css_generate_css in Override css 7

Generate css

Return value

string Generated css

3 calls to override_css_generate_css()
override_css_init in ./override_css.module
Implements hook_init().
override_css_preview_css in ./override_css.admin.inc
override_css_store_css in ./override_css.module
Store css

File

./override_css.module, line 313

Code

function override_css_generate_css() {
  $css = '';
  $available_properties = override_css_properties_list();
  $selectors = override_css_selector_load_all(TRUE, TRUE);
  foreach ($selectors as $selector) {
    $css .= $selector['selectors'] . ' { ';
    $properties = array_filter($selector['properties']);
    foreach ($properties as $property) {
      $disabled = isset($selector['properties_values'][$property . '_disabled']) ? $selector['properties_values'][$property . '_disabled'] : FALSE;
      if (isset($selector['properties_values'][$property]) && !empty($selector['properties_values'][$property]) || $disabled) {
        $name = $available_properties[$property]['name'];
        switch ($available_properties[$property]['type']) {
          case 'border':
            $border = array();
            if ($selector['properties_values'][$property . '_disabled']) {
              $border[] = '0';
            }
            else {
              $border[] = (int) $selector['properties_values'][$property . '_size'] . 'px';
              $border[] = $selector['properties_values'][$property . '_type'];
              $border[] = $selector['properties_values'][$property];
            }
            $border_string = implode(' ', $border);
            $css .= $name . ': ' . $border_string . '; ';
            break;
          case 'shadow':
            $shadow = array();
            if ($selector['properties_values'][$property . '_disabled']) {
              $shadow[] = 'none';
            }
            else {
              $shadow[] = (int) $selector['properties_values'][$property . '_horizontal'] . 'px';
              $shadow[] = (int) $selector['properties_values'][$property . '_vertical'] . 'px';
              $shadow[] = (int) $selector['properties_values'][$property . '_blur'] . 'px';
              $shadow[] = (int) $selector['properties_values'][$property . '_spread'] . 'px';
              $shadow[] = $selector['properties_values'][$property];
              $shadow[] = $selector['properties_values'][$property . '_direction'];
            }
            $shadow_string = implode(' ', $shadow);
            $css .= '-webkit-' . $name . ': ' . $shadow_string . '; ';
            $css .= '-moz-' . $name . ': ' . $shadow_string . '; ';
            $css .= $name . ': ' . $shadow_string . '; ';
            break;
          case 'color':
            if ($selector['properties_values'][$property . '_disabled']) {
              $css .= $name . ': inherit; ';
            }
            else {
              $css .= $name . ': ' . $selector['properties_values'][$property] . '; ';
            }
            break;
          case 'background-color':
            if ($selector['properties_values'][$property . '_disabled']) {
              $css .= $name . ': transparent; ';
            }
            else {
              $percent = (int) $selector['properties_values'][$property . '_opacity'];
              if ($percent > 0) {
                $rgb = override_css_html2rgb($selector['properties_values'][$property]);
                $opacity = 100 - $percent;
                $css .= 'background-color: rgb(' . $rgb[0] . ', ' . $rgb[1] . ', ' . $rgb[2] . ');';
                $css .= 'background-color: rgba(' . $rgb[0] . ', ' . $rgb[1] . ', ' . $rgb[2] . ', ' . round($opacity / 100, 2) . ');';
              }
              else {
                $css .= $name . ': ' . $selector['properties_values'][$property] . '; ';
              }
            }
            break;
          case 'image':
            if ($selector['properties_values'][$property . '_disabled']) {
              $css .= $name . ': none; ';
            }
            else {

              //image
              module_load_include('inc', 'system', 'system.admin');
              $image_path = file_create_url($selector['properties_values'][$property]);
              $css .= $image_path ? $name . ': url(' . $image_path . '); ' : '';

              //extra properties
              $css .= 'background-position: ' . $selector['properties_values'][$property . '_position_horizontal'] . ' ' . $selector['properties_values'][$property . '_position_vertical'] . ';';
              $css .= 'background-attachment: ' . $selector['properties_values'][$property . '_attachment'] . ';';
              $css .= 'background-repeat: ' . $selector['properties_values'][$property . '_repeat'] . ';';
              switch ($selector['properties_values'][$property . '_stretch']) {
                case 'horizontal':
                  $css .= 'background-size: 100% auto;';
                  break;
                case 'vertical':
                  $css .= 'background-size: auto 100%;';
                  break;
                case 'both':
                  $css .= 'background-size: 100% 100%;';
                  break;
              }
            }
            break;
          case 'radius':
            if ($selector['properties_values'][$property . '_disabled']) {
              $css .= '-webkit-' . $name . ': 0; ';
              $css .= '-moz-' . $name . ': 0; ';
              $css .= $name . ': 0; ';
            }
            else {
              $suffix = isset($available_properties[$property]['unit']) ? $available_properties[$property]['unit'] : '';
              $css .= '-webkit-' . $name . ': ' . $selector['properties_values'][$property] . $suffix . '; ';
              $css .= '-moz-' . $name . ': ' . $selector['properties_values'][$property] . $suffix . '; ';
              $css .= $name . ': ' . $selector['properties_values'][$property] . $suffix . '; ';
            }
            break;
          case 'font':
            if ($selector['properties_values'][$property . '_disabled']) {
              $css .= $name . ': inherit; ';
            }
            else {

              //local fonts
              if (!is_numeric($selector['properties_values'][$property])) {
                $fonts = override_css_local_font_list();
                if (array_key_exists($selector['properties_values'][$property], $fonts)) {
                  $css .= $name . ': ' . $fonts[$selector['properties_values'][$property]] . '; ';
                }
              }
              else {
                $fonts = override_css_external_font_list();
                if ($font = fontyourface_get_fonts('enabled = 1 AND fid = ' . $selector['properties_values'][$property])) {
                  if (isset($font[0])) {
                    $css .= fontyourface_font_css($font[0]);
                  }
                }
              }
            }
            break;
          default:
            if ($selector['properties_values'][$property . '_disabled']) {
              $css .= $name . ': none; ';
            }
            else {
              $suffix = isset($available_properties[$property]['unit']) ? $available_properties[$property]['unit'] : '';
              $css .= $name . ': ' . $selector['properties_values'][$property] . $suffix . '; ';
            }
            break;
        }
      }
    }
    $css .= "}\n";
  }
  return $css;
}