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) »
  • Event Search max_results?
Pages: [1]

Author Topic: Event Search max_results?  (Read 1507 times)

pbarmak

  • I post occasionally
  • **
  • Posts: 111
  • Karma: 3
  • CiviCRM version: 3.3.5
  • CMS version: Pressflow 6.19
  • MySQL version: 5.1
  • PHP version: 5.2.10
Event Search max_results?
October 07, 2010, 12:58:59 pm
I must be doing something silly here, but I can't figure it out.

I ran this URL (via PHP code and just in a browser after logging in), but I'm getting more than 1 result back, even though I have "return.max_results=1" set.  What am I doing wrong?

http://localhost/sites/all/modules/civicrm/extern/rest.php?q=civicrm/event/search&key=<sitekey>&api_key=<apikey>&event_type_id=7&return.max_results=1&is_public=1&is_active=1

Also, is there a way I can sort (run an order by) in my call or must I sort after the results come back?  I'd like to have the query order prior to returning max_results (want the next X events by start date).  The code seems to set $sort to false.

Thanks.

pbarmak

  • I post occasionally
  • **
  • Posts: 111
  • Karma: 3
  • CiviCRM version: 3.3.5
  • CMS version: Pressflow 6.19
  • MySQL version: 5.1
  • PHP version: 5.2.10
Re: Event Search max_results?
October 07, 2010, 03:26:11 pm
Well, I don't know if it helps, but there seems to be an issue using a period for "return.max_results" - it seems like the period gets translated to an underscore somewhere.  So by the time api/v2/Event.php gets it, it's "return_max_results".  But the function in Event.php is looking for the period, not underscore.

What's going on here?  Something wrong in my setup or is there a bug somewhere?

Also, another problem: when I have it return custom fields, the key in the result has the ID of the custom field's table in it.  So I see things like:
<custom_11_749>1002</custom_11_749>
...
<custom_11_820>486</custom_11_820>

What is the value of having the row's ID as part of the key name?

Finally, is there a way I can create my own version of api/v2/Event.php in my custom php directory?  I tried putting a new one in there (like I do with customized templates and other php code), but it doesn't seem to recognize it in my custom folder.  I'd rather not change the original and have it accidentally overridden next time I upgrade.

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: Event Search max_results?
October 07, 2010, 11:25:52 pm
There is no real value in having the row's ID as part of the key name. We are in the process of establishing API conventions (http://wiki.civicrm.org/confluence/display/CRM/API+Conventions.
If you want to create your own API you have to rename it and place it in the api/v2 directory. I have for example my own version of the contact for a customer xxx so I have Xxxcontact.php in the API directory.
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: Event Search max_results?
October 08, 2010, 03:30:16 am
Hi,

Would be good that the api takes into account the custom PHP folder (if set). Didn't find the wiki page about the todo. Is there a specific 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: Event Search max_results?
October 08, 2010, 05:11:09 am
I agree, looking for an API in the customisation folder makes a lot of sense!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

pbarmak

  • I post occasionally
  • **
  • Posts: 111
  • Karma: 3
  • CiviCRM version: 3.3.5
  • CMS version: Pressflow 6.19
  • MySQL version: 5.1
  • PHP version: 5.2.10
Re: Event Search max_results?
October 08, 2010, 08:04:41 am
Erik,
For your own version of the api file, Xxxcontact.php, can you give me an example URL/REST call?  I tried to put a new file in that folder, but when I tried to make the call, I got an "Unknown function invocation" error.  I must be doing something wrong, would love an example.

For the desire to get the API to look at the custom folder, shall I open up a JIRA or are you guys handling that in the new API convention discussion?  I really don't like having to remember that my customized APIs are in the main folder.

Thanks so much for revamping the APIs, they are so helpful to us folks that need the integration.

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: Event Search max_results?
October 08, 2010, 08:33:18 am
Here is an example REST call :
Code: [Select]
https://<example.nl>/sites/all/modules/civicrm/extern/rest.php?q=civicrm/dgwcontact/get&contact_id=1234&key=<site_key>&PHPSESSID= <phpsessid>

Does that help or would you like the PHP code as well? And if you want to take part in the revamping you'll be very welcome  ;D
Erik
« Last Edit: October 08, 2010, 08:35:20 am by Erik Hommel »
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

pbarmak

  • I post occasionally
  • **
  • Posts: 111
  • Karma: 3
  • CiviCRM version: 3.3.5
  • CMS version: Pressflow 6.19
  • MySQL version: 5.1
  • PHP version: 5.2.10
Re: Event Search max_results?
October 08, 2010, 08:50:52 am
Hmm, maybe having the PHP code would be helpful (or my eyes are just getting old :).

I made a file called feievent.php and put it in api/v2.  I then changed the function name to be civicrm_feievent_search in that file.  And then I made the call with:
Code: [Select]
http://localhost/sites/all/modules/civicrm/extern/rest.php?q=civicrm/feievent/search&key=<key>&api_key=<key>

