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 (Moderator: Donald Lobo) »
  • Interaction between php, smarty, and javascript
Pages: [1]

Author Topic: Interaction between php, smarty, and javascript  (Read 1104 times)

lee.gooding

  • I post occasionally
  • **
  • Posts: 83
  • Karma: 1
    • Clear River Church
  • CiviCRM version: 4.5
  • CMS version: Drupal 7
  • MySQL version: 5.7
  • PHP version: 5.3
Interaction between php, smarty, and javascript
June 11, 2014, 08:32:48 am
I'm hoping someone can help me determine if I am approaching this correctly. I'm in the process of building an extension and I'm trying to figure out the "correct" way to communicate between my tpl file and my php file.

I am using the PHP file for API calls and SQL queries. The TPL is being used to display the information, and jquery is being used to handle clicks and manipulation of the page after it is displayed.

Here is an example I need help with.

I have a list of "contacts" that are buttons. The contact id is a value assigned to the button. When I click the contact button it will trigger the click function (through jQuery). At this point I need to call a function (passing the id) in my PHP that will return a list of data that is associated with that ID. I'm thinking about using the jQuery ajax to call the PHP and return the results.

Is that the correct approach? I feel like there should be a more direct/efficient way to do this. Would that be through the creation of my own API that I can then call directly from smarty?

I appreciate any insight into this!
« Last Edit: June 11, 2014, 11:04:47 am by lee.gooding »

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: Interaction between php, smarty, and javascript
June 11, 2014, 11:09:41 am
Based on what you said, it sounds to me like your approach is correct.
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Interaction between php, smarty, and javascript
June 11, 2014, 11:29:23 am
Good question!

Here are some resources you should read if you have not already done so:
https://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference
https://wiki.civicrm.org/confluence/display/CRMDOC/API+Reference
http://wiki.civicrm.org/confluence/display/CRMDOC/Create+a+Module+Extension#CreateaModuleExtension-Howdoesoneaddanajax/web-servicecallback?

Now to break down your requirements:
  • Assigning the contact id to an html button: yes it makes sense to do this in smarty. You can mark it up however you want, but I'm personally partial to using html5-style data. Semantically it makes sense to use data for anything that will solely be used by javascript, since html5 data serves no other purpose.
  • Fetching data from the server: In most cases it makes sense to use CRM.api3 rather than writing and executing your own callback. In some rare cases where the civicrm api truly cannot fetch the data you need, you could optionally write your own api instead of going to the trouble of writing a full callback in php. This may or may not be more convenient for you and is mostly a matter of personal preference. Either way please be sure that you have addressed the question of permissions.
  • You mentioned calling an api from smarty. I assume you meant to say javascript because (while possible) there's no point really to calling an api from smarty since it is usually cleaner to call the api from php and assign the results to smarty. If you really meant smarty then I would question why you are using ajax at all. If the data you're fetching is small and fairly static, then it would all fit in the dom so you might as well pre-load it and avoid the extra server hits and code complexity.
  • Finally, if you really are writing a javascript-based application, you might want to consider an MVC such as angular or backbone to structure it. Both are included in the CiviCRM package (BB as of 4.3, Angular as of 4.5) and the core team has begun adopting both of them, with recent discussions leaning toward favoring Angular.
Try asking your question on the new CiviCRM help site.

lee.gooding

  • I post occasionally
  • **
  • Posts: 83
  • Karma: 1
    • Clear River Church
  • CiviCRM version: 4.5
  • CMS version: Drupal 7
  • MySQL version: 5.7
  • PHP version: 5.3
Re: Interaction between php, smarty, and javascript
June 12, 2014, 12:46:35 pm
Coleman,

Thanks for your detailed response.

1) I'm not familiar with HTML5 data, I will definitely look into that. I'm currently working with jquery and using that to pull data from classes, values, etc.

