You are here

live_css.module in Live CSS 6

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(
      'edit css',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_perm().
 */
function live_css_perm() {
  return array(
    'edit css',
  );
}

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

  //check permissions
  if (user_access('edit css')) {
    drupal_add_link(array(
      'id' => 'bespin_base',
      'href' => '/' . drupal_get_path('module', 'live_css') . '/bespin/prebuilt/',
      'rel' => '',
    ));
    drupal_add_css(drupal_get_path('module', 'live_css') . '/bespin/prebuilt/BespinEmbedded.css', 'module', 'all', FALSE);
    drupal_add_js(drupal_get_path('module', 'live_css') . '/bespin/prebuilt/BespinEmbedded.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?1');
  }
}
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] . '/';
  }
  $path = substr($path, 0, strpos($path, '?'));

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

Functions

Namesort descending Description
live_css_init Implements hook_init().
live_css_menu Implements hook_menu().
live_css_perm Implements hook_perm().
live_css_save