Skip to content

Instantly share code, notes, and snippets.

@Firesphere
Last active December 5, 2018 06:03
Show Gist options
  • Save Firesphere/f722ca45834f40bcc6ed5d40bd0b39cd to your computer and use it in GitHub Desktop.
Save Firesphere/f722ca45834f40bcc6ed5d40bd0b39cd to your computer and use it in GitHub Desktop.
Page Controller
<% if $Messages.Count %>
<% loop $Messages %>
<% include Message isSearch=$Top.isSearch %>
<% end_loop %>
<% end_if %>
<!-- Both edit and new will hit this template -->
$EditForm
<?php
namespace Firesphere\StripeSlack\Controllers;
use Firesphere\StripeSlack\Forms\SearchForm;
use Firesphere\StripeSlack\Helpers\Emojis;
use Firesphere\StripeSlack\Models\Channel;
use Firesphere\StripeSlack\Models\Message;
use Firesphere\StripeSlack\Models\SlackUser;
use PageController;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\PaginatedList;
use SilverStripe\View\ArrayData;
/**
* Class \Firesphere\StripeSlack\Controllers\SlackPageController
*
*/
class SlackPageController extends PageController
{
private static $allowed_actions = [
'edit',
];
private static $url_handlers = [
'edit' => 'edit',
'new' => 'edit'
];
// The $Messages in the template will hit this method
public function getMessages()
{
$channelID = Controller::curr()->getRequest->param('ID');
/** @var Channel $channel */
$channel = Channel::get()->filter(['Name' => $channelID])->first();
if (!$channel) {
$channel = Channel::get()->filter(['SlackId' => $channelID])->first();
}
$vars = Controller::curr()->getRequest->getVars();
unset($vars['message']);
if (!$channel) {
return $this->httpError(404, 'Channel not found');
}
$messages = $channel->Messages()->filter(['ParentID' => 0]);
$vars = Controller::curr()->getRequest()->getVars();
// Returns the "Messages", to be looped in the template. The request should be available
return PaginatedList::create($messages, $this->getRequest);
}
public function edit()
{
return $this;
}
public function getEditForm()
{
$data = [];
$this->getRequest->getSession->set('ObjectType', 'new');
if ($id = $this->getRequest()->URLParams['ID']) {
$data = MyObject::get()->byID($id);
$this->getRequest->getSession->set('ObjectType', $id);
}
$form = Form::create($this, __FUNCTION__, $myGeneratedFieldList, $myGeneratedFieldActions);
$form->populateWith($data);
}
public function handleForm($data, $form)
{
$fromSession = $this->getRequest()->getSession()->get('ObjectType');
if ($fromSession === 'new') {
// handle new submission
} else {
// Handle edit submission
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment