This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmediamosa_ck.class.inc
1303 lines (1164 loc) · 37.5 KB
/
mediamosa_ck.class.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
/**
* @file
* Global class for settings and static functions.
*/
class mediamosa_ck extends mediamosa_sdk {
// ------------------------------------------------------------------- Consts.
// Permissions.
const PERMISSION_ADMIN_CONFIGURATION = 'mediamosa-ck admin configuration';
const PERMISSION_ADD_MEDIA = 'add media to mediamosa';
// ---------------------------------------------------------------- Functions.
/**
* Create or return the current connector.
*
* @param boolean $reset_session
* TRUE / FALSE to reset session.
*
* @return MediaMosaCkConnectorWrapper
* Returns the connector object.
*/
public static function get_connector($reset_session = FALSE) {
$mediamosa_connector = &drupal_static(__CLASS__ . '::' . __FUNCTION__, FALSE);
// Create when not found.
if (empty($mediamosa_connector)) {
$mediamosa_connector = new MediaMosaCkConnectorWrapper();
}
// Reset session when needed.
if ($reset_session) {
$mediamosa_connector->reset_session();
}
return $mediamosa_connector;
}
/**
* Display an user friendly error.
*
* In case of responding to an exception, use set_message_exception() instead.
*
* This wrapper is indended when in the future if we want to tell the user
* that something went wrong and log the problem. We might want to make
* options for the admin in which we log the error, but will not bore the user
* with the error.
*
* @see set_message_exception()
*/
public static function watchdog_error($message, array $variables = array()) {
// Log it first.
self::watchdog($message, $variables, WATCHDOG_ERROR);
// Tell user.
drupal_set_message(t($message, $variables), 'error');
}
/**
* Display an readable version of the exception for the user.
*
* Will also log the exception.
*
* @param Exception $exception
* The exception that is going to be logged.
* @param string $message
* The message that explains when or where the exception occurred.
* @param array $variables
* Array of variables to replace in the message.
*/
public static function set_message_exception(Exception $e, $message, array $variables = array()) {
// Log it first.
self::watchdog_exception($e);
$message = t($message, $variables);
// Tell user.
$variables = array(
'!message' => $message,
'!message_exception' => $e->getMessage(),
);
drupal_set_message(strtr('!message: !message_exception', $variables), 'error');
}
/**
* Wrapper for quicker logging.
*/
public static function watchdog($message, array $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL, $type = 'MediaMosa CK') {
$type = empty($type) ? 'MediaMosa CK' : 'MediaMosa CK - ' . $type;
watchdog(substr($type, 0, 64), $message, $variables, $severity, $link);
}
/**
* Logs an exception.
*
* This is a wrapper function for watchdog() which automatically decodes an
* exception.
*
* @param Exception $exception
* The exception that is going to be logged.
* @param string $message
* The message to store in the log. If empty, a text that contains all useful
* information about the passed-in exception is used.
* @param array $variables
* Array of variables to replace in the message on display. Defaults to the
* return value of drupal_decode_exception().
* @param string $severity
* The severity of the message, as per RFC 3164.
* @param string $link
* A link to associate with the message.
* @param string $type
* The category to which this message belongs.
*/
public static function watchdog_exception(Exception $exception, $message = NULL, array $variables = array(), $severity = WATCHDOG_ERROR, $link = NULL, $type = 'MediaMosa CK') {
$type = empty($type) ? 'MediaMosa CK' : 'MediaMosa CK - ' . $type;
watchdog_exception($type, $exception, $message, $variables, $severity, $link);
}
/**
* Wrapper for quick debug messages.
*/
public static function debug($file, $line, $message, array $variables = array(), $severity = WATCHDOG_DEBUG, $link = NULL, $type = 'MediaMosa CK - Debug') {
$variables = array(
'@file' => $file,
'@line' => $line,
'@message' => t($message, $variables),
);
$message = '@message (In file @file on line @line).';
drupal_set_message(t($message, $variables));
watchdog(substr($type, 0, 64), $message, $variables, $severity, $link);
}
/**
* Return the last error of the connector.
*/
public static function get_connector_last_error() {
$mediamosa_connector = self::get_connector();
return $mediamosa_connector->get_last_error();
}
/**
* Return the last error of the connector.
*/
public static function get_connector_last_error_text() {
$mediamosa_connector = self::get_connector();
return $mediamosa_connector->get_last_error_text();
}
/**
* Get current session user ID.
*
* Get the current session user ID to use for ownership or calls where
* user_id is required.
*/
public static function session_user_id() {
global $user;
// We use mail address as owner.
if (variable_get('mediamosa_ck_asset_owner', 'mail') == 'mail') {
return isset($user->mail) ? $user->mail : '<anonymous>';
}
else {
return isset($user->name) ? $user->name : '<anonymous>';
}
}
/**
* Builds static unique ID.
*
* Builds static unique ID for entries in static array. Use for storing REST
* call output based on the (asset/mediafile) ID and its options used for the
* REST call.
*/
public static function build_static_id($rest_options) {
// Should make it unique enough.
return sha1(print_r($rest_options, TRUE));
}
/**
* Do request to MediaMosa.
*
* @param string $uri
* The REST uri
* @param array $options
* Options for REST call.
* @param boolean $check
* Do result check, will display drupal message when needed.
* @param boolean $cached
* Cache the result for REST call result.
* @param boolean $reset
* Reset the cache.
*
* @return string
* return result.
*/
public static function request($uri, array $options = array(), $check = FALSE, $cached = FALSE, $reset = FALSE) {
// Get the connection class object.
$mediamosa_connector = self::get_connector();
// Do the request.
if ($mediamosa_connector) {
// Only GET will be supported for caching.
if ($options['method'] != 'GET' && $cached) {
// Do not set for other than GET.
assert(0);
$cached = FALSE;
}
if ($cached) {
// Reset the cache first?
if ($reset) {
drupal_static_reset('mediamosa_ck_requests');
}
$rest_calls = &drupal_static('mediamosa_ck_requests', array());
$static_id = self::build_static_id($options);
// Stored?
if (!isset($rest_calls[$uri][$static_id])) {
// Store it.
$rest_calls[$uri][$static_id] = $mediamosa_connector->request($uri, $options);
}
// Check the result, shows drupal message when needed.
if ($check) {
mediamosa_connector::check_result($rest_calls[$uri][$static_id], FALSE, '%message');
}
// Return the result.
return $rest_calls[$uri][$static_id];
}
$result = $mediamosa_connector->request($uri, $options);
if ($check) {
mediamosa_connector::check_result($result, FALSE, '%message');
}
return $result;
}
elseif (!empty($options['fatal'])) {
throw new Exception('Unable to load connector');
}
}
/**
* Do GET request to MediaMosa.
*
* @param string $uri
* The REST uri
* @param array $options
* Options for REST call.
* @param boolean $check
* Do result check.
* @param boolean $cached
* Cache the result for REST call result.
* @param boolean $reset
* Reset the cache.
*
* @return object
* The response object.
*/
public static function request_get($uri, array $options = array(), $check = FALSE, $cached = FALSE, $reset = FALSE) {
$options['method'] = 'GET';
return self::request($uri, $options, $check, $cached, $reset);
}
/**
* Do cached GET request to MediaMosa.
*
* @param string $uri
* The REST uri
* @param array $options
* Options for REST call.
* @param boolean $check
* Do result check.
* @param boolean $reset
* Reset the cache.
*
* @return mediamosa_connector_response
* The response object.
*/
public static function request_get_cached($uri, array $options = array(), $check = FALSE, $reset = FALSE) {
return self::request_get($uri, $options, $check, TRUE, $reset);
}
/**
* Do GET request to MediaMosa but any error is fatal (expecting 601 code).
*
* @param string $uri
* The REST uri
* @param array $options
* Options for REST call.
* @param boolean $check
* Do result check.
* @param boolean $cached
* Cache the result for REST call result.
* @param boolean $reset
* Reset the cache.
*
* @return mediamosa_connector_response
* The response object.
*/
public static function request_get_fatal($uri, array $options = array(), $check = FALSE, $cached = FALSE, $reset = FALSE) {
$options['method'] = 'GET';
$options['fatal'] = TRUE;
return self::request($uri, $options, $check, $cached, $reset);
}
/**
* Do GET request to MediaMosa but any error is fatal (expecting 601 code).
*
* @param string $uri
* The REST uri
* @param array $options
* Options for REST call.
* @param boolean $check
* Do result check.
* @param boolean $reset
* Reset the cache.
*
* @return mediamosa_connector_response
* The response object.
*/
public static function request_get_fatal_cached($uri, array $options = array(), $check = FALSE, $reset = FALSE) {
return self::request_get_fatal($uri, $options, $check, TRUE, $reset);
}
/**
* Do POST request to MediaMosa.
*
* @param string $uri
* The REST uri
* @param array $options
* Options for REST call.
* @param boolean $check
* Do result check.
*
* @return mediamosa_connector_response
* The response object.
*/
public static function request_post($uri, array $options = array(), $check = FALSE) {
$options['method'] = 'POST';
return self::request($uri, $options, $check);
}
/**
* Do POST request to MediaMosa but any error is fatal (expecting 601 code).
*
* @param string $uri
* The REST uri
* @param array $options
* Options for REST call.
* @param boolean $check
* Do result check.
*
* @return object
* The response object.
*/
public static function request_post_fatal($uri, array $options = array(), $check = FALSE) {
$options['method'] = 'POST';
$options['fatal'] = TRUE;
return self::request($uri, $options, $check);
}
/**
* Return the setting for mediafile downloadable.
*/
public static function setting_mediafile_downloadable() {
return variable_get('mediamosa_ck_mediafile_downloadable', TRUE);
}
/**
* Return the setting for mediafile creation of still after upload.
*/
public static function setting_mediafile_upload_create_still() {
return variable_get('mediamosa_ck_mediafile_upload_create_still', TRUE);
}
/**
* Return the setting for mediafile tag on original.
*/
public static function setting_mediafile_tag() {
return variable_get('mediamosa_ck_mediafile_tag', '');
}
/**
* Return the setting for mediafile is private.
*/
public static function setting_mediafile_isprivate() {
return variable_get('mediamosa_ck_mediafile_isprivate', FALSE);
}
/**
* Opposite of parse_url.
*
* @param array $urls
*
* @param boolean $return_parts
* Return the array with host and uri.
*/
public static function build_url($urls, $return_parts = FALSE) {
$result = array(
'host' => array($urls['scheme'] . '://'),
'uri' => array(),
);
// Setup default.
$urls += array(
'user' => '',
'pass' => '',
'port' => 0,
'path' => '',
'query' => '',
'fragment' => '',
);
// Add user:pass.
if ($urls['user'] != '' || $urls['pass'] != '') {
$result['host'][] = $urls['user'];
$result['host'][] = ':';
$result['host'][] = $urls['pass'];
$result['host'][] = '@';
}
// Host.
$result['host'][] = trim($urls['host'], '/');
// Optional port.
if ($urls['port']) {
$result['host'][] = ':' . $urls['port'];
}
// Path.
if ($urls['path'] != '') {
$result['uri'][] = '/' . trim($urls['path'], '/');
}
// Query.
if ($urls['query'] != '') {
$result['uri'][] = '?' . $urls['query'];
}
// Fragment.
if ($urls['fragment'] != '') {
$result['uri'][] = $urls['fragment'];
}
// Now rebuild url.
return $return_parts ? array(
'host' => implode('', $result['host']),
'uri' => implode('', $result['uri'])) : (implode('', $result['host']) . implode('', $result['uri'])
);
}
/**
* Wrapper around http_build_query().
*
* @param array $query
*/
public static function http_build_query($query) {
return strtr(http_build_query($query, '', '&'), array(
'%5B' => '[',
'%5D' => ']',
'%5b' => '[',
'%5d' => ']',
)
);
}
/**
* Opposite of http_build_query().
*
* @param string $query_str
*/
public static function parse_query($query_str) {
// Split the url.
$query = array();
// Split values.
foreach (explode('&', $query_str) as $valuepair) {
if (strpos($valuepair, '=')) {
list($name, $value) = explode('=', $valuepair, 2);
$query[urldecode($name)][] = urldecode($value);
}
else {
$query[urldecode($valuepair)][] = '';
}
}
// Now make single item queries with 0 keys no array.
foreach ($query as $name => $value) {
if (count($value) == 1 && key($value) == 0) {
$query[$name] = reset($value);
}
}
return $query;
}
/**
* Convert any date into unix timestamp.
*
* @param date $any_date
* The date to convert.
*
* @return int
* The number of seconds since 1970 (UTC/GMT).
*/
public static function date2unix($any_date) {
// Parse the date, date_parse is new in 5.2.
$date_parsed = date_parse($any_date);
// Convert to integer.
return gmmktime($date_parsed['hour'], $date_parsed['minute'], $date_parsed['second'], $date_parsed['month'], $date_parsed['day'], $date_parsed['year']);
}
/**
* Converts mediafile duration format into unix timestamp.
*
* @param date $any_date
* The date to convert.
*
* @return int
* The number of seconds since 1970 (UTC/GMT).
*/
public static function duration2unix($mediafile_duration) {
// Split into pieces.
if (preg_match('/(?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d+).(?P<milliseconds>\d+)/', $mediafile_duration, $matches)) {
$hours = isset($matches['hours']) ? $matches['hours'] : '';
$minutes = isset($matches['minutes']) ? $matches['minutes'] : '';
$seconds = isset($matches['seconds']) ? $matches['seconds'] : '';
$milliseconds = isset($matches['milliseconds']) ? $matches['milliseconds'] : '';
// Convert to # of seconds.
return ((int) $hours * 3600) + ((int) $minutes * 60) + (int) $seconds + round((int) $milliseconds / 100);
}
return 0;
}
/**
* Get active metadata fields.
*
* This function will return the active metadata fields on the active
* MediaMosa connection. For now we return DC/QDC, but in future we need to
* find out or query MediaMosa what metadata is allowed. MediaMosa does not
* yet have a REST call that will return the metadata fields in MediaMosa.
*/
public static function get_metadata_fields() {
$metadata_fields = &drupal_static('mediamosa_ck_metadata_fields');
if (!isset($metadata_fields)) {
// The metadata fields.
$metadata_fields = array(
'dc' => array(
'title' => 'Dublin Core',
'xpath' => 'dublin_core',
'fields' => array(
'type' => 'CHAR',
'format' => 'CHAR',
'language' => 'CHAR',
'title' => 'CHAR',
'creator' => 'CHAR',
'publisher' => 'CHAR',
'subject' => 'CHAR',
'description' => 'CHAR',
'contributor' => 'CHAR',
'date' => 'DATETIME',
'identifier' => 'CHAR',
'source' => 'CHAR',
'relation' => 'CHAR',
'coverage_temporal' => 'CHAR',
'coverage_spatial' => 'CHAR',
'rights' => 'CHAR',
),
),
'qdc' => array(
'title' => 'Qualified Dublin Core',
'xpath' => 'qualified_dublin_core',
'fields' => array(
'title_alternative' => 'CHAR',
'description_abstract' => 'CHAR',
'created' => 'DATETIME',
'issued' => 'DATETIME',
'hasformat' => 'CHAR',
'isformatof' => 'CHAR',
'format_medium' => 'CHAR',
'format_extent' => 'CHAR',
'license' => 'CHAR',
'rightsholder' => 'CHAR',
'isreferencedby' => 'CHAR',
),
),
'asset' => array(
'title' => 'Asset',
'xpath' => 'asset',
'fields' => array(
'tag' => 'CHAR',
),
),
);
// Give chance for others to change/add the metadata fields.
drupal_alter('mediamosa_ck_metadata_fields', $metadata_fields);
}
return $metadata_fields;
}
/**
* Returns the metadata for display usage.
*
* @param object $asset
* The asset response object.
*/
public static function get_metadata_asset($asset, array $options = array()) {
$options += array(
'datetime' => array(),
);
$options['datetime'] += array(
'type' => 'medium',
'format' => '',
// Time is stored as GMT.
'timezone' => 'GMT',
'langcode' => NULL,
);
$metadata = array();
foreach (static::get_metadata_fields() as $context => $set) {
if (isset($asset->{$set['xpath']})) {
$metadata[$context] = array(
'title' => $set['title'],
'data' => array(),
);
foreach ($set['fields'] as $field => $type) {
$metadata[$context]['data'][$field] = array('label' => static::get_metadata_label($field));
if (isset($asset->{$set['xpath']}->{$field})) {
foreach ($asset->{$set['xpath']}->{$field} as $value) {
switch ($type) {
case 'CHAR':
$value = (string) $value;
if ($value !== '') {
$metadata[$context]['data'][$field]['label'] = static::get_metadata_label($field);
$metadata[$context]['data'][$field]['contents'][] = (string) $value;
}
break;
case 'DATETIME':
$value = (string) $value;
if ($value !== '') {
$metadata[$context]['data'][$field]['label'] = static::get_metadata_label($field);
$metadata[$context]['data'][$field]['contents'][] = mediamosa_sdk::datestamp2date($value, $options['datetime']['type'], $options['datetime']['format'], $options['datetime']['timezone'], $options['datetime']['langcode']);
}
break;
case 'INT':
$value = (string) $value;
if ($value !== '') {
$metadata[$context]['data'][$field]['label'] = static::get_metadata_label($field);
$metadata[$context]['data'][$field]['contents'][] = (int) $value;
}
break;
}
}
}
}
}
}
return $metadata;
}
/**
* Convert metadata name into usable label for UI.
*
* @param string $name
* The name of the metadata field. Can be in format [contextset].[name],
* for example; 'dc.title'.
*
* @return string
* The label to use.
*/
public static function get_metadata_label($metadata_name) {
// Contains context set?
if (strpos($metadata_name, '.') !== FALSE) {
list(, $label) = explode('.', $metadata_name, 2);
}
else {
$label = $metadata_name;
}
$label_fixes = array(
'hasformat' => 'has format',
'isreferencedby' => 'is referenced by',
'isformatof' => 'is format of',
'videotimestamp' => 'video timestamp',
'videotimestampmodified' => 'video modified',
'app_id_search' => 'app id',
'title_alternative' => 'alternative title',
);
// Fix possible values.
$label = (isset($label_fixes[$label]) ? $label_fixes[$label] : $label);
// Remove _ and -.
$label = drupal_ucfirst(str_replace(array('_', '-'), ' ', $label));
return t($label);
}
/**
* Return the selection list for metadata.
*
* @return array
* An array that can be used directly as SELECT #options array.
*/
public static function get_metadata_fields_options() {
// Empty first.
$options = &drupal_static('mediamosa_ck_metadata_options', array());
if (empty($options)) {
$metadata_definition = self::get_metadata_fields();
// Build the select.
foreach ($metadata_definition as $metadata_group => $metadata_set) {
$group_title = $metadata_set['title'];
foreach (array_keys($metadata_set['fields']) as $name) {
$name = $metadata_group . '.' . $name;
$options[$group_title][$name] = self::get_metadata_label($name);
}
}
}
return $options;
}
/**
* Returns the metadata field that is used for title.
*
* Must be in format as SET.NAME and must be metadata.
*
* @see get_asset_title()
*/
public static function get_metadata_name_title() {
return 'dc.title';
}
/**
* Returns the metadata field that is used for title.
*
* Must be in format as SET.NAME and must be metadata.
*
* @see get_asset_description()
*/
public static function get_metadata_name_description() {
return 'dc.description';
}
/**
* Return the fields that are used for searching.
*/
public static function get_metadata_search_fields() {
return array('dc.title', 'dc.description');
}
/**
* Returns the (trimmed) title of the asset using the metadata.
*
* @param object $asset
* The asset in simplexmlelement or other object.
* @param string $default
* Default value when empty (=== '').
*
* @return string
* The title or ''.
*
* @see get_metadata_name_title()
* @see get_metadata_name_description()
*/
public static function get_asset_title($asset, $default = '') {
// Inside XML result?
if (isset($asset->items)) {
return self::get_asset_title($asset->items->item, $default);
}
$default = trim($default) === '' ? t('@asset_id', array('@asset_id' => (string) $asset->asset_id)) : $default;
$title = trim(self::get_asset_value($asset, self::get_metadata_name_title()));
if ($title !== '') {
return $title;
}
// Return filename if we have one.
if (isset($asset->mediafile_filename) && !empty($asset->mediafile_filename)) {
return (string) $asset->mediafile_filename;
}
// See if asset is synced to filemanaged.
$file = mediamosa_ck::get_filemanaged_from_assets(array((string) $asset->asset_id));
if ($file) {
return $file[(string) $asset->asset_id]['filename'];
}
return $default;
}
/**
* Returns the description of the asset using the metadata.
*
* @param object $asset
* The asset in simplexmlelement or other object.
* @param string $default
* Default value when empty (=== '').
*
* @return string
* The title or ''.
*
* @see get_metadata_name_title()
* @see get_metadata_name_description()
*/
public static function get_asset_description($asset, $default = '') {
return self::get_asset_value($asset, self::get_metadata_name_description(), $default);
}
/**
* Returns the value of an asset using it scope name ([set].[name]).
*
* Used now for retieving metadata values.
*
* @param object $asset
* The asset in simplexmlelement or other object.
* @param string $field
* The name of the field to retrieve.
* @param string $default
* The default value when value was not found or empty string (=== '').
*
* @return string
* The value or default.
*
* @see get_metadata_name_title()
*/
public static function get_asset_value($asset, $field, $default = '') {
// Inside XML result?
if (isset($asset->items)) {
return self::get_asset_value($asset->items->item, $field, $default);
}
if (is_array($asset->{$field})) {
if (count($asset->{$field}) > 0) {
return $asset->{$field}[0];
}
// the field is an array but does not contain any elements, return default
return $default;
}
// Might be object that already has it set as [set].[name]
if ((string) $asset->{$field} !== '') {
return (string) $asset->{$field};
}
// See if field contains set.
if (strpos($field, '.') !== FALSE) {
// Need parts.
list($set, $name) = explode('.', $field, 2);
// Need metadata info.
$metadata_fields = self::get_metadata_fields();
// Field name is known metadata?
if ($asset instanceof SimpleXMLElement && isset($metadata_fields[$set]) && !empty($metadata_fields[$set]['fields'][$name])) {
$xpath = $metadata_fields[$set]['xpath'] . '/' . $name;
$value = $asset->xpath($xpath);
if ($value) {
return (string) reset($value);
}
}
}
// Not found. In future version we can extend it more 'normal' fields.
return $default;
}
/**
* Lookup the Drupal user using his name. Although I would like to use
* user_load_by_name(), I better use something I can pre-cache when a lot of
* users need to be looked up.
*
* @param $name
* The name of the Drupal user.
*
* @return
* The user array.
*/
public static function lookup_user_with_name($name) {
// Precache.
self::precache_users_with_names(array($name));
// Get the users.
$users_names = &drupal_static('mediamosa_ck_cache_drupal_users_names', array());
// Get the user from the cache.
return isset($users_names[$name]) ? $users_names[$name] : NULL;
}
/**
* Lookup the Drupal users using his name. Although I would like to use
* user_load_by_name(), I better use something I can pre-cache when a lot of
* users need to be looked up. Also we only need mail, uid and name from the
* users table.
*
* @param $names
* The names of the Drupal users to precache.
*/
public static function precache_users_with_names(array $names) {
// Store static. Storing it twice does not cost lost more memory, as the
// users are objects, and these are referenced.
$users_names = &drupal_static('mediamosa_ck_cache_drupal_users_names', array());
$users_mails = &drupal_static('mediamosa_ck_cache_drupal_users_mails', array());
$query = NULL;
// Now see who we miss.
foreach ($names as $name) {
if (!isset($users_names[$name])) {
if (!isset($query)) {
$query = db_select('users', 'u')->fields('u', array('mail', 'uid', 'name'));
}
$query->condition('name', $name);
}
}
// Any new users not in cache?
if (isset($query)) {
$result = $query->execute();
foreach ($result as $user) {
$users_names[$user->name] = $user;
$users_mails[$user->mail] = $user;
}
}
}
/**
* Lookup the Drupal user using his mail address.
*
* @param string $mail
* The mail address of the Drupal user.
*
* @return array
* The user array.
*/
public static function lookup_user_with_mail($mail) {
// Precache.
self::precache_users_with_mails(array($mail));
// Get the users.
$users_mails = &drupal_static('mediamosa_ck_cache_drupal_users_mails', array());
// Get the user from the cache.
return isset($users_mails[$mail]) ? $users_mails[$mail] : NULL;
}
/**
* Lookup the Drupal users using his name.
*
* Although I would like to use user_load_by_name(), I better use something
* we can pre-cache when a lot of users need to be looked up. Also we only
* need mail, uid and name from the users table.
*
* @param string $mails
* The mail address of the Drupal users to precache.
*/
public static function precache_users_with_mails(array $mails) {
// Store static. Storing it twice does not cost lost more memory, as the
// users are objects, and these are referenced.
$users_names = &drupal_static('mediamosa_ck_cache_drupal_users_names', array());
$users_mails = &drupal_static('mediamosa_ck_cache_drupal_users_mails', array());
$query = NULL;
// Now see who we miss.
foreach ($mails as $mail) {
if (!isset($users_mails[$mail])) {
if (!isset($query)) {
$query = db_select('users', 'u')->fields(
'u',
array('mail', 'uid', 'name')
);
}
$query->condition('mail', $mail);
}
}
// Any new users not in cache?
if (isset($query)) {
$result = $query->execute();
foreach ($result as $user) {
$users_names[$user->name] = $user;