Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Actions and conditions in template functions


Go to End


2 Posts   4125 Views

Avatar
DesignCollective

Community Member, 66 Posts

21 July 2010 at 2:19pm

Edited: 21/07/2010 2:19pm

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();
     
   }
	
}


?>

Avatar
Willr

Forum Moderator, 5523 Posts

21 July 2010 at 4:59pm

I'll attempt to explain them..

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.

Well to be clear if you have say videos/upload which is an upload page VideoSubmitForm is a function. The action or more specifically the URLAction is 'upload'. The function is simply a function which could be called anyway. I'm not sure what would happen if you had a template action and a form processing function at the same time but I believe it should be smart enough to use the form action.

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.

You can use $this->urlParams['Action'] to get the name of the current action (or blank if no action available) in which case 'index' is called.

3. In the above vein... How do I manipulate $Content from within actions?

In your action you can return an array to override any of the variables for example

function upload() {

$content = DBField::create('HTMLText', '<p>My New Upload Content</p>');

return array('Content' => $content);
}