You are here

override_css.module in Override css 7

File

override_css.module
View source
<?php

define('OVERRIDE_CSS_FILE_PATH', 'override_css');

/**
 * Implements hook_menu().
 */
function override_css_menu() {
  $items = array();
  $items['admin/appearance/override_css/css'] = array(
    'title' => 'CSS',
    'page callback' => 'override_css_preview_css',
    'access arguments' => array(
      'override css admin',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'override_css.admin.inc',
    'weight' => 1,
  );
  $items['admin/appearance/override_css/admin'] = array(
    'title' => 'Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'override_css_admin_form',
    ),
    'access arguments' => array(
      'override css admin',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'override_css.admin.inc',
    'weight' => 2,
  );
  $items['admin/appearance/override_css/list/%override_css_selector/values'] = array(
    'title' => 'Values',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'override_css_selector_value',
      4,
    ),
    'access arguments' => array(
      'override css settings',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'override_css.admin.inc',
    'weight' => -10,
  );
  $items['admin/appearance/override_css/list/%override_css_selector/reset'] = array(
    'title' => 'Reset',
    'page callback' => 'override_css_reset_selector_page',
    'page arguments' => array(
      4,
    ),
    'access arguments' => array(
      'override css settings',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'override_css.admin.inc',
    'weight' => -9,
  );
  return $items;
}

/**
 * Implements hook_menu_alter().
 */
function override_css_menu_alter(&$items) {
  $items['admin/appearance/override_css']['type'] = MENU_LOCAL_TASK;
  $items['admin/appearance/override_css/list']['type'] = MENU_DEFAULT_LOCAL_TASK;
}

/**
 * Implements hook_init().
 */
function override_css_init() {
  global $theme;
  if (($theme != variable_get('admin_theme', 'seven') || variable_get('override_css_admin_theme', FALSE)) && variable_get('override_css_settings_enabled', TRUE)) {

    //add ctools css
    ctools_include('css');
    $override_css = ctools_css_retrieve('override-css');
    if (!empty($override_css)) {
      drupal_add_css($override_css, array(
        'group' => CSS_THEME,
        'every_page' => TRUE,
        'weight' => 10,
      ));
    }
    else {
      drupal_add_css(override_css_generate_css(), array(
        'type' => 'inline',
        'group' => CSS_THEME,
        'every_page' => FALSE,
        'weight' => 10,
        'media' => 'all',
      ));
    }

    //add fonts
    $fonts = fontyourface_get_fonts('enabled = 1');
    foreach ($fonts as $font) {
      fontyourface_font_registry($font);
    }
  }
}

/**
 * Implements hook_permission().
 */
function override_css_permission() {
  return array(
    'override css admin' => array(
      'title' => t('Administer override css.'),
      'description' => t(''),
    ),
    'override css settings' => array(
      'title' => t('Override css.'),
      'description' => t(''),
    ),
  );
}

/**
 * Implements hook_ctools_plugin_directory().
 */
function override_css_ctools_plugin_directory($module, $plugin) {
  if ($module == 'ctools' && !empty($plugin)) {
    return "plugins/{$plugin}";
  }
}

/**
 * Implements hook_views_api().
 */
function override_css_views_api() {
  return array(
    'api' => 2,
  );
}

/**
 * Implements hook_views_default_views().
 */
function override_css_views_default_views() {
  $return = array();
  $files = file_scan_directory(drupal_get_path('module', 'override_css') . '/views', "/\\.view.inc\$/");
  foreach ($files as $path => $file) {
    require $path;
    if (isset($view)) {
      $return[$view->name] = $view;
    }
  }
  return $return;
}

/**
 * Disable/enable a single selector group.
 */
function override_css_crud_set_status($object, $status) {
  ctools_include('export');
  $status = ctools_export_set_object_status($object, $status);
  override_css_store_css();
  return $status;
}

/**
 * Load the data for a single selector group.
 */
function override_css_selector_load($name) {
  ctools_include('export');
  $result = ctools_export_crud_load('override_css', $name);
  if ($result && isset($result->name)) {
    $result->properties_values = override_css_get_properties_values($result->name);
  }
  return $result ? (array) $result : FALSE;
}

/**
 * Load all selector groups.
 */
function override_css_selector_load_all($reset = FALSE, $filter = FALSE) {
  $selector_groups = array();
  ctools_include('export');
  foreach (ctools_export_crud_load_all('override_css', $reset) as $name => $selector_group) {
    $selector_groups[$name] = (array) $selector_group;

    //remove disabled items
    if ($filter && isset($selector_group->disabled) && $selector_group->disabled) {
      unset($selector_groups[$name]);
    }
  }
  if (count($selector_groups)) {
    $groups = array_keys($selector_groups);
    foreach ($groups as $name) {
      $selector_groups[$name]['properties_values'] = override_css_get_properties_values($name);
    }
  }
  return $selector_groups;
}

/**
 * Get additional properties values
 */
function override_css_get_properties_values($name) {
  $properties_values = db_select('override_css_properties', 'o')
    ->fields('o', array(
    'properties_values',
  ))
    ->condition('name', $name)
    ->execute()
    ->fetchField();
  return $properties_values ? unserialize($properties_values) : array();
}
function override_css_selector_reset($selector) {
  db_delete('override_css_properties')
    ->condition('name', $selector['name'])
    ->execute();
}
function override_css_properties_list($optionlist = FALSE) {
  $properties = array(
    'background_image' => array(
      'name' => 'background-image',
      'title' => 'Achtergrondafbeelding',
      'type' => 'image',
      'unit' => '',
    ),
    'background_color' => array(
      'name' => 'background-color',
      'title' => 'Achtergrondkleur',
      'type' => 'background-color',
      'unit' => '',
    ),
    'font_size' => array(
      'name' => 'font-size',
      'title' => 'Lettergrootte',
      'type' => 'size',
      'unit' => 'px',
    ),
    'color' => array(
      'name' => 'color',
      'title' => 'Letterkleur',
      'type' => 'color',
    ),
    'padding' => array(
      'name' => 'padding',
      'title' => 'Padding',
      'type' => 'size',
      'unit' => 'px',
    ),
    'margin' => array(
      'name' => 'margin',
      'title' => 'Marge',
      'type' => 'size',
      'unit' => 'px',
    ),
    'margin_top' => array(
      'name' => 'margin-top',
      'title' => 'Ruimte bovenkant',
      'type' => 'size',
      'unit' => 'px',
    ),
    'margin_bottom' => array(
      'name' => 'margin-bottom',
      'title' => 'Ruimte onderkant',
      'type' => 'size',
      'unit' => 'px',
    ),
    'border' => array(
      'name' => 'border',
      'title' => 'Rand',
      'type' => 'border',
      'unit' => '',
    ),
    'border_radius' => array(
      'name' => 'border-radius',
      'title' => 'Afronding van de hoeken',
      'type' => 'radius',
      'unit' => 'px',
    ),
    'box_shadow' => array(
      'name' => 'box-shadow',
      'title' => 'Schaduw',
      'type' => 'shadow',
      'unit' => '',
    ),
    'font_family' => array(
      'name' => 'font-family',
      'title' => 'Lettertype',
      'type' => 'font',
      'unit' => '',
    ),
  );
  ksort($properties);
  if ($optionlist) {
    $values = array_keys($properties);
    return array_combine($values, $values);
  }
  else {
    return $properties;
  }
}

/**
 * Title callback
 **/
function override_css_selector_title($selector) {
  return 'Selector "' . $selector['title'] . '"';
}

/**
 * Store css
 *
 * @return -
 */
function override_css_store_css() {
  ctools_include('css');
  $css = override_css_generate_css();
  ctools_css_store('override-css', $css, FALSE);
}

/**
 * Generate css
 *
 * @return string Generated css
 */
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;
}
function override_css_html2rgb($color) {
  if ($color[0] == '#') {
    $color = substr($color, 1);
  }
  if (strlen($color) == 6) {
    list($r, $g, $b) = array(
      $color[0] . $color[1],
      $color[2] . $color[3],
      $color[4] . $color[5],
    );
  }
  elseif (strlen($color) == 3) {
    list($r, $g, $b) = array(
      $color[0] . $color[0],
      $color[1] . $color[1],
      $color[2] . $color[2],
    );
  }
  else {
    return false;
  }
  $r = hexdec($r);
  $g = hexdec($g);
  $b = hexdec($b);
  return array(
    $r,
    $g,
    $b,
  );
}
function override_css_font_option_list() {
  $list = array();
  $list[''] = 'Default';
  $list['Lokaal'] = override_css_local_font_list();
  $external_fonts = override_css_external_font_list();
  if (count($external_fonts)) {
    $list['Extern'] = $external_fonts;
  }
  return $list;
}
function override_css_font_list_preview() {
  $external_fonts = override_css_external_font_list();
  return override_css_local_font_list_preview() . (count($external_fonts) ? override_css_external_font_list_preview() : '');
}

/*
 * Local font families
 * Source: http://www.onedesigns.com/tutorials/font-families-for-cross-compatible-typography
 */
function override_css_local_font_list() {
  $local = array(
    'arial' => 'Arial, Helvetica, Tahoma, sans-serif',
    'helvetica' => 'Helvetica, Arial, "Nimbus Sans L", sans-serif',
    'myriad' => '"Myriad Pro", Arial, Helvetica, Tahoma, sans-serif',
    'verdana' => 'Verdana, Geneva, "DejaVu Sans", sans-serif',
    'lucida' => '"Lucida Grande", "Lucida Sans Unicode", "Bitstream Vera Sans",sans-serif',
    'georgia' => 'Georgia, "URW Bookman L", serif',
    'palatino' => 'Palatino, "Palatino Linotype", "URW Palladio L", serif',
    'courier' => '"Courier New", Courier, "Nimbus Mono L", monospace',
  );
  ksort($local);
  return $local;
}

/*
 * Local font families preview
 */
function override_css_local_font_list_preview() {
  $output = '';
  $fonts = override_css_local_font_list();
  $blocks = array();
  foreach ($fonts as $font => $family) {
    $blocks[] = '<div>' . ucfirst($font) . '<div><span style=\'font-family: ' . $family . '; color: #000000; font-style: normal; font-weight: normal; font-size: 40px; line-height: 40px;\'>AaGg</span></div></div>';
  }

  //tabelrijen samenstellen
  $rows = array();
  $row = 0;
  $columns = 0;
  foreach ($blocks as $block) {
    $rows[$row][] = $block;
    $columns++;
    if ($columns == 4) {
      $row++;
      $columns = 0;
    }
  }

  // lege kolommen opvullen
  if ($columns > 0 && $columns < 4) {
    while ($columns < 4) {
      $rows[$row][] = '';
      $columns++;
    }
  }
  return '<strong>Preview lokale lettertypes</strong>' . theme('table', array(
    'rows' => $rows,
    'attributes' => array(
      'class' => 'views-view-grid',
    ),
  ));
}

/*
 * External fonts
 */
function override_css_external_font_list() {
  $external = array();
  $fonts = fontyourface_get_fonts('enabled = 1');
  foreach ($fonts as $font) {
    $external[$font->fid] = $font->name;
  }
  return $external;
}

/*
 * External fonts preview
 */
function override_css_external_font_list_preview() {
  return '<strong>Preview externe lettertypes</strong>' . views_embed_view('override_css_enabled_fonts');
}

/**
 * Dummy page for menu callbacks
 */
function override_css_page() {
  return 'hello';
}