civicrm developer training extend and customize civicrm

41
CiviCRM Developer Training Extend and customize CiviCRM

Upload: calista-wilby

Post on 14-Dec-2015

235 views

Category:

Documents


10 download

TRANSCRIPT

Page 1: CiviCRM Developer Training Extend and customize CiviCRM

CiviCRM Developer TrainingExtend and customize CiviCRM

Page 2: CiviCRM Developer Training Extend and customize CiviCRM

Introduction

• Matthew Briney ([email protected])• Chang Xiao ([email protected])• IRC: changx• Forum changx (xcf33)

Page 3: CiviCRM Developer Training Extend and customize CiviCRM

Development Environment

• Tools for Local Development – LAMP Stack• Acquia Dev Desktop (Mac & Win)• http://network.acquia.com/downloads/6.x/

• MySQL Workbench – ERD Diagrams• http://www.mysql.com/downloads/workbench/

Page 4: CiviCRM Developer Training Extend and customize CiviCRM

Cron Jobs

http://www.example.org/sites/all/modules/civicrm/bin/civimail.cronjob.php?name=user&pass=pass&key=key

Page 5: CiviCRM Developer Training Extend and customize CiviCRM

Develop For CiviCRM

• Topics• CiviCRM Architecture• Public APIs• Hooks

Page 6: CiviCRM Developer Training Extend and customize CiviCRM

CiviCRM Architecture

DAO BAO

Page 7: CiviCRM Developer Training Extend and customize CiviCRM

Directory Structure

Page 8: CiviCRM Developer Training Extend and customize CiviCRM

Example

• CRM/Mailing/BAO/Job.phpcorresponds to:

• CRM_Mailing_BAO_Job• Snippet:

civicrm_initialize();require_once(‘CRM/Mailing/BAO/Job.php’);$mailing_job = new CRM_Mailing_BAO_Job();

• $mailing_job->id = 45$mailing_job->getMailingSize();

Page 9: CiviCRM Developer Training Extend and customize CiviCRM

How to Customize CiviCRM

• Write a module (API, hooks)• Theme overrides (API, Smarty)• Custom report• Custom search• Custom payment gateway

Page 10: CiviCRM Developer Training Extend and customize CiviCRM

Public APIs

• http://wiki.civicrm.org/confluence/display/CRMDOC33/CiviCRM+Public+APIs

• Current Version: V2• Upcoming Version: V3 (CiviCRM 3.4/4.0)

Page 11: CiviCRM Developer Training Extend and customize CiviCRM

Public API

• For 3.4/4.0• Look for civicrm.settings.php and change the API• Define(‘CIVICRM_API_VERSION’, ‘3’);• OR explicitly specify in the $param of the API cal

$param = array(‘version’ => ‘2’,

);

Page 12: CiviCRM Developer Training Extend and customize CiviCRM

Invoking the API

• Template level (smarty)• REST interface• AJAX

(http://wiki.civicrm.org/confluence/display/CRMDOC33/AJAX+Interface)

• PHP (most common)

Page 13: CiviCRM Developer Training Extend and customize CiviCRM

API Call (Template)

• {crmAPI entity="nameobject" action="namemethod" var="namevariable" extraparam1="aa" extraparam2="bb”}

• entity (contact, contribution)• action (get, search) [retrieving method]• var smarty template variable name• extraparam parameters from the API documentation• return fields to return.

Page 14: CiviCRM Developer Training Extend and customize CiviCRM

API Call (Template)Example

• {crmAPI entity='contact' action="search" var="contacts”}<ul>

{foreach from=$contacts item=contact}<li id="contact_{$contact.contact_id}">

{$contact.sort_name}</li>

{/foreach} </ul>

{crmAPI entity='contact' action="search" var="contacts" country="France" contact_type="Individual" return ="sort_name,email"}

Page 15: CiviCRM Developer Training Extend and customize CiviCRM

API (REST Interface)

• https://www.example.org/path/to/civi/codebase/civicrm/extern/rest.php?q=civicrm/<function>

• Has complete access to all public APIs

Page 16: CiviCRM Developer Training Extend and customize CiviCRM

API PHP

• Default API• Most reliable• Called by modules• Snippet:

$params = array( 'contact_id' => 10, 'return_custom_N' => 1 );$retrieved = civicrm_contact_get( $params );if ( civicrm_error( $retrieved ) ) {

return $retrieved['error_message'];} else {

print_r($retrieved);}

Page 17: CiviCRM Developer Training Extend and customize CiviCRM

API V3

• Entity-action model• Should you use it?

Page 18: CiviCRM Developer Training Extend and customize CiviCRM

Live DEMO

• API Explorer• Access it at: /civicrm/ajax/doc#explorer• For version 3.4/4.0

Page 19: CiviCRM Developer Training Extend and customize CiviCRM

CiviCRM Hooks

• http://wiki.civicrm.org/confluence/display/CRMDOC33/CiviCRM+hook+specification#CiviCRMhookspecification

• Fires before or after an action or at specific time• Have specific naming conventions.

Page 20: CiviCRM Developer Training Extend and customize CiviCRM

Example 1

• Snippet: (send an email when the contact is created)

my_module_civicrm_post( $op, $objectName, $objectId, &$objectRef ) {

if($op == ‘create’) {if($objectName == ‘Individual’) {

// send the new contact an email

mail($objectRef->email, ‘hi, welcome’, ‘Welcome to the site’);

}}

}

Page 21: CiviCRM Developer Training Extend and customize CiviCRM

function my_module_civicrm_alterMailParams(&$params) {

$token_params = array('subject' => $params['subject'],'from' => $params['from'],);

// do token replacement here$tracking = array('utm_source' => token_replace(variable_get('civitracker_mailing_pattern_source',

'[sitename]'), 'civitracker_mail', NULL, '[', ']', $token_params),'utm_campaign' =>

token_replace(variable_get('civitracker_mailing_pattern_campaign', '[date]'), 'civitracker_mail', NULL, '[', ']', $token_params),

'utm_medium' => 'email',);

if(variable_get('civitracker_mailing_toggle', 1) == 1) {if(isset($params['html']) && $params['html'] != '') {$params['html'] = civitracker::url_append($params['html'], $tracking);}if(isset($params['text']) && $params['text'] != '') {$params['text'] = civitracker::url_append($params['text'], $tracking);}}

}}

