XML to JSON in PHP

Zimuel writes; Last friday, in occasion of the April Zend Framework Bug-Hunt, I started to look at this bug: ZF-3257. This is an issue related to the Zend_Json class that occurs during the conversion from XML to JSON for some specific XML documents, like this one:

$xml= 'bar';

The result using Zend_Json::fromXml($xml, false) , where false indicated the usage of XML attributes, was:

{"a":{"b":{"@attributes":{"id":"foo"}}}}

As you can see the bar value, of the a element, is not represented in JSON. This issue comes also with other XML documents, and in general when an XML node has a single character data child, any attributes are lost.

For instance, the following code:

$xml = 'bar';
echo Zend_Json::fromXml($xml, false);

Produced the output:

{"a":{"b":"bar"}}

in this case the attribute id and the value foo are lost.
Find a solution @ Zimuel’s blog.

ExtJs, ExtDesigner and Zend Framework

Nils-Fredrik G. Kaland writes; Let’s say you are working on the user interface in Ext Designer / Sencha Ext Js, and after a while you find out you have ended up with a great amount of data stores. You also need to handle lots of Ajax requests and all the server side coding this invokes. But, why not think two steps forward already when working with the interface in Ext Designer? Of course, you are an experienced developer – so you have your database model ready. More on that later. Let’s start with the user interface in Ext Designer.

via ExtJs, ExtDesigner and Zend Framework

DataTables – table plug-in for jQuery

The other day I had to write a  NOC application that shows all active phone lines in an out of a customers telecom cluster, thats a lot of constantly changing data that needed to be displayed.

Some design requirements was;

  • *NO* refreshes i.e. blinking screen parts (gives NOC personel an epileptic fit after a day staring at it).
  • No direct database access – read only Json proxy.
  • Near realtime – 1 second updates.
  • Group lines on customer. Toggle on and off.
  • Sortable on all visible columns.
  • Searchable and one click filters.
  • Has to work in an Dojo ContentPane.
  • 100% translatable

To the rescue; DataTables is a plug-in for the jQuery Javascript library.  It is a highly flexible tool, based upon the foundations of progressive enhancement, which will add advanced interaction controls to any HTML table.

I can’t recommend it highly enough, configuration is as easy or as hard as you require it to be, draw up a file with a regular table,thead,tbody,tfoot table put it’s id to be example and place this code snippet into the file;
/*
* Example init
*/
$(document).ready(function(){
$('#example').dataTable();
});

Then popp over to Allans excellent Examples page! and if you dont have a sortable, searchable and great looking grid in about 5 minutes, I owe you a beer!
Key features:

  • Variable length pagination
  • On-the-fly filtering
  • Multi-column sorting with data type detection
  • Smart handling of column widths
  • Display data from almost any data source
  • DOM, Javascript array, Ajax file and server-side processing (PHP, C#, Perl, Ruby, AIR, Gears etc)
  • Fully internationalisable
  • jQuery UI ThemeRoller support
  • Rock solid – backed by a suite of 1300+ unit tests
  • Wide variety of plug-ins inc. TableTools, FixedHeader and KeyTable
  • It’s free!
  • State saving
  • Hidden columns
  • Dynamic creation of tables
  • Ajax auto loading of data
  • Custom DOM positioning
  • Single column filtering
  • Alternative pagination types
  • Non-destructive DOM interaction
  • Sorting column(s) highlighting
  • Extensive plug-in support
  • – Sorting, type detection, API functions, pagination and filtering
  • Fully themeable by CSS
  • Solid documentation
  • Full support for Adobe AIR

Go NOW and take a peek DataTables (table plug-in for jQuery).

App_Controller_Helper_Params for JSON and XML POSTs

Matthew Weier O’Phinney shares a bit of very useful code to inject request params into a Zend Framework request object from a JSON or XML POST request.

“Below is a plugin I use to translate JSON or XML raw post request data to request user parameters.
Note that it expects a “Content-Type” header of either “application/json” or “application/xml”. If those are detected, it then does the translation and injection.
Once it has, you can then simply access the parameters from your request object like any others.
I actually use this with dojox.data.JsonRestStore already, so you should be set. :)”

class App_Controller_Helper_Params extends Zend_Controller_Action_Helper_Abstract { 
/**
 * @var array Parameters detected in raw content body
 */
 protected $_bodyParams = array(); 
/**
 * Do detection of content type, and retrieve parameters from raw body if
 * present
 *
 * @return void
 */
 public function init() {
 $request = $this->getRequest();
 $contentType = $request->getHeader('Content-Type');
 $rawBody = $request->getRawBody();
 if (!$rawBody) {
   return;
 }
switch (true) {
 case (strstr($contentType, 'application/json')):
   $this->setBodyParams(Zend_Json::decode($rawBody));
   break;
 case (strstr($contentType, 'application/xml')):
   $config = new Zend_Config_Xml($rawBody);
   $this->setBodyParams($config->toArray());
   break;
 default:
   if ($request->isPut()) {
    parse_str($rawBody, $params);
   $this->setBodyParams($params);
 }
 break;
 }
}

/**
* Set body params
*
* @param array $params
* @return Scrummer_Controller_Action
*/
public function setBodyParams(array $params)
{
  $this->_bodyParams = $params;
  return $this;
}

/**
* Retrieve body parameters
*
* @return array
*/
public function getBodyParams()
{
  return $this->_bodyParams;
}

/**
* Get body parameter
*
* @param string $name
* @return mixed
*/
public function getBodyParam($name)
{
  if ($this->hasBodyParam($name)) {
    return $this->_bodyParams[$name];
  }
  return null;
}

/**
* Is the given body parameter set?
*
* @param string $name
* @return bool
*/
public function hasBodyParam($name)
{
  if (isset($this->_bodyParams[$name])) {
    return true;
  }
  return false;
}

/**
 * Do we have any body parameters?
 *
 * @return bool
 */
 public function hasBodyParams()
 {
   if (!empty($this->_bodyParams)) {
     return true;
   }
   return false;
 }
 
 /**
 * Get submit parameters
 *
 * @return array
 */
 public function getSubmitParams()
 {
   if ($this->hasBodyParams()) {
     return $this->getBodyParams();
    }
   return $this->getRequest()->getPost();
 } 

 public function direct()
 {
   return $this->getSubmitParams();
 }
}