From 5e26d2ce125b7fd866e9ffe25ef9b0e8c14ca7b8 Mon Sep 17 00:00:00 2001
From: Chris Skene <chris@xtfer.com>
Date: Tue, 29 Nov 2011 14:09:03 +1100
Subject: [PATCH] Update CSS injector to save CSS in the database

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

diff --git a/docroot/sites/all/modules/contrib/css_injector/css_injector.admin.inc b/docroot/sites/all/modules/contrib/css_injector/css_injector.admin.inc
index 9f9f8f5..5926e79 100644
--- a/docroot/sites/all/modules/contrib/css_injector/css_injector.admin.inc
+++ b/docroot/sites/all/modules/contrib/css_injector/css_injector.admin.inc
@@ -97,13 +97,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(
@@ -112,7 +113,8 @@ function css_injector_edit($form_state, $crid = NULL) {
       'rule_conditions' => '',
       'media' => 'all',
       'preprocess' => 1,
-      'css_text' => '',
+      'css' => '',
+      'edit_files' => 0,
     );
   }
 
@@ -123,11 +125,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,
   );
 
@@ -173,6 +175,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',
@@ -214,7 +222,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/docroot/sites/all/modules/contrib/css_injector/css_injector.install b/docroot/sites/all/modules/contrib/css_injector/css_injector.install
index 8442947..f6f7560 100644
--- a/docroot/sites/all/modules/contrib/css_injector/css_injector.install
+++ b/docroot/sites/all/modules/contrib/css_injector/css_injector.install
@@ -34,6 +34,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',
@@ -45,6 +49,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'),
   );
@@ -71,3 +81,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

