Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

experimenting new plugin

jackmaessenjackmaessen ✭✭✭
edited January 2015 in Vanilla 2.0 - 2.8

I am experimenting with a new plugin in which i want to change the background-color for 2 classes: .Unread and .New
In the css file (changebackgroundcolor.css) i put this:

/* lightblue en lichtgreen bg-color for new topic and new comment in existing topic */   
li.Item.Unread {
background: rgba(27, 117, 186, 0.2) !important;
}

li.Item.New {
background: rgba(189, 209, 41, 0.2) !important;
} 

These classes have default a white background.
So what i want to achieve: if the user is logged in, these lines above should overwrite the default ones.

I started with an example plugin but do not succeed in it yet:

<?php if (!defined('APPLICATION')) exit();


// Define the plugin:
$PluginInfo['ChangeBackgroundColor'] = array(
   'Name' => 'ChangeBackgroundColor',
   'Description' => "Changes backgroundcolor of discussions for new discussion and new comment",
   'Version' => '1.0',
   'Requires' => FALSE, 
   'HasLocale' => FALSE,
   'License'=>"GNU GPL2",
   'Author' => "jackmaessen" 
);

class ChangeBackgroundColorPlugin extends Gdn_Plugin {

   public function Base_Render_Before($Sender) {

   $Session = Gdn::Session();   

   if ($Session->IsValid()) { // if user is logged in, load css file below to overwrite the 2 classes


      $sender->AddCssFile($this->GetResource('design/changebackgroundcolor.css', FALSE, FALSE)); 
      }

   }

   public function Setup() {
   // nothing to setup
   }
}

Need some help with it...

Comments

  • Try

    $Sender->AddCssFile('changebackgroundcolor.css', 'plugins/ChangeBackgroundColor');
    

    Or better use AssetModel_StyleCss_Handler instead of Base_Render_Before.

  • BleistivtBleistivt Moderator
    edited January 2015

    But this is probably the problem:

    $sender
    

    while function names are not, variable names are case sensitive in PHP

  • jackmaessenjackmaessen ✭✭✭
    edited January 2015

    Works Great!


    The main problem was: i already had given the 2 classes these different background-colors when i noticed when logged out, every topic gets the .Unread class, so everything had the lightblue color; which ofcourse was not the intention. So i thought: try to overwrite them if user is logged in and keep the default classes with white background color.

    Great work @Bleistivt :D

  • jackmaessenjackmaessen ✭✭✭
    edited January 2015

    I added some lines of code to this plugin for people to make it easy to change the background-colors by form input instead of changing the css file. I made this piece of code and it changes the content of changebackgroundcolor.css.

    <?php
    
    if($_SERVER['REQUEST_METHOD'] == 'POST') {
    
    
    $filename = 'changebackgroundcolor.css'; // content of this file should be changed by form input
    
    
    //create a file handler by opening the file
    $myTextFileHandler = @fopen('changebackgroundcolor.css','r+');
    
    //truncate the file to zero; we should overwrite content instead of adding new lines
    @ftruncate($myTextFileHandler, 0);
    
    // newtopic background
    $content = 'li.Item.New { background: ';
    $content .= $_POST['newtopic']; // read new topic input
    $content .= ';}';
    
    // newcomment background
    $content .= 'li.Item.Unread { background: ';
    $content .= $_POST['newcomment']; // read new comment input
    $content .= ';}';
    
    // first check if file is writable
    if (is_writable($filename)) {
    
        // In our example we're opening $filename in append mode.
        // The file pointer is at the bottom of the file hence
        // that's where $content will go when we fwrite() it.
        if (!$handle = fopen($filename, 'a')) {
             echo "Cannot open file ($filename)";
             exit;
        }
    
        // Write $content to our opened file.
        if (fwrite($handle, $content) === FALSE) {
            echo 'Cannot write to file'.$filename;
            exit;
        }
    
        echo 'Backgroundcolors changed successfully to file'.$filename;
    
        fclose($handle);
    
    } else {
        echo 'The file'.$filename.'is not writable';
    }
    
    } //end if submit
    ?>
    
    <form method="post" action="">
    <p>color <b>new topic</b> background</p>
    <input type="text" name="newtopic" />
    
    <br /><br />
    <p>color <b>new comment</b> background</p>
    <input type="text" name="newcomment" />
    
    <br /><br />
    <input type="submit" value="Submit">
    
    </form>
    

    My question: how can i put this in a file (settings.php i think??) to make this work in the plugin?

  • public function SettingsController_ChangeBackgroundColor_Create($Sender) {
        $Sender->Permission('Garden.Settings.Manage');
        $Sender->Render($this->GetView('settings.php'));
    }
    

    and add this to your plugininfo array:

    'SettingsUrl' => '/settings/ChangeBackgroundColor',
    

    You can do it like that, but it is not recommended.

    You are basically doing everything in the view this way and not making use of the framework.

    Better rebuild your form using Gdn_Form and do all your processing in the hook.
    And use the higher level file function like file_get_contents and file_put_contents or even Gdn_FileSystem.
    You should also save the files you write in the cache or uploads directory and not in the plugin folder itself to still be able to enforce proper permissions.

    This post contains a lot of useful information:
    http://vanillaforums.org/discussion/comment/210058/#Comment_210058

Sign In or Register to comment.