-
Notifications
You must be signed in to change notification settings - Fork 6
/
bpi.push.inc
516 lines (454 loc) · 13 KB
/
bpi.push.inc
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<?php
/**
* @file
* Syndication logic here.
*/
/**
* Start a content push session.
*
* @see bpi_menu()
*
* @param string $type
* Request type, simple http or ajax.
* @param int $nid
* Node id, whose content is to be pushed.
*
* @return Array/string
* Ajax command set in case of ajax request, push content page
* markup otherwise.
*/
function bpi_push_action($type, $nid) {
$is_ajax = ($type == 'ajax') ? TRUE : FALSE;
if (empty($nid)) {
return;
}
if (!bpi_push_available($nid)) {
drupal_access_denied();
drupal_exit();
}
$push_form = drupal_get_form('bpi_push_form', $nid);
if ($is_ajax) {
$commands = array();
$commands[] = ajax_command_ding_popup(
'bpi-push',
t('Push to BPI'),
drupal_render($push_form),
array('refresh' => TRUE)
);
return array('#type' => 'ajax', '#commands' => $commands);
}
else {
return $push_form;
}
}
/**
* BPI http action, with no ajax functionality.
*
* @param int $nid
* The node id, waiting to be pushed.
*
* @return array
* Form structure.
*/
function bpi_http_push_action($nid) {
return drupal_get_form('bpi_http_push_action_form', $nid);
}
/**
* BPI http push form, used in save and push on node/edit page.
*
* @param array $form
* Form structure.
* @param array $form_state
* Form state values.
* @param int $nid
* The node id, waiting to be pushed.
*
* @return array
* Form structure.
*
* @ingroup forms
*/
function bpi_http_push_action_form($form, &$form_state, $nid) {
$node_title = '';
if (!empty($nid)) {
$node = node_load($nid);
$node_title = $node->title;
}
$form['bpi_push_nid'] = array(
'#type' => 'hidden',
'#value' => isset($nid) ? $nid : 0,
);
$dictionaries = bpi_get_bpi_dictionaries();
$categories = array_values($dictionaries['category']);
$form['bpi_push_category'] = array(
'#type' => 'select',
'#title' => t('Category'),
'#options' => array_combine($categories, $categories),
'#default_value' => ',',
);
$audiences = array_values($dictionaries['audience']);
$form['bpi_push_audience'] = array(
'#type' => 'select',
'#title' => t('Audience'),
'#options' => array_combine($audiences, $audiences),
'#default_value' => ',',
);
$form['bpi_push_images'] = array(
'#type' => 'checkbox',
'#title' => t('Push with images'),
);
$form['bpi_push_ccl'] = array(
'#type' => 'checkbox',
'#title' => t('I want be anonymous'),
'#default_value' => FALSE,
);
$form['bpi_push_editable'] = array(
'#type' => 'checkbox',
'#title' => t('Editable'),
'#default_value' => '1',
);
return confirm_form(
$form,
t('Are you sure you want to push content %title to BPI well?', array('%title' => $node_title)),
'admin/content'
);
}
/**
* Submit handler for the push node form on node/edit page.
*
* @see bpi_push_form()
*
* @param array $form
* Form structure.
* @param array $form_state
* Form state values.
*
* @ingroup forms
*/
function bpi_http_push_action_form_submit($form, &$form_state) {
$nid = $form_state['input']['bpi_push_nid'];
$category = $form_state['input']['bpi_push_category'];
$audience = $form_state['input']['bpi_push_audience'];
$with_images = $form_state['input']['bpi_push_images'];
$authorship = $form_state['input']['bpi_push_ccl'];
$editable = $form_state['input']['bpi_push_editable'];
if (empty($nid)) {
return;
}
$node = node_load($nid);
$bpi_content = bpi_convert_to_bpi($node, $category, $audience, $with_images, $authorship, $editable);
try {
$bpi = bpi_client_instance();
$push_result = $bpi->push($bpi_content)->getProperties();
if (!empty($push_result['id'])) {
db_merge('bpi_syndicated')
->fields(array(
'nid' => $nid,
'bid' => $push_result['id'],
'status' => BPI_PUSHED,
'timestamp' => time(),
))
->condition('nid', $nid, '=')
->execute();
drupal_set_message(t('Node %title was successfully pushed to BPI well.', array('%title' => $node->title)));
}
drupal_goto('admin/bpi');
}
catch (Exception $e) {
watchdog_exception('bpi', $e);
$msg = t('Error occurred when pushing %title to BPI well. Check reports for more information.', array('%title' => $node->title));
drupal_set_message($msg, 'error');
}
}
/**
* BPI push form.
*
* @param array $form
* Form structure.
* @param array $form_state
* Form state values.
* @param int $nid
* The node id, waiting to be pushed.
*
* @return array
* Form structure.
*
* @ingroup forms
*/
function bpi_push_form($form, $form_state, $nid) {
$node_title = '';
if (!empty($nid)) {
$node = node_load($nid);
$node_title = $node->title;
}
$form['bpi_push_nid'] = array(
'#type' => 'hidden',
'#value' => isset($nid) ? $nid : 0,
);
$dictionaries = bpi_get_bpi_dictionaries();
$categories = array_values($dictionaries['category']);
$form['bpi_push_category'] = array(
'#type' => 'select',
'#title' => t('Category'),
'#options' => array_combine($categories, $categories),
'#default_value' => ',',
);
$audiences = array_values($dictionaries['audience']);
$form['bpi_push_audience'] = array(
'#type' => 'select',
'#title' => t('Audience'),
'#options' => array_combine($audiences, $audiences),
'#default_value' => ',',
);
$form['bpi_push_images'] = array(
'#type' => 'checkbox',
'#title' => t('Push with images'),
);
$form['bpi_push_ccl'] = array(
'#type' => 'checkbox',
'#title' => t('I want be anonymous'),
'#default_value' => FALSE,
);
$form['bpi_push_editable'] = array(
'#type' => 'checkbox',
'#title' => t('Editable'),
'#default_value' => '1',
);
return confirm_form(
$form,
t(
'Are you sure u want to push content <strong>%title</strong> to BPI well?',
array('%title' => $node_title)
),
'admin/content'
);
}
/**
* Submit handler for the push node form.
*
* @see bpi_push_form()
*
* @param array $form
* Form structure.
* @param array $form_state
* Form state values.
*
* @ingroup forms
*/
function bpi_push_form_submit($form, &$form_state) {
$nid = $form_state['input']['bpi_push_nid'];
$category = $form_state['input']['bpi_push_category'];
$audience = $form_state['input']['bpi_push_audience'];
$with_images = $form_state['input']['bpi_push_images'];
$authorship = $form_state['input']['bpi_push_ccl'];
$editable = $form_state['input']['bpi_push_editable'];
if (empty($nid)) {
return;
}
$node = node_load($nid);
$bpi_content = bpi_convert_to_bpi($node, $category, $audience, $with_images, $authorship, $editable);
try {
$bpi = bpi_client_instance();
$push_result = $bpi->push($bpi_content)->getProperties();
if (!empty($push_result['id'])) {
db_merge('bpi_syndicated')
->fields(array(
'nid' => $nid,
'bid' => $push_result['id'],
'status' => BPI_PUSHED,
'timestamp' => time(),
))
->condition('nid', $nid, '=')
->execute();
drupal_set_message(t('Node %title was successuflly pushed to BPI well.', array('%title' => $node->title)));
}
}
catch (Exception $e) {
watchdog_exception('bpi', $e);
$msg = t('Error occured when pushing %title to BPI well. Check reports for more information.', array('%title' => $node->title));
drupal_set_message($msg, 'error');
}
}
/**
* AJAX callback for the push button in bpi push node form.
*
* @see bpi_push_form()
*
* @param array $form
* Form structure.
* @param array $form_state
* Form state values.
* @return array
* A set of ajax commands.
*
* @ingroup forms
*/
function bpi_push_action_ajax_callback($form, &$form_state) {
$response = array(
'#type' => 'ajax',
'#commands' => array()
);
$html = theme('status_messages');
$response['#commands'][] = ajax_command_ding_popup(
'bpi-push',
t('Push to BPI'),
$html
);
return $response;
}
/**
* Convert node object to bpi-related array structure,
* suitable for pushing to the well.
*
* @param stdClass $node
* Node object being processed.
* @param string $category
* Selected category at the BPI side.
* @param string $audience
* Selected audience at the BPI side.
* @param bool $with_images
* Include images or not.
* @param bool $authorship
* Include author name or not.
* @param int $editable
* 1 - to mark as editable, 0 - not editable.
*
* @return array
* An array of node values, used by the BPI web service.
*
* @todo Add a hook allowing changing the values before they are sent to BPI.
* @todo Split this function into smaller parts (ex: images, texts).
*/
function bpi_convert_to_bpi($node, $category, $audience, $with_images = FALSE, $authorship = FALSE, $editable = 1) {
$bpi_content = array();
$bpi_content['agency_id'] = variable_get('bpi_agency_id', '');
$bpi_content['local_id'] = $node->nid;
$bpi_content['bpi_id'] = isset($node->bpi_id) ? $node->bpi_id : NULL;
$user = user_load($node->uid);
$bpi_content['firstname'] = $user->name;
$bpi_content['lastname'] = '';
$bpi_content['title'] = $node->title;
$teaser_map = variable_get('bpi_field_teaser', '');
$body_map = variable_get('bpi_field_body', '');
$materials_map = variable_get('bpi_field_materials', '');
$materials_map = field_view_field('node', $node, $materials_map);
$teaser_field = field_view_field('node', $node, $teaser_map);
$body_field = field_view_field('node', $node, $body_map);
$teaser = '';
$body = '';
// Whether the field is a textarea with summary, fetch the summary, if not -
// fetch it's safe value.
if (!empty($teaser_field) && isset($teaser_field['#items'][0]['safe_summary'])) {
$teaser = $teaser_field['#items'][0]['safe_summary'];
}
elseif (isset($teaser_field['#items'][0]['safe_value'])) {
$teaser = $teaser_field['#items'][0]['safe_value'];
}
$materials_drupal = array();
foreach ($materials_map as $key => $value) {
if (is_numeric($key)) {
$record = $value['#object']->getRecord();
$ac_identifier_array = array_shift(
array_values($record['ac:identifier'])
);
$parts = explode('|', $ac_identifier_array[0]);
$materials_drupal[] = $parts[1] . ':' . $parts[0];
}
}
if (!empty($body_field) && isset($body_field['#items'][0]['safe_value'])) {
$body = $body_field['#items'][0]['safe_value'];
}
// Empty the teaser, if body and teaser are mapped to same fields
// and the values are identical.
if ($teaser_map == $body_map && $teaser == $body) {
$teaser = '';
}
$bpi_content['body'] = html_entity_decode($body);
$bpi_content['teaser'] = html_entity_decode($teaser);
$dt = new DateTime();
$dt->setTimestamp($node->changed);
$bpi_content['creation'] = $dt->format(DateTime::W3C);
$bpi_content['type'] = $node->type;
$bpi_content['category'] = $category;
$bpi_content['audience'] = $audience;
$bpi_content['related_materials'] = $materials_drupal;
$bpi_content['editable'] = (int) $editable;
$bpi_content['authorship'] = ($authorship) ? FALSE : TRUE;
$bpi_content['images'] = array();
if ($with_images) {
$image_fields = bpi_fetch_image_fields($node->type);
if (!empty($image_fields)) {
foreach ($image_fields as $field_name) {
$field_value = field_view_field('node', $node, $field_name);
if (!empty($field_value['#items'][0]['uri'])) {
$file_url = file_create_url($field_value['#items'][0]['uri']);
// Image pseudo-check.
if (@getimagesize($file_url)) {
$bpi_content['images'][] = array(
'path' => $file_url,
'alt' => '',
'title' => '',
);
}
}
}
}
}
else {
$bpi_content['body'] = preg_replace(
'~(<p>)?<img.+?/>(</p>)?~is',
'',
$bpi_content['body']
);
}
watchdog(
'bpi',
'Transformed node to BPI content: !content',
array(
'!content' => '<pre>' . htmlspecialchars(
print_r($bpi_content, TRUE)
) . '</pre>'
),
WATCHDOG_DEBUG
);
return $bpi_content;
}
/**
* Fetch image field types.
*
* @param string $bundle
* Node type.
* @return array
* Array of field names which are actually image fields.
*/
function bpi_fetch_image_fields($bundle) {
$potential_image_fields = array('image_image', 'media_generic');
$field_instances = field_info_instances('node', (string)$bundle);
$image_fields = array();
if (is_array($field_instances)) {
foreach ($field_instances as $key => $instance) {
if (in_array($instance['widget']['type'], $potential_image_fields)) {
$image_fields[] = $key;
}
}
}
return $image_fields;
}
/**
* Fetch dictionaries from BPI ws.
*
* @return array
* Nested array of dictionaries, keyed by dictionary
* type.
*/
function bpi_get_bpi_dictionaries() {
$dictionaries = array('category' => array(), 'audience' => array());
try {
$bpi = bpi_client_instance();
$dictionaries = $bpi->getDictionaries();
}
catch (Exception $e) {
watchdog_exception('bpi', $e);
}
return $dictionaries;
}