Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Data binding with PHP backend

    Hi,

    I implemented fetch/add/update/remove interface for RestDataSource by
    PHP and can connect ListGrid with my PHP backend (MySQL) successfully.

    However, when I try to add more features such as sorting and filtering,
    I got some problems ...

    For sorting, I found SC will provide an extra GET parameter, '_sortBy',
    for "fetch" command. The sorting feature of ListGrid doesn't work
    at the beginning ... however, if I scroll the ListGrid to the end, then
    the sorting is working again (looks like ListGrid must have all data fetched
    from DB to browser to make sorting work).

    And for filtering, it never works.

    So I guess, both sorting and filtering is the responsibility of PHP
    backend. I need to do something in PHP to support that feature, am I
    correct ?

    What kind of response the RestDataSource expected from my PHP ?
    Any documentation or examples ?
    It will be great if SmartGWT can provide some PHP template code to handle
    sort/filter request.

    Regards
    KC

    #2
    Hi,

    I solved the problem.
    The sorting/filtering does need PHP backend's help to fully functional.

    Base on the parameter '_sortBy' and '_textMatchStyle' send by SC, combined
    with MySQL's "ORDER BY ..." and "WHERE fieldName LIKE ..." condition, the
    ListGrid's sorting/filtering function work perfectly :-)

    Regards
    KC

    Comment


      #3
      Great! If you can any chance create a simple tutorial outlining your work it will be very useful to other users.

      Sanjiv

      Comment


        #4
        Good to hear kccheng. Just so you're aware, filtering and sorting can be done client-side, server-side or dynamically in a mix (called Adaptive Filtering / Adaptive Sorting). The SmartClient docs for ResultSet explain this in detail (note these don't appear within SmartGWT yet, but will soon).

        Comment


          #5
          Originally posted by sjivan
          Great! If you can any chance create a simple tutorial outlining your work it will be very useful to other users.

          Sanjiv
          I'll love to do that ... but may take a while to clean up my code

          Regards
          KC

          Comment


            #6
            Originally posted by kccheng
            I'll love to do that ... but may take a while to clean up my code
            I've got no problem with ugly code. Maybe it's a lot faster to view your code than trying all by myself as i did with gwt-ext with php backend.

            Comment


              #7
              Originally posted by xelibrety
              I've got no problem with ugly code. Maybe it's a lot faster to view your code than trying all by myself as i did with gwt-ext with php backend.
              No problem, here is my PHP:

              Code:
              <?php
              ini_set("include_path", "/opt/iPortal2/config");
              require_once("iportal2.inc.php");
              require_once("asset.inc.php");
              
              if (!function_exists('lcfirst')) {
                  function lcfirst( $str )
                  { return (string)(strtolower(substr($str,0,1)).substr($str,1));}
              }
              if (!function_exists('ucfirst')) {
                  function ucfirst( $str )
                  { return (string)(strtoupper(substr($str,0,1)).substr($str,1));}
              }
              
              $my = mysql_connect($ASSET_HOST, $ASSET_USER, $ASSET_PASSWD);
              mysql_select_db($ASSET_DB);
              mysql_query("SET NAMES UTF8", $my)
                  or die('mysql_query: set UTF8 NAMES failed: ' . mysql_error());
              
              if (!($result = mysql_query('select count(id) from asset'))) {
                  die('invalid query: ' . mysql_error());
              }
              $row = mysql_fetch_row($result);
              $totalRows = $row[0];
              
              if (isset($_GET['_operationType']) && strcmp($_GET['_operationType'], "fetch") == 0) {
              
                  $sqlcmd  = 'select * from asset ';
                  if (isset($_GET['_textMatchStyle'])) {
                      $sqlcmd .= 'where ';
                      $first = 1;
                      $hasFilter = 0;
                      foreach ($_GET as $key => $value) {
                          $value = trim($value);
                          if (preg_match("/^[\$_](.*)/", $key) || $value == "null" || strlen($value) == 0)
                              continue;
                          if ($first) {
                              $first = 0;
                          } else {
                              $sqlcmd .= ' AND ';
                          }
                          $sqlcmd .= ucfirst($key) . ' like "%' . $value . '%" ';
                          $hasFilter = 1;
                      }
                      if (!$hasFilter) {
                          $sqlcmd .= ' id > 0 ';
                      }
                  }
                  if (isset($_GET['_sortBy'])) {
                      if ($_GET['_sortBy'][0] == '-') {
                          $sort = substr($_GET['_sortBy'],1);
                          $sqlcmd .= 'ORDER BY ' . $sort . ' DESC ';
                      } else {
                          $sort = $_GET['_sortBy'];
                          $sqlcmd .= 'ORDER BY ' . $sort . ' ASC ';
                      }
                  }
                  if ($hasFilter) {
                      // update totalRows ... before we attcah "LIMIT ..."
                      if (!($result = mysql_query($sqlcmd))) {
                          die('invalid query: ' . mysql_error());
                      }
                      $totalRows = mysql_num_rows($result);
                  }
              
                  if (isset($_GET['_startRow']) || isset($_GET['_endRow'])) {
                      $sqlcmd .= 'limit ' . $_GET['_startRow'] . ',' . $_GET['_endRow'];
                  }
              
                  if (!($result = mysql_query($sqlcmd))) {
                      $json = array();
                      $json['response'] = array();
                      $json['response']['status']  = -1;
                      $json['response']['sqlcmd']  = '"' . $sqlcmd . '"';
                  } else {
              
                      $json = array();
                      $json['response'] = array();
                      $json['response']['status']    = 0;
                      $json['response']['startRow']  = $_GET['_startRow'];
                      $json['response']['endRow']    = $_GET['_endRow'];
                      $json['response']['totalRows'] = $totalRows;
                      $json['response']['data'] = array();
              
                      $i = 0;
                      while ($row = mysql_fetch_assoc($result)) {
              
                          $json['response']['data'][$i] = array();
                          for ($col=0; $col<$asset_length; ++$col) {
                              $dbkey = $asset[$col]['column'];
                              $dskey = lcfirst($dbkey);
                              if (isset($row[$dbkey]) && $asset[$col]['display_flag'] && strlen($row[$dbkey]) > 0) {
                                  $json['response']['data'][$i][$dskey] = $row[$dbkey];
                              }
                          }
                          ++$i;
                      }
                  }
                  echo json_encode($json);
              } else if (isset($_POST['_operationType']) && strcmp($_POST['_operationType'], "add") == 0) {
              
                  $sqlcmd  = 'insert into asset set ';
                  $first = 1;
                  foreach ($_POST as $key => $value) {
                      /* NOTE: SC will pass parameters such as $_POST['isFolder'] & $_POST['groupParentId']
                       * and $_POST['$399'] ... parameter not start by '$' such as 'isFolder' has "null" value ... */
                      $value = trim($value);
                      if (preg_match("/^[\$_](.*)/", $key) || $value == "null" || strlen($value) == 0)
                          continue;
              
                      if ($first) {
                          $first = 0;
                      } else {
                          $sqlcmd .= ',';
                      }
                      $sqlcmd .= ucfirst($key) . '="' . $value . '"';
                  }
              
                  if (!($result = mysql_query($sqlcmd))) {
                      $json = array();
                      $json['response'] = array();
                      $json['response']['status']  = -1;
                  } else {
                      $json = array();
                      $json['response'] = array();
                      $json['response']['status']  = 0;
                      $json['response']['data']    = array();
                      $json['response']['data'][0] = array();
                      foreach ($_POST as $key => $value) {
                          /* NOTE: SC will pass parameters such as $_POST['isFolder'] & $_POST['groupParentId']
                           * and $_POST['$399'] ... parameter not start by '$' such as 'isFolder' has "null" value ... */
                          $value = trim($value);
                          if (preg_match("/^[\$_](.*)/", $key) || $value == "null" || strlen($value) == 0)
                              continue;
                          $json['response']['data'][0][$key] = $value;
                      }
                  }
                  echo json_encode($json);
              } else if (isset($_POST['_operationType']) && strcmp($_POST['_operationType'], "update") == 0) {
              
                  $sqlcmd = 'select count(IpAddress) from asset where IpAddress="' . $_POST[ipAddress] . '"';
                  if (!($result = mysql_query($sqlcmd))) {
                      die('invalid query: ' . mysql_error());
                  }
                  $row = mysql_fetch_row($result);
                  $ipExisted = $row[0];
              
                  if ($ipExisted) {   // UPDATE
                      $sqlcmd  = 'update asset set ';
                      $first = 1;
                      foreach ($_POST as $key => $value) {
                          /* NOTE: SC will pass parameters such as $_POST['isFolder'] & $_POST['groupParentId']
                           * and $_POST['$399'] ... parameter not start by '$' such as 'isFolder' has "null" value ... */
                          $value = trim($value);
                          if (preg_match("/^[\$_](.*)/", $key) || $value == "null" || strlen($value) == 0)
                              continue;
                          if ($first) {
                              $first = 0;
                          } else {
                              $sqlcmd .= ',';
                          }
                          $sqlcmd .= ucfirst($key) . '="' . $value . '"';
                      }
                      $sqlcmd .= ' where IpAddress="'. $_POST['ipAddress'] . '"';
              
                  } else {    // ADD
              
                      $sqlcmd  = 'insert into asset set ';
                      $first = 1;
                      foreach ($_POST as $key => $value) {
                          /* NOTE: SC will pass parameters such as $_POST['isFolder'] & $_POST['groupParentId']
                           * and $_POST['$399'] ... parameter not start by '$' such as 'isFolder' has "null" value ... */
                          $value = trim($value);
                          if (preg_match("/^[\$_](.*)/", $key) || $value == "null" || strlen($value) == 0)
                              continue;
                          if ($first) {
                              $first = 0;
                          } else {
                              $sqlcmd .= ',';
                          }
                          $sqlcmd .= ucfirst($key) . '="' . $value . '"';
                      }
                  }
              
                  if (!($result = mysql_query($sqlcmd))) {
                      $json = array();
                      $json['response'] = array();
                      $json['response']['status']  = -1;
                      $json['response']['sqlcmd']  = '"' . $sqlcmd . '"';
                  } else {
                      $json = array();
                      $json['response'] = array();
                      $json['response']['status']  = 0;
                      $json['response']['data']    = array();
                      $json['response']['data'][0] = array();
                      foreach ($_POST as $key => $value) {
                          /* NOTE: SC will pass parameters such as $_POST['isFolder'] & $_POST['groupParentId']
                           * and $_POST['$399'] ... parameter not start by '$' such as 'isFolder' has "null" value ... */
                          $value = trim($value);
                          if (preg_match("/^[\$_](.*)/", $key) || $value == "null" || strlen($value) == 0)
                              continue;
                          $json['response']['data'][0][$key] = $value;
                      }
                  }
                  echo json_encode($json);
              } else if (isset($_POST['_operationType']) && strcmp($_POST['_operationType'], "remove") == 0) {
              
              
                  $sqlcmd = 'delete from asset where IpAddress="' . $_POST['ipAddress'] . '"';
                  if (!($result = mysql_query($sqlcmd))) {
                      $json = array();
                      $json['response'] = array();
                      $json['response']['status']  = -1;
                      $json['response']['sqlcmd']  = '"' . $sqlcmd . '"';
                  } else {
                      $json = array();
                      $json['response'] = array();
                      $json['response']['status']  = 0;
                      $json['response']['data']    = array();
                      $json['response']['data'][0] = array();
                      $json['response']['data'][0]['ipAddress'] = $_POST['ipAddress'];
                  }
                  echo json_encode($json);
              }
              
              ?>

              Comment


                #8
                Thanks!
                Actually i meant the java code where you implement the connection to the PHP script. Or better the code fragment where you send a sort/query command to the script, but this also helps a lot right now.

                Comment


                  #9
                  Originally posted by xelibrety
                  Thanks!
                  Actually i meant the java code where you implement the connection to the PHP script. Or better the code fragment where you send a sort/query command to the script, but this also helps a lot right now.
                  The java code is very simple ... my version is modified from PatternReuseSample
                  which came from Showcase.

                  Code:
                  package com.LinuxDAQ_Labs.www.samples.smartgwt.client.data;
                  
                  import com.smartgwt.client.data.RestDataSource;
                  import com.smartgwt.client.data.DataSourceField;
                  import com.smartgwt.client.types.DSDataFormat;
                  import com.smartgwt.client.data.fields.*;
                  
                  public class AssetDS extends RestDataSource {
                  
                      private static AssetDS instance = null;
                  
                      public AssetDS(String id) {
                          setID(id);
                          int minWidth = 100;
                  
                          setRecordXPath("/iPortal2/asset");
                  
                          DataSourceTextField ipAddressField  = new DataSourceTextField("ipAddress",  "IpAddress", minWidth);
                          ipAddressField.setRequired(true);
                          ipAddressField.setPrimaryKey(true);
                  
                          DataSourceTextField customerIdField = new DataSourceTextField("customerId",  "CustomerId", minWidth);
                  //      customerIdField.setRequired(true);
                  //      customerIdField.setForeignKey(id + ".ownerFirstName");
                  //      customerIdField.setRootValue("1");
                  
                          DataSourceTextField hostNameField  = new DataSourceTextField("hostName",  "HostName", minWidth);
                          DataSourceTextField macAddressField  = new DataSourceTextField("macAddress",  "MacAddress", minWidth);
                          DataSourceFloatField latitudeField = new DataSourceFloatField("latitude",  "Latitude", minWidth);
                          DataSourceFloatField longitudeField = new DataSourceFloatField("longitude", "Longitude", minWidth);
                          DataSourceFloatField zindexField = new DataSourceFloatField("zindex",  "Zindex", minWidth);
                          DataSourceTextField assetEntryTypeField  = new DataSourceTextField("assetEntryType",  "AssetEntryType", minWidth);
                          DataSourceEnumField assetCategoryField  = new DataSourceEnumField("assetCategory",  "AssetCategory", minWidth);
                          assetCategoryField.setValueMap("UNKNOWN", "DESKTOP", "SERVER", "FIREWALL", "SWITCH", "ROUTER", "PRINTER", "IDS", "AP");
                          DataSourceTextField networkIdentityField  = new DataSourceTextField("networkIdentity",  "NetworkIdentity", minWidth);
                          DataSourceTextField environmentIdentityField  = new DataSourceTextField("environmentIdentity",  "EnvironmentIdentity", minWidth);
                          DataSourceEnumField assetValueField  = new DataSourceEnumField("assetValue",  "AssetValue", minWidth);
                          assetValueField.setValueMap("L", "M", "H");
                          DataSourceEnumField criticalityField  = new DataSourceEnumField("criticality",  "Criticality", minWidth);
                          criticalityField.setValueMap("L", "M", "H");
                          DataSourceEnumField sensitivityField  = new DataSourceEnumField("sensitivity",  "Sensitivity", minWidth);
                          sensitivityField.setValueMap("L", "M", "H");
                          DataSourceTextField assetNameField  = new DataSourceTextField("assetName",  "AssetName", minWidth);
                          DataSourceTextField productNameField  = new DataSourceTextField("productName",  "ProductName", minWidth);
                          DataSourceTextField vendorField  = new DataSourceTextField("vendor",  "Vendor", minWidth);
                          DataSourceTextField ownerFirstNameField = new DataSourceTextField("ownerFirstName",  "OwnerFirstName", minWidth);
                          DataSourceTextField ownerLastNameField = new DataSourceTextField("ownerLastName",  "OwnerLastName", minWidth);
                          DataSourceTextField ownerPhoneNumberField = new DataSourceTextField("ownerPhoneNumber",  "OwnerPhoneNumber", minWidth);
                          DataSourceTextField ownerEmailField = new DataSourceTextField("ownerEmail",  "OwnerEmail", minWidth);
                          DataSourceTextField maintainerFirstNameField = new DataSourceTextField("maintainerFirstName",  "MaintainerFirstName", minWidth);
                          DataSourceTextField maintainerLastNameField = new DataSourceTextField("maintainerLastName",  "MaintainerLastName", minWidth);
                          DataSourceTextField maintainerPhoneNumberField = new DataSourceTextField("maintainerPhoneNumber",  "MaintainerPhoneNumber", minWidth);
                          DataSourceTextField maintainerEmailField = new DataSourceTextField("maintainerEmail",  "MaintainerEmail", minWidth);
                          DataSourceTextField businessUnitField = new DataSourceTextField("businessUnit",  "BusinessUnit", minWidth);
                          DataSourceTextField lineOfBusinessField = new DataSourceTextField("lineOfBusiness",  "LineOfBusiness", minWidth);
                          DataSourceTextField divisionField = new DataSourceTextField("division",  "Division", minWidth);
                          DataSourceTextField departmentField = new DataSourceTextField("department",  "Department", minWidth);
                          DataSourceTextField personnelSeqField = new DataSourceTextField("personnelSeq",  "PersonnelSeq", minWidth);
                          DataSourceTextField buildingField = new DataSourceTextField("building",  "Building", minWidth);
                          DataSourceTextField roomField = new DataSourceTextField("room",  "Room", minWidth);
                          DataSourceTextField rackNumberField = new DataSourceTextField("rackNumber",  "RackNumber", minWidth);
                          DataSourceTextField addressLine1Field = new DataSourceTextField("addressLine1",  "AddressLine1", minWidth);
                          DataSourceTextField addressLine2Field = new DataSourceTextField("addressLine2",  "AddressLine2", minWidth);
                          DataSourceTextField cityField = new DataSourceTextField("city",  "City", minWidth);
                          DataSourceTextField stateField = new DataSourceTextField("state",  "State", minWidth);
                          DataSourceTextField countryField = new DataSourceTextField("country",  "Country", minWidth);
                          DataSourceTextField zipCodeField = new DataSourceTextField("zipCode",  "ZipCode", minWidth);
                  
                          setFields(
                              customerIdField,
                              ipAddressField,
                              hostNameField,
                              macAddressField,
                              latitudeField, longitudeField, zindexField,
                              assetEntryTypeField,
                              assetCategoryField,
                              networkIdentityField,
                              environmentIdentityField,
                              assetValueField,
                              criticalityField,
                              sensitivityField,
                              assetNameField,
                              productNameField,
                              vendorField,
                              ownerFirstNameField, ownerLastNameField, ownerPhoneNumberField, ownerEmailField,
                              maintainerFirstNameField, maintainerLastNameField, maintainerPhoneNumberField, maintainerEmailField,
                              businessUnitField,
                              lineOfBusinessField,
                              divisionField,
                              departmentField,
                              personnelSeqField,
                              buildingField, roomField, rackNumberField,
                              addressLine1Field, addressLine2Field,
                              cityField, stateField, countryField, zipCodeField
                          );
                  
                          setDataFormat(DSDataFormat.JSON);
                          setFetchDataURL("php/assetDS.php");
                          setAddDataURL("php/assetDS.php");
                          setUpdateDataURL("php/assetDS.php");
                          setRemoveDataURL("php/assetDS.php");
                          setClientOnly(false);
                      }
                  
                  }
                  Hope this help.

                  Comment


                    #10
                    Thank you, that's really what i was looking for.

                    Comment


                      #11
                      Redundant PHPs

                      Hi,

                      yes, i've been looking for a smart example like that too. Perfect. Nearly. As we all are juggling with more than one table, it's neccessary to create as many xxxDS.php files, as there are tables in your project. Do you know, if there's a way to handle the DS with a single PHP? The numerous xxxDS.java classes are needed, no discussion, but inside the php only the table name needs to be changed, if the rest of the table has some standards (id as primary key for access, etc.). Is the accessed table name passed to the PHP by the kernel DS or is there another way to limit the amount of PHPs ?

                      Comment


                        #12
                        Originally posted by CarstenRoesner
                        Hi,

                        yes, i've been looking for a smart example like that too. Perfect. Nearly. As we all are juggling with more than one table, it's neccessary to create as many xxxDS.php files, as there are tables in your project. Do you know, if there's a way to handle the DS with a single PHP? The numerous xxxDS.java classes are needed, no discussion, but inside the php only the table name needs to be changed, if the rest of the table has some standards (id as primary key for access, etc.). Is the accessed table name passed to the PHP by the kernel DS or is there another way to limit the amount of PHPs ?
                        I will prefer multiple PHP and keep the PHP as simple as possible.
                        However, if you do want to use single PHP to handle multiple
                        tables ... I believe $_GET['_dataSource'] and $_POST['_dataSource']
                        are what you need.

                        Comment


                          #13
                          I am also trying to use datasources, but there must be a stupid mistake in my code, but cannot find it. Can sy tell me what is the problem?

                          Testing this with Firebug, form submits a POST request (as Firebug shows) but PHP fills the GET array and $_POST is null.
                          But the grid is only sending empty POST requests for the server for all operation.
                          If I use simple DataSource or RestDataSource, then it sometimes does not send anything (in Firebug there is no outgoing request) or I get an error with IDACall, it's weird.

                          Also checked the SC-console, sometimes there is an error with this.getDatasource() method...

                          I'm using smartgwt 1.0b1, because b2 freezes with my tree menu (it is also a datasource problem, see http://code.google.com/p/smartgwt/issues/detail?id=135).

                          It would be a great help for me, thanks in advance.

                          Code:
                                  SC.say( "start" );
                          
                                  final DynamicForm form = new DynamicForm();
                                  form.setMethod( FormMethod.POST );
                                  TextItem tf = new TextItem( "hostName" );
                                  tf.setAttribute( "hostName", "Example1" );
                                  SubmitItem sendButton = new SubmitItem( "Send" );
                                  sendButton.addClickHandler( new com.smartgwt.client.widgets.form.fields.events.ClickHandler()
                                  {
                          
                                      public void onClick( com.smartgwt.client.widgets.form.fields.events.ClickEvent event ) {
                                          SC.say( "" + form.getValueAsString( "hostName" ) );
                                          form.saveData();
                                      }
                                  } );
                          
                                  MyDataSource ds = new MyDataSource( "ds-1" );
                                  ds.setDataFormat( DSDataFormat.JSON );
                          
                                  form.setDataSource( ds );
                                  form.setFields( tf, sendButton );
                          
                                  addMember( form );
                          
                                  final ListGrid grid = new ListGrid();
                                  MyDataSource ds2 = new MyDataSource( "ds-2" );
                          
                                  grid.setFields( new ListGridField[] { new ListGridField( "hostName", "HostName" ) } );
                          
                                  ListGridRecord record = new ListGridRecord();
                                  record.setAttribute( "hostName", "Example2" );
                          
                                  //grid.addData( record );
                                  grid.setDataSource( ds2 );
                          
                                  IButton fetchButton = new IButton( "Fetch" );
                                  fetchButton.addClickHandler( new com.smartgwt.client.widgets.events.ClickHandler() {
                          
                                      public void onClick( com.smartgwt.client.widgets.events.ClickEvent event ) {
                                          grid.fetchData();
                                          grid.fetchData( new Criteria( "hostName", "something" ) );
                                      }
                          
                                  } );
                                  IButton addButton = new IButton( "Add" );
                                  addButton.addClickHandler( new com.smartgwt.client.widgets.events.ClickHandler() {
                          
                                      public void onClick( com.smartgwt.client.widgets.events.ClickEvent event ) {
                                          ListGridRecord rec = new ListGridRecord();
                                          rec.setAttribute( "hostName", "newExample" );
                                          grid.addData( rec );
                                      }
                          
                                  } );
                                  IButton updateButton = new IButton( "Update" );
                                  updateButton.addClickHandler( new com.smartgwt.client.widgets.events.ClickHandler() {
                          
                                      public void onClick( com.smartgwt.client.widgets.events.ClickEvent event ) {
                                          ListGridRecord rec = grid.getRecord( 0 );
                                          if ( null != rec ) {
                                              rec.setAttribute( "hostName", rec.getAttribute( "hostName" ) + "2" );
                                              grid.updateData( rec );
                                          } else {
                                              SC.say( "No record to update." );
                                          }
                                      }
                          
                                  } );
                                  IButton removeButton = new IButton( "Remove" );
                                  removeButton.addClickHandler( new com.smartgwt.client.widgets.events.ClickHandler() {
                          
                                      public void onClick( com.smartgwt.client.widgets.events.ClickEvent event ) {
                                          ListGridRecord rec = grid.getRecord( 0 );
                                          if ( null != rec ) {
                                              grid.removeData( rec );
                                          } else {
                                              SC.say( "No record to delete." );
                                          }
                                      }
                          
                                  } );
                                  addMember( grid );
                                  addMember( fetchButton );
                                  addMember( addButton );
                                  addMember( updateButton );
                                  addMember( removeButton );
                          MyDataSource:

                          Code:
                          class MyDataSource extends RestDataSource {
                          
                              public MyDataSource( String id ) {
                                  setID( id );
                          
                                  setDataURL( "test.php" );
                                  setAddDataURL( "test.php" );
                                  setFetchDataURL( "test.php" );
                                  setRemoveDataURL( "test.php" );
                                  setUpdateDataURL( "test.php" );
                                  setClientOnly( false );
                                  setDataFormat( DSDataFormat.JSON );
                                  // setDataTransport( RPCTransport.SCRIPTINCLUDE );
                                  // setRecordXPath( "/item" );
                          
                                  setOperationBindings();
                                  OperationBinding fetch = new OperationBinding();
                                  fetch.setOperationType( DSOperationType.FETCH );
                                  fetch.setDataProtocol( DSProtocol.POSTPARAMS );
                                  fetch.setDataFormat( DSDataFormat.JSON );
                                  OperationBinding add = new OperationBinding();
                                  add.setOperationType( DSOperationType.ADD );
                                  add.setDataProtocol( DSProtocol.POSTPARAMS );
                                  add.setDataFormat( DSDataFormat.JSON );
                                  OperationBinding update = new OperationBinding();
                                  update.setOperationType( DSOperationType.UPDATE );
                                  update.setDataProtocol( DSProtocol.POSTPARAMS );
                                  update.setDataFormat( DSDataFormat.JSON );
                                  OperationBinding remove = new OperationBinding();
                                  remove.setOperationType( DSOperationType.REMOVE );
                                  remove.setDataProtocol( DSProtocol.POSTPARAMS );
                                  remove.setDataFormat( DSDataFormat.JSON );
                          
                                  setOperationBindings( fetch, add, update, remove );
                              }
                          
                          }
                          test.php:

                          Code:
                          <?php
                          
                          class Test {
                          	
                          	var $logFile;
                          	
                          	function Test() {
                          		$this->logFile = fopen( "log.html", "a+" );
                          	}
                          	
                          	function _debug( $str ) {
                          		echo $str;
                          		fputs( $this->logFile, $str );
                          	}
                          	
                          	function _debugln( $str="" ) {
                          		$this->_debug( $str . "<br/>\n" );
                          	}
                          	
                          	function _debugA( $array=null, $name="" ) {
                          		$prefix = "Array <b>" . $name . "</b>";
                          		if ( null == $array ) {
                          			$this->_debugln( $prefix . " is null." );
                          			return;
                          		}
                          		if ( ! is_array( $array ) ) {
                          			$this->_debugln( $prefix . " is not an array." );
                          			return;
                          		}
                          		$len = sizeof( $array );
                          		if ( 0 == $len ) {
                          			$this->_debugln( $prefix . " length is 0." );
                          			return;
                          		}
                          		$this->_debug( $prefix . " (" . $len . "): [ " );
                          		foreach ($array as $key => $value ) {
                          			$this->_debug( $key . "=" . $value . ", " );
                          		}
                          		$this->_debugln( " ]" );
                          	}
                          	
                          	function run() {
                          		$this->_debugln( date( "Y-m-d H:i:s" ) . " Test started." );
                          		
                          		$this->_debugA( $_POST, "POST" );
                          		$this->_debugA( $_GET, "GET" );
                          		
                          		$this->_debugln( "<hr/>" );
                          		
                          		fflush( $this->logFile );
                          		fclose( $this->logFile );
                          	}
                          	
                          }
                          
                          $t = new Test();
                          $t->run();
                          ?>

                          Comment


                            #14
                            finally my application works very nice now. just one other question.

                            how did you solve the security problem with php as backend?
                            did you use sessions or something like that to verify that not everyone can use the scripts?
                            or is there an other easy way to allow access only for the smartgwt-application?
                            Last edited by xelibrety; 15 Mar 2009, 13:59.

                            Comment


                              #15
                              Originally posted by xelibrety
                              finally my application works very nice now. just one other question.

                              how did you solve the security problem with php as backend?
                              did you use sessions or something like that to verify that not everyone can use the scripts?
                              or is there an other easy way to allow access only for the smartgwt-application?
                              I believe all backend face th same problem, not just PHP.
                              For me, since my PHP is used to access MySQL, so I use MySQL's
                              user to protect the PHP code.


                              KC

                              Comment

                              Working...
                              X