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

Who Locked the Thread - Help in development

Fuhrmann

Well-known member
#1
Hello everyone! ;)

My name is Ricardo I'm from Brazil and got a license Xenforo recently (just yesterday morning).

I've been following the forum a few weeks ago (actually a couple) but I'm register here since the day I discovered Xenforo.

I hold a forum on FPS games and I intend to migrate from my current installation of vBulletin for Xenforo! Absolutely!

Going straight to the point now ... As soon as I got into the Xenforo began developing a small addon, just to experiment and learn. I would like a review, analysis or some tips (I hope not to abuse :D ).

My AddOn is quite simple. Each topic is locked, it is recorded in the database the ID of the user who locked the topic (user_id). So every time someone visits a topic is shown in the following sentence: "This thread was locked by: _____username here_____ ".

I am not familiar with MVC, so please take this into consideration!
I do not know if someone has already developed something similar, but decided to try anyway!

As you can see, very simple, just for my learning.

This is how the end result will be:

Sem título2.png

The folder structure of my AddOn is as follow

Sem título.png

Will only work on threads that are lock after installing the AddOn (I do not know if it makes sense ...or if someone will like)

The AddOn is attached!

(My English is bad, but I struggled!)
 

Attachments

Fuhrmann

Well-known member
#3
Actually I wonder if the structure is correct. I did this AddOn based on various tips and tutorials found here on the forum, but wanted to know if is correct the way I did so that I can continue developing other addons with the same structure.

BTW thanks for the reply!
 
R

ragtek

Guest
#4
Small code review without installing the add-on
PHP:
class WhoLocked_Model_WhoLocked extends XenForo_Model {

    public static function getUserWhoLocked($threadId){
        $db = XenForo_Application::get('db');
        $who_locked_id =  $db->fetchOne('
                            SELECT xf_who_locked
                                FROM xf_thread
                            WHERE thread_id = ?
                            ', $threadId);
        $user = XenForo_Model::create('XenForo_Model_User');
        return $user->getUserById($who_locked_id);
    }

}
1. why is the method static, you don't call it static:p.
2. http://xenforo.com/community/threads/calling-models.16406/
3. to get the database object, you can use $db = $this->_getDb() inside the model

4. do you really need the extra query?
IMO no.
If you pass the whole $thread array to the method, you'll also have the value for xf_who_locked ;)

5.
PHP:
    public static function template_create($templateName, array &$params, XenForo_Template_Abstract $template)
    {
        if ($templateName == 'PAGE_CONTAINER'){
            $template->preloadTemplate('who_locked_bit');
        }
    }
PAGE_CONTAINEr is used on most pages, so you'll cache the template "too often"
thread_view should also do it;)

PHP:
    public function lockThreads(array $threadIds, array $options = array(), &$errorKey = '', array $viewingUser = null)
    {
        $visitor = XenForo_Visitor::getInstance(); //Get User Data
        list($threads, $forums) = $this->getThreadsAndParentData($threadIds, $viewingUser);

        if (empty($options['skipPermissions']) && !$this->canLockUnlockThreadsData($threads, $forums, $errorKey, $viewingUser))
        {
            return false;
        }

        $this->_updateThreadsBulk($threads, $forums, array('discussion_open' => 0));
        $this->_updateThreadsBulk($threads, $forums, array('xf_who_locked' => $visitor['user_id']));

        return true;
    }
i would do it this way=>

PHP:
      public function lockThreads(array $threadIds, array $options = array(), &$errorKey = '', array $viewingUser = null)
    {
        $lock = parent::lockThreads($threadIds, $options, $errorKey, $viewingUser);
        if ($lock) {

            $visitor = XenForo_Visitor::getInstance(); //Get User Data
            list($threads, $forums) = $this->getThreadsAndParentData($threadIds, $viewingUser);

            $this->_updateThreadsBulk($threads, $forums, array('xf_who_locked' => $visitor['user_id']));
            return true;
        }
            return false;

    }
you should ALWAYS run the parent method!
You don't know if somebody have other add-ons overwriting the same method. That#s why you HAVE TO run it;)
I'm trying to avoid c&p from the original methods and i'm trying to work only with the returned value, or IF you need to overwrite it, i'm doing something like:

PHP:
        public function actionAddThread()
        {
            $forumId = $this->_input->filterSingle('node_id', XenForo_Input::UINT);
            if ($this->_getEventModel()->isEventForum($forumId)
                AND isset($start)
                    ) {
                // THIS IS A LITTLE BIT UGLY, BUT EFICIENT


            $this->_assertPostOnly();

            $forumName = $this->_input->filterSingle('node_name', XenForo_Input::STRING);

            $ftpHelper = $this->getHelper('ForumThreadPost');
            $forum = $ftpHelper->assertForumValidAndViewable($forumId ? $forumId : $forumName);

            $forumId = $forum['node_id'];
 
// do my stuff for this special event forum
     }
            else {
                return parent::actionAddThread();
            }

        }
Hope this helps:)
 

Jake Bunce

XenForo moderator
Staff member
#5
And there are actually two ways to lock a thread. You only covered the inline moderation method. You can also lock a thread using Thread Tools -> Edit Thread inside of the thread. This is the controller for that action:

XenForo_ControllerPublic_Thread::actionSave

And the who_locked_bit template is being included on the thread page even if the thread isn't locked. It shows an empty box. You should not display that box unless the thread is locked.
 

Fuhrmann

Well-known member
#6
Great! Thanks ragtek and Jake! I'll make the changes!

So, the trick is always run the parent method.:D

Really thanks!!
 
F

Floris

Guest
#7
Are you going to release this as a plugin on this web site in the add-ons forum?
 
F

Floris

Guest
#9
President Obama was asking, I am just relaying the question. ..

what you mean why!

Why does anybody release an add-on?

Whatever.