CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Changing the XML output in REST.php
Pages: [1] 2

Author Topic: Changing the XML output in REST.php  (Read 1790 times)

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Changing the XML output in REST.php
January 12, 2011, 10:14:40 am
At the moment we have a problem in the REST.php for XML, because it only caters for 2 levels in result arrays. The formatting of the XML in the REST is now done with the function CRM_Utils_Array::xml. This formats text in a string like this:
Code: [Select]
    static function &xml( &$list, $depth = 1, $seperator = "\n" ) {
        $xml = '';
        foreach( $list as $name => $value ) {
            $xml .= str_repeat( ' ', $depth * 4 );
            if ( is_array( $value ) ) {
                $xml .= "<{$name}>{$seperator}";
                $xml .= self::xml( $value, $depth + 1, $seperator );
                $xml .= str_repeat( ' ', $depth * 4 );
                $xml .= "</{$name}>{$seperator}";
            } else {
                // make sure we escape value
                $value = self::escapeXML( $value );
                $xml .= "<{$name}>$value</{$name}>{$seperator}";
            }
        }
        return $xml;
    }

In the REST I want to change this and use the PHP XMLWriter to do the XML formatting, so it will become something like:
Code: [Select]
        $xml = new XMLWriter();
        $xml->openURI('php://output');
        $xml->startDocument('1.0');
        $xml->setIndent(2);
        $xml->startElement("Resultset");
        $xml->writeAttribute("is_error", 0);
        $xml->writeAttribute("count", 1);
        $xml->startElement("Values");
        $xml->writeElement("id", 1);
        $xml->writeElement("last_name", "Hommel");
        $xml->writeElement("first_name", "Erik");
        $xml->endElement();
        $xml->endElement();
        $xml->endDocument();
        $xml->flush();
        return $xml;
The result of this will be:
− <Resultset is_error="0" count="1">
− <Values>
<id>1</id>
<last_name>Hommel</last_name>
<first_name>Erik</first_name>
</Values>
</Resultset>

I have no in depth knowledge of XML or the REST, but it seems to make sense to use the XMLWriter functionality so we have proper formatted XML? Any other opinions or suggestions? Should we use the same formatting for the CRM_Utils_Array::xml function?

Erik
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Changing the XML output in REST.php
January 12, 2011, 10:50:23 am
Will the 'values' array no longer be indexed by the entity_id? I think we talked about making that optional by passing in a param but it might be a bit much to change it completely

Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Changing the XML output in REST.php
January 12, 2011, 10:53:00 am
I think it still will, I was just putting down the general concept. I will come back with an actual example once I have got a working version
Erik
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Changing the XML output in REST.php
January 12, 2011, 10:56:39 am
Cool - would be nice to build in the 'sequential' or better named param while you are at it e.g


   
Code: [Select]
    $pledge = array( );
    while ( $dao->fetch( ) ) {
      if ($params['sequential']){
             $pledge[] = $query->store( $dao );   
      }else{
        $pledge[$dao->pledge_id] = $query->store( $dao );
      }
     }
    $dao->free( );

This is one X & I talked about to avoid having to use a foreach loop if you only expect one result
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Changing the XML output in REST.php
January 12, 2011, 11:07:48 am
I am not sure I get that.....and positive I do not get it  ;D
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Changing the XML output in REST.php
January 12, 2011, 11:14:51 am
So we talked about if you pass in $params=array('name'='Erik')

you get

$result = array($values = array(
                                     23 =array(name = Erik, country = Netherlands,id=23)
                                     257=array(name = Erik, country = Maldives,id=257)


whereas if you passed in

 $params=array('name'='Erik', 'sequential'=1)

you'd get

$result = array($values = array(
                                     0=array(name = Erik, country = Netherlands,id=23)
                                     1=array(name = Erik, country = Maldives,id=257)

Which means if you only expect one or you don't care about the rest you can go

$result['values'][0] to get your result
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Changing the XML output in REST.php
January 13, 2011, 09:03:21 am
Just did a check, but the function CRM_Utils_Array::xml is only used in the REST.php. That being the case, I suggest we remove the function from the CRM_Util_Array and create a new function in the api/v3/utils. Agree or object anyone?
Erik
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Changing the XML output in REST.php
January 13, 2011, 09:29:03 am

any specific reason why u dont want to upgrade the function in CRM/Utils/Array.php, so future xml generation for other purposes can just call that function?

I think the functions in the api utils.php should be focussed on data validation / formatting etc for the BAO functions. generic functions should go under CRM/*

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Changing the XML output in REST.php
January 13, 2011, 09:32:41 am
The only reason I had in mind is because it is not used anywhere else. Do not have a problem with the function being in the CRM/*. Do you agree we should then change the function to use XMLWriter?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Changing the XML output in REST.php
January 16, 2011, 01:18:01 pm
I'd suggest to put the sequential mode by default and add a "nested", cause:

1) as it moves under results, it won't be compatible with v2 anyway
2) that's really a pain when using the {crmAPI} to have to deal with the id as key
3) that's really a pain when using the json to have to deal with the id as key
4) the json is nicer to read


X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Changing the XML output in REST.php
January 16, 2011, 01:33:13 pm
I think that nested should remain the default for php calls & for REST sequential should be set as the default.

But if they have their API verson set to v2 then the default should be unchanged for REST
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Changing the XML output in REST.php
January 16, 2011, 11:23:39 pm
Is it best to do the 'flattening' as suggested in each API or should we take care of it in api.php as the versioning stuff is in there anyway? Or is civicrm_create_success a better place?
Erik
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Changing the XML output in REST.php
January 16, 2011, 11:28:29 pm
civicrm_create_success is better- this is the same topic as Xavier's other post - but I haven't looked at the code to see how easily it would fit
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Changing the XML output in REST.php
January 16, 2011, 11:46:51 pm
i'll have a go on Wednesday.... :D
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Changing the XML output in REST.php
January 17, 2011, 01:07:51 am
Beware that some apis already provide a flattened array (eg. civicrm_tag_get)

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Changing the XML output in REST.php

This forum was archived on 2017-11-26.