-
Notifications
You must be signed in to change notification settings - Fork 0
/
civicrm_taxonomy_sync.module
executable file
·358 lines (320 loc) · 11.1 KB
/
civicrm_taxonomy_sync.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
// $Id$
/**
* @file
* This module automatically create a tag in civicrm for each forum.
*/
/**
* Implements hook_taxonomy
*/
function civicrm_taxonomy_sync_taxonomy($op, $type, $term) {
if( $type != 'term') {
return;
}
if ( ! civicrm_initialize( ) ) {
return;
}
require_once 'civicrm.config.php';
require_once 'CRM/Core/Config.php';
require_once 'api/v2/Tag.php';
$tid = $term['tid'];
switch ($op) {
case 'insert':
case 'update':
$vocabulary_row = civicrm_taxonomy_sync_get_vocabulary();
// Validating that it is a forum term
if ($vocabulary_row->vid == $term['vid']) {
//Create the new civitag if it does not exist
//Finding the parent term
$parentid = $term['parent'][0];
$term_row = civicrm_taxonomy_sync_relation($parentid);
if (is_object($term_row)) {
$params = array("name" => $term['name'], "description" => $term['description'],
"parent_id" => $term_row->civicrm_tagid, "used_for" => "civicrm_contact");
if ($op == 'update') {
$tag = civicrm_tag_get($params);
if (!$tag['is_error']) {
$params['tag']= $tag['id'];
}
}
$result = civicrm_tag_create ($params);
if (civicrm_error($result)) {
drupal_set_message(t("ERROR creating in civicrm %tagname: %message", array('%tagname' => $term['name'], '%message' => $result['error_message' ])), "error");
}else {
// Create the relation row between drupal and civicrm tag
civicrm_taxonomy_sync_create_relation($tid, $result['tag_id']);
drupal_set_message(t("Civicrm tag %tagname was created succesfully."), array('%tagname' => $term['name']), "status");
}
}
else {
drupal_set_message(t("Parent tag relation couldn't be found. Parent tag id: %parentid", array('%parentid' => $parentid)), "error");
}
}
break;
case 'delete':
$term_row = civicrm_taxonomy_sync_relation($tid);
//Delete the civitag if it does exist
if (is_object($term_row)) {
$civitagid = $term_row->civicrm_tagid;
$params = array(
'tag_id' => $civitagid
);
$result = civicrm_tag_delete( $params );
if (civicrm_error($result)) {
drupal_set_message(t("Civicrm tag couldn't be removed."), "error");
}else {
//Removing the relation in relation table
civicrm_taxonomy_sync_remove_relation($tid);
drupal_set_message(t("Civicrm tag was removed succesfully."), "status");
}
}
break;
default:
break;
}
}
/*
* Implements hook_comment()
*/
function civicrm_taxonomy_sync_comment($node_array, $op) {
if ( ! civicrm_initialize( ) ) {
return;
}
require_once 'civicrm.config.php';
require_once 'CRM/Core/Config.php';
require_once 'api/v2/EntityTag.php';
require_once 'api/v2/UFGroup.php';
if ($op == 'insert' || $op == 'update') {
$nid = $node_array['nid'];
$node = node_load( $nid );
$node_type = $node->type;
$civicrm_forum_sync_nodetype = array("forum");
if ( in_array($node_type, $civicrm_forum_sync_nodetype)) {
$query = "SELECT * FROM {term_node} WHERE nid = %d";
$term_node = db_fetch_object( db_query($query,$node->nid));
if(is_object($term_node)) {
$term_row = civicrm_taxonomy_sync_relation($term_node->tid);
if (is_object($term_row)) {
if (!$term_row->civicrm_tagid) {
drupal_set_message(t("Tag sync avoided."), "status");
return;
}
global $user;
$user_id = $user->uid;
$civi_contact_id = civicrm_uf_match_id_get ($user_id);
if ( !$civi_contact_id ) {
drupal_set_message(t("There is no Civicrm contact for the given user."), "status");
return;
}
$civicrm_tag_id = $term_row->civicrm_tagid;
$entityParams = array('tag_id' => $civicrm_tag_id,
'contact_id' => $user_id
);
// now assign the created contact to the tag searched.
$entityTag = civicrm_entity_tag_add( $entityParams);
}
}
}
}
}
/**
* Creates the relation between civi tag and drupal term
*/
function civicrm_taxonomy_sync_create_relation($tid, $civitag_id) {
$query = "SELECT * FROM {civicrm_taxonomy_sync} WHERE tid = %d";
$term_row = db_fetch_object( db_query($query,$tid));
if( !is_object($term_row)) {
$query = "INSERT INTO {civicrm_taxonomy_sync} (tid, civicrm_tagid) VALUES (%d, %d)";
$term_row = db_query($query,$tid,$civitag_id);
}
}
/**
* Removes the relation between civi tag and drupal term
*
*
*/
function civicrm_taxonomy_sync_remove_relation($tid) {
$query = "SELECT * FROM {civicrm_taxonomy_sync} WHERE tid = %d";
$term_row = db_fetch_object( db_query($query,$tid));
if( is_object($term_row)) {
$query = "DELETE FROM {civicrm_taxonomy_sync} WHERE tid = %d";
$term_row = db_query($query,$tid);
}
}
/**
* Returns the relation between civitags and drupal forum vocabulary
*
*
*/
function civicrm_taxonomy_sync_relation($tid) {
$query = "SELECT * FROM {civicrm_taxonomy_sync} WHERE tid = %d";
$term_row = db_fetch_object( db_query($query,$tid));
return $term_row;
}
/**
* Implements hook_nodeapi
*/
function civicrm_taxonomy_sync_nodeapi( &$node, $op, $a3 = null, $a4 = null ) {
// check if the node if of type og
$content_types_array = array();
$civicrm_forum_sync_nodetype = array("forum");
if ( !in_array($node->type, $civicrm_forum_sync_nodetype)) {
drupal_set_message(t("No forum content type"), "status");
return;
}
if ( ! civicrm_initialize( ) ) {
return;
}
switch ( $op ) {
case 'insert':
case 'update':
require_once 'civicrm.config.php';
require_once 'CRM/Core/Config.php';
require_once 'api/v2/EntityTag.php';
require_once 'api/v2/UFGroup.php';
//Find the term related with this node
$query = "SELECT * FROM {term_node} WHERE nid = %d";
$term_node = db_fetch_object( db_query($query,$node->nid));
if(is_object($term_node)) {
//Find the tag relation
$term_row = civicrm_taxonomy_sync_relation($term_node->tid);
if (is_object($term_row)) {
if (!$term_row->civicrm_tagid) {
drupal_set_message(t("Tag sync avoided."), "status");
return;
}
global $user;
$user_id = $user->uid;
$civi_contact_id = civicrm_uf_match_id_get ($user_id);
if ( !$civi_contact_id ) {
drupal_set_message(t("There is no Civicrm contact for the given user."), "status");
return;
}
$civicrm_tag_id = $term_row->civicrm_tagid;
$entityParams = array('tag_id' => $civicrm_tag_id,
'contact_id' => $user_id
);
// now assign the created contact to the tag searched.
$entityTag = civicrm_entity_tag_add( $entityParams);
}
else {
drupal_set_message(t('The forum vocabulary term does not exist as a term through the admin interface, please add it.'),'error');
}
}
case 'delete':
break;
default:
break;
}
}
/**
* Implements hook_menu
*/
function civicrm_taxonomy_sync_menu() {
$items['admin/settings/civicrm-forum-sync'] = array(
'title' => t('Civicrm Forums Sync'),
'description' => t('Interface to import forums tag to civicrm tags.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('civicrm_taxonomy_sync_import_forums_form'),
'access arguments' => array('administer civicrm_taxonomy_sync'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_perm().
*/
function civicrm_taxonomy_sync_perm() {
return array('administer civicrm_taxonomy_sync');
}
/*
*
* Submit action to create the civitags from the forum terms
*
*/
function civicrm_taxonomy_sync_import_forums_form_submit($form, $form_state) {
if ( ! civicrm_initialize( ) ) {
return;
}
require_once 'civicrm.config.php';
require_once 'CRM/Core/Config.php';
require_once 'api/v2/Tag.php';
$action = $form_state['clicked_button']['#parents'][0];
if ($action == 'remove') {
$query = "TRUNCATE {civicrm_taxonomy_sync}";
db_query($query);
}
else {
$tags_array = civicrm_taxonomy_sync_get_forum_hierarchy_array();
foreach ($tags_array as $term_object) {
$tid = $term_object->tid;
$term = taxonomy_get_term($tid);
$term_row = civicrm_taxonomy_sync_relation($term_object->parent);
$parentid = NULL;
if (!is_object($term_row)) {
$parentid = NULL;
}
else {
$parentid = $term_row->civicrm_tagid;
}
$params = array("name" => trim($term->name), "description" => $term->description,
"parent_id" => $parentid, "used_for" => "civicrm_contact");
//Checking if tag already exist
$params_get = array("name" => trim($term->name));
$tag = civicrm_tag_get($params_get);
if ($tag['is_error']) {
$result = civicrm_tag_create ($params);
if (civicrm_error($result)) {
drupal_set_message(t("ERROR creating tag %tagname: %message", array('%tagname' => $term->name,'%message' => $result['error_message'] )), "error");
}else {
// Create the relation row between drupal and civicrm tag
civicrm_taxonomy_sync_create_relation($tid, $result['tag_id']);
drupal_set_message(t("Civicrm tag %tagname was created succesfully.", array('%tagname' => $term->name) ), "status");
}
}else {
// Create the relation row between drupal and civicrm tag
civicrm_taxonomy_sync_create_relation($tid, $tag['id']);
drupal_set_message(t("Civicrm tag %tagname was created succesfully.", array('%tagname' => $term->name) ), "status");
}
}
}
}
/*
*
* Form for Init civitag and forums sync
*
*/
function civicrm_taxonomy_sync_import_forums_form() {
$form['submit'] = array('#type' => 'submit',
'#value' => t('Import'),
);
$form['remove'] = array('#type' => 'submit',
'#value' => 'Remove old data',
'#description' => "Remove old data first when reimporting tags"
);
return $form;
}
/*
* Finds the forum vocabulary id in the site
*/
function civicrm_taxonomy_sync_get_vocabulary() {
$query = "SELECT * FROM {vocabulary} WHERE name = 'Forums'";
$vocabulary_row = db_fetch_object( db_query($query));
return $vocabulary_row;
}
/*
* Return an array with the drupal forum hierarchy
*/
function civicrm_taxonomy_sync_get_forum_hierarchy_array() {
//Finding the forums vocabulary id
$vocabulary_row = civicrm_taxonomy_sync_get_vocabulary();
$forum_vid = $vocabulary_row->vid;
// Extractin the hierarchy just for forum items
$query = "SELECT * FROM {term_hierarchy} WHERE {term_hierarchy}.tid IN ( SELECT tid FROM {term_data} WHERE {term_data}.vid = %d ) ORDER BY parent ASC";
$forum_hierarchy = db_query($query,$forum_vid);
$forum_hierarchy_array = array();
while ( $hierarchy = db_fetch_object($forum_hierarchy)) {
$forum_hierarchy_array[] = $hierarchy;
}
return $forum_hierarchy_array;
}