TheBigK
Well-known member
I'm a total n00b to XF development and PHP as well and stuck with the code. It'd be really nice if you could help with the following.
Addon Idea: 'Invite Me'
What It Does: Shows a button that says "I'm Interested", which when clicked, registers the Username of the user in a database table. If the user has already clicked the button, it will appear disabled with a message "You're already in the invite list".
The admin gets to see the list of all of the users who have clicked the button in the admin control panel as
[Username] |
What I've done so far:-
1. I began developing this addon by first creating the Route Prefix:-
2. I then created ControllerPublic as follows [This one is messy and I'm stuck here]
3. DataWriter:
Questions:
1. I've been told that this one won't need a DataWriter and I can achieve saving of the username through a query. But, that'd be 'optimisation' . I wish to understand how to write this controller that uses a datawriter.
2. I've absolutely no clue about the $viewParams - and what should it have for the approach that I've adopted.
3.
//Get information about the visitor
$visitor = XenForo_Visitor::getInstance();
Why is it that I can't use $visitor defined in my actionIndex() in actionAcceptInvite(), without having to declare it again? Isn't it 'public' and should be usable throughout the class?
Addon Idea: 'Invite Me'
What It Does: Shows a button that says "I'm Interested", which when clicked, registers the Username of the user in a database table. If the user has already clicked the button, it will appear disabled with a message "You're already in the invite list".
The admin gets to see the list of all of the users who have clicked the button in the admin control panel as
[Username] |
What I've done so far:-
1. I began developing this addon by first creating the Route Prefix:-
PHP:
<?php
class InviteMe_Prefix_Route_Index implements XenForo_Route_Interface
{
public function match($routePath, Zend_Controller_Request_Http $request, XenForo_Router $router)
{
return $router->getRouteMatch('InviteMe_ControllerPublic_Index',$routePath);
}
}
2. I then created ControllerPublic as follows [This one is messy and I'm stuck here]
PHP:
<?php
class InviteMe_ControllerPublic_Index extends XenForo_ControllerPublic_Abstract
{
public function actionIndex()
{
//Get InviteMeModel
$inviteMeModel = $this->getModelFromCache('InviteMe_Model_Invite');
//Get information about the visitor
$visitor = XenForo_Visitor::getInstance();
//Get the list of existing applicants
$alreadyApplied = $inviteMeModel->getApplied();
//Check if user's username already exists in our invite table
if (in_array($visitor['username'], $alreadyApplied))
{
return $this->responseError('You are already in our waiting list.');
}
$viewParams = array($visitor['username']);
return $this->responseView('InviteMe_ViewPublic_Index', 'inviteMe', $viewParams);
}
public function actionAcceptInvite()
{
//Make this a POST action
$this->_assertPostOnly();
//Get information about the visitor
$visitor = XenForo_Visitor::getInstance();
//Make this available for registered members only
$this->_assertRegistrationRequired();
//Create A DataWriter To Save The User ID To Database
$dw = XenForo_DataWriter::create('InviteMe_DataWrite_Index');
$dw->set('user_id', $visitor['username']);
$dw->save();
return $this->responseRedirect(XenForo_ControllerResponse_Redirect::SUCCESS, XenForo_Link::buildPublicLink('invite'));
}
}
3. DataWriter:
PHP:
<?phpclass InviteMe_DataWriter_Index extends XenForo_DataWriter
{
protected function _getExistingData($data)
}
Questions:
1. I've been told that this one won't need a DataWriter and I can achieve saving of the username through a query. But, that'd be 'optimisation' . I wish to understand how to write this controller that uses a datawriter.
2. I've absolutely no clue about the $viewParams - and what should it have for the approach that I've adopted.
3.
//Get information about the visitor
$visitor = XenForo_Visitor::getInstance();
Why is it that I can't use $visitor defined in my actionIndex() in actionAcceptInvite(), without having to declare it again? Isn't it 'public' and should be usable throughout the class?