| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Enter description here...
|
| 5 |
*
|
| 6 |
* @param unknown_type $op
|
| 7 |
* @param unknown_type $arg
|
| 8 |
* @return unknown
|
| 9 |
*/
|
| 10 |
function civicrm_crmapi($op, $arg = NULL) {
|
| 11 |
switch ($op) {
|
| 12 |
case 'meta':
|
| 13 |
return array('civicrm' => array(
|
| 14 |
'name' => 'CiviCRM',
|
| 15 |
'description' => 'CiviCRM is the first open source and freely downloadable constituent relationship management solution. CiviCRM is web-based, open source, internationalized, and designed specifically to meet the needs of advocacy, non-profit and non-governmental groups.',
|
| 16 |
'version' => 0.1,
|
| 17 |
'module' => 'civicrm',
|
| 18 |
));
|
| 19 |
|
| 20 |
default:
|
| 21 |
break;
|
| 22 |
}
|
| 23 |
}
|
| 24 |
|
| 25 |
|
| 26 |
function civicrm_crmapi_contact_fields(){
|
| 27 |
|
| 28 |
civicrm_initialize(TRUE);
|
| 29 |
|
| 30 |
//Get default CiviCRM Profile
|
| 31 |
$profile_id = variable_get("crmapi_civicrm_contact_profile", 1);
|
| 32 |
|
| 33 |
//Get the fields for that profile
|
| 34 |
$crm_fields = crm_uf_get_profile_fields($profile_id, true);
|
| 35 |
|
| 36 |
$crmapi_contact_fields = crmapi_contact_fields();
|
| 37 |
|
| 38 |
//dpr($crm_fields);
|
| 39 |
|
| 40 |
//loop through crmapi standard crmapi fields, giving crm fields
|
| 41 |
foreach ($crmapi_contact_fields as $crmapi_field => $crmapi_field_title) {
|
| 42 |
|
| 43 |
$crm_field = $crm_fields[civicrm_crmapi_contact_field_map($crmapi_field)];
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
$fields[$crmapi_field] = array(
|
| 48 |
'#crm_field_name' => $crm_field['name'],
|
| 49 |
'#required' => $crm_field['is_required'],
|
| 50 |
'#title' => $crmapi_field_title,
|
| 51 |
'#type' => crmapi_contact_field_types($crmapi_field),
|
| 52 |
);
|
| 53 |
}
|
| 54 |
|
| 55 |
return $fields;
|
| 56 |
|
| 57 |
}
|
| 58 |
|
| 59 |
function civicrm_crmapi_contact_field_map($crmapi_field){
|
| 60 |
$field_map = array(
|
| 61 |
|
| 62 |
|
| 63 |
);
|
| 64 |
}
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
/**
|
| 69 |
* Implementation of hook_crmapi_contact_form
|
| 70 |
*
|
| 71 |
* @param int $uid
|
| 72 |
* @return mixed additions to $user->crmapi['crmapi_user']
|
| 73 |
*/
|
| 74 |
function civicrm_crmapi_contact_form($edit=array(), $section=NULL){
|
| 75 |
|
| 76 |
civicrm_initialize(TRUE);
|
| 77 |
global $user;
|
| 78 |
|
| 79 |
$form = civicrm_crmapi_contact_fields();
|
| 80 |
|
| 81 |
if($section=="user") {
|
| 82 |
$fields = $edit['crmapi']['contact'];
|
| 83 |
} else {
|
| 84 |
$fields = $user['crmapi']['contact'];
|
| 85 |
}
|
| 86 |
foreach ($fields as $name => $value) {
|
| 87 |
$form[$name]['#default_value'] = $value;
|
| 88 |
}
|
| 89 |
$visible_fields = variable_get('crmapi_crmapi_'.$section.'_fields', array());
|
| 90 |
$required_fields = variable_get('crmapi_crmapi_'.$section.'_fields_requred', array());
|
| 91 |
|
| 92 |
foreach($form as $index => $form_item) {
|
| 93 |
if (!in_array($index, $visible_fields)) {
|
| 94 |
unset($form[$index]);
|
| 95 |
}
|
| 96 |
if (in_array($index, $required_fields)) {
|
| 97 |
$form[$index]['#required']=="true";
|
| 98 |
}
|
| 99 |
}
|
| 100 |
|
| 101 |
$form['first_name']['#weight'] = 0;
|
| 102 |
$form['last_name']['#weight'] = 1;
|
| 103 |
$form['address1']['#weight'] = 2;
|
| 104 |
$form['city']['#weight'] = 4;
|
| 105 |
$form['state']['#weight'] = 5;
|
| 106 |
$form['country']['#weight'] = 6;
|
| 107 |
$form['postal_code']['#weight'] = 7;
|
| 108 |
|
| 109 |
|
| 110 |
if ($category !='crmapi-contact') {
|
| 111 |
$form['submit'] = array(
|
| 112 |
'#type' => 'submit',
|
| 113 |
'#value' => 'Submit',
|
| 114 |
'#weight' => 10,
|
| 115 |
);
|
| 116 |
}
|
| 117 |
|
| 118 |
return $form;
|
| 119 |
}
|
| 120 |
|
| 121 |
/**
|
| 122 |
* Implementation of hook_crmapi_contact_form
|
| 123 |
*
|
| 124 |
* @param int $uid
|
| 125 |
* @return mixed additions to $user->crmapi['crmapi_user']
|
| 126 |
*/
|
| 127 |
function civicrm_crmapi_contact_form_submit($form, &$form_state){
|
| 128 |
|
| 129 |
}
|
| 130 |
/**
|
| 131 |
* Implementation of hook_crmapi_user
|
| 132 |
*
|
| 133 |
* @param int $uid
|
| 134 |
* @return mixed additions to $user->crmapi['crmapi_user']
|
| 135 |
*/
|
| 136 |
function civicrm_crmapi_user($op, &$edit, &$account, $category = NULL){
|
| 137 |
civicrm_initialize(TRUE);
|
| 138 |
|
| 139 |
switch($op){
|
| 140 |
case "view":
|
| 141 |
return civicrm_crmapi_user_view($account);
|
| 142 |
break;
|
| 143 |
case 'update':
|
| 144 |
return civicrm_form_data($edit, $account, $category, false, false );
|
| 145 |
}
|
| 146 |
|
| 147 |
}
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
/**
|
| 152 |
* Implementation of hook_crmapi_user_contact_load
|
| 153 |
* Thanks, ECiviCRM!
|
| 154 |
**/
|
| 155 |
|
| 156 |
function civicrm_crmapi_user_contact_load($uid){
|
| 157 |
|
| 158 |
civicrm_initialize(TRUE);
|
| 159 |
// load contact from CiviCRM
|
| 160 |
if (is_a($civicrm_cid = crm_uf_get_match_id($uid), 'CRM_Core_error')) {
|
| 161 |
drupal_set_message('User ID not found in CiviCRM.', 'error');
|
| 162 |
return false;
|
| 163 |
};
|
| 164 |
|
| 165 |
$contact = civicrm_crmapi_contact_load($civicrm_cid, $uid);
|
| 166 |
|
| 167 |
return $contact;
|
| 168 |
}
|
| 169 |
|
| 170 |
|
| 171 |
/**
|
| 172 |
* implementation of hook_crmapi_contact_load();
|
| 173 |
*
|
| 174 |
* uid not required... added for reference.
|
| 175 |
**/
|
| 176 |
function civicrm_crmapi_contact_load($civicrm_cid, $uid=0){
|
| 177 |
|
| 178 |
civicrm_initialize(TRUE);
|
| 179 |
|
| 180 |
if (is_a($crm_contact = crm_get_contact(array('contact_id' => $civicrm_cid)), 'CRM_Core_error')) {
|
| 181 |
drupal_set_message('Contact not found in CiviCRM.', 'error');
|
| 182 |
return false;
|
| 183 |
};
|
| 184 |
|
| 185 |
$contact = array(
|
| 186 |
'uid' => $uid,
|
| 187 |
'crmapi_contact_id' => $civicrm_cid,
|
| 188 |
|
| 189 |
'first_name' => $crm_contact->contact_type_object->first_name,
|
| 190 |
'last_name' => $crm_contact->contact_type_object->last_name,
|
| 191 |
'middle_name' => $crm_contact->contact_type_object->middle_name,
|
| 192 |
'prefix' => $crm_contact->contact_type_object->prefix_id,
|
| 193 |
'suffix' => $crm_contact->contact_type_object->suffix_id,
|
| 194 |
'display_name' => $crm_contact->display_name,
|
| 195 |
'sort_name' => $crm_contact->sort_name,
|
| 196 |
'email' => $crm_contact->contact_type_object->location[1]->email[1]->email,
|
| 197 |
|
| 198 |
'address1' => $crm_contact->location[1]->address->street_address,
|
| 199 |
//'address2' => '',
|
| 200 |
'city' => $crm_contact->location[1]->address->city,
|
| 201 |
'state' => civicrm_transform_state($crm_contact->location[1]->address->state_province_id),
|
| 202 |
'country' => civicrm_transform_country($crm_contact->location[1]->address->country_id),
|
| 203 |
'postal_code' => $crm_contact->location[1]->address->postal_code,
|
| 204 |
|
| 205 |
'crmapi_contact_data' => $crm_contact,
|
| 206 |
/*
|
| 207 |
'phone_home' => ,
|
| 208 |
'phone_work' => 'Phone (Work)',
|
| 209 |
'phone_work_ext' => 'Phone (Work Extension)',
|
| 210 |
|
| 211 |
'organization' => 'Organization',
|
| 212 |
'occupation' => 'Occupation',
|
| 213 |
'employer' => 'Employer',
|
| 214 |
*/
|
| 215 |
);
|
| 216 |
|
| 217 |
/*
|
| 218 |
TODO: figure out how to handle multiple addresses per contact.
|
| 219 |
foreach($crm_contact->location as $civicrm_location){
|
| 220 |
//dpr($civicrm_location);
|
| 221 |
$contact['addresses'][] = array(
|
| 222 |
'address1' => $civicrm_location->address->street_address,
|
| 223 |
//'address2' => '',
|
| 224 |
'city' => $civicrm_location->address->city,
|
| 225 |
'state' => civicrm_transform_state($civicrm_location->address->state_province_id),
|
| 226 |
'country' => civicrm_transform_country($civicrm_location->address->country_id),
|
| 227 |
'postal_code' => $civicrm_location->address->postal_code,
|
| 228 |
);
|
| 229 |
}
|
| 230 |
*/
|
| 231 |
|
| 232 |
|
| 233 |
return $contact;
|
| 234 |
|
| 235 |
}
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
function civicrm_crmapi_user_view($user){
|
| 240 |
|
| 241 |
$contact = $user->crmapi['contact'];
|
| 242 |
|
| 243 |
//$output = theme("crmapi_contact", $contact);
|
| 244 |
|
| 245 |
$output = implode( "<br>",$user->crmapi['contact']);
|
| 246 |
|
| 247 |
$items[t('CiviCRM')]['civicrm'] = array(
|
| 248 |
'title' => t('Contact Information'),
|
| 249 |
'value' => $output,
|
| 250 |
'class' => 'crmapi_contact',
|
| 251 |
);
|
| 252 |
|
| 253 |
return $items;
|
| 254 |
|
| 255 |
}
|
| 256 |
|
| 257 |
|
| 258 |
/**
|
| 259 |
* Issues a db_query() against the CiviCRM database.
|
| 260 |
*/
|
| 261 |
function civicrm_db_query($query) {
|
| 262 |
global $db_url; // normally Drupal's DSN.
|
| 263 |
$original_url = $db_url; // we'll save it off temporarily.
|
| 264 |
$db_url = CIVICRM_DSN; // set it to the CiviCRM DSN.
|
| 265 |
db_set_active('civicrm'); // then make CiviCRM active.
|
| 266 |
$args = func_get_args(); // load all the args passed.
|
| 267 |
array_shift($args); // shove off the SQL query.
|
| 268 |
$result = db_query($query, $args); // make the query against CiviCRM.
|
| 269 |
$db_url = $original_url; // toggle back to Drupal's DSN.
|
| 270 |
db_set_active('default'); // and make that active again.
|
| 271 |
return $result; // return CiviCRM database result.
|
| 272 |
}
|
| 273 |
|
| 274 |
|
| 275 |
/**
|
| 276 |
* Given a CiviCRM country ID, return the name.
|
| 277 |
*/
|
| 278 |
function civicrm_transform_country($id = NULL) {
|
| 279 |
$results = civicrm_db_query('SELECT cc.id, cc.name FROM {civicrm_address} ca LEFT JOIN {civicrm_country} cc ON (ca.country_id = cc.id) GROUP BY cc.name;');
|
| 280 |
while ($result = db_fetch_object($results)) {
|
| 281 |
if ($result->id == $id) { return $result->name; }
|
| 282 |
}
|
| 283 |
}
|
| 284 |
|
| 285 |
|
| 286 |
/**
|
| 287 |
* Given a CiviCRM country name, return the ID.
|
| 288 |
*/
|
| 289 |
function civicrm_transform_country_name($name = NULL) {
|
| 290 |
$results = civicrm_db_query('SELECT cc.id, cc.name FROM {civicrm_address} ca LEFT JOIN {civicrm_country} cc ON (ca.country_id = cc.id) GROUP BY cc.name;');
|
| 291 |
while ($result = db_fetch_object($results)) {
|
| 292 |
if ($result->name == $name) { return $result->id; }
|
| 293 |
}
|
| 294 |
}
|
| 295 |
|
| 296 |
/**
|
| 297 |
* Given a CiviCRM state ID, return the two letter abbreviation.
|
| 298 |
*/
|
| 299 |
function civicrm_transform_state($id = NULL) {
|
| 300 |
|
| 301 |
$states = civicrm_states_array();
|
| 302 |
$states = array_flip($states); return $states[$id];
|
| 303 |
}
|
| 304 |
|
| 305 |
|
| 306 |
function civicrm_states_array($state_abbr = NULL) {
|
| 307 |
$states = array(
|
| 308 |
'AL' => 1000, 'AK' => 1001, 'AZ' => 1002, 'AR' => 1003,
|
| 309 |
'CA' => 1004, 'CO' => 1005, 'CT' => 1006, 'DC' => 1050,
|
| 310 |
'DE' => 1007, 'FL' => 1008, 'GA' => 1009, 'HI' => 1010,
|
| 311 |
'ID' => 1011, 'IL' => 1012, 'IN' => 1013, 'IA' => 1014,
|
| 312 |
'KS' => 1015, 'KY' => 1016, 'LA' => 1017, 'ME' => 1018,
|
| 313 |
'MD' => 1019, 'MA' => 1020, 'MI' => 1021, 'MN' => 1022,
|
| 314 |
'MS' => 1023, 'MO' => 1024, 'MT' => 1025, 'NE' => 1026,
|
| 315 |
'NV' => 1027, 'NH' => 1028, 'NJ' => 1029, 'NM' => 1030,
|
| 316 |
'NY' => 1031, 'NC' => 1032, 'ND' => 1033, 'OH' => 1034,
|
| 317 |
'OK' => 1035, 'OR' => 1036, 'PA' => 1037, 'RI' => 1038,
|
| 318 |
'SC' => 1039, 'SD' => 1040, 'TN' => 1041, 'TX' => 1042,
|
| 319 |
'UT' => 1043, 'VT' => 1044, 'VA' => 1045, 'WA' => 1046,
|
| 320 |
'WV' => 1047, 'WI' => 1048, 'WY' => 1049,
|
| 321 |
);
|
| 322 |
if ($state_abbr) {
|
| 323 |
return $states[$state_abbr];
|
| 324 |
} else {
|
| 325 |
return $states;
|
| 326 |
}
|
| 327 |
}
|