From 8dc05c3ca96e6e7ad6d5e9345bebda7d467d4e5f Mon Sep 17 00:00:00 2001
From: Chris Skene <chris@xtfer.com>
Date: Tue, 29 Nov 2011 14:35:56 +1100
Subject: [PATCH] Add CSS to database storage

---
 css_injector.admin.inc |   28 ++++++++++++++++++----------
 css_injector.install   |   42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 10 deletions(-)

diff --git a/css_injector.admin.inc b/css_injector.admin.inc
index 820ca94..18db34c 100644
--- a/css_injector.admin.inc
+++ b/css_injector.admin.inc
@@ -96,13 +96,14 @@ function css_injector_edit($form_state, $crid = NULL) {
       '#value' => $crid,
     );
 
-    $path = file_create_path(_css_injector_rule_path($rule['crid']));
-    if (file_exists($path)) {
-      $rule['css_text'] = file_get_contents($path);
-    }
-    else {
-      $rule['css_text'] = '';
+    // Optionally, load CSS from the file store
+    if (isset($rule['edit_files']) && $rule['edit_files'] == 1) {
+      $path = file_create_path(_css_injector_rule_path($rule['crid']));
+      if (file_exists($path)) {
+        $rule['css'] = file_get_contents($path);
+      }
     }
+
   }
   else {
     $rule = array(
@@ -111,7 +112,8 @@ function css_injector_edit($form_state, $crid = NULL) {
       'rule_conditions' => '',
       'media' => 'all',
       'preprocess' => 1,
-      'css_text' => '',
+      'css' => '',
+      'edit_files' => 0,
     );
   }
 
@@ -122,11 +124,11 @@ function css_injector_edit($form_state, $crid = NULL) {
     '#required' => TRUE,
   );
 
-  $form['css_text'] = array(
+  $form['css'] = array(
     '#type' => 'textarea',
     '#title' => t('CSS code'),
     '#rows' => 10,
-    '#default_value' => $rule['css_text'],
+    '#default_value' => $rule['css'],
     '#required' => TRUE,
   );
 
@@ -172,6 +174,12 @@ function css_injector_edit($form_state, $crid = NULL) {
     '#title' => t('Preprocess CSS'),
     '#default_value' => $rule['preprocess'],
   );
+  $form['edit_files'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Edit CSS from file storage'),
+    '#default_value' => $rule['edit_files'],
+    '#description' => 'By default, CSS injector will load CSS in edit mode from the database. Check this box to always load from the disk instead (for example, if you wish to edit your CSS files via FTP). Note that selecting this option may lead to loss of data.'
+  );
 
   $form['buttons']['save'] = array(
     '#type' => 'submit',
@@ -213,7 +221,7 @@ function css_injector_edit_save($form, &$form_state) {
   $rule = $form_state['rule'];
   drupal_write_record('css_injector_rule', $rule, empty($rule['crid']) ? NULL : 'crid');
 
-  file_save_data($rule['css_text'], file_create_path(_css_injector_rule_path($rule['crid'])), FILE_EXISTS_REPLACE);
+  file_save_data($rule['css'], file_create_path(_css_injector_rule_path($rule['crid'])), FILE_EXISTS_REPLACE);
   _css_injector_load_rule(NULL, TRUE);
 
   drupal_set_message('Your CSS injection rule was saved.');
diff --git a/css_injector.install b/css_injector.install
index 09f60e6..0d5bb47 100644
--- a/css_injector.install
+++ b/css_injector.install
@@ -33,6 +33,10 @@ function css_injector_schema() {
         'description' => 'The data to evaluate when determining if the CSS should be injected',
         'type' => 'text',
         'not null' => TRUE),
+      'css' => array(
+        'description' => 'The CSS rules to inject',
+        'type' => 'text',
+        'not null' => TRUE),
       'media' => array(
         'description' => 'The media type of the CSS file (screen, print, etc.)',
         'type' => 'varchar',
@@ -44,6 +48,12 @@ function css_injector_schema() {
         'unsigned' => TRUE,
         'not null' => TRUE,
         'default' => 0),
+      'edit_files' => array(
+        'description' => 'Whether to load the CSS from disk or the database',
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0),
     ),
     'primary key' => array('crid'),
   );
@@ -70,3 +80,35 @@ function css_injector_update_6000() {
   db_drop_field($ret, 'css_injector_rule', 'file_path');
   return $ret;
 }
+
+/**
+ * Implements HOOK_update_N
+ */
+function css_injector_update_6001() {
+  $ret = array();
+  // Add the field 'css'
+  db_add_field($ret, 'css_injector_rule', 'css', array(
+    'description' => 'The CSS rules to inject',
+    'type' => 'text', 
+    'not null' => TRUE)
+  );
+  db_add_field($ret, 'css_injector_rule', 'edit_files', array(
+    'description' => 'Whether to load the CSS from disk or the database',
+    'type' => 'int',
+    'unsigned' => TRUE,
+    'not null' => TRUE,
+    'default' => 0)
+  );
+  // Load all CSS in files into the database
+  $rules =  _css_injector_load_rule(NULL, TRUE);
+  foreach ($rules as $rule) {
+    $path = file_create_path(_css_injector_rule_path($rule['crid']));
+    if (file_exists($path)) {
+      $rule['css'] = file_get_contents($path);
+      $rule['preprocess'] = (int) $rule['preprocess'];
+      $rule = (object) $rule;
+      drupal_write_record('css_injector_rule', $rule, 'crid');
+    }
+  }
+  return $ret;
+}
\ No newline at end of file
-- 
1.7.5.4