But I still get that error.  If, however, I modify the original Event.php and add a new function in there (civicrm_event_recentsearch), I can call that just fine.  So maybe my php is wrong somewhere.  Thanks, appreciate the help.

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: Event Search max_results?
October 08, 2010, 09:00:29 am
I can not see anything wrong with your call, but here is part of my function dgwcontact_get (some of it in Dutch  :)):
Code: [Select]
function civicrm_dgwcontact_get($inparms) {
    /*
     * initialize output array
     */
    $outparms = array("");


    /*
     * array to hold all possible input parms
     */
    $valid_input = array("contact_id", "persoonsnummer_first", "achternaam",
        "geboortedatum", "bsn", "contact_type");

    /*
     * check if input parms hold at least one valid parameter
     */
    $valid = false;
    foreach ($valid_input as $validparm) {
        if (isset($inparms[$validparm])) {
            $valid = true;
        }
    }
    if (!$valid) {
        return civicrm_create_error( 'Geen geldige input parameters voor
            dgwcontact_get' );
    }

    /*
     * only if valid parameters used
     */
    if ($valid) {

        /*
         * standard API returns default 25 rows. For DGW changed here: if no
         * rowCount passed, default = 100
         */
        if (isset($inparms['rowCount'])) {
            $rowCount =(int) $inparms['rowCount'];
        } else {
            $rowCount = 100;
        }

        /*
         * if contact_id entered, no further parms needed
         */
        if (isset($inparms['contact_id'])) {
            $civiparms1 = array(
                "contact_id" => $inparms['contact_id'],
                "rowCount"   => $rowCount);
            $civires1 = &civicrm_contact_get($civiparms1);
            /*
            $outparms[0]['contact_id'] = $civires1[1]['display_name'];
            return $outparms; */
        } else {


            /*
             * if persoonsnummer_first entered, no further parms needed
             */
            if (isset($inparms['persoonsnummer_first'])) {
                $civiparms1 = array(
                    "contact_type"  =>  "Individual",
                    DGWPERSFIRST    =>  $inparms['persoonsnummer_first']);
                $civires1 = &civicrm_contact_get($civiparms1);
            } else {
                /*
                 * use bsn if entered
                 */
                if (isset($inparms['bsn'])) {
                    if (!empty($inparms['bsn'])) {
                        $civiparms1[DGWPERSBSN] = $inparms['bsn'];
                    }
                }
                /*
                 * use achternaam if entered
                 */
                if (isset($inparms['achternaam'])) {
                    if (!empty($inparms['achternaam'])) {
                        $civiparms1['last_name'] = trim($inparms['achternaam']);
                    }
                }
                /*
                 * use geboortedatum if entered
                 */
                if (isset($inparms['geboortedatum'])) {
                    $valid_date = check_date_valid($inparms['geboortedatum']);
                    if (!$valid_date) {
                        return civicrm_create_error("Onjuist datumformaat
                                voor geboortedatum");
                    }
                $civiparms1['birth_date'] = date("Ymd",
                            strtotime($inparms['geboortedatum']));
                }
                /*
                 * use contact type if entered
                 */
                if (isset($inparms['contact_type'])) {
                    if (!empty($inparms['contact_type'])) {
                        $civiparms1['contact_type'] = $inparms['contact_type'];
                    }
                }
                $civiparms1['rowCount'] = $rowCount;
                $civires1 = &civicrm_contact_get($civiparms1);
            }
        }

        /*
         * check results from civicrm_contact_get, if error return error
         */
        if (civicrm_error($civires1)) {
            return civicrm_create_error($civires1['error_message']);
        } else {
            /*
             * if no error, set contact part of output parms. Result could
             * contain more contacts, so for each contact in $civires
             */
            $i = 1;
            foreach ($civires1 as $result) {

blabla


    $outparms[0]['record_count'] = ($i - 1);
    return ($outparms);
}
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

pbarmak

  • I post occasionally
  • **
  • Posts: 111
  • Karma: 3
  • CiviCRM version: 3.3.5
  • CMS version: Pressflow 6.19
  • MySQL version: 5.1
  • PHP version: 5.2.10
Re: Event Search max_results?
October 08, 2010, 09:13:49 am
Ahh, I figured it out - it was the naming of my file, not the code.  In CRM/Utils/REST.php, there is a ucfirst function call on the group name as it tries to find the file, so it's looking for a capital first letter, but everything else lowercase, just like you said with your "Xxxcontact.php".  Of course, I used FEIcontact.php and feicontact.php, but never realized it should be Feicontact.php.  My mistake, I should have stuck with exactly what you said.

But thanks so much for sharing your code!  I'm sure it will help at some point.  Once I complete my "fixes" to the original Event.php, I should post my new code as well.

So, for anyone else trying to do this and isn't thinking about the file name like me, make sure your copied file has only (and exactly only) the first character in Caps in the filename.  Then make sure each function within that file has "civicrm" as the prefix, then your filename (in all lowercase), then the function name.  So something like "civicrm_myfilename_search".  Hope that helps someone down the road.

BTW, Erik, it looks like CRM/Utils/REST.php is the correct file to update the code so it'll look for a custom api/v2 directory.  Would you agree?

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Event Search max_results?
October 08, 2010, 09:25:46 am
Quote from: pbarmak on October 08, 2010, 09:13:49 am
it looks like CRM/Utils/REST.php is the correct file to update the code so it'll look for a custom api/v2 directory.  Would you agree?

Yes, I would suggest that we simply use the existing config, as done for the CRM/xxx files.php

So your structure in your /custom folder is

custom/CRM/... (existing feature)
custom/api/v2 (the new feature)

Make sense ?

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

pbarmak

  • I post occasionally
  • **
  • Posts: 111
  • Karma: 3
  • CiviCRM version: 3.3.5
  • CMS version: Pressflow 6.19
  • MySQL version: 5.1
  • PHP version: 5.2.10
Re: Event Search max_results?
October 08, 2010, 09:31:06 am
Makes sense.  Where would I find an example that checks for the existence of the custom path/file first, the defaults to the main path/file if the custom is not found?  Want to make sure I use the same method.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Event Search max_results?

This forum was archived on 2017-11-26.