Fork me on GitHub
Current Releases are 2.1 stable (major release) and 2.0.18.11 (security patch for 2.0)
We've also rebooted our GitHub repo, docs, and more!

Question about storing variable between events.

peregrineperegrine ☯ hasta luego :)In-Flight MVP
edited March 2012 in Questions

I am trying to learn OOP, however some things I have not yet learned to do (obviously).

I am trying to store the $Discussion->CountUnreadComments and carry it over to use in another event.

I can capture it fine. Can anyone tell me what I am doing wrong. This is just an interim piece of code. I can't get past this part. Any clues to get me on my way.

class UnreadMessCount extends Gdn_Plugin {

    public function PluginController_UnreadMessCount_Create($Sender, $Args = array()) {
    
        $Sender->Permission('Plugins.UnreadMessCount.Manage');
    }
   
    public function DiscussionsController_BeforeDiscussionContent_Handler(&$Sender) {
     
     
     $Me = ($Sender->EventArguments['Discussion']);
    //  this shows up fine.
    echo "here is Before discussion content " . $Me->CountUnreadComments;
     $this->CacheUnread($Me);
   
   
   }
   public function DiscussionController_BeforeDiscussion_Handler($Me) { 
      // this doesn't show count
      echo "here is Before discussion " . $Me->CountUnreadComments;
       $this->CacheUnread($Me);
   
        } 


public function CacheUnread($Me) {
    
// places unread messcount  in front of comments
        echo "here is Cache Function " . $Me->CountUnreadComments;


}
}

Monetary Donations will be appreciated if you use my plugins. Thanks in Advance.
As a waiter gets a tip for a good meal, tips for successful solutions appreciated as well. Peregrine

Best Answer

  • x00x00 MVP
    edited March 2012 Answer ✓

    I should have read your post more clearly, I have limited time to go through all these problems.

    You are going about this wrong way. If you are goign to do any cache it is goign to be on the same request not different ones.

    http is stateless. What you are trying to do is meaningless becuase, it would not identify the time it occurred, and you be able would not garetee it occurs like this.

    What you really want is the unread count. which is essentially $Sender->Discussion->CountComments - $Sender->Discussion->CountCommentWatch;

    You can use that in BeforeCommentMeta.

    No need to cache the value, and you can forget about DiscussionsController it is not relevant.

    grep is your friend.

    peregrine

