Opublikowane
2007-07-15 14:52
This is Part 2 of my series on Writing Components For CiviCRM. Part 1 discussed what a component is, and what it does. The key tasks were:
- Define and manage a set of data base tables.
- Set up UI to view and edit contacts that contain its data.
- Make the content searchable.
<table> <base>CRM/Voter</base> <class>Campaign</class> <name>civicrm_voter_campaign</name> <comment>Campaign objects represent an election for a candidate or issue.</comment> <add>1.6</add> <field> <name>id</name> <type>int unsigned</type> <required>true</required> <comment>Unique Campaign ID</comment> <add>1.6</add> </field> <primaryKey> <name>id</name> <autoincrement>true</autoincrement> </primaryKey> <field> <name>default_campaign</name> <title>Default Campaign</title> <type>boolean</type> <default>0</default> <comment>Default campaign for this install.</comment> <add>1.6</add> </field> <field> <name>campaign_name</name> <required>true</required> <title>Campaign Title</title> <type>varchar</type> <length>30</length> <comment>Display name or title of the campaign</comment> <add>1.6</add> </field> <field> <name>start_date</name> <uniqueName>campaign_start_date</uniqueName> <title>Campaign Start Date</title> <type>date</type> <comment>Opening of political campaign.</comment> <add>1.6</add> </field> <field> <name>election_date</name> <title>Date Of Election</title> <type>date</type> <comment>Opening of political campaign.</comment> <add>1.6</add> </field> </table>Let's look at some of these directives. <base> tells CiviCRM's build system (more on that below) where you want the builder to put any PHP files it generates for you. This is relative to the top-level CiviCRM directory, where you'll find the api/, CRM/, sql/, templates/ and xml/ directories. The <class> directive tells the builder how to define the PHP loader and wrapper classes. CiviCRM uses the PHP PEAR DB_DataObject classes to handle most of the dirty work of getting at your data. It does this via a CiviCRM base class CRM_Core_DAO, which is defined in CRM/Core/DAO.php. CRM_Core_DAO handles your INSERT, UPDATE and DELETE statements, and you'll very rarely need to worry about these yourself. Every table in CiviCRM has a corresponding DAO (i.e., "Data Access Object") class, named using your value of class, and the "path" you gave in base. For the Campaign object:
- The builder will define a class CRM_Voter_DAO_Campaign, and
- The builder will put this definition in a CRM/Voter/DAO/Campaign.php file.
<tables xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="Voter.xml" parse="xml" />
<xi:include href="VoterInfo.xml" parse="xml" />
<xi:include href="Campaign.xml" parse="xml" />
<xi:include href="Canvass.xml" parse="xml" />
<xi:include href="CanvassContact.xml" parse="xml" />
<xi:include href="Districts.xml" parse="xml" />
</tables>
To get the builder to load this, the last step is to add the path to your files.xml file to the list in xml/schema/Schema.xml. It should be fairly obvious what to change. Once you've done this, you can run the builder utility, which is in
distmaker/distmaker.sh. You define a distmaker.conf file telling the builder where you want distmaker to put your files, and run it from the shell to generate a tar ball. To build the PHP5 version for Drupal, you type:
$ ./distmaker d5
If you have everything set up right, the script will generate a version civicrm_41.mysql that contains your newly defined tables, and a set of DAO class files to handle low level access.
You're still not quite done yet, however. You'll need to create BAO ("Business-logic Access Object") class definition files for each of your tables. The easiest thing here is to use another component as a model. I used Membership and Quest (a project Lobo & Co. did for a group of university admissions departments) as mine.
That's enough for today. I'll describe how to get your various files loaded, and how to export an API in the next installment.
Filed under