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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

[Solved] Need help understanding Contollers


Go to End


4 Posts   840 Views

Avatar
cumquat

Community Member, 201 Posts

13 March 2013 at 12:26am

Edited: 14/03/2013 3:13am

Hi there,

I have an issue (one of many) i don't seem to be able to grasp how to use page controllers properly as i'm sure this current issue relates to this. I have a simply one field form that checks an external api and returns an xml list dependant on the MessageID sent in the form this is then styled by using an associated XSL file.
This works, if i hard code the MessageID in and obviously don't use the form, i get a nice styled response. when i add the form in it still works but it opens a new blank page with the data but the end of the URL is the form name. I'm trying to render the response of the form query within the same page or at the very least a page that looks like it's in the site.

Below is my current base code and as i said i'm sure the issue lays with my lack of knowledge with page controllers.

Any help please.

Mick

class SMSResponsePage_Controller extends Page_Controller {
	
	function msgCheck() {
		$fields = new FieldList(
			TextField::create("msgID","Msg ID")
		);
        $actions = new FieldList(
		new FormAction('domsgcheck', 'Get message')
		);
		$validator = new RequiredFields('msgID');
		$form = new Form($this, 'msgCheck', $fields,  $actions, $validator);
		return $form;
	}
	function domsgcheck($data, $form){
		$theID = $_POST["msgID"];
		$data = http_build_query(array(
			'orgcode' => 'XXXX',
			'apipin' => 'XXXX',
			'msgid' =>  $theID,
			'checkmode' => 'TESTXML'
		));
		
		$context = stream_context_create(array(
			'http' => array(
				'method' => 'POST',
				'header' => 'Content-Type: application/x-www-form-urlencoded',
				'content' => $data
			)
		));
		
		$xmlresponse = file_get_contents('http://XXXXXXXXXXXXXXXXXX.asp', false, $context);
		
		$xml = new DOMDocument();
		$xml->loadXML(trim($xmlresponse));
		$xsl = new DOMDocument;
		$xsl->load('../Search/css/sms.xsl');
		$proc = new XSLTProcessor();
		$proc->importStyleSheet($xsl);
		$response = $proc->transformToXML($xml);
		return $response;

	}

Avatar
cumquat

Community Member, 201 Posts

14 March 2013 at 3:20am

*Bump* Anyone, do you need more info?

Mick

Avatar
Devlin

Community Member, 344 Posts

14 March 2013 at 5:53am

Edited: 14/03/2013 6:10am

Not tested, but to get the idea....

class SMSResponsePage_Controller extends Page_Controller {
	static $url_handlers = array(
		'show/$msgID!/' => 'handleAction',
	);

	static $allowed_actions = array (
		'msgCheck',
		'show',
	);

	// form
	function msgCheck() {
		// ...
	}

	// form submit
	function domsgcheck($data, $form){ 
		if( !empty($data["msgID"]) ) {
			Director::redirect( $this->Link('show').(int)$data["msgID"] );
		}

		Director::redirectBack();
	}
	
	function parseXSLSomething() {
		$params = $this->getURLParams();
		if ( !empty($params['msgID']) ) {
			$theID = (int)$params['msgID'];
			
			// ...
			
			// return $response
		}
		
		return false;
	}
}

The form redirects to mysite.com/thisPage/show/123 and then you can call your XSL file in the template with $parseXSLSomething.

Avatar
cumquat

Community Member, 201 Posts

14 March 2013 at 9:32pm

Bloody supurb cheers for that, i didn't think of passing it to the url. Made a couple of tickles to the code which is below. Many thanks for that.

Mick

class SMSResponsePage_Controller extends Page_Controller {
	
	static $url_handlers = array( 
      'show/$msgID!/' => 'handleAction', 
   );

   static $allowed_actions = array ( 
      'msgCheck', 
      'show', 
   );
	
	public function init() {
		parent::init();
	
		 
	}
	
	 
	function msgCheck() {
		$fields = new FieldList(
			TextField::create("msgID","Msg ID")
		);
        $actions = new FieldList(
		new FormAction('domsgcheck', 'Get message')
		);
		$validator = new RequiredFields('msgID');
		$form = new Form($this, 'msgCheck', $fields,  $actions, $validator);
		return $form;
	}
	
	function domsgcheck($data, $form){ 
      if( !empty($data["msgID"]) ) { 
         
      Controller::curr()->redirect($this->Link('show/').(int)$data["msgID"] ); 
	  }

      Controller::curr()->redirectBack();
   } 
	
	
	function xslthing(){
 
		$params = $this->getURLParams(); 
      		if ( !empty($params['ID']) ) { 
         		$theID = (int)$params['ID']; 
		 
				$data = http_build_query(array(
					'orgcode' => 'xxx',
					'apipin' => 'xxx',
					'msgid' =>  $theID ,
					'checkmode' => 'TESTXML'
				));
				
				$context = stream_context_create(array(
					'http' => array(
						'method' => 'POST',
						'header' => 'Content-Type: application/x-www-form-urlencoded',
						'content' => $data
					)
				));
				
				$xmlresponse = file_get_contents('http://xxxxxxxxxxxxxxxx.asp', false, $context);
				
				$xml = new DOMDocument();
				$xml->loadXML(trim($xmlresponse));
				$xsl = new DOMDocument;
				$xsl->load('../Search/css/sms.xsl');
				$proc = new XSLTProcessor();
				$proc->importStyleSheet($xsl);
				$response = $proc->transformToXML($xml);
		
				return $response;
	
			}
		return false;
		
	}	
	
	
	


}