Answers

  • peregrineperegrine ☯ hasta luego :) In-Flight MVP
    edited March 2012

    I altered the code: Perhaps, this is not the place to post this, since it is a request for a clue in coding help. I just saw other discussions where there was some help and thought someone in the know could provide a clue.

    I tried creating a class

    
    class unread {
                  var $count;
                  function __construct($unread_count) {           
                            $this->count = $unread_count;            
                    }               
                    
                    function set_count($new_count) {
                             $this->count = $new_count;
                    }       
            
                    function get_count() {           
                             return $this->count;            
                     }     
    
    
    
    and I can reference it in this function
     public function DiscussionsController_BeforeDiscussionContent_Handler(&$Sender) {
         
        
         $Object = ($Sender->EventArguments['Discussion']);
              
                $did = $Object->DiscussionID;
                $cuc =  $Object->CountUnreadComments;
                $unc = new unread($cuc);
                echo "The count is : ".$unc->get_count();  // this prints fine
               // $unr[$did] = $cuc;
    }
    
    
    
    but this won't work (I've tried all gyrations passing $unc as an argument and w/o)
    
    
    public function DiscussionController_BeforeDiscussion_Handler() { 
            
                var_dump($unc);
               
            } 
    

    Monetary Donations will be appreciated if you use my plugins. Thanks in Advance.
    As a waiter gets a tip for a good meal, tips for successful solutions appreciated as well. Peregrine

  • peregrineperegrine ☯ hasta luego :) In-Flight MVP

    thanks @hbf

    I noticed the thread you mentioned before, but I have less skills than businessdad, those instructions are too cryptic for me. I'm about ready to throw in the towel on this one. I thought it would be useful to people other than me to have a plugin to display unread comments with an icon in the singular discussion page when using the replyto plugin (which throws recent comments in the middle of discussion). I've got everything mapped out, just can't pass the variable.

    I've tried thousands of ways to do it creating classes, creating functions, no luck

    I tried this I don't know where to put or how to set the arguments
      $this->EventArgument['Dvars'] = "Dvars";
    
    
     public $Dvars = array();
        public function PluginController_UnreadMessCount_Create($Sender, $Args = array()) {
        
            $Sender->Permission('Plugins.UnreadMessCountManage');
        }
    
     public function DiscussionsController_BeforeDiscussionContent_Handler($Sender) {
        
       
         $Doid = $Sender->EventArguments['Discussion']->DiscussionID;
         $unreadcount = $Sender->EventArguments['Discussion']->CountUnreadComments;
         $sesname = Gdn::Session()->User->Name;
         $Dvars[$sesname][$Doid]= $unreadcount;
         $mycount = $Dvars[$sesname][$Doid]; 
         echo "======test display in discussions page  - $mycount - $sesname - $Doid =======";
       
       }
     //  $this->EventArgument['Dvars'] = "123";
     //  public function DiscussionController_BeforeCommentMeta_Handler($Sender,&$Dvars) { 
       public function DiscussionController_BeforeCommentMeta_Handler($Sender) { 
               echo $this->Dvars;
               $Doid =  $Sender->EventArguments['Discussion']->DiscussionID;
               $sesname= Gdn::Session()->User->Name ; 
               
              $mycount = $Dvars[$sesname][$Doid]; 
              echo "======test display in single discussion page  - $mycount - $sesname - $Doid =======";
        } 
    
    
    }
    
    

    Monetary Donations will be appreciated if you use my plugins. Thanks in Advance.
    As a waiter gets a tip for a good meal, tips for successful solutions appreciated as well. Peregrine

  • x00x00 MVP
    edited March 2012

    I think you are over complicating things.

    protected $CountUnreadComments;
    
    public function CacheUnread($CountUnreadComments){
         $this->CountUnreadComments=$CountUnreadComments;
    }
    
    public function GetCacheUnread(){
        return $this->CountUnreadComments;
    }

    you don't need additional classes.

    grep is your friend.

    peregrine
  • peregrineperegrine ☯ hasta luego :) In-Flight MVP
    edited March 2012

    I was hoping you would chime in @x00 and I'm hoping you might have some more insights :)

    Ok, no additional classes. Revised below. Same problem - different code. The routine and functions appear to work in Discussions page. But ...

    But, when I click on an individual discussion - my save count does not appear and is NULL the same recurring problem I am trying to solve. Obviously I'm doing something wrong. I'll clean up my logic to do other things later meanwhile it is as simple as I can get to show the variable is not being retained through events.

    class UnreadIcon extends Gdn_Plugin {
    
        protected $CountUnreadComments;
    
        public function CacheUnread($CountUnreadComments){
        $this->CountUnreadComments=$CountUnreadComments;
        }
    
        public function GetCacheUnread(){
        return $this->CountUnreadComments;
        }  
    
    
    
        public function PluginController_UnreadIcon_Create($Sender) {
    
            $Sender->Permission('Plugins.UnreadIcon.Manage');
        }
    
       public function DiscussionsController_BeforeDiscussionContent_Handler($Sender) {
    
    
         $CountUnreadComments =  $Sender->EventArguments['Discussion']->CountUnreadComments;
         $this->CacheUnread($CountUnreadComments);
    
         $mycount = $this->GetCacheUnread();
         echo "==Works -- test display in discussionS page  -count is:( $mycount )";
    
       }
    
    
       public function DiscussionController_BeforeCommentMeta_Handler($Sender) { 
    
               $mycount = $this->GetCacheUnread();
            // this does not display $mycount
                var_dump($mycount);  // this becomes NULL
       echo "== PROBLEM =test display in single discussion page  -count is:( $mycount )";
    
       }
    
    
    }
    

    Monetary Donations will be appreciated if you use my plugins. Thanks in Advance.
    As a waiter gets a tip for a good meal, tips for successful solutions appreciated as well. Peregrine

  • peregrineperegrine ☯ hasta luego :) In-Flight MVP

    I'm going to go the cookie route, since I don't know how to save and pass arguments properly within the controller functions listed above (despite looking for examples). and I think the objects get destroyed in between pages. Thanks for the attempts at help @x00

    Monetary Donations will be appreciated if you use my plugins. Thanks in Advance.
    As a waiter gets a tip for a good meal, tips for successful solutions appreciated as well. Peregrine

  • hbfhbf wiki guy? MVP

    While i see why this isnt working as desired... or more specifically, why it is giving you the result you are raising as an issue, it would be difficult to explain in a very clear way why it is what it is other than to say what was already said.... there really is no need for a new class to handle thcaching of this info.

    I think if you were to describe the desired end results, a simple functional specification if you will, i bet someone could give you a clear and easy answer to how to accomplish your end goal.

  • peregrineperegrine ☯ hasta luego :) In-Flight MVP
    edited March 2012

    thanks for the support - I hope I could get a simple and clear answer. If i don't get any clear answers the cookie route will be the way.

    Simple and Clear - I want to save a variable while I am viewing the discussions**** page - the one with the list of discussions. The variable would be an array (consisting of discussionid and unread comment count for each discussion) I chose this event on that page. public function DiscussionsController_BeforeDiscussionContent_Handler($Sender)

    I want to retrieve saved variable (that array) in the individual Discussion Page

    public function DiscussionController_BeforeCommentMeta_Handler($Sender)

    I actually thought I was pretty clear, but maybe not. As the vanilla software is written, By the time you click an individual discussion title from the discussions page the unread comment count gets zeroed out (this is why i wanted to save it).

    What i want to do is save it under a different variable name or array and hen I get to the individual discussion page , I recall my saved variable and the # or unread messages for that particular discussion and highlight the most recent number of comments that match the unread message count for that user. I really question whether it can be done without writing to file, database a session, or a cookie. I posed this questions before and I was told just save the variable in an object.

    I know I can save and retrieve things between 2 events that occur on the same web page. But this will not work because the object is getting destroyed once a new web page is viewed ( as in moving from discussionS to discussion).

    Monetary Donations will be appreciated if you use my plugins. Thanks in Advance.
    As a waiter gets a tip for a good meal, tips for successful solutions appreciated as well. Peregrine

  • hbfhbf wiki guy? MVP

    unfortunately i think this is going to be a bit more complicated than you want when trying to cover all potential situations, as you will need to know where you are at in the post index and be able to update your "unread post" count independently of the way the core is currently handling it. unread counts are based off of the last post in the highest indexed page request. so if you have multiple pages of unread messages and if the user does not read them in order, your code will have to be pretty robust to get it right.

    but, i can say that, since you are traversing between more than one controller, i believe your best bet is through the use of the session object. cookies will work but are going to be pretty clunky and slow, and you will have to deal with persistence.

    sorry i can't be of more help.

    peregrine
  • peregrineperegrine ☯ hasta luego :) In-Flight MVP

    Thanks for the replies @hbf and x00.

    the CountCommentWatch sure save a lot of extra coding. Thanks for giving the question a second view I appreciate the help.

    I posted the plugin, it is named unreadicon-plugin and has a bit of a screen shot to show what I was trying to implement.

    http://vanillaforums.org/addon/unreadicon-plugin

    Monetary Donations will be appreciated if you use my plugins. Thanks in Advance.
    As a waiter gets a tip for a good meal, tips for successful solutions appreciated as well. Peregrine

  • hbfhbf wiki guy? MVP

    interesting, i'll install this on my test site and let you know how it works for me.

  • hbfhbf wiki guy? MVP

    the yellow dot is not consistently being applied. it only showed up on one of the threads i reviewed. i'll test more of the plugin tomorrow after i have a backlog of posts to read through.

    here is the test site if you would like to poke around. http://homebrewforums.net/themetest/

    user pics and uploaded files will not show, as i don't like to clone that content, it just takes up drive space unnecessarily.

    peregrine
  • peregrineperegrine ☯ hasta luego :) In-Flight MVP
    edited March 2012

    Thanks for the test @hbf I see the problem, it is with multiple pages, it should work fine with a single page of comments. Will upload fixes later in version 1.2.

    Monetary Donations will be appreciated if you use my plugins. Thanks in Advance.
    As a waiter gets a tip for a good meal, tips for successful solutions appreciated as well. Peregrine

  • hbfhbf wiki guy? MVP

    peregrine said: Thanks for the test @hbf I see the problem, it is with multiple pages, it should work fine with a single page of comments. Will upload fixes later in version 1.2.

    i'll try your new version out. but this version also has problems with identifying "new" posts when, for instance there are 100 new posts and you are viewing an old page that you have seen already. i think the logic will need a bit more work. you can probably leverage the 'postnum' plugin to help figure out where you are at in the stack of posts.

  • peregrineperegrine ☯ hasta luego :) In-Flight MVP
    edited March 2012

    Uploaded version 1.2 - see if you have the same problems. Logic for both icons has been replaced and should span multiple pages in most cases.

    The problem with postnum is it breaks down (logically) with reply-to plugin. I'll look at it if more closely if it doesn't solve the issues you see. @hbf .

    Monetary Donations will be appreciated if you use my plugins. Thanks in Advance.
    As a waiter gets a tip for a good meal, tips for successful solutions appreciated as well. Peregrine

Sign In or Register to comment.