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) »
  • Question about using User Framework (UF) functions via REST interface, and more!
Pages: [1]

Author Topic: Question about using User Framework (UF) functions via REST interface, and more!  (Read 1563 times)

ryanrd

  • I’m new here
  • *
  • Posts: 6
  • Karma: 1
Question about using User Framework (UF) functions via REST interface, and more!
December 31, 2010, 07:10:46 pm
Hello, I'm new to working with the civicrm API and had a few questions, I am afraid I'm missing something small and am going to end up reinventing the wheel :)

I want to use civicrm_uf_id_get with the REST interface, the function exists in the api/v2/UFGroup.php file.
When I do rest.php?q=civicrm/uf_group/id_get.....  the REST.php concats the path and checks to see if the function civicrm_uf_group_id_get exists in UFGroup.php and gives an error since it doesn't (I want it to look for civicrm_uf_id_get)

So I read the documents on your API conventions and this function also happens to be one of the functions which doesn't conform to the $params behavior (its expecting a contactId).  Whats the best way for me to use this function over REST in my own project without causing future conflicts as you make changes to the API?  

My first blush idea / attempt is to wrapper the built in api function while still conforming to the REST function naming / params convention - so I'm doing something like this in api/v2/UFMisc.php

Code: [Select]
<?php
   
    
include_once('UFGroup.php');

    function 
civicrm_uf_misc_id_get($params) {
        
        if(!
is_array($params)) 
          
$params = array('contact_id' => $params);
      
        
$contactID = isset($params['contact_id'])?$params['contact_id']:NULL;
    
        if(
is_null($contactID)) {
          return 
civicrm_create_error('Function takes param of array("contact_id" => N)');
        }
    
        return 
civicrm_uf_id_get($contactID);
    }
    

Is there any easier way then this?  as far as I can tell I can only use civicrm_uf_group_create and the civicrm_uf_join_* functions via REST

http://wiki.civicrm.org/confluence/display/CRMDOC33/Profile+APIs




I was also wondering if there any method of doing campaign contribution page creation, or pcp creation via the API as well as soft contributions.   If not, whats the best methodology for writing the api so I can contribute it back to the core project?  

[Edited to fix wording, on my previous civi project we referred to each fundraising page as a campaign (instead of a contribution page), pcp's being personal campaign page confused it more]
« Last Edit: January 04, 2011, 09:43:33 pm by ryanrd »

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: Question about using User Framework (UF) functions via REST interface, and more!
December 31, 2010, 08:22:26 pm

i suspect the api team will respond to your message soon :)

In the meantime:

I think the best way is to rename all the functions to be valid and conform to the api standards. We could keep the existing functions as is for one more release and let folks know that they are deprecated. That might be a good patch worth submitting, IMO :)

