HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

how to put style element in if statement

jackmaessenjackmaessen ✭✭✭
edited January 2015 in General Banter

I am trying to set the class Category-tuts-scripts-pagina-s to display: none if the logged in user is a normal member, and to display it if he is a mod or admin.

I tried the following:

public function discussionController_render_before($sender) {


      if (!Gdn::Session()->CheckPermission('Garden.Moderation.Manage')) { // if not moderation permission
        echo 'style=".Category-tuts-scripts-pagina-s: display: none;"'; // do not display this catogory class
      }

I am having problems to make this work. Can someone help me with this?

Comments

  • Simply hiding admin/mod content with css sounds like a bad idea.

    But use $sender->AddCssFile instead of echo style if you really want to do it that way

  • jackmaessenjackmaessen ✭✭✭
    edited January 2015

    @R_J yes i know it is not the best way. What i am trying to achieve:

    i made a tuts-scriptscategory with simple pages plugin.
    I gave only mods permission to post topics in this category and normal members should only view this category. The problem: members should been able to view this in the discussion list but NOT in the category list. Otherwise, they think they can post topics in that category when they click on that category and click on Start New Topic

  • The style attribute you are applying makes no sense. You cannot place selectors inside of a style attribute, only rules.

    Take your CSS, put it in an external sheet in your plugin's design folder, and conditionally load the sheet.

    public function discussionController_render_before($Sender) {
      if(Gdn::Session()->CheckPermission('Garden.Moderation.Manage') == false) {
        $Sender->AddCssFile($this->GetResource('design/hidecats.css', FALSE, FALSE));
      }
    }
    

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Code now looks like below:

    /**
         * Special display rules for SimplePage discussions.
         */
        public function discussionController_render_before($sender) {
    
    
           if(Gdn::Session()->CheckPermission('Garden.Moderation.Manage') == false) { // if not moderation permission
            $sender->AddCssFile($this->GetResource('design/hidecats.css', FALSE, FALSE)); // load this specific css
    
          }
         // rest of code...
    

    And in design/hidecats.css i put this line:

    .Category-tuts-scripts-pagina-s {
    display: none !important;
    }
    

    I login as a normal members without special permissions, but he doesn't load the css file as i can see in the source

  • edited January 2015

    Try like this

    $Sender->AddCssFile('hidecats.css', 'themes/yourtheme/design/hidecats.css ');

    If this is being run on Simple Pages which is a plugin you need to specify the folder for the file more because it is looking for the file in the design folder for simple pages.

    If it is in the design folder for simple pages

    $Sender->AddCssFile('hidecats.css', 'plugins/simplepages/design/hidecats.css ');

  • @vrijvlinder, i tried your option above but he doesn't load the css file either

  • edited January 2015

    Give me a link, it could be a cache problem... or look at it using another browser.

    try with a slash in fornt of the link

    $Sender->AddCssFile('hidecats.css', '/themes/yourtheme/design/hidecats.css ');

  • forum.webprofis.nl/

    The first under the Category list: Tuts & Scripts pagina's should be on display: none for a normal member

  • @jackmaessen said:
    Code now looks like below:

    /**
       * Special display rules for SimplePage discussions.
       */
      public function discussionController_render_before($sender) {
        
        
         if(Gdn::Session()->CheckPermission('Garden.Moderation.Manage') == false) { // if not moderation permission
          $sender->AddCssFile($this->GetResource('design/hidecats.css', FALSE, FALSE)); // load this specific css
          
        }
         // rest of code...
    

    Well I guess it is time to learn how to debug ;)

    Add this to your config:

    $Configuration['Garden']['Errors']['LogEnabled'] = TRUE;
    $Configuration['Garden']['Errors']['LogFile'] = 'log/DebugLog';
    

    Okay, now ask yourself "is the if clause wrong or the code to add the css file?" Put some debug text to your if clause

    // black magic!
    LogMessage(__FILE__, __LINE__, 'Object', 'Method', 'If only this text appears, the permission check fails');
    
    if (Gdn::Session...) {
        // black magic!
         LogMessage(__FILE__, __LINE__, 'Object', 'Method', 'CSS location: '.$this->GetResource('design/hidecats.css', FALSE, FALSE));
    
         $sender->AddCssFile(...
    

    After looking at a discussion, you should find a logfile in your forums /log directory (if not you might have the wrong file permissions).

    If you see the first message in the file, you simply have ensured that your function get's called. If you also find the second text there, you permission check is passed successful. From the output you should be able to decide if also the correct resource can be found.

Sign In or Register to comment.