You are here

live_css.module in Live CSS 7

File

live_css.module
View source
<?php

/**
 * Implements hook_menu().
 */
function live_css_menu() {
  $items = array();
  $items['css/save'] = array(
    'page callback' => 'live_css_save',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/config/development/live_css'] = array(
    'title' => t('Live CSS'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'live_css_admin',
    ),
    'description' => t('Configure the live CSS editor.'),
    'access arguments' => array(
      'access administration pages',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function live_css_permission() {
  return array(
    'edit css' => array(
      'title' => t('Edit and save CSS'),
      'description' => t('Edit and save CSS with the live editor.'),
    ),
  );
}

/**
 * Implements hook_settings()
 */
function live_css_admin() {
  $form = array();

  //get theme names
  $form['live_css_theme'] = array(
    '#type' => 'select',
    '#title' => t('Editor Theme'),
    '#default_value' => variable_get('live_css_theme', 'twilight'),
    '#options' => live_css_list_themes(),
  );
  $form['live_css_hideadmin'] = array(
    '#type' => 'checkbox',
    '#title' => t('Hide Admin Menu'),
    '#default_value' => variable_get('live_css_hideadmin', 1),
    '#description' => t('Automatically hides the administration menu when editing CSS.'),
  );
  $form['live_css_hidemodules'] = array(
    '#type' => 'checkbox',
    '#title' => t('Only show theme CSS'),
    '#default_value' => variable_get('live_css_hidemodules', 0),
    '#description' => t('Removes module and other styles from the CSS list.'),
  );
  return system_settings_form($form);
}
function live_css_list_themes() {
  $result = array();
  $files = live_css_list_files(dirname(__FILE__) . '/ace/src');
  foreach ($files as $file) {
    if (substr($file, 0, 5) == 'theme') {
      $theme = substr($file, 6, strlen($file) - 9);
      $name = ucwords(str_replace('_', ' ', $theme));
      $result[$theme] = $name;
    }
  }
  return $result;
}

/* get the directory listing for the theme files of ace */
function live_css_list_files($folder) {
  $results = array();
  $handler = opendir($folder);
  while ($file = readdir($handler)) {
    if ($file != '.' && $file != '..') {
      $results[] = $file;
    }
  }
  closedir($handler);
  return $results;
}

/**
 * Implements hook_init().
 */
function live_css_init() {

  //check permissions
  if (user_access('edit css')) {

    //set settings
    $theme = variable_get('live_css_theme', 'twilight');
    $settings = array();
    $settings['theme'] = $theme;
    $settings['autoload'] = false;
    $settings['hideadmin'] = variable_get('live_css_hideadmin', 1);
    $settings['hidemodules'] = variable_get('live_css_hidemodules', 0);
    drupal_add_js(array(
      'live_css' => $settings,
    ), 'setting');
    drupal_add_js(drupal_get_path('module', 'live_css') . '/ace/src/ace.js');
    drupal_add_js(drupal_get_path('module', 'live_css') . '/ace/src/mode-css.js');
    drupal_add_js(drupal_get_path('module', 'live_css') . '/ace/src/theme-' . $theme . '.js');
    drupal_add_js(drupal_get_path('module', 'live_css') . '/plugins.js');

    //load the list of stylesheets
    drupal_add_css(drupal_get_path('module', 'live_css') . '/css.css', 'module', 'all', FALSE);
    drupal_add_js(drupal_get_path('module', 'live_css') . '/css.js');
  }
}
function live_css_save() {
  $css = $_POST['css'];
  $href = $_POST['href'];

  //calculate the file path relative to the base drupal folder
  $parts = split('/', $href);
  $path = '';
  for ($i = 3; $i < count($parts); $i++) {
    $path .= $parts[$i] . '/';
  }
  if (strpos($path, '?') > 0) {
    $path = substr($path, 0, strpos($path, '?'));
  }
  else {
    $path = substr($path, 0, -1);
  }
  $path = $_SERVER['DOCUMENT_ROOT'] . '/' . $path;

  //save file back
  $fh = fopen($path, 'w') or die("Can't open file " . $path . " at " . $href);
  fwrite($fh, $css);
  fclose($fh);
  echo drupal_json_encode(array(
    'result' => 'success',
    'filename' => $path,
  ));
}

Functions