I dont think we have campaign/pcp/soft contrib api's as yet. If you can conform to the standards (as they exist and u interpret them) and submit them as patches that would be a great first start. Also please include unit tests. Most of the API team (erik, eileen, xavier, coleman) should be back next week, so pinging and chatting with them on irc should help

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

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Question about using User Framework (UF) functions via REST interface, and more!
January 01, 2011, 04:05:13 pm
My preferred option is to add the uf_id into the contact_get as a return option (ie. you could put in $params['return.uf_id'] in your parameters. Xavier is a bit more of a BAO purist & might disagree but it's an option I've often wanted in the contact_get.



Re soft contributions - you can certainly create soft contributions by using the contribution add API in v2

$params['soft_credit_to']

You may also be able to add them as a return value

$params['soft_credit_to']
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: Question about using User Framework (UF) functions via REST interface, and more!
January 03, 2011, 06:44:40 am
Quote from: ryanrd on December 31, 2010, 07:10:46 pm

I want to use civicrm_uf_id_get with the REST interface, the function exists in the api/v2/UFGroup.php file.
When I do rest.php?q=civicrm/uf_group/id_get.....  the REST.php concats the path and checks to see if the function civicrm_uf_group_id_get exists in UFGroup.php and gives an error since it doesn't (I want it to look for civicrm_uf_id_get)

So I read the documents on your API conventions and this function also happens to be one of the functions which doesn't conform to the $params behavior (its expecting a contactId).  Whats the best way for me to use this function over REST in my own project without causing future conflicts as you make changes to the API? 

Agree with you, that's a mistake in the code, not conform with the naming convention. Could you fix and submit a patch ?

(the UF is a bit of a grey area for me, I always mix up the UF as stuff that links civi contact and CMS user and the custom sets).

Anyway, the variable has to be param (an array) indeed, and the name of the function civicrm_entity_action within v2/Entity.php


Quote from: ryanrd on December 31, 2010, 07:10:46 pm

Code: [Select]
       
        if(!is_array($params))
          $params = array('contact_id' => $params);
     
        $contactID = isset($params['contact_id'])?$params['contact_id']:NULL;
   
        if(is_null($contactID)) {
          return civicrm_create_error('Function takes param of array("contact_id" => N)');
        }
   
   

There is a function helper to deal with verifying the param: civicrm_verify_mandatory (in api/v2/utils.php). Check the code for instance in Tag.php for an example of usage.



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

ryanrd

  • I’m new here
  • *
  • Posts: 6
  • Karma: 1
Re: Question about using User Framework (UF) functions via REST interface, and more!
January 03, 2011, 11:15:58 am
Quote from: Eileen on January 01, 2011, 04:05:13 pm
My preferred option is to add the uf_id into the contact_get as a return option (ie. you could put in $params['return.uf_id'] in your parameters. Xavier is a bit more of a BAO purist & might disagree but it's an option I've often wanted in the contact_get.

Having that as a feature would be a handy helper so things didn't have to be done in two steps.   Since for my particular project I think I'll also need to use civicrm_uf_match_id_get is there another function that I can get that from?


Quote from: xavier on January 03, 2011, 06:44:40 am

Agree with you, that's a mistake in the code, not conform with the naming convention. Could you fix and submit a patch ?

(the UF is a bit of a grey area for me, I always mix up the UF as stuff that links civi contact and CMS user and the custom sets).

Anyway, the variable has to be param (an array) indeed, and the name of the function civicrm_entity_action within v2/Entity.php

...

There is a function helper to deal with verifying the param: civicrm_verify_mandatory (in api/v2/utils.php). Check the code for instance in Tag.php for an example of usage.


Thanks for the tip on the civicrm_verify_mandatory, the reason why I put the check for is_array (and shoved a non array into an fake params array) is that the current definition for the functions paramaters listed a contact_id, so I when I first was writing it I was writing it in place in UFGroup (to replace the existing civcrm_uf_id_get) and wanted to preserve legacy behavior incase some other part of the code used that function.  


So as for resolving the naming / params whats the most recommended method for doing this:

Should I create a UF.php, put civicrm_uf_id_get and civicrm_uf_match_id and move the (corrected param versions) of these functions out of UFGroup.php (I don't see them being used by anything in UFGroup.php)?  

Or

Should I fix them in UFGroup.php, and just make UF.php do an include('UFGroup.php') to satisfy the REST interfaces function lookup - if doing this I could also add a UFProfile.php and UFField.php to move towards making those usable.

Or

???




Quote from: Donald Lobo on December 31, 2010, 08:22:26 pm
I dont think we have campaign/pcp/soft contrib api's as yet. If you can conform to the standards (as they exist and u interpret them) and submit them as patches that would be a great first start. Also please include unit tests. Most of the API team (erik, eileen, xavier, coleman) should be back next week, so pinging and chatting with them on irc should help

And the standards are these - http://wiki.civicrm.org/confluence/display/CRM/API+Conventions ?

I read somewhere (probably the forums) about a new version of the API's coming, are they going to be essentially the same just with changes to the functions which don't use a $params or are they a large departure from the api/v2 structure?   I'm still evaluating for this project if we are going to do everything through the API (so we can use it in more situations / configurations ) or if I'll be reusing some previous code that just used the internal functions directly for creating campaigns and pcps, so I'd like to know if I'm going to have to rework a lot of this down the road a little bit.
« Last Edit: January 03, 2011, 12:42:58 pm by ryanrd »

ryanrd

  • I’m new here
  • *
  • Posts: 6
  • Karma: 1
Re: Question about using User Framework (UF) functions via REST interface, and more!
January 03, 2011, 08:31:22 pm
Quote from: Eileen on January 01, 2011, 04:05:13 pm

Re soft contributions - you can certainly create soft contributions by using the contribution add API in v2

$params['soft_credit_to']

You may also be able to add them as a return value

$params['soft_credit_to']

is there a trick to search for soft contributions using /contribution/get ?

I'm also wondering if theres a page which lists desired coding style, I'm trying to mimic what I see as far as whitespace usage, but if theres more than that any tips would be greatly appreciated.

ryanrd

  • I’m new here
  • *
  • Posts: 6
  • Karma: 1
Re: Question about using User Framework (UF) functions via REST interface, and more!
January 03, 2011, 11:01:58 pm
I upgraded my install to 3.3.1 so that I could make sure I was current and have found what I think is a bug

in civicrm/CRM/Utils/REST.php line 168-170   In some situations the array thats being foreach'd contains hash => string entries instead of hash => array entries


Code: [Select]
foreach ( $result as $n => $v ) {
               
$xml .= "<Result>\n" . CRM_Utils_Array::xml( $v ) . "</Result>\n";
           
}

$result =

Array
(
    [is_error] => 1
    [error_message] => 4 contributions matching input params
    [error_data] => Array
        (
            [2] => Array
                (

....

so the loops being called like:   

CRM_Utils_Array::xml( 1 );   
CRM_Utils_Array::xml( '4 contributions matching input params' );
CRM_Utils_Array::xml( Array( [2] => Array ( ..... );


Heres a sample of the output:


(this is a query ran without returnFirst set, so I'm expecting it to deliver the list in Result->error_data)


Code: [Select]
<?xml version="1.0"?>

<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Result>
</Result>
<Result>
</Result>
<Result>
    <2>
        <contact_id>2</contact_id>
        <contact_type>Individual</contact_type>
...

Heres what I expect to get ( basically how the else functions - $xml .= "<Result>\n" . CRM_Utils_Array::xml( $result ) . "</Result>\n"; )

Code: [Select]
<?xml version="1.0"?>
<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Result>
    <is_error>1</is_error>
    <error_message>4 contributions matching input params</error_message>
    <error_data>
        <2>
            <contact_id>2</contact_id>
            <contact_type>Individual</contact_type>

I'm getting this behavior when trying to do:
    /sites/all/modules/civicrm/extern/rest.php?q=civicrm/contribution/get&contact_id=2&key=YOUR_KEY&api_key=USER_API_KEY

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: Question about using User Framework (UF) functions via REST interface, and more!
January 04, 2011, 07:39:32 am

Seems like the code does not detect that it is an error message and hence does the "wrong" xml conversion. I suspect we need to flatten the error message and return the error data

wanna submit a patch for this?

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

ryanrd

  • I’m new here
  • *
  • Posts: 6
  • Karma: 1
Re: Question about using User Framework (UF) functions via REST interface, and more!
January 04, 2011, 11:14:06 am
Yeah, I'll submit a patch.  Whats a good test case for one that should return this first case so that I can be sure not to break anything?

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Question about using User Framework (UF) functions via REST interface, and more!
January 04, 2011, 01:20:59 pm
Ryan,

In answer to your question about v3 of the API - v3 is primary about moving towards the standards that have been our goal for some time without having to handle backwards compatibility. The instance you have hit is a good example - to patch it within the v2 api folder it would need to handle both an array (correct) and the existing variable (incorrect) which make it tedious & ugly so we will ship v3 in a new folder (can you guess what it's called?).

There is an SVN trunk set up for v3 - not quite sure our timeframes yet ... and we are actively recruiting to the API team.

Re the file name - I think the reason Xavier & I are confused about that file is it is poorly named (actually I find the use of the term 'userframework' in Civi to refer to anything other than the UF Match table confusing!). So, I vote you move the function into v3/UF.php
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

ryanrd

  • I’m new here
  • *
  • Posts: 6
  • Karma: 1
Re: Question about using User Framework (UF) functions via REST interface, and more!
January 04, 2011, 01:58:31 pm
Quote from: Eileen on January 04, 2011, 01:20:59 pm
Ryan,

In answer to your question about v3 of the API - v3 is primary about moving towards the standards that have been our goal for some time without having to handle backwards compatibility. The instance you have hit is a good example - to patch it within the v2 api folder it would need to handle both an array (correct) and the existing variable (incorrect) which make it tedious & ugly so we will ship v3 in a new folder (can you guess what it's called?).

There is an SVN trunk set up for v3 - not quite sure our timeframes yet ... and we are actively recruiting to the API team.

Re the file name - I think the reason Xavier & I are confused about that file is it is poorly named (actually I find the use of the term 'userframework' in Civi to refer to anything other than the UF Match table confusing!). So, I vote you move the function into v3/UF.php

Okay, good to know its essentially the same just not guaranteed backwards compatible, that should help me justify the time investment towards writing it.   I'll maintain my diffs and then submit a patch when completed.  I appreciate all the help and feedback, I'll ask less questions once I'm comfortable in understanding how to determine whats a certain way on purpose, and what just hasn't been reworked yet :)


Do you have any comment on the xml thing I mentioned a couple posts up?  Since the xml function is recursive it seems to handle hierarchical just fine so I'm not sure if I'm just looking at left over code or if I'm missing some bigger purpose.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Question about using User Framework (UF) functions via REST interface, and more!
January 04, 2011, 02:07:16 pm
The function you have there should be fixed.

Basically in v3 the search functions will all go but the GET functions will all return multiple.

Copy the function into your v3 folder. Rename the search function to GET & delete the GET function & see if it works
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

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Question about using User Framework (UF) functions via REST interface, and more!
January 04, 2011, 02:10:25 pm
BTW questions are fine - ask away!
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

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Question about using User Framework (UF) functions via REST interface, and more!
January 20, 2011, 12:51:53 am
The function is now v3 compliant here (although there are still some more desirable improvements to that class)

http://svn.civicrm.org/civicrm/branches/trunk.api/api/v3/UFMatch.php

& the example

http://svn.civicrm.org/civicrm/branches/trunk.api/api/v3/examples/UFMatchGet.php

X / Erik - might want to double check the naming is Ok w REST as the double caps in UF is a bit odd
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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Question about using User Framework (UF) functions via REST interface, and more!

This forum was archived on 2017-11-26.