-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkkrabb_module_rest.inc
1170 lines (1082 loc) · 38 KB
/
kkrabb_module_rest.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Get metadata for Kolkrabbinn
* content type
*
* @return Array
*/
function kkrabb_type_info(){
$array = array();
foreach (array_keys( field_info_instances("node",'kkrabb_event')) as $value) {
$struct = field_info_field($value);
$type = null;
switch ($struct['type']) {
case 'image':
$type = (object)array(
'type' => 'image',
'length' => null
);
break;
case 'taxonomy_term_reference':
case 'entityreference':
$type = (object)array(
'type' => 'foreign-key',
'length' => null
);
break;
case 'text':
case 'text_long':
$type = (object)array(
'type' => $struct['columns']['value']['type'],
'length' => (isset($struct['columns']['value']['length']))
? (int)$struct['columns']['value']['length']
: null
);
break;
case 'number_integer':
case 'number_float':
$type = (object)array(
'type' => $struct['columns']['value']['type'],
'length' => null,
);
break;
case 'datestamp':
$type = (object)array(
'type' => 'datetime',
'length' => null,
);
break;
case 'list_boolean':
$type = (object)array(
'type' => 'array:bool',
'length' => null,
);
break;
case 'list_integer':
$type = (object)array(
'type' => 'array:int',
'length' => null,
);
break;
case 'list_text':
$type = (object)array(
'type' => 'array:text',
'length' => null,
);
break;
default:
$type = (object)array(
'type' => null,
'length' => null
);
break;
}
$array[] = (object)array(
'name' => $struct['field_name'],
'type' => $type ,
//'columns' => $struct['columns'],
);
}
return $array;
}
/**
* Get metadata for vocabulary / taxonomy
*
* @return Array
*/
function kkrabb_taxonomy_info(){
$array = array();
foreach (array_keys( field_info_instances("node",'kkrabb_event')) as $value){
$struct = field_info_field($value);
if($struct['type']=='taxonomy_term_reference'){
//print_r($struct);
$taxid = taxonomy_vocabulary_machine_name_load($struct['settings']['allowed_values'][0]['vocabulary']);
$array[] = (object)array(
'name' => $taxid->name,
'machine_name' => $taxid->machine_name,
'field_name' => $struct['field_name'],
'description' => $taxid->description,
'hierarchy' => $taxid->hierarchy,
'terms' => array_map(function($n){
//return $n;
return (object)array(
'name' => $n->name,
'description' => $n->description,
'language' => $n->language,
'weight' => (int)$n->weight,
'depth' => (int)$n->depth
);
}, taxonomy_get_tree($taxid->vid))
);
}
}
return $array ;
}
/**
* Get the event prefix that this Kolkrabbi uses
*
* @return string
*/
function kkrabb_get_prefix(){
return variable_get("kkrab_module_identifier_prefix");
}
/**
* Return an object array containing informations
* about providers that this Kolkrabbi is subscribing to.
*
* @return array
*/
function kkrabb_get_providers(){
$out = array();
$providers = db_query('select * from `kkrabb_module_service`')->fetchAll();
foreach ($providers as $value) {
$out[] = array(
'name' => $value->name,
'url' => $value->url,
'prefix' => $value->prefix
);
}
return $out;
}
/**
* Root for REST SERVER.
*
* GET: will print out dicoverable options
* POST: not supported
* PUT: not supported
* DELETE: not supported
* OPTIONS: sends needed HTTP headers
*
*/
function rest_server(){
//print_r(field_info_instances('kkrabb_event'));die();
switch ( strtoupper($_SERVER['REQUEST_METHOD'])) {
case 'GET':
header("Access-Control-Allow-Origin: *");
$def_lang = language_default();
_util_rest_authentication();
$data = _util_rest_response_template();
$data->code = 200;
$data->msg = "Success";
$data->message = "";
$data->data = (object)array(
'urls' => array(
(object)array(
'url' => "http://".$_SERVER['HTTP_HOST'].'/'.kkrabb_module_api_url_prefix,
'description' => 'This request'
),
(object)array(
'url' => "http://".$_SERVER['HTTP_HOST'].'/'.kkrabb_module_api_url_prefix."/categories",
'description' => 'Get all categories and their terms'
),
(object)array(
'url' => "http://".$_SERVER['HTTP_HOST'].'/'.kkrabb_module_api_url_prefix."/categories/%",
'description' => 'Get a category and it\'s terms. Substitute % for identifier'
),
(object)array(
'url' =>"http://".$_SERVER['HTTP_HOST'].'/'.kkrabb_module_api_url_prefix."/events",
'description' => 'Get events'
),
(object)array(
'url' => "http://".$_SERVER['HTTP_HOST'].'/'.kkrabb_module_api_url_prefix."/events/%",
'description' => 'Get an event. Substitute % for identifier'
)
)
);
$data->data->languages = (object)array('default'=>(object)array(
'code' => $def_lang->language,
'name' => $def_lang->name,
'native' => $def_lang->native,
),'list'=>array());
foreach (language_list() as $item) {
$data->data->languages->list[] = array(
'code' => $item->language,
'name' => $item->name,
'native' => $item->native,
);
}
$data->data->structure = kkrabb_type_info();
$data->data->taxonomies = kkrabb_taxonomy_info();
$data->data->prefix = kkrabb_get_prefix();
$data->data->provides = kkrabb_get_providers();
_util_rest_response($data,200);
break;
case 'OPTIONS':
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: x-kkrab-key, x-kkrab-signature');
break;
case 'POST':
case 'PUT':
case 'DELETE':
default:
$data = _util_rest_response_template();
$data->code = 501;
_util_rest_response($data,501);
break;
}
}
/**
* Request for categories
*
* GET: will print out dicoverable options
* POST: not supported
* PUT: not supported
* DELETE: not supported
* OPTIONS: sends needed HTTP headers
*
*
*/
function rest_server_categories(){
switch ( strtoupper($_SERVER['REQUEST_METHOD'])) {
//GET
// get all categories
case 'GET':
header("Access-Control-Allow-Origin: *");
//print_r(taxonomy_get_vocabularies('kkrabb_event'));
//print_r(taxonomy_get_tree(5));
//return;
_util_rest_authentication();
$data = _util_rest_response_template();
$data->code = 200;
$data->msg = "Ok";
$data->message = "All categories";
$data->data = array();
$vocabulary_ids = array_map(function($n){
return $n->vid;
},taxonomy_get_vocabularies('kkrabb_event'));
foreach ($vocabulary_ids as $value) {
$vocabulary = taxonomy_vocabulary_load($value);
$obj = (object)array(
'name' => $vocabulary->name,
'machine_name' => $vocabulary->machine_name,
'description' => $vocabulary->description,
'language' => $vocabulary->language,
'weight' => $vocabulary->weight,
);
$obj->terms = array();
foreach (taxonomy_get_tree($vocabulary->vid) as $value) {
$obj->terms[] = (object)array(
'name' => $value->name,
'description' => $value->description,
'language' => ($value->language=='und')? null : $value->language,
'weight' => $value->weight,
//'value' => $value
);
}
$data->data[] = $obj;
}
_util_rest_response($data,200);
break;
//OPTIONS
// send headers to allow for JavaScript clients
case 'OPTIONS':
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: x-kkrab-key, x-kkrab-signature');
break;
case 'POST':
case 'PUT':
case 'DELETE':
default:
$data = _util_rest_response_template();
$data->code = 501;
$data->msg = "Not supported";
$data->message = "Method {$_SERVER['REQUEST_METHOD']} not supported";
_util_rest_response($data,501);
break;
}
}
/**
* Request for one category item
*
* GET: will print out category and terms
* POST: not supported
* PUT: not supported
* DELETE: not supported
* OPTIONS: not supported
*
*/
function rest_server_categories_item($id){
switch ( strtoupper($_SERVER['REQUEST_METHOD'])) {
case 'GET':
header("Access-Control-Allow-Origin: *");
_util_rest_authentication();
$data = _util_rest_response_template();
if( ($item = taxonomy_vocabulary_machine_name_load($id))!=false ){
$data->code = 200;
$data->msg = "Ok";
$data->message = "Category [{$item->name}] and terms";
$vocabulary = taxonomy_vocabulary_load($item->vid);
$data->data = (object)array(
'name' => $vocabulary->name,
'machine_name' => $vocabulary->machine_name,
'description' => $vocabulary->description,
'weight' => $vocabulary->weight,
);
$data->data->terms = array();
foreach (taxonomy_get_tree($vocabulary->vid) as $value) {
$data->data->terms[] = (object)array(
'name' => $value->name,
'description' => $value->description,
'weight' => $value->weight,
);
}
_util_rest_response($data,200);
}else{
$data->code = 404;
$data->msg = "Not found";
$data->message = "Category [{$id}] not found";
_util_rest_response($data,404);
}
break;
case 'OPTIONS':
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: x-kkrab-key, x-kkrab-signature');
break;
case 'POST':
case 'PUT':
case 'DELETE':
default:
break;
}
}
/**
* Request events
*
* GET: will print out events
* POST: not supported
* PUT: not supported
* DELETE: not supported
* OPTIONS: not supported
*
*/
function rest_server_events(){
switch ( strtoupper($_SERVER['REQUEST_METHOD'])) {
case 'GET':
_util_rest_authentication();
header("Access-Control-Allow-Origin: *");
$return = _util_rest_response_template();
$return->code = 200;
$return->msg = "Ok";
$return->message = "All events";
$events = kkrabb_module_events_cache_get();
$return->taxonomies = kkrabb_taxonomy_info();
#$data->data->taxonomies = kkrabb_taxonomy_info();
$return->data = array();
foreach ($events as $item) {
$return->data[] = $item;
}
_util_rest_response($return,200);
break;
case 'OPTIONS':
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: x-kkrab-key, x-kkrab-signature');
break;
case 'POST':
break;
case 'PUT':
break;
case 'DELETE':
break;
}
}
/**
* Request one event
*
* GET: will print out one event
* POST: not supported
* PUT: not supported
* DELETE: not supported
* OPTIONS: not supported
*
*/
function rest_server_events_item($id){
switch ( strtoupper($_SERVER['REQUEST_METHOD'])) {
case 'GET':
_util_rest_authentication();
header("Access-Control-Allow-Origin: *");
$data = _util_rest_response_template();
/*
$events_query = new EntityFieldQuery;
$events_query = $events_query
->entityCondition('entity_type', 'node')
->propertyCondition('type', 'kkrabb_event')
->propertyCondition('status', 1, '=')
->fieldCondition('field_kkrabb_event_id','value',$id,'=');;
$events_query_result = $events_query->execute();
$res = array_pop($events_query_result['node']);
$node = node_load($res->nid);
$node_array = (array)$node;
foreach ($node_array as $key => $value) {
$info = field_info_field($key);
if( isset($info['type']) && $info['type'] == 'taxonomy_term_reference' ){
//print_r($info);
}
}
//return;
*/
if( ($node=kkrabb_module_event_cache_get($id)) != null ){
$data->code = 200;
$data->msg = "Ok";
$data->message = "Return event [{$id}]";
$data->data = $node;
_util_rest_response( $data ,200);
}else{
$data->code = 404;
$data->msg = "Not found";
$data->message = "Event with identifier [{$id}] not found";
_util_rest_response( $data ,404);
}
break;
case 'OPTIONS':
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: x-kkrab-key, x-kkrab-signature');
break;
case 'POST':
break;
case 'PUT':
break;
case 'DELETE':
break;
}
}
/**
* Authinticate user.
*
* This is an end-point function, i.e.
* if the user is not authenticated, it
* will send HTTP headers and response
* and then exit()
*
*/
function _util_rest_authentication(){
return true;
//KEY MISSING
// user is trying to access but hasn't
// provided a key or signature
if( !isset($_SERVER['HTTP_X_KKRAB_KEY']) && !isset($_REQUEST['HTTP_X_KKRAB_SIGNATURE']) ){
$data = _util_rest_response_template();
$data->code = 401;
$data->msg = "Unauthorized";
$data->message = "credentials not provided";
_util_rest_response($data,401);
exit();
}
//QUERY DATABASE
// get user my key
$result = db_query("select * from `kkrabb_module_authentication` where `key` = :key",array(
'key' => $_SERVER['HTTP_X_KKRAB_KEY']
));
$record = $result->fetchObject();
//USER NOT FOUND
// database was queried but no key found
if($record===false){
$data = _util_rest_response_template();
$data->code = 403;
$data->msg = "Forbidden";
$data->message = "Incorrect credentials";
_util_rest_response($data,401);
exit();
}
//INVALID SIGNATURE
// md5( db:key + db:secret ) != request:signature
if( $_SERVER['HTTP_X_KKRAB_SIGNATURE'] != md5($record->key.$record->secret) ){
$data = _util_rest_response_template();
$data->code = 403;
$data->msg = "Forbidden";
$data->message = "Incorrect credentials";
_util_rest_response($data,401);
exit();
}
}
function _util_rest_response_template(){
return (object)array(
'code' => null,
'msg' => null,
'message' => null,
'data' => null
);
}
function _util_rest_response( $data, $code = 200){
header("Content-type:application/json");
switch ($code) {
//200 OK
// Standard response for successful HTTP requests.
case 200:
http_response_code(200);
break;
//201 Created
// The request has been fulfilled and resulted in a new resource being created
case 201:
http_response_code(201);
break;
//400 Bad Request
// The request cannot be fulfilled due to bad syntax
case 400:
http_response_code(400);
break;
//401 Unauthorized
// Similar to 403 Forbidden, but specifically for use when authentication is
// required and has failed or has not yet been provided.
case 401:
http_response_code(401);
break;
//403 Forbidden
// The request was a valid request, but the server is refusing to respond to it.[2]
// Unlike a 401 Unauthorized response, authenticating will make no difference.
case 403:
http_response_code(403);
break;
//404 Not Found
// The requested resource could not be found but may be available again in the future
case 404:
http_response_code(404);
break;
//500 Internal Server Error
// A generic error message, given when no more specific message is suitable.
case 500:
http_response_code(500);
break;
//501 Not Implemented
// The server either does not recognize the request method, or it lacks the ability to fulfill the request.
case 501:
http_response_code(501);
break;
default:
//TODO
break;
}
echo json_encode($data);
}
/**
* Get all events.
*
* Query for all events stored in the cache table.
* @return array
*/
function kkrabb_module_events_cache_get(){
$return = array();
$result = db_query("SELECT * FROM `kkrabb_module_cache` WHERE `deleted` != 1 AND `published` = 1 ORDER BY `from`");
foreach ($result as $record) {
$return[] = json_decode($record->data,false);
}
return $return;
}
/**
* Get one event
*
* Query for one event from the cache table
* @param mixed $id
* @return stdClass
*/
function kkrabb_module_event_cache_get($id){
$result = db_query(
"SELECT * FROM `kkrabb_module_cache` WHERE `deleted` != 1 AND `published` = 1 AND `id`=:id",
array(
':id'=>$id
)
);
$obj = $result->fetchObject();
return ( $obj ) ? json_decode($obj->data,false) : false ;
}
/**
* Get image via http from provider
* and store it in /tmp folder
*
* @param string $uri
* @param string $name name of the image
*/
function _kkrabb_fetch_image($uri,$name){
$handle = curl_init();
curl_setopt($handle, CURLOPT_USERAGENT, "kolkrabbin" );
curl_setopt($handle, CURLOPT_URL, $uri);
curl_setopt($handle, CURLOPT_POST, false);
curl_setopt($handle, CURLOPT_BINARYTRANSFER, true);
curl_setopt($handle, CURLOPT_HEADER, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 10);
$response = curl_exec($handle);
$hlength = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
$body = substr($response, $hlength);
//ERROR
// response is not 200, return false
if ($httpCode != 200) {
watchdog('kkrabb_module',"Can't fetch image {$uri}",null,WATCHDOG_ERROR);
return false;
//SUCCESS
// response in 200, save file to /tmp
// and then return the path to it
}else{
$random_name = file_directory_temp()."/".$name;
$fp = fopen($random_name,'w');
fwrite($fp, $body);
fclose($fp);
return $random_name;
}
}
/**
* Sync one provider
*
* This function requires the ID of provider from
* the 'kkrabb_module_service' db table
*
* @param int $id
* @return bool
*/
function _kkrabb_sync_provider($id){
//TRANSACTION BEGIN
// if something happens we want to be able to
// do a complete db rolback, that is why we start
// a database transaction
$transaction = db_transaction();
//GET CONFIG
// fetch a config record of provider
$provider_record = db_query('select * from kkrabb_module_service where id = :id',array(':id'=>$id))->fetchObject();
//FETCH META
// do a http request to provider and get his metadata.
// once the meta-data has been consumed, an HTTP call is made
// to get events. This is why the event consumption is done inside
// this IF statement.
if( ($return = file_get_contents($provider_record->url, false))!==false ){
$provider_meta = json_decode($return);
//NOT 200
// response code from provider is not 200
// that means error and we need to exit
if( $provider_meta===null || $provider_meta->code != 200 ){
$transaction->rollback(); //DB rollback
watchdog('kkrabb_module',"Provider {$provider_record->name} ".
"can't provide meta-data",null,WATCHDOG_ERROR);
echo __LINE__. "$provider_meta===null || $provider_meta->code != 200 \n";
return false;
}
//GET LANGUAGES
// get all installed languages of consumer
$consumer_languages = language_list();
watchdog('kkrabb_module',"Provider {$provider_record->name} queried ".
"for metadata",null,WATCHDOG_INFO);
//SYNC LANGUAGES
// if allowed, make sure that the languages
// are in sync
if($provider_record->create_language){
foreach ($provider_meta->data->languages->list as $value) {
if( array_key_exists($value->code, $consumer_languages) ){
}else{
locale_add_language($value->code, $value->name, $value->native);
watchdog('kkrabb_module',"Provider {$provider_record->name} created ".
"language [{$value->code}] - {$value->name}",null,WATCHDOG_INFO);
}
}
}
//UPDATED LANGUAGE LIST
// maybe we added new languages to consumer or not in the IF statement
// above, eather way we update available language list of consumer.
$consumer_languages = language_list();
//SYNC TAXONOMIES
// if allowed, set vocabulary in sync
if($provider_record->create_taxonomies){
foreach ($provider_meta->data->taxonomies as $vocabulary) {
$consumer_vocabulary_item = taxonomy_vocabulary_machine_name_load($vocabulary->machine_name);
//VOCABULARY NOT FOUND
// this machine name doesn't exist, create it
if(!$consumer_vocabulary_item){
taxonomy_vocabulary_save((object)array(
'name' => $vocabulary->name,
'machine_name' => $vocabulary->machine_name,
'description' => $vocabulary->description,
'hierarchy' => $vocabulary->hierarchy,
));
$consumer_vocabulary_item = taxonomy_vocabulary_machine_name_load($vocabulary->machine_name);
watchdog('kkrabb_module',"Provider {$provider_record->name} created ".
"vocabulary [{$vocabulary->machine_name}] - {$vocabulary->name}",null,WATCHDOG_INFO);
//VOCABULARY FOUND
// no need to create it
}else{
}
//ADD TERMS
//
foreach ($vocabulary->terms as $term) {
$t = taxonomy_get_term_by_name($term->name,$vocabulary->machine_name);
if( count($t)>0 ){
}else{
taxonomy_term_save((object)array(
'vid' => $consumer_vocabulary_item->vid,
'name' => $term->name,
'description' => $term->description,
'weight' => $term->weight,
));
watchdog('kkrabb_module',"Provider {$provider_record->name} created ".
"term {$vocabulary->machine_name} -> {$term->name}",null,WATCHDOG_INFO);
}
}
//CREATE FIELD IN CONTENT TYPE
// now we have to create a field in kkrabb_event content type
// that references this vocabulary/taxonomy that we were just
// creating. But only if the field doesn't exists.
if (!field_info_field($vocabulary->field_name)) {
//db_drop_table('field_data_'.$vocabulary->field_name); //TODO debug only
//db_drop_table('field_revision_'.$vocabulary->field_name); //TODO debug only
// Create the field
$field = array(
'field_name' => $vocabulary->field_name,
'type' => 'taxonomy_term_reference',
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => $vocabulary->machine_name,
'parent' => 0
)
),
),
);
field_create_field($field);
// Create the instance on the bundle.
$instance = array(
'field_name' => $vocabulary->field_name,
'entity_type' => 'node',
'label' => $vocabulary->name,
'bundle' => 'kkrabb_event',
'required' => false,
'widget' => array(
'type' => 'options_select',
),
);
field_create_instance($instance);
watchdog('kkrabb_module',"Provider {$provider_record->name} created ".
"field:taxonomy_term_reference - {$vocabulary->machine_name} -> {$vocabulary->machine_name}",null,WATCHDOG_INFO);
}
}
}
//FETCH EVENTS
// now he have kolkrabbinn content-type as we want it and it's ready
// to consume events. Lets start by makeing an HTTP call to provider
if(($events = file_get_contents($provider_record->url.'/events', false))!==false ){
$provider_events = json_decode($events);
//NOT 200
// response code from provider is not 200
// that means error and we need to exit
if( $provider_events->code != 200 ){
$transaction->rollback(); //DB rollback
watchdog('kkrabb_module',"Provider {$provider_record->name} ".
"can't provide events data",null,WATCHDOG_ERROR);
echo __LINE__. "$provider_events->code != 200 \n";
return false;
}
//FOR EVERY EVENT
// for every event entry in provider data-dump,
// create,update or delete
foreach($provider_events->data as $event){
//WHICH LANGUAGES
// cross-reference which translations should be imported.
// If the translation is in language 'und', we want it, If the event
// is in language that we have currently installed we want it, else we don't
$language_position_array = array();
for($i=0;$i<count($event->meta->languages);$i++){
if( $event->meta->languages[$i]->language == 'und' && $event->meta->languages[$i]->active == true){
$language_position_array[] = $i;
}else if( array_key_exists($event->meta->languages[$i]->language, $consumer_languages) && $event->meta->languages[$i]->active == true ){
$language_position_array[] = $i;
}
}
//DO ACTUAL CREATE/UPDATE
// and now for the actual operation. Try to find
// a node to update or create a new one.
$translation_id = null;
foreach ($language_position_array as $index) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->propertyCondition('type', 'kkrabb_event')
->fieldCondition('field_kkrabb_event_id', 'value', $event->record->id->value, '=');
$result = $query->execute();
//PLACEHOLDER
// placeholder for the node that is to be injected into
// the database;
$node = null;
//OLD NODE
// node was found that has the same eventID as the incomming one
if( isset($result['node']) ){
//NODE
// now we know that a event exists that has the same eventID. Now he have
// to find the correct translation node. To do that we go to the 'node' table
// where we will extract the 'tnid'.
// TODO this line provides error: Strict warning: Only variables should be passed by reference in
$node_record = db_query(
'select * from `node` where nid=:nid',
array(':nid'=>array_pop(array_keys($result['node'])) )
)->fetchObject();
//TRANSLATIONS
// next we use this 'tnid' and find all translations of this node
$node_translations = translation_node_get_translations($node_record->tnid);
//TRANSLATION FOUND
// translation array is not null and it contains a key
// that is the same as the language that we are dealing with, we
// therefor load that node
if( count($node_translations) != 0 && array_key_exists($event->record->title[$index]->lang,$node_translations) ){
$node = node_load( $node_translations[$event->record->title[$index]->lang]->nid );
watchdog('kkrabb_module',"Event:{$event->record->id->value}/[{$event->record->title[$index]->lang}] ".
" updated",null,WATCHDOG_INFO);
//TRANSLATION FOUND
// translation was found but it doesn't contain the language
// that we are dealing with. We need to create new node translation
}else if( count($node_translations) != 0 && !array_key_exists($event->record->title[$index]->lang,$node_translations) ){
watchdog('kkrabb_module',"Event:{$event->record->id->value}/[{$event->record->title[$index]->lang}] ".
" translated",null,WATCHDOG_INFO);
$node = new stdClass();
$node->type = "kkrabb_event";
$node->title = $event->record->title[$index]->value;
$node->language = $event->record->title[$index]->lang;
node_object_prepare($node);
$node->uid = 1;
$node->tnid = $node_record->tnid;
$translation_id = $node_record->tnid;
//NOT FOUND
// the translation array is null or empty but since we know that at
// least one node contains our eventID, we will create new translation
}else if(empty($node_translations)){
watchdog('kkrabb_module',"Event:{$event->record->id->value}/[{$event->record->title[$index]->lang}] ".
" translated for the first time",null,WATCHDOG_INFO);
$node = new stdClass();
$node->type = "kkrabb_event";
$node->title = $event->record->title[$index]->value;
$node->language = $event->record->title[$index]->lang;
node_object_prepare($node);
$node->uid = 1;
$node->tnid = $node_record->tnid;
$translation_id = $node_record->tnid;
}else{
//TODO is there an else statement?
}
//NEW NODE
// no node was found with this eventID so
// we need to create one
}else{
echo "New node: {$event->record->id->value}\n";
$node = new stdClass();
$node->type = "kkrabb_event";
$node->title = $event->record->title[$index]->value;
$node->language = $event->record->title[$index]->lang;
node_object_prepare($node);
$node->uid = 1;
if($translation_id!=null){
$node->tnid = $translation_id;
}
watchdog('kkrabb_module',"Event:{$event->record->id->value}/[{$event->record->title[$index]->lang}] ".
" created",null,WATCHDOG_INFO);
}
//GET IMAGES
// fetch an image via HTTP, if there is a record of an image from
// provider but it can't be downloaded, we will stop the process
// and roleback everything that we have done
if( $event->record->image[$index]->value->uri != null &&
($file_path = _kkrabb_fetch_image($event->record->image[$index]->value->uri,$event->record->image[$index]->value->filename)) !== false ){
$file = (object) array(
'uid' => 1,
'uri' => $file_path,
'filemime' => $event->record->image[$index]->value->filemime,
'status' => 1,
);
// You can specify a subdirectory, e.g. public://foo/
$file = file_copy($file, 'public://');
$node->field_image_background['und'][0] = (array) $file;
watchdog('kkrabb_module',"Event:{$event->record->id->value}/[{$event->record->title[$index]->lang}] ".
" fecthing image - {$event->record->image[$index]->value->filename}",null,WATCHDOG_INFO);
}else{
/*
$transaction->rollback(); //DB rollback
watchdog('kkrabb_module',"Provider {$provider_record->name} ".
"can't cant produce image {$event->record->image[$index]->value->filename}",null,WATCHDOG_ERROR);
echo __LINE__. "get image \n";
return false;
*/
}
//GET THUMB
// fetch an image via HTTP, if there is a record of an image from
// provider but it can't be downloaded, we will stop the process
// and roleback everything that we have done
if( $event->record->thumb[$index]->value->uri != null &&
($file_path = _kkrabb_fetch_image($event->record->thumb[$index]->value->uri,$event->record->thumb[$index]->value->filename)) !== false ){
$file = (object) array(
'uid' => 1,
'uri' => $file_path,
'filemime' => $event->record->thumb[$index]->value->filemime,
'status' => 1,
);
// You can specify a subdirectory, e.g. public://foo/
$file = file_copy($file, 'public://');
$node->field_kkrabb_event_imgs['und'][0] = (array) $file;
watchdog('kkrabb_module',"Event:{$event->record->id->value}/[{$event->record->title[$index]->lang}] ".
" fecthing thumb - {$event->record->image[$index]->value->filename}",null,WATCHDOG_INFO);
}else{
/*
$transaction->rollback(); //DB rollback
watchdog('kkrabb_module',"Provider {$provider_record->name} ".
"can't cant produce thumb {$event->record->image[$index]->value->filename}",null,WATCHDOG_ERROR);
echo __LINE__. "get thumb \n";
return false;
*/
}
//UPDATE DATA
// now we repopulate the node with field-data from the provider
$node->title = $event->record->title[$index]->value;
$node->field_kkrabb_event_title_short['und'][0]['value'] = $event->record->title_short[$index]->value;
$node->field_kkrabb_event_teaser_capt['und'][0]['value'] = $event->record->description[$index]->value;
$node->field_kkrabb_event_teaser_text['und'][0]['value'] = $event->record->body_short[$index]->value;
$node->field_kkrabb_event_description['und'][0]['value'] = $event->record->body[$index]->value;
$node->field_kkrabb_event_id['und'][0]['value'] = $event->record->id->value;
$node->field_kkrabb_event_registr_email['und'][0]['value'] = $event->record->register_email->value;
$node->field_kkrabb_event_registr_req['und'][0]['value'] = (int)$event->record->register_req->value;
$node->field_kkrabb_event_date_from['und'][0]['value'] = strtotime($event->record->date_from->value);
$node->field_kkrabb_event_date_to['und'][0]['value'] = strtotime($event->record->date_to->value);
$node->field_kkrabb_event_beg_unixtime['und'][0]['value'] = strtotime($event->record->date_timestamp->value);
$node->field_kkrabb_event_address1['und'][0]['value'] = $event->record->address->value;
$node->field_kkrabb_event_address2['und'][0]['value'] = $event->record->address_cont->value;
$node->field_event_video_youtube['und'][0]['value'] = $event->record->youtube->value;
$node->field_kkrabb_event_facebook_id['und'][0]['value'] = $event->record->facebook_id->value;
$node->field_kkrabb_event_facebook_page['und'][0]['value'] = $event->record->facebook_page->value;
$node->field_kkrabb_event_tickets_avail['und'][0]['value'] = (int)$event->record->tickets_avail->value;
$node->field_kkrabb_event_tickets_url['und'][0]['value'] = $event->record->tickets_url->value;
$node->field_kkrabb_event_gps_long['und'][0]['value'] = $event->record->gps_long->value;
$node->field_kkrabb_event_gps_lat['und'][0]['value'] = $event->record->gps_lat->value;
//TODO cant insert multiple values
//$node->ield_kkrabb_event_price_kr['und'] = array_map($event->record->price->value, function($n){
// return array('value' => $n);