Page 22: CiviCRM Developer Training Extend and customize CiviCRM

Hands on exercise

Page 23: CiviCRM Developer Training Extend and customize CiviCRM

Other useful functions

• Single Value Query: $query = “Select Blah FROM Blah”CRM_Core_DAO::singleValueQuery( $query );

Page 24: CiviCRM Developer Training Extend and customize CiviCRM

Example (save a record)

• Create a new dashlet

• Snippet:civicrm_initiate();require_once(‘CRM/Core/DAO/Dashboard.php’);$dashboard = new CRM_Core_DAO_Dashboard();$dashboard->domain_id = 1;$dashboard->label = $title;$dashboard->url = ‘path/to/dashlet’;$dashboard->permission = 'view civicrm_tools dashlets’;$dashboard->is_active = 1;$dashboard->created_date = date('Y-m_d h:i:s');$dashboard->save();

Page 25: CiviCRM Developer Training Extend and customize CiviCRM

Custom Reports

Page 26: CiviCRM Developer Training Extend and customize CiviCRM

Custom Reports

• http://wiki.civicrm.org/confluence/display/CRMDOC33/Custom+Reports+(How+to)

• Grab a report that you wish to copy• /var/www/htdocs/sites/all/modules/civicrm/CRM/Report/Form/Contact• Create a custom report template and copy the file into

/var/www/htdocs/custom_templates/CRM/Report/Form/Contact• Rename the file name and class name

AreaDemoSummary.phpCRM_Report_Form_Contact_AreaDemoSummary extends CRM_Report_Form

• Copy template .tpl file from civicrm/templates/CRM/Report/Form/Contact/Summary.tplto /var/www/htdocs/custom_templates/CRM/Report/Form/Contact/AreaDemoSummary.tpl

Page 27: CiviCRM Developer Training Extend and customize CiviCRM

Custom Reports

• Register the report template atcivicrm/admin/report/register&reset=1

• Make sure you have custom template directory set up atcivicrm/admin/setting/path&reset=1

Page 28: CiviCRM Developer Training Extend and customize CiviCRM

Anatomy of a Report

• __construct()• select()• from()• where()

Page 29: CiviCRM Developer Training Extend and customize CiviCRM

Field Definition

• __construct()

Define display columns , filters

http://pastebin.com/FsKELJbX

Page 33: CiviCRM Developer Training Extend and customize CiviCRM

• groupBy()• orderBy()• postProcess()

Page 34: CiviCRM Developer Training Extend and customize CiviCRM

Example

• Going through examples.

Page 35: CiviCRM Developer Training Extend and customize CiviCRM

Custom Searches

Page 36: CiviCRM Developer Training Extend and customize CiviCRM

Process

• Build search file• Place file in custom search folder• Register report with CiviCRM• /sites/all/modules/civicrm/CRM/Contact/Form/Search/

Custom

Page 37: CiviCRM Developer Training Extend and customize CiviCRM

Documentation

• http://wiki.civicrm.org/confluence/display/CRMDOC33/Custom+Search+Components

• /sites/all/modules/civicrm/CRM/contact/Form/Search/Custom

Page 38: CiviCRM Developer Training Extend and customize CiviCRM

Template Override

Page 39: CiviCRM Developer Training Extend and customize CiviCRM

Template Structure

• Templates located in: /civicrm/templates/CRM• Organized by feature

Page 40: CiviCRM Developer Training Extend and customize CiviCRM

Overriding Templates

• Copy the file from • /civicrm/templates/CRM

• to the same folder structure in • /sites/all/civicrm_templates/CRM

Page 41: CiviCRM Developer Training Extend and customize CiviCRM

• Identify the page you want to edit (template)• Find the correct template file• Copy the template directory outside of

/sites/all/modules/civicrm• Set the configuration settings at adminster->global-

>directories• Make the changes