2) I ended up extending CRM.api3 and added a custom function that allows me run my query. Basically I need to query a SQL view that I have created to aggregate data for contacts. I am potentially working with a large amount of data and preloading all data for the page probably isn't a good approach. By extending the API I'll be able to grab what I need, when I need it.

3) I understand that it is ideal to make api calls from PHP and then pass the variables to smarty. I need to have the actual page pass data to the API calls. To do this I make the API calls from javascript within the page.

4) I'll also look into Angular. At a glance it looks like it could really help with the development of my extension. I was originally planning to use jQuery to handle most of my page manipulation. Is there any documentation related to Angular and Civi? I searched the wiki and didn't really find anything.

Quote from: Coleman Watts on June 11, 2014, 11:29:23 am
Good question!

Here are some resources you should read if you have not already done so:
https://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference
https://wiki.civicrm.org/confluence/display/CRMDOC/API+Reference
http://wiki.civicrm.org/confluence/display/CRMDOC/Create+a+Module+Extension#CreateaModuleExtension-Howdoesoneaddanajax/web-servicecallback?

Now to break down your requirements:
  • Assigning the contact id to an html button: yes it makes sense to do this in smarty. You can mark it up however you want, but I'm personally partial to using html5-style data. Semantically it makes sense to use data for anything that will solely be used by javascript, since html5 data serves no other purpose.
  • Fetching data from the server: In most cases it makes sense to use CRM.api3 rather than writing and executing your own callback. In some rare cases where the civicrm api truly cannot fetch the data you need, you could optionally write your own api instead of going to the trouble of writing a full callback in php. This may or may not be more convenient for you and is mostly a matter of personal preference. Either way please be sure that you have addressed the question of permissions.
  • You mentioned calling an api from smarty. I assume you meant to say javascript because (while possible) there's no point really to calling an api from smarty since it is usually cleaner to call the api from php and assign the results to smarty. If you really meant smarty then I would question why you are using ajax at all. If the data you're fetching is small and fairly static, then it would all fit in the dom so you might as well pre-load it and avoid the extra server hits and code complexity.
  • Finally, if you really are writing a javascript-based application, you might want to consider an MVC such as angular or backbone to structure it. Both are included in the CiviCRM package (BB as of 4.3, Angular as of 4.5) and the core team has begun adopting both of them, with recent discussions leaning toward favoring Angular.

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Interaction between php, smarty, and javascript
June 12, 2014, 02:46:53 pm
1) It's pretty simple, your doctype does not need to be real html-5 to use it. Just add any property starting with data- to your markup, e.g.
Code: [Select]
<a class="contact-link" href="#" data-cid="42" data-contact_type="Individual">. Then from jQuery you can do something like
Code: [Select]
$('.contact-link').data('cid') to retrieve a bit of data.
4) It's early days with Angular and Civi, not much documentation yet, but you can see some good discussions of it on this board: http://forum.civicrm.org/index.php/board,91.0.html
Try asking your question on the new CiviCRM help site.

lee.gooding

  • I post occasionally
  • **
  • Posts: 83
  • Karma: 1
    • Clear River Church
  • CiviCRM version: 4.5
  • CMS version: Drupal 7
  • MySQL version: 5.7
  • PHP version: 5.3
Re: Interaction between php, smarty, and javascript
June 23, 2014, 08:16:14 am
This is awesome! You just made my life a lot easier :). Now I can be consistent with how I am attaching data to the UI.

Quote from: Coleman Watts on June 12, 2014, 02:46:53 pm
1) It's pretty simple, your doctype does not need to be real html-5 to use it. Just add any property starting with data- to your markup, e.g.
Code: [Select]
<a class="contact-link" href="#" data-cid="42" data-contact_type="Individual">. Then from jQuery you can do something like
Code: [Select]
$('.contact-link').data('cid') to retrieve a bit of data.
4) It's early days with Angular and Civi, not much documentation yet, but you can see some good discussions of it on this board: http://forum.civicrm.org/index.php/board,91.0.html

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Interaction between php, smarty, and javascript

This forum was archived on 2017-11-26.