You are here

function _simplemenu_add_css in SimpleMenu 7

Same name and namespace in other branches
  1. 6 simplemenu.module \_simplemenu_add_css()

\brief Generate the CSS and add it to the page.

This function generates the dynamic CSS and then insert that to the header of the page.

The function regenerates the CSS only when the settings were modified. Otherwise, it uses the cached version.

The function has a fall back, in case the dynamic CSS cannot be created.

1 call to _simplemenu_add_css()
simplemenu_init in ./simplemenu.module
Implements hook_init().

File

./simplemenu.module, line 117
Creates a simplemenu.

Code

function _simplemenu_add_css() {
  global $user;
  $simplemenu_path = drupal_get_path('module', 'simplemenu');
  $css_path = 'public://css';

  // same path as concatenated Core CSS
  if (file_prepare_directory($css_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
    $fix = variable_get('simplemenu_fix', 'scroll');

    // XXX add a variable simplemenu_update which is set to TRUE whenever
    //     the settings get modified and false here
    $output_filename = variable_get('simplemenu_css_filename', '');
    if (!$output_filename) {
      $tags = array(
        '@MENUBAR_ZINDEX@' => simplemnu_get_zindex('simplemenu_menubar_zindex', 9999),
        '@DROPDOWN_ZINDEX@' => simplemnu_get_zindex('simplemenu_dropdown_zindex', 9999),
      );
      switch ($fix) {
        case 'top':
          $tags['@FIX@'] = "position: fixed;\n  top: 0; left: 0;";
          break;
        case 'bottom':
          $tags['@FIX@'] = "position: fixed;\n  bottom: 0; left: 0;";
          break;
        default:

          // scroll
          $tags['@FIX@'] = 'position: relative;';
          break;
      }
      $css = file_get_contents($simplemenu_path . '/simplemenu.css.tpl');
      $css = strtr($css, $tags);
      $css_hash = hash('sha256', $css);
      $output_filename = $css_path . '/simplemenu-' . $css_hash . '.css';
      if (!file_exists($output_filename)) {

        // new content, create a new file
        file_put_contents($output_filename, $css);
      }
      else {

        // this call is rather ugly, but we must make sure that the
        // system cache will take the current Simplemenu CSS in account
        _drupal_flush_css_js();
      }

      //variable_set('simplemenu_css_filename', $output_filename);
    }
    drupal_add_css($output_filename);
  }
  else {

    // in case we cannot create the dynamic CSS
    $last_msg = variable_get('simplemenu_css_error', 0);
    if ($last_msg != -1 && $last_msg + 3600 > time() || $user->uid == 1) {

      // avoid displaying the error on each page... only once per hour.
      // (unless you are the admin, in which case you probably want to know!)
      variable_set('simplemenu_css_error', time());
      drupal_set_message(t('Simplemenu could not create the folder @path in order to save the dynamic CSS data.', array(
        '@path' => $css_path,
      )), 'warning');
    }

    // use a default that cannot react to the dynamic changes...
    drupal_add_css($simplemenu_path . '/simplemenu.css');
  }
}