I don't understand the process of how template functions in the Controller can understand what action is currently "active".
I have a page that will display videos submitted by users at the top. Instead of immediately showing the form, I'd like to have a link at the bottom saying "Submit video" that will redirect to the same page with an action e.g /videos/submit which will display the form and nothing else.
1. General question first. So I have a template function, e.g. VideoSubmitForm() (which will return, in this case, a form into the template) and I have a "function" that is, as far as I understand by definition, an "action". How are or should the two be different? How do we know we're dealing with an action vs a template function. If I just put in my mydomain.com/VideoSubmitForm - in what way is this processed differently than let's say mydomain.com/submit?
2. I'd like the template function to know what "action" we are on. If the action is nil, the videos are displayed. If the "submit" action is evoked, the videos aren't displayed (meaning, that the $Videos, a has_many element, should not be read - do I override the actual $Videos somehow or should I just create a custom template function to pull the videos? Similarly, the $VideoSubmitForm() will return the form in this case.
Then, on submission, (e.g. /videos/videosubmitted action) I'd like to have a message saying something like thanks for your submission, still no $Videos and a link that will return to itself.
3. In the above vein... How do I manipulate $Content from within actions?
Adding current code if it helps understanding.
<?php
class VideoPage extends Page {
static $db = array(
'EmailRecepient'=>'Text'
);
static $has_one = array(
);
static $has_many = array(
'Videos'=>'Video'
);
static $defaults = array(
);
static $allowed_children = array("none");
public function getCMSFields()
{
$f = parent::getCMSFields();
$videoManager = new DataObjectManager(
$this, // Controller
'Videos', // Source name
'Video', // Source class
array(
'Published' => 'Published',
'Title' => 'Title',
'Platform' => 'Platform',
'Name' => 'Submitted by'
), // Headings
'getCMSFields_forPopup', // Detail fields (function name or FieldSet object)
'', // Filter clause
'Published ASC, Created DESC' // Sort clause
// Join clause
);
$f->addFieldToTab("Root.Content.Videos",$videoManager);
return $f;
}
}
class VideoPage_Controller extends Page_Controller {
public function init() {
parent::init();
}
/* function index() { //DO I NEED THIS?
//Director::redirect('home');
//return $this->customise(array()
}
*/
function show() {
$params = Director::urlParams();
$id = (int)$params['ID'];
$object = DataObject::get_by_id("Video", $id);
if($object) {
return $this
->customise(array('Video' => $object))
->renderWith(array('VideoPage', 'Page')); //Layout
} else {
Director::redirect('not-found');
}
}
function submit() {
//
}
function VideoForm() {
// Create fields but conditionally
if(Session::get('VideoSubmitted')) {
//return false;
//DISPLAY FEEDBACK AND LINK TO /page alone sans action
}
$fields = new FieldSet(
new TextField('Name',_t('Name','Your name')),
new EmailField('Email',_t('Email','Your email')),
new CheckboxField('ShowEmail',_t('VideoForm.ShowEmail','Show E-mail address')),
new TextField('Title',_t('VideoForm.Title','Title of Video')),
new OptionsetField('Platform', _t('VideoForm.Platform','Video platform'), array(
'YouTube' => 'YouTube',
'Vimeo' => 'Vimeo'
)),
new TextField('VideoID',_t('VideoForm.VideoID','Video ID')),
new TextAreaField('Description',_t('VideoForm.Description','Video Description')),
new HiddenField ('Published','Published','0'),
new HiddenField ('VideoPageID','VideoPageID',$this->ID)
);
// Create actions
$actions = new FieldSet(
new FormAction('doVideoForm', 'Submit')
);
$validator = new RequiredFields('Name', 'Email', 'Platform','VideoID');
return new Form($this, 'VideoForm', $fields, $actions, $validator);
}
function doVideoForm($data, $form) {
$submission = new Video();
$form->saveInto($submission);
$submission->write();
Session::set('VideoSubmitted', true);
//on write SUBMIT EMAIL TO $EmailRecipient
//$email = new Email($from, $to, $subject, $body);
//$email->send();
Director::redirectBack();
}
}
?>