Gepubliceerd
2007-07-26 16:47
This is Part 3 of my series on Writing Components For CiviCRM. Part 1 discussed what a component is, and what it does. Part 2 explained how to use CiviCRM's XML-based data definition language to define your tables. But while your tables may now be in place, CiviCRM doesn't know what to do with your data. We'll need to do a bit of editing on CiviCRM Core in order to hook up your data in CiviCRM, and I'll briefly describe some of the places we will need to "hack" core so that your data will appear in the UI.
So what do I mean by "hooking up data"? CiviCRM needs to know when to load your code so that users of your components can do things like:
- View your data via the contact view page.
- Edit your data in contact edit pages.
- Search for contacts using your data as keys.
- Access your data via Drupal modules or similar external code.
- Import and export data that will ultimately be stored in your tables.
- Implement special menu handlers if we display special pages, so CiviCRM will Invoke the code to render our pages.
- Be able to create and delete data objects you create, and have your objects get created/deleted as needed when a contact gets created or deleted.
- Set any administrative settings your component needs.
First Steps Hooking Up
I've already alluded to the first part of the hook up: you need to write BAO (business logic) classes to handle the "higher level" parts of what it takes to manipulate your objects in the database. In Voter's case, we need make sure that contact-related data gets loaded whenever its corresponding contact is loaded (CiviCRM does not do this for us automatically in the 1.x versions), and we need to add code to the Canvass class to manage assigning voters to a canvass (so we can contact them), assigning volunteers to blocks of voters within a canvass (so voters are contacted only once), and to allow our code to get a link to a voter so a volunteer can make a call, or so we can put the voter's information on a "walk list" before we send the volunteer door-to-door to talk to voters. All of this core logic goes into one or the other BAO class, except for a few places where we need to "patch" core CiviCRM to get our code to execute. To do these things, almost all BAO classes will need to define the following functions. In CiviCRM 1.x, most of these are "static", or "class" methods of your BAO class; I'll point out the exceptions when we get to these. The core methods are:- static &create(&$params, &$ids)
- Creation and update code for an object that has "sub-objects". The $ids field gives us back the CiviCRM IDs of these sub-objects.
- static retrieve( &$params, &$defaults )
- This is the easiest interface to use in order to retrieve a single record from the database as a BAO object.
- static exportableFields( )
- This is part (although, unfortunately, not all of what you need to implement in order to let the standard exporter export your data).
- Voter objects need to create a VoterInfo object to store Campaign-specific information for that voter.
- Canvass objects need to be able to retrieve the Voter objects assigned to them. I have a couple of methods for this important operation, including an "iterator" method that makes it easier to retrieve sequences of voters.
- Canvass objects need to be able to retrieve the various voter contact records for that canvass.
- Canvass object need to "lock" Voter objects so they do not get assigned to multiple volunteers.
- CRM_Contact_BAO_Contact
- In create() needs to handle the 'Voter' contact_subtype when we create Voter records
- deleteContact() and importableFields() need special handling for Voters as well.
- CRM_Contact_Form_Edit
- setDefaultValues(), buildQuickForm() and postProcess() all require modification before we can edit Voter objects in the UI.
Filed under
Comments
Is there any update on this? Does 3.2 or later require the hacks to core as well? Does everything else still apply?
thanks
to extend CiviCRM and use Civi Hooks / customized templates to modify the functionality
lobo