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) »
  • Hook to theyworkforyou.com
Pages: [1]

Author Topic: Hook to theyworkforyou.com  (Read 646 times)

lcooperuk

  • I’m new here
  • *
  • Posts: 3
  • Karma: 0
  • CiviCRM version: 4.2.7
  • CMS version: Joomla 2.5.7
  • MySQL version: 5.5.29-log
  • PHP version: 5.2.17
Hook to theyworkforyou.com
February 22, 2013, 02:54:22 pm
I am trying to populate a custom field on the Contact record with parliamentary information which exists on an external url at a site called theyworkforyou

I know what I need, but not sure how to go about it.

Whenever a contact is added or changed I would like to invoke a hook to update certain custom fields.

I have read through the hooks documents, but still am not sure what I need to code in the .php file in the custom PHP directory, other than using "hook_civicrm_pre", but I am not sure how to use that.

I am sure somebody must have done something similar, if not, can somebody please give me some guidelines (yes, I've read the custom email example) on how to achieve this.

Does anybody have an example of the complete .php file, including a URL call and database update that I can base my code on?

Yes, I know its cheating, but I am not a developer, so would welcome help please.  I'm fine with the SQL stuff, its just the webhooks I cant figure out.

Once completed, I'm happy to share my efforts with others.  (Its a Joomla installation)

Thanks.
« Last Edit: February 22, 2013, 03:14:50 pm by lcooperuk »

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: Hook to theyworkforyou.com
February 25, 2013, 03:45:26 am
see http://book.civicrm.org/developer/current/techniques/hooks/
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.

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: Hook to theyworkforyou.com
February 25, 2013, 11:59:28 pm
You probably need the civicrm post hook. This is called whenever an object has been updated in the database. Here is an example for keeping an emailaddress synchronized between two applications.

In this example a specific function is called whenever there is an edit or create action on an email.

Code: [Select]
function silcivi_civicrm_post( $op, $objectName, $objectId, &$objectRef ) {
    if ( $objectName == "Email" ) {
        if ( $op == "edit" || $op == "create" ) {
            if ( isset( $objectRef->email ) && !empty( $objectRef->email ) ) {
                $emailExists = false;
                /*
                 * retrieve first, middle and last name of contact
                 */
                $getContactParams = array(
                    'version'           =>  3,
                    'contact_id'        =>  $objectRef->contact_id,
                    'return.first_name' =>  1,
                    'return.middle_name'=>  1,
                    'return.last_name'  =>  1
                );
                $getContact = civicrm_api("Contact", "Getsingle", $getContactParams );
                if ( isset( $getContact['first_name'] ) ) {
                    $first_name = $getContact['first_name'];
                } else {
                    $first_name = "";
                }
                if ( isset( $getContact['middle_name'] ) && !empty( $getContact['middle_name'] ) ) {
                    $last_name = $getContact['middle_name']." ";
                }
                if ( isset( $getContact['last_name'] ) ) {
                    if ( isset( $last_name ) ) {
                        $last_name .= $getContact['last_name'];
                    } else {
                        $last_name = $getContact['last_name'];
                    }
                }
                $last_name = trim( $last_name );
                if ( $op == "create" ) {
                    $checkParams = array(
                        'action'        =>  'check',
                        'email'         =>  $objectRef->email,
                        'first_name'    =>  $first_name,
                        'last_name'     =>  $last_name
                    );
                    $emailExists = _silcivi_processApi( $checkParams );
                }
                if ( !$emailExists ) {
                        $processParams = array(
                            'action'        =>  $op,
                            'email'         =>  $objectRef->email,
                            'email_id'      =>  $objectRef->id,
                            'contact_id'    =>  $objectRef->contact_id,
                            'first_name'    =>  $first_name,
                            'last_name'     =>  $last_name
                        );
                        $result = _silcivi_processApi( $processParams );
                }
            }
        }
    }
}

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

lcooperuk

  • I’m new here
  • *
  • Posts: 3
  • Karma: 0
  • CiviCRM version: 4.2.7
  • CMS version: Joomla 2.5.7
  • MySQL version: 5.5.29-log
  • PHP version: 5.2.17
Re: Hook to theyworkforyou.com
February 26, 2013, 07:16:27 am
Thank you Erik; that's exactly the sort of example I was looking for.  I already have a PHP script to update the information, just couldn't understand how to implement a hook.  Thanks again.

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: Hook to theyworkforyou.com
February 26, 2013, 11:11:54 pm
Have fun playing with the hooks :-)
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) »
  • Hook to theyworkforyou.com

This forum was archived on 2017-11-26.