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) »
  • Stuck on javascript use of API
Pages: [1]

Author Topic: Stuck on javascript use of API  (Read 1294 times)

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Stuck on javascript use of API
March 15, 2012, 09:38:03 pm
I am attempting to call the API from some javascript running on the same website as CiviCRM. I added this to the page: /civicrm/user?reset=1. This environment is Drupal 6.x and CiviCRM 4.1.1

But the second and third "alert" never gets called.  Any suggestions?  BTW: What is the absolute path to "js/rest.js"

My source code inside my hook called "mymodule_civicrm_alterContent"


Quote
$tmp_js_path = "../{$config->resourceBase}js/rest.js";
      print "<br><br>js path: ".$tmp_js_path;
      
      $extra_js = "<script type='text/javascript' src='$tmp_js_path'></script>
            <script>
               function change_participant_status() {
        window.alert('Test');
       
        $().crmAPI('Contact','get',{'version' :'3', 'first_name' :'Sarah', 'last_name' :'Gladstone'}
         ,{ success:function (data){   
                $.each(data, function(key, value) { window.alert('has')  });
           }
        });
       
       window.alert('test 2');
      
        }  </script>";
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Stuck on javascript use of API
March 15, 2012, 10:53:12 pm
I would recommend reading developer book, check api section: http://book.civicrm.org/developer/techniques/api

Hth
Kurund
Found this reply helpful? Support CiviCRM

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Stuck on javascript use of API
March 16, 2012, 01:09:18 am
1) use firebug, put breakpoints
2) use console.log instead of alert
3) you don't have to create a module to add a bit of js code in a civi page, use dave .extra.tpl

If your call is to change participant status, I have blogged an example I think a while ago (
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Stuck on javascript use of API
March 16, 2012, 07:29:23 am

I got a bit further. The error I am getting in the firebug console is:   $().crmAPI is not a function @ http://mysite.org/civicrm/user?reset=1:576

The code sample I am testing came from the developers handbook.

Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Stuck on javascript use of API
March 16, 2012, 07:43:05 am
Hi,

Try cj().crmAPI instead

Be sure that at least a jquery & js/rest.js are inserted

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

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Stuck on javascript use of API
March 16, 2012, 07:50:48 pm
Thanks for all the suggestions, my code is now working.  The blog post at: http://civicrm.org/blogs/xavier/improve-your-workflow-ajax-one-click-activity-status-changing   was particularly helpful.

The final working call is:
Code: [Select]
cj().crmAPI('Participant','update',{'version' :'3', 'participant_id' :participant_id_parm, 'status_id' :attended_status_id }
,{callBack: function(result,settings){
          if (result.is_error == 1) {
              window.alert(result.error_message);
          }else{
          window.alert('Thank you for attending');
    var tmp_label_id = 'pid-' + participant_id_parm;
    var tmp_label = document.getElementById(tmp_label_id);
 
    tmp_label.innerHTML = 'Attended';
           
          }} } );

My main concerns are about the authorities needed.     The feature I created is designed to appear on the end-user dashboard for members to use to cancel or mark attended any of their own event registrations.

In order for this to work, I had to give members the following permissions:
Access CiviCRM
Access CiviEvent
Edit Event Participants

----
Which means they have WAY too much authority, when all I want them to be able to do is cancel their own event registration, or mark themselves attended.

Any ideas on how to allow this, while not giving out too much authority?
« Last Edit: March 16, 2012, 07:54:00 pm by Sarah Gladstone »
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Stuck on javascript use of API
March 17, 2012, 11:18:39 am
Hi,

instead of 'participant_id', could you use 'id', that's the "more official" way (we handle both as aliases)?

As for the permissions, that's a two steps trick:

On the latest version of civi, we have introduced a new permission to access the API that you should give to your members (instead of access CiviCRM).

By itself, it will only let you access the API, but it will block on the next permission (Access civievent & Edit event participants).

To solve that, you'll need to write a custom module that uses the alterAPIPermissions hook to bypass the access civievent & Edit event participants for participant create/update.

You might want to add extra controls in the hook (eg. checking the user is modifying her participation record only and that's only the status or whatever your use case)

http://wiki.civicrm.org/confluence/display/CRMDOC40/CiviCRM+hook+specification#CiviCRMhookspecification-hookcivicrmalterAPIPermissions

Would be great if you could blog about it when you get it working

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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Stuck on javascript use of API

This forum was archived on 2017-11-26.