Check if file exists then load it

incrn8 - September 11, 2007 - 22:51
Project:Alter CSS
Version:5.x-1.0
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

First off, let me say thanks for such an awesome module. Its really going to help me out.

That said, I would love to have one extra feature. Some of the themes I use have multiple stylesheets, so editing an existing stylesheet other than style.css would be a great feature. I played around with the code myself, and I could get the module to load in an existing stylesheet without overwriting it. However, any further editing was not saved. My PHP skill level prevents me from finding the problem.

Here is the code that I was using (with some unchanged surrounding code):

<?php
 
if ($styles == ALTER_CSS_EMPTY) {
    echo
"ERROR: Theme information doesn't exist for ".$_POST['theme'].".";
    exit;
  }
 
$name = strtolower($_POST['name']);
 
$styles[$name] = dirname($styles['backup']).'/'.$name.'.css';         // removed "style_" prefix here
 
if (file_exists($styles[$name]) && empty($_POST['replace'])) {        // the first new "if"
   
file_get_contents($styles[$_POST['name']]);
  }
  if (!
file_exists($styles[$name])) {                                   // the second new "if"
   
file_put_contents($styles[$name],$_POST['data']);
  }
  if (!empty(
$_POST['replace'])) {
    unset(
$styles[$_POST['replace']]);
   
unlink(dirname($styles['backup']).'/'.$_POST['replace'].'.css');    // removed "style_" prefix here too
   
if ($default_style == $_POST['replace']) {
     
variable_set('alter_css_'.$_POST['theme'].'_default',$name);
     
copy($styles[$name],dirname($styles['backup']).'/style.css');
    }
  }
?>

To get this feature to work properly, I also had to remove your style_ prefixes that you add to the new stylesheet filenames. Maybe the prefix can be entered by the user, or just a checkbox to either use prefixes or not?

Anyway, if you have any ideas on this, I would really appreciate it.

#1

Shawn Conn - September 12, 2007 - 12:46

Thanks for the feedback. Yes, the module is a bit short sighted in that respect. When I designed the module I only took into account my specific purposes of only needing style.css changed. I will take this request in mind in future releases. It should be pretty easy check for *.css in a theme directory. Also, adding a definable prefix set in the module settings menu should be easy as well. As far as the code, I'm a little busy right now to look into. I know I made a lot of assumptions that I would only be dealing with "style.css" and "style_.css". I'll try to get back to you about that.

#2

incrn8 - September 12, 2007 - 18:52

Thanks for the reply. There's definitely no rush on this. I'll take another crack at it when I have a bit more time too. I agree that most themes will use only style.css, but I quite like using the zen theme as a starting point, and that one uses zen.css to override styles. Not sure why they couldn't just use style.css, but I guess that's their prerogative :)

 
 

Drupal is a registered trademark of Dries Buytaert.