1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Whatch Thread Permission not working

Discussion in 'XenForo Development Discussions' started by fredrikse, Oct 9, 2012.

  1. fredrikse

    fredrikse Active Member

    Hi,

    This spring Ragtek helped me out creating an add-on (http://xenforo.com/community/resources/permissions-for-watch-thread.592/) that via permission can turn on/off the possibility to watch a thread. At the time all was good and the "Watch thread" link appeared/disappeared accordingly to the permission that was set, in the thread view. After a while I realized that we had only taken care of 1 of the 3 location that as anything to do with this feature. I e-mailed Ragtek and got this answer back:

    For this add-on to be useful the possibility to watch a thread when creating a thread or editing a thread must be taken care of so permission will affect these parts to.

    I tried to understand what Ragtek wanted be to do. Due to a bug in Xenforo something is not working. This is what I came up with:

    Code:
    <xen:if is="{$canWatchThread}">
            <fieldset>
                <dl class="ctrlUnit">
                    <dt>{xen:phrase options}:</dt>
                    <dd><ul>
                        <li><xen:include template="helper_thread_watch_input" /></li>
                    </ul></dd>
                </dl>
     
                <xen:include template="thread_fields_status" />
            </fieldset>
         
            <!-- slot: after_options -->
        </xen:if>
    But now the option to watch a thread when creating/editing a thread is gone regardless what the permission setting is. Something is clearly wrong.

    Code:
    <xen:if is="{$canWatchThread}">
    This IF-statement I found in the template thread_view where the "Watch thread" link was placed. That's why I thought this was the variable to use.

    I hope anything of what I just said makes any sense to anyone. Any help or pointers on this would be very much appreciated :)
     
  2. fredrikse

    fredrikse Active Member

    I try to use this condition but it does not work:

    Code:
    <xen:if is="{xen:helper ismemberof, $visitor, 11}">
    Why? o_O The Watch thread option is still visible when the user is not a member of this secondary group which I am using for this purpose.

    I'm trying to get this to work with help of TMS.
     
  3. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    {$canWatchThread} is not available to the thread_create template. You need to modify the controller. Probably add the red code:

    library/XenForo/ControllerPublic/Forum.php

    Code:
    	public function actionCreateThread()
    	{
    		$forumId = $this->_input->filterSingle('node_id', XenForo_Input::UINT);
    		$forumName = $this->_input->filterSingle('node_name', XenForo_Input::STRING);
    
    		$ftpHelper = $this->getHelper('ForumThreadPost');
    		$forum = $ftpHelper->assertForumValidAndViewable($forumId ? $forumId : $forumName);
    
    		$forumId = $forum['node_id'];
    
    		$this->_assertCanPostThreadInForum($forum);
    
    		$attachmentParams = $this->getModelFromCache('XenForo_Model_Forum')->getAttachmentParams($forum, array(
    			'node_id' => $forum['node_id']
    		));
    
    		$maxResponses = XenForo_Application::get('options')->pollMaximumResponses;
    		if ($maxResponses == 0)
    		{
    			$maxResponses = 10; // number to create for non-JS users
    		}
    		if ($maxResponses > 2)
    		{
    			$pollExtraArray = array_fill(0, $maxResponses - 2, true);
    		}
    		else
    		{
    			$pollExtraArray = array();
    		}
    
    		$viewParams = array(
    			'thread' => array(
    				'discussion_open' => 1,
    				'prefix_id' => $forum['default_prefix_id'],
    			),
    			'forum' => $forum,
    			'nodeBreadCrumbs' => $ftpHelper->getNodeBreadCrumbs($forum),
    
    			'prefixes' => $this->_getPrefixModel()->getUsablePrefixesInForums($forumId),
    
    			'attachmentParams' => $attachmentParams,
    
    			'watchState' => $this->_getThreadWatchModel()->getThreadWatchStateForVisitor(false),
    
    			'canWatchThread' => $this->_getThreadModel()->canWatchThread(array('node_id' => $forum['node_id']), $forum),
    
    			'captcha' => XenForo_Captcha_Abstract::createDefault(),
    
    			'pollExtraArray' => $pollExtraArray,
    
    			'canLockUnlockThread' => $this->_getForumModel()->canLockUnlockThreadInForum($forum),
    			'canStickUnstickThread' => $this->_getForumModel()->canStickUnstickThreadInForum($forum),
    
    			'attachmentConstraints' => $this->getModelFromCache('XenForo_Model_Attachment')->getAttachmentConstraints(),
    		);
    		return $this->responseView('XenForo_ViewPublic_Thread_Create', 'thread_create', $viewParams);
    	}
    
    Normally that function requires the thread record which doesn't exist at this point, but it only references the node_id of the thread which I manually added in place of the thread record. Theoretically that will work. I am not familiar with ragtek's addon though.
     
    fredrikse likes this.
  4. fredrikse

    fredrikse Active Member

    Yeeeeeeees! It worked. You are the man, Jake :)
     
  5. fredrikse

    fredrikse Active Member

    I just realized on thing. Will the check boxes still be marked like in the picture even though I can't see them? My guess is yes.

    threadwatch.png
     
  6. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Not if the input fields are removed from the page by your {$canWatchThread} condition.
     
    fredrikse likes this.
  7. fredrikse

    fredrikse Active Member

    With the option fields hidden I created a new thread. When I looked at the summary page for watched threads the thread I just created was there. Doesn't that mean that the parameters are passed on regardless wether the check boxes are visible or not?
     
  8. fredrikse

    fredrikse Active Member

    The last template I had to put a conditional check in was the navigation template. I guess I have the same issue here with this template as with thread_create, that {$canWatchThread} is not available. Can I extend it like we did in forum.php?
     
  9. fredrikse

    fredrikse Active Member

    I found a template with the name helper_thread_watch_input.

    This is the code:

    PHP:
    <label for="ctrl_watch_thread"><input type="checkbox" name="watch_thread" value="1" id="ctrl_watch_thread" class="Disabler" {xen:checked $watchState} /> {xen:phrase watch_this_thread}...</label>
        <
    ul id="ctrl_watch_thread_Disabler">
            <
    li><label for="ctrl_watch_thread_email"><input type="checkbox" name="watch_thread_email" value="1" id="ctrl_watch_thread_email" {xen:checked "{$watchState} == 'watch_email'"} /> {xen:phrase and_receive_email_notifications}</label></li>
        </
    ul>
        <
    input type="hidden" name="watch_thread_state" value="1" />
    I guess I could work something out with TMS here. If the group permission is set the values for these checkboxes should be set to "0"? Is it necessary to do on the last one which is hidden and seems to control the state of thread?

    PHP:
    <input type="checkbox" name="watch_thread" value="1" id="ctrl_watch_thread" class="Disabler" {xen:checked $watchState} />
    <
    input type="checkbox" name="watch_thread_email" value="1" id="ctrl_watch_thread_email" {xen:checked "{$watchState} == 'watch_email'"} />
    <
    input type="hidden" name="watch_thread_state" value="1" />
     
  10. fredrikse

    fredrikse Active Member

    Would this work?



    PHP:
    <label for="ctrl_watch_thread"><input type="checkbox" name="watch_thread" <xen:if is="{$canWatchThread}">value="0"<xen:else />value="1"</xen:if> id="ctrl_watch_thread" class="Disabler" {xen:checked $watchState} /> {xen:phrase watch_this_thread}...</label>
        <
    ul id="ctrl_watch_thread_Disabler">
            <
    li><label for="ctrl_watch_thread_email"><input type="checkbox" name="watch_thread_email" value="1" id="ctrl_watch_thread_email" {xen:checked "{$watchState} == 'watch_email'"} /> {xen:phrase and_receive_email_notifications}</label></li>
        </
    ul>
        <
    input type="hidden" name="watch_thread_state" value="1" />
    I created the following conditional statement: <xen:if is="{$canWatchThread}">value="0"<xen:else />value="1"</xen:if>

    Can I put the condition in the HTML like I have done?
     
  11. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    It should work, but I am not familiar with ragtek's addon. You need to check the source code of the rendered page and make sure the fields do not exist when the user does not have permission.
     
  12. fredrikse

    fredrikse Active Member

    I will do some more troubleshooting. Looking at the actual HTML output is a good idea to verify if what effect the condition has had on the HTML. In this case Ragtek has not altered this template at all. I was the one realizing this template needed to be altered for the whole idea to work.
     
  13. fredrikse

    fredrikse Active Member

    Is {$canWatchThread} available to the template helper_thread_watch_input?
     
  14. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Yes, by way of the code change in my first reply. That template is included by thread_create.
     
  15. fredrikse

    fredrikse Active Member

    I realized I had forgotten to set a condition around:

    PHP:
    {xen:helper ismemberof$visitor11}">{xen:checked $watchState}</xen:if>
    Now the checkboxes are unmarked when a member is not a member of groupID 11. Now I just have to locate the user settings for watch state in the user profile :)
     
    Insy likes this.
  16. fredrikse

    fredrikse Active Member

    The funny thing now when my test user is not part of this access group and I create a test thread, the thread created is still watched when I look at "watched threads" in the navigation. How is this possible when the checkboxes is unchecked?

    This is the final code I use in helper_thread_watch_input:

    PHP:
    <label for="ctrl_watch_thread"><input type="checkbox" name="watch_thread" <xen:if is="{xen:helper ismemberof, $visitor, 11}">value="1"<xen:else />value="0"</xen:if> id="ctrl_watch_thread" class="Disabler" <xen:if is="{xen:helper ismemberof, $visitor, 11}">{xen:checked $watchState}</xen:if> /> {xen:phrase watch_this_thread}...</label>
    <
    ul id="ctrl_watch_thread_Disabler">
      <
    li><label for="ctrl_watch_thread_email"><input type="checkbox" name="watch_thread_email" <xen:if is="{xen:helper ismemberof, $visitor, 11}">value="1"<xen:else />value="0"</xen:if> id="ctrl_watch_thread_email" <xen:if is="{xen:helper ismemberof, $visitor, 11}">{xen:checked "{$watchState} == 'watch_email'"}</xen:if> /> {xen:phrase and_receive_email_notifications}</label></li>
    </
    ul>
    <
    input type="hidden" name="watch_thread_state" <xen:if is="{xen:helper ismemberof, $visitor, 11}">value="1"<xen:else />value="0"</xen:if> />
     
  17. fredrikse

    fredrikse Active Member

    From what I can see from my research that I just did it seems like I have to uncheck these boxes in the user profile settings page. When I do that and post a thread and the user is not part of this group, a thread watch is not created.
     
  18. fredrikse

    fredrikse Active Member

    I think I have solved the mystery why created threads are still watched when the checkboxes are unchecked for a certain user. The key to get this function to work is to control the watch thread settings in the account settings for the user. Because I have noticed when I manually check/uncheck the boxes it effects the checkboxes in the template thread_create and no watch is therefore created, like I want it to be. What happens when I use TMS and set a condition and alter the value of the checkboxes is that the checkboxes get unchecked but a thread watch is still created. This must have something to do with that in the database the values of thread watch is still active. Doesn't this make sense?

    My conclusion is that I need to alter the settings in the databas as well depending on wether the user is active in this group or not.
     
  19. fredrikse

    fredrikse Active Member

    I found default_watch_state in the db:

    db.png

    Default_watch_state ca have the following values:
    • NULL
    • watch_no_email
    • watch_email
    What I would like to do is to set default_watch_state to watch_email or NULL depending on the membership of a certain group. To me it feels like the best way to go.
    What's the best solution to achieve this? :unsure:
     

Share This Page