• This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

[part 4] Creating a add-on to insert tabs in profile page (using hooks)

Status
Not open for further replies.
Step 12 - Creating the Controller

Here is the final code, below explained:

PHP:
/**
      * Action to send a note to the specified user.
    */
    public function actionSendNote()
    {
        //Make sure we are using a method form POST not GET
        $this->_assertPostOnly();

        //Lets get the ID of the user who is given the note
        $visitor = XenForo_Visitor::getInstance()->toArray();

        //The user ID who has received the note
        $userId = $this->_input->filterSingle('user_id', XenForo_Input::UINT);

        //Get the message content
        $note_message = $this->_input->filterSingle('note_message', XenForo_Input::STRING);

        //The date of the note given
        $note_date = XenForo_Application::$time;

        //Instance the DataWriter
        $dw = XenForo_DataWriter::create('newProfileTabs_DataWriter_Notes');
        $dw->set('given_user_id', $visitor['user_id']);
        $dw->set('received_user_id', $userId);
        $dw->set('note_message', $note_message);
        $dw->set('note_date', $note_date);
        $dw->save();

        $note = array('user' => $visitor, 'note_message' => $note_message, 'note_date' => $note_date);

        $viewParams = array(
            'note' => $note
        );

        return $this->responseView('XenForo_ViewPublic_Base', 'newProfileTab_ourTab_newnote', $viewParams);
    }

Explaining:

Sorry folks, getting a bit lost here. Im guessing theres supposed to be more in the members.php file? However in the guide it says above is the final code....
 
Ah ok, Thanks Fuhrmann :D

When i wrote "the final code is here" i was saying about the code of actionSendNote. If you made the step by step of this tutorial, your file Member.php should be like this:

PHP:
<?php
class newProfileTabs_Extend_ControllerPublic_Member extends XFCP_newProfileTabs_Extend_ControllerPublic_Member
{
    /**
      * Action to load all the users notes given by admin/moderators.
    */
    public function actionNotes()
    {
        //Get the model
        $notesModel = $this->_getProfileTabsModel();

        //Filtering the user ID in the url
        $userId = $this->_input->filterSingle('user_id', XenForo_Input::UINT);

        //Get more fields
        $user = $this->getHelper('UserProfile')->assertUserProfileValidAndViewable($userId);

        //get all notes that was given to this user with this id
        $notes = $notesModel->getAllNotesReceivedByUserId($userId);
        //Prepare the user array
        $notes = $notesModel->prepareNotes($notes);

        /*Returning all the values to our template newProfileTab_ourTab_content
          with the variables $notes, and $user.*/
        $viewParams = array(
            'notes' => $notes,
            'user' => $user,
        );

        return $this->responseView(
            'XenForo_ViewPublic_Base', 'newProfileTab_ourTab_notes',    $viewParams
        );
    }

    /**
      * Action to send a note to the specified user.
    */
    public function actionSendNote()
    {
        //Make sure we are using a method form POST not GET
        $this->_assertPostOnly();

        //Lets get the ID of the user who is given the note
        $visitor = XenForo_Visitor::getInstance()->toArray();

        //The user ID who has received the note
        $userId = $this->_input->filterSingle('user_id', XenForo_Input::UINT);

        //Get the message content
        $note_message = $this->_input->filterSingle('note_message', XenForo_Input::STRING);

        //The date of the note given
        $note_date = XenForo_Application::$time;

        //Instance the DataWriter
        $dw = XenForo_DataWriter::create('newProfileTabs_DataWriter_Notes');
        $dw->set('given_user_id', $visitor['user_id']);
        $dw->set('received_user_id', $userId);
        $dw->set('note_message', $note_message);
        $dw->set('note_date', $note_date);
        $dw->save();

        $note = array('user' => $visitor, 'note_message' => $note_message, 'note_date' => $note_date);

        $viewParams = array(
            'note' => $note
        );

        return $this->responseView('XenForo_ViewPublic_Base', 'newProfileTab_ourTab_newnote', $viewParams);
    }
   
    /**
    * @return newProfileTabs_Model_newProfileModel
    */
    protected function _getProfileTabsModel()
    {
        return $this->getModelFromCache('newProfileTabs_Model_newProfileModel');
    }

}
?>
 
A quick note:

For stuff like
PHP:
//The date of the note given
        $note_date = XenForo_Application::$time;
$dw->set('note_date', $note_date);

i would suggest to do this inside of the datawriter
and because you're doing this already
PHP:
            'note_date' => array('type' => 'uint', 'default' => XenForo_Application::$time

you don't need this 2 lines inside of the controller;)
 
A quick note:

For stuff like
PHP:
//The date of the note given
        $note_date = XenForo_Application::$time;
$dw->set('note_date', $note_date);

i would suggest to do this inside of the datawriter
and because you're doing this already
PHP:
            'note_date' => array('type' => 'uint', 'default' => XenForo_Application::$time

you don't need this 2 lines inside of the controller;)

That's true. Thanks ragtek. Always helping!:ROFLMAO:
 
Status
Not open for further replies.
Top Bottom