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) »
  • API helper function proposal _civicrm_validate_param
Pages: [1]

Author Topic: API helper function proposal _civicrm_validate_param  (Read 1443 times)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
API helper function proposal _civicrm_validate_param
September 26, 2010, 02:37:35 am
Hi,

I'm code QAing with eileen on the pledge api.

Most of the api is boilerplate code that verify the param attributes (if param[id] is set, if it's an int...). what about having an helper taking a json (or php object, don't care).

eg for civicrm_crashtest_dummy, that has int contact_id and string secondparam as mandatory param, and optionally int rowCount (if not defined, 25 rows)

Code: [Select]
function civicrm_crashtest_dummy ($param) {

$statusParam=_civicrm_validate_param ("{
mandatory: {'contact_id','secondparam'...}
integer: {'contact_id','rowCount'},
string: {'secondparam"},
default:{'rowcount': 25}
}");
if ( civicrm_error( $statusParam ) ) {
        return $statusParam;
    }


If would check that the mandatory parameters are there, have the right format, and handle the default values

Added benefit, we get for free a "explain/discovery" mode, that can returns that json and make it easier to know what's to be expected as a param.


What do you think?
X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

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: API helper function proposal _civicrm_validate_param
September 26, 2010, 07:44:30 am
I am not sure Xavier....would I not use it in the same way as I now look at the documentation? In other words, would not updating and extending the documentation achieve the same goal? Or am I overseeing something?
Erik
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: API helper function proposal _civicrm_validate_param
September 26, 2010, 08:55:28 am
1) might generate the doc automatically from that

2) I was thinking of an extra param (eg like json=1 can always be added): explain=1, that whould return that

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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API helper function proposal _civicrm_validate_param
September 26, 2010, 08:59:56 am
Sorry I'm assuming you meant the explain thing ?

Obviously, the main goal is to get rid of the boilerplate code in the api (and to do something nice in Briton)

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

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: API helper function proposal _civicrm_validate_param
September 26, 2010, 09:10:35 am
It is always nice to do nice things in Briton :-)
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: API helper function proposal _civicrm_validate_param
September 26, 2010, 02:55:26 pm
Hi Xavier,

I believe the code is more likely to be kept up-to-date than the documentation - especially if it affects functionality. What happens is that often we spend an afternoon trying to figure out why something doesn't work like it should, digging through the code. Then we finally get it, pass in the 'right' params & go home & maybe mention it on the forum. Whereas, if the code were 'in our way' we'd probably submit a patch.

However, there are 2 functions that can do some of our work for us:

$fields = CRM_Pledge_DAO_Pledge::fields( );
$customFields = CRM_Core_BAO_CustomField::getFields($customDataType);

Both of these will return details about the fields. The DAO function returns the unique name of the field not the table name & there is some question as to whether the unique name is acceptable

The fields function returns required fields too - although in the case of pledges I added a couple more to the list that I considered required:
 'start_date' because I needed at least one of start_date, create_date or scheduled_date for the others to default to - it would accept without but the entry created was unacceptable.
'installments' -not a db field at all but required for it to work. I  was too lazy to write the code that says 'installments, amount, installment_amount - 2 out of 3 ain't bad.
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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API helper function proposal _civicrm_validate_param
September 26, 2010, 11:49:28 pm
Hi,

Just to clarify: the main goal is to make writing an api easier. Right now, about half the code is validating the param. _civicrm_validate_param is meant to solve that and reduce the pain to write an api.

The side effect of being able to offer a discover/explain mode is just that, a side effect

As a validation tool, do you think it's complete enough ?

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: API helper function proposal _civicrm_validate_param
September 26, 2010, 11:53:43 pm
I'm seeing them interacting with your function idea to form part of this array:

$statusParam=_civicrm_validate_param ("{
mandatory: {'contact_id','secondparam'...}
integer: {'contact_id','rowCount'},
string: {'secondparam"},
default:{'rowcount': 25}
}");
if ( civicrm_error( $statusParam ) ) {
        return $statusParam;
    }


So that we don't hand maintain an array like that for each API - a function would generate an array like that from the custom_fields & dao fields + another function that is generic to all the apis (with rowcount etc). But we'd still have to hand-add a small number like 'soft_credit_to' for contributions I think
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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API helper function proposal _civicrm_validate_param
October 04, 2010, 11:57:03 pm
Hi,

We implemented a simplier version at the sprint: it only test for mandatory params (either as defined from the dao or a list of fields).

We have an option to test that all of the params are mandatory or at least one.

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

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: API helper function proposal _civicrm_validate_param
October 05, 2010, 01:59:51 am
The function is civicrm_verify_mandatory, resides in api/v2/utils.php and it checks if the params is an array, and if the passed mandatory fields are in the params. A call is included in the Tag API:
Code: [Select]
function civicrm_tag_create( &$params )
{
  _civicrm_initialize( true );
  try {
   
    civicrm_verify_mandatory ($params,'tag',array ('name'));

    if ( !array_key_exists ('used_for', $params)) {
      $params ['used_for'] = "civicrm_contact";
    }
   
    require_once 'CRM/Core/BAO/Tag.php';
    $ids = array( 'tag' => CRM_Utils_Array::value( 'tag', $params ) );
    if ( CRM_Utils_Array::value( 'tag', $params ) ) {
        $ids['tag'] = $params['tag'];
    }

    $tagBAO = CRM_Core_BAO_Tag::add($params, $ids);

    if ( is_a( $tagBAO, 'CRM_Core_Error' ) ) {
        return civicrm_create_error( "Tag is not created" );
    } else {
        $values = array( );
        _civicrm_object_to_array($tagBAO, $values);
        $tag = array( );
        $tag['tag_id']   = $values['id'];
        $tag['is_error'] = 0;
    }
    return $tag;
  } catch (PEAR_Exception $e) {
    return civicrm_create_error( $e->getMessage() );
  } catch (Exception $e) {
    return civicrm_create_error( $e->getMessage() );
  }
}

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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • API helper function proposal _civicrm_validate_param

This forum was archived on 2017-11-26.