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) »
  • Aborting Contact Modification Operations Using Hooks
Pages: [1]

Author Topic: Aborting Contact Modification Operations Using Hooks  (Read 1170 times)

spork

  • I’m new here
  • *
  • Posts: 13
  • Karma: 1
  • CiviCRM version: 4.0.1
  • CMS version: Drupal 7
Aborting Contact Modification Operations Using Hooks
January 11, 2011, 03:11:17 pm
I have a situation where I am synchronizing type "school" contacts with a "master" site's school contacts. Whenever a school contact is created/modified/deleted on a local site I need those changes to to be sent out to the master site too. It is required that the modification fail on the local side if there is a failure sending those modifications to the master site.

The issue I'm having is that while I can send out the the modifications to the master site using a hook_civicrm_pre (or post) hook, how am I supposed to properly fail/abort the local operation if there is a error modifying the master site? It is explicitly said hook_civicrm_pre is not for aborting operations but hook_civicrm_validate is for validating specific forms, and will not capture all modifications to a certain contact type...so, I am wondering what's the best/proper way to go about this?

Thanks
spork

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: Aborting Contact Modification Operations Using Hooks
January 11, 2011, 06:38:31 pm

i'm assuming civicrm is the local site and the master site is some other db (potentially not civicrm?)

i dont think there is an easy way to do this in civicrm currently (other than aborting the process in the _pre hook)

if you have logging enabled (a 3.3 feature), you can potentially do a rollback in the _post hook if the master fails. however u'll need to write the rollback code, since we've not gotten that far as yet with the logging code

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

spork

  • I’m new here
  • *
  • Posts: 13
  • Karma: 1
  • CiviCRM version: 4.0.1
  • CMS version: Drupal 7
Re: Aborting Contact Modification Operations Using Hooks
January 12, 2011, 02:25:53 pm
ok,

the master site does in fact happen to be civicrm, but we have the master and local sites talking to one another through an http api which abstracts away most everything underneath. we considered using civicrm's multisite options to share contacts but as far as i know there is no built in way to share contacts of type x across sites while keeping others local.

depending of the order of pre and post (is pre called on all objects before any are written to DB or is pre/dbwrite/post done object by object?), aborting the process in the pre hook would be problematic since some objects might get written to the db while others coming after the abort would end up not being committed (unless all submissions are treated as a sql transaction maybe?).

i suppose i will try working out a solution doing an undo/rollback using hook_civicrm_pre and post (pre to save the unmodified contact in a temp location in case we need to undo). i'd still want to message the failure to the user somehow though...   

thanks,
spork


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: Aborting Contact Modification Operations Using Hooks
January 13, 2011, 02:14:36 am
I am dealing with a similar kind of synchronization process, and we are not using the new logging functionality (unfortunately - but it would have extended the project too much). So I create my own logging by using the pre hook to store the DB values before the process. If the update fails on the other site, I get an error back and restore the DB values. If the update on the other site is succesfull, they send a specifically create API back so the logging is removed.

Would like to know how you cope with the new logging functionality?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

spork

  • I’m new here
  • *
  • Posts: 13
  • Karma: 1
  • CiviCRM version: 4.0.1
  • CMS version: Drupal 7
Re: Aborting Contact Modification Operations Using Hooks
January 14, 2011, 12:42:16 pm
Quote from: Erik Hommel on January 13, 2011, 02:14:36 am
Would like to know how you cope with the new logging functionality?

I am implementing it for now as described above due to project time issues, but knowing how to do it more elegantly in the future would certainly be good...  ;D

spork

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: Aborting Contact Modification Operations Using Hooks
January 15, 2011, 01:53:46 am
So what are you using at the moment? The same way as I have done it?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

spork

  • I’m new here
  • *
  • Posts: 13
  • Karma: 1
  • CiviCRM version: 4.0.1
  • CMS version: Drupal 7
Re: Aborting Contact Modification Operations Using Hooks
January 15, 2011, 10:14:08 am
Quote from: Erik Hommel on January 15, 2011, 01:53:46 am
So what are you using at the moment? The same way as I have done it?

pretty much. my quick fix plan is to save each school contact info in a global php array (i'm assuming pre and post run in the same process otherwise maybe i'll have to use a db table instead of global array) in hook_civicrm_pre and then in hook_civicrm_post i try sending the modifications to the master . if it fails then i search through the array to get the contact with the matching id and restore the old contact info. then i'll use drupal_set_message to send an error message to the user.

spork

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Aborting Contact Modification Operations Using Hooks

This forum was archived on 2017-11-26.