You are here

function rate_ui_write_css in Rate 7.2

Write CSS file for widget.

Parameters

array $storage:

Return value

string Filepath to CSS file

1 call to rate_ui_write_css()
rate_ui_widget_save_widget in ui/rate_ui.form.inc
Save widget settings from storage array into database.

File

ui/rate_ui.form.inc, line 1041
This file contains the differtent Rate UI forms.

Code

function rate_ui_write_css($storage) {
  $css = array();
  $type = $storage['type'];
  $images = $storage['images'];
  $sprites = $storage['sprites'];
  $separate = $storage['separate_images'];
  $imagesource = $storage['imagesource'];
  if ($images && $sprites) {

    // Start with the revoke button?
    $start = empty($storage['button0_label']) ? 1 : 0;
    for ($i = $start; $i <= $storage['buttoncount']; ++$i) {
      if (empty($storage["button{$i}_label"])) {

        // Button label is empty. Do not use this button.
        continue;
      }
      $kinds = array(
        'default' => '',
        'highlighted' => '.highlighted',
        'default_voted' => '.voted',
        'highlighted_voted' => '.voted.highlighted',
        'disabled' => '.disabled',
        'disabled_voted' => '.voted.disabled',
      );
      foreach ($kinds as $kind => $extra_classes) {

        // If we do not have separate buttons, the fields for all buttons have
        // "button1" in the name, except for the revoke button.
        $num = $separate && $i > 1 ? $i : 1;
        $x = (int) $storage["button{$num}_{$kind}_x"];
        $y = (int) $storage["button{$num}_{$kind}_y"];
        $width = (int) $storage["button{$num}_{$kind}_width"];
        $height = (int) $storage["button{$num}_{$kind}_height"];
        $source = $imagesource == 'upload' ? $storage['sprite_file'] : $storage['sprite_filepath'];
        if (is_numeric($source) && ($file = file_load($source))) {
          $source = file_create_url($file->uri);
        }
        else {
          $source = url($source);
        }
        $definition = ".rate-widget-{$type} .button{$i}{$extra_classes}";
        $css[$definition] = array();
        $css[$definition]['display'] = 'inline-block';
        $css[$definition]['width'] = "{$width}px";
        $css[$definition]['height'] = "{$height}px";
        $css[$definition]['text-indent'] = '-9999px';
        $css[$definition]['background'] = "url({$source}) -{$x}px -{$y}px";
      }
    }
  }
  if ($css) {
    $output = '';
    foreach ($css as $selector => $definitions) {
      $data = array();
      foreach ($definitions as $property => $value) {
        $data[] = "  {$property}: {$value};";
      }
      $definitions = implode("\n", $data);
      $output .= "{$selector} {\n{$definitions}\n}\n";
    }
    $filename = 'public://rate/' . $storage['type'] . '.css';
    file_put_contents($filename, $output);
    return $filename;
  }
}