Jump to:

23456 Posts in 18905 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » [Solved] Need help understanding Contollers

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

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

Page: 1
Go to End
Author Topic: 398 Views
  • cumquat
    Avatar
    Community Member
    195 Posts

    [Solved] Need help understanding Contollers Link to this post

    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;

       }

  • cumquat
    Avatar
    Community Member
    195 Posts

    Re: [Solved] Need help understanding Contollers Link to this post

    *Bump* Anyone, do you need more info?

    Mick

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: [Solved] Need help understanding Contollers Link to this post

    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.

  • cumquat
    Avatar
    Community Member
    195 Posts

    Re: [Solved] Need help understanding Contollers Link to this post

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

    }

    398 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.