forked from horde/turba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Driver.php
3312 lines (3047 loc) · 125 KB
/
Driver.php
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
/**
* Copyright 2000-2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (ASL). If you did
* did not receive this file, see http://www.horde.org/licenses/apache.
*
* @category Horde
* @copyright 2000-2017 Horde LLC
* @license http://www.horde.org/licenses/apache ASL
* @package Turba
*/
/**
* Provides a common abstracted interface to the various directory search
* drivers. It includes functions for searching, adding, removing, and
* modifying directory entries.
*
* @author Chuck Hagenbuch <[email protected]>
* @author Jon Parise <[email protected]>
* @category Horde
* @copyright 2000-2017 Horde LLC
* @license http://www.horde.org/licenses/apache ASL
* @package Turba
*/
class Turba_Driver implements Countable
{
/**
* The symbolic title of this source.
*
* @var string
*/
public $title;
/**
* Hash describing the mapping between Turba attributes and
* driver-specific fields.
*
* @var array
*/
public $map = array();
/**
* Hash with all tabs and their fields.
*
* @var array
*/
public $tabs = array();
/**
* List of all fields that can be accessed in the backend (excludes
* composite attributes, etc.).
*
* @var array
*/
public $fields = array();
/**
* Array of fields that must match exactly.
*
* @var array
*/
public $strict = array();
/**
* Array of fields to search "approximately" (@see
* config/backends.php).
*
* @var array
*/
public $approximate = array();
/**
* The name of a field to store contact list names in if not the default.
*
* @var string
*/
public $listNameField = null;
/**
* The name of a field to use as an alternative to the name field if that
* one is empty.
*
* @var string
*/
public $alternativeName = null;
/**
* The internal name of this source.
*
* @var string
*/
protected $_name;
/**
* Hash holding the driver's additional parameters.
*
* @var array
*/
protected $_params = array();
/**
* What can this backend do?
*
* @var array
*/
protected $_capabilities = array();
/**
* Any additional options passed to Turba_Object constructors.
*
* @var array
*/
protected $_objectOptions = array();
/**
* Number of contacts in this source.
*
* @var integer
*/
protected $_count = null;
/**
* Hold the value for the owner of this address book.
*
* @var string
*/
protected $_contact_owner = '';
/**
* Mapping of Turba attributes to ActiveSync fields.
*
* @var array
*/
protected static $_asMap = array(
'name' => 'fileas',
'lastname' => 'lastname',
'firstname' => 'firstname',
'middlenames' => 'middlename',
'nickname' => 'nickname',
'namePrefix' => 'title',
'nameSuffix' => 'suffix',
'homeStreet' => 'homestreet',
'homeCity' => 'homecity',
'homeProvince' => 'homestate',
'homePostalCode' => 'homepostalcode',
'homeCountryFree' => 'homecountry',
'otherStreet' => 'otherstreet',
'otherCity' => 'othercity',
'otherProvince' => 'otherstate',
'otherPostalCode' => 'otherpostalcode',
'otherCountryFree' => 'othercountry',
'workStreet' => 'businessstreet',
'workCity' => 'businesscity',
'workProvince' => 'businessstate',
'workPostalCode' => 'businesspostalcode',
'workCountryFree' => 'businesscountry',
'title' => 'jobtitle',
'company' => 'companyname',
'department' => 'department',
'office' => 'officelocation',
'spouse' => 'spouse',
'website' => 'webpage',
'assistant' => 'assistantname',
'manager' => 'managername',
'yomifirstname' => 'yomifirstname',
'yomilastname' => 'yomilastname',
'imaddress' => 'imaddress',
'imaddress2' => 'imaddress2',
'imaddress3' => 'imaddress3',
'homePhone' => 'homephonenumber',
'homePhone2' => 'home2phonenumber',
'workPhone' => 'businessphonenumber',
'workPhone2' => 'business2phonenumber',
'fax' => 'businessfaxnumber',
'homeFax' => 'homefaxnumber',
'pager' => 'pagernumber',
'cellPhone' => 'mobilephonenumber',
'carPhone' => 'carphonenumber',
'assistPhone' => 'assistnamephonenumber',
'companyPhone' => 'companymainphone',
'radioPhone' => 'radiophonenumber'
);
/**
* Constructs a new Turba_Driver object.
*
* @param string $name Source name
* @param array $params Hash containing additional configuration
* parameters.
*/
public function __construct($name = '', array $params = array())
{
$this->_name = $name;
$this->_params = $params;
}
/**
* Returns the current driver's additional parameters.
*
* @return array Hash containing the driver's additional parameters.
*/
public function getParams()
{
return $this->_params;
}
/**
* Checks if this backend has a certain capability.
*
* @param string $capability The capability to check for.
*
* @return boolean Supported or not.
*/
public function hasCapability($capability)
{
return !empty($this->_capabilities[$capability]);
}
/**
* Returns the attributes that are blob types.
*
* @return array List of blob attributes in the array keys.
*/
public function getBlobs()
{
global $attributes;
$blobs = array();
foreach (array_keys($this->fields) as $attribute) {
if (isset($attributes[$attribute]) &&
$attributes[$attribute]['type'] == 'image') {
$blobs[$attribute] = true;
}
}
return $blobs;
}
/**
* Returns the attributes that represent dates.
*
* @return array List of date attributes in the array keys.
* @since 4.2.0
*/
public function getDateFields()
{
global $attributes;
$dates = array();
foreach (array_keys($this->fields) as $attribute) {
if (isset($attributes[$attribute]) &&
$attributes[$attribute]['type'] == 'monthdayyear') {
$dates[$attribute] = '0000-00-00';
}
}
return $dates;
}
/**
* Translates the keys of the first hash from the generalized Turba
* attributes to the driver-specific fields. The translation is based on
* the contents of $this->map.
*
* @param array $hash Hash using Turba keys.
*
* @return array Translated version of $hash.
*/
public function toDriverKeys(array $hash)
{
if (!empty($hash['name']) &&
!empty($this->listNameField) &&
!empty($hash['__type']) &&
is_array($this->map['name']) &&
($hash['__type'] == 'Group')) {
$hash[$this->listNameField] = $hash['name'];
unset($hash['name']);
}
// Add composite fields to $hash if at least one field part exists
// and the composite field will be saved to storage.
// Otherwise composite fields won't be computed during an import.
foreach ($this->map as $key => $val) {
if (!is_array($val) ||
empty($this->map[$key]['attribute']) ||
array_key_exists($key, $hash)) {
continue;
}
foreach ($this->map[$key]['fields'] as $mapfields) {
if (isset($hash[$mapfields])) {
// Add composite field
$hash[$key] = null;
break;
}
}
}
$fields = array();
foreach ($hash as $key => $val) {
if (!isset($this->map[$key])) {
continue;
}
if (!is_array($this->map[$key])) {
$fields[$this->map[$key]] = $val;
} elseif (!empty($this->map[$key]['attribute'])) {
$fieldarray = array();
foreach ($this->map[$key]['fields'] as $mapfields) {
$fieldarray[] = isset($hash[$mapfields])
? $hash[$mapfields]
: '';
}
$fields[$this->map[$key]['attribute']] = Turba::formatCompositeField($this->map[$key]['format'], $fieldarray);
} else {
// If 'parse' is not specified, use 'format' and 'fields'.
if (!isset($this->map[$key]['parse'])) {
$this->map[$key]['parse'] = array(
array(
'format' => $this->map[$key]['format'],
'fields' => $this->map[$key]['fields']
)
);
}
foreach ($this->map[$key]['parse'] as $parse) {
$splitval = sscanf($val, $parse['format']);
$count = 0;
$tmp_fields = array();
foreach ($parse['fields'] as $mapfield) {
if (isset($hash[$mapfield])) {
// If the compositing fields are set
// individually, then don't set them at all.
break 2;
}
$tmp_fields[$this->map[$mapfield]] = $splitval[$count++];
}
// Exit if we found the best match.
if ($splitval[$count - 1] !== null) {
break;
}
}
$fields = array_merge($fields, $tmp_fields);
}
}
return $fields;
}
/**
* Takes a hash of Turba key => search value and return a (possibly
* nested) array, using backend attribute names, that can be turned into a
* search by the driver. The translation is based on the contents of
* $this->map, and includes nested OR searches for composite fields.
*
* @param array $criteria Hash of criteria using Turba keys.
* @param string $search_type OR search or AND search?
* @param array $strict Fields that must be matched exactly.
* @param boolean $match_begin Whether to match only at beginning of
* words.
* @param array $custom_strict Custom set of fields that are to matched
* exactly, but are glued using $search_type
* and 'AND' together with $strict fields.
* Allows an 'OR' search pm a custom set of
* $strict fields.
*
* @return array An array of search criteria.
*/
public function makeSearch($criteria, $search_type, array $strict,
$match_begin = false, array $custom_strict = array())
{
$search = $search_terms = $subsearch = $strict_search = array();
$glue = $temp = '';
$lastChar = '\"';
$blobs = $this->getBlobs();
foreach ($criteria as $key => $val) {
if (!isset($this->map[$key])) {
continue;
}
if (is_array($this->map[$key])) {
/* Composite field, break out the search terms. */
$parts = explode(' ', $val);
if (count($parts) > 1) {
/* Only parse if there was more than 1 search term and
* 'AND' the cumulative subsearches. */
for ($i = 0; $i < count($parts); ++$i) {
$term = $parts[$i];
$firstChar = substr($term, 0, 1);
if ($firstChar == '"') {
$temp = substr($term, 1, strlen($term) - 1);
$done = false;
while (!$done && $i < count($parts) - 1) {
$lastChar = substr($parts[$i + 1], -1);
if ($lastChar == '"') {
$temp .= ' ' . substr($parts[$i + 1], 0, -1);
$done = true;
} else {
$temp .= ' ' . $parts[$i + 1];
}
++$i;
}
$search_terms[] = $temp;
} else {
$search_terms[] = $term;
}
}
$glue = 'AND';
} else {
/* If only one search term, use original input and
'OR' the searces since we're only looking for 1
term in any of the composite fields. */
$search_terms[0] = $val;
$glue = 'OR';
}
foreach ($this->map[$key]['fields'] as $field) {
if (!empty($blobs[$field])) {
continue;
}
$field = $this->toDriver($field);
if (!empty($strict[$field])) {
/* For strict matches, use the original search
* vals. */
$strict_search[] = array(
'field' => $field,
'op' => '=',
'test' => $val,
);
} elseif (!empty($custom_strict[$field])) {
$search[] = array(
'field' => $field,
'op' => '=',
'test' => $val,
);
} else {
/* Create a subsearch for each individual search
* term. */
if (count($search_terms) > 1) {
/* Build the 'OR' search for each search term
* on this field. */
$atomsearch = array();
for ($i = 0; $i < count($search_terms); ++$i) {
$atomsearch[] = array(
'field' => $field,
'op' => 'LIKE',
'test' => $search_terms[$i],
'begin' => $match_begin,
'approximate' => !empty($this->approximate[$field]),
);
}
$atomsearch[] = array(
'field' => $field,
'op' => '=',
'test' => '',
'begin' => $match_begin,
'approximate' => !empty($this->approximate[$field])
);
$subsearch[] = array('OR' => $atomsearch);
unset($atomsearch);
$glue = 'AND';
} else {
/* $parts may have more than one element, but
* if they are all quoted we will only have 1
* $subsearch. */
$subsearch[] = array(
'field' => $field,
'op' => 'LIKE',
'test' => $search_terms[0],
'begin' => $match_begin,
'approximate' => !empty($this->approximate[$field]),
);
$glue = 'OR';
}
}
}
if (count($subsearch)) {
$search[] = array($glue => $subsearch);
}
} else {
/* Not a composite field. */
if (!empty($blobs[$key])) {
continue;
}
if (!empty($strict[$this->map[$key]])) {
$strict_search[] = array(
'field' => $this->map[$key],
'op' => '=',
'test' => $val,
);
} elseif (!empty($custom_strict[$this->map[$key]])) {
$search[] = array(
'field' => $this->map[$key],
'op' => '=',
'test' => $val,
);
} else {
$search[] = array(
'field' => $this->map[$key],
'op' => 'LIKE',
'test' => $val,
'begin' => $match_begin,
'approximate' => !empty($this->approximate[$this->map[$key]]),
);
}
}
}
if (count($strict_search) && count($search)) {
return array(
'AND' => array(
$search_type => $strict_search,
array(
$search_type => $search
)
)
);
} elseif (count($strict_search)) {
return array(
$search_type => $strict_search
);
} elseif (count($search)) {
return array(
$search_type => $search
);
}
return array();
}
/**
* Translates a single Turba attribute to the driver-specific
* counterpart. The translation is based on the contents of
* $this->map. This ignores composite fields.
*
* @param string $attribute The Turba attribute to translate.
*
* @return string The driver name for this attribute.
*/
public function toDriver($attribute)
{
if (!isset($this->map[$attribute])) {
return null;
}
return is_array($this->map[$attribute])
? $this->map[$attribute]['fields']
: $this->map[$attribute];
}
/**
* Translates a hash from being keyed on driver-specific fields to being
* keyed on the generalized Turba attributes. The translation is based on
* the contents of $this->map.
*
* @param array $entry A hash using driver-specific keys.
*
* @return array Translated version of $entry.
*/
public function toTurbaKeys(array $entry)
{
$new_entry = array();
foreach ($this->map as $key => $val) {
if (!is_array($val)) {
$new_entry[$key] = (isset($entry[$val]) && (!empty($entry[$val]) || (is_string($entry[$val]) && strlen($entry[$val]))))
? trim($entry[$val])
: null;
}
}
return $new_entry;
}
/**
* Searches the source based on the provided criteria.
*
* @todo Allow $criteria to contain the comparison operator (<, =, >,
* 'like') and modify the drivers accordingly.
*
* @param array $search_criteria Hash containing the search criteria.
* @param string $sort_order The requested sort order which is passed
* to Turba_List::sort().
* @param string $search_type Do an AND or an OR search (defaults to
* AND).
* @param array $return_fields A list of fields to return; defaults to
* all fields.
* @param array $custom_strict A list of fields that must match exactly.
* @param boolean $match_begin Whether to match only at beginning of
* words.
* @param boolean $count_only Only return the count of matching entries,
* not the entries themselves.
*
* @return mixed Turba_List|integer The sorted, filtered list of search
* results or the number of matching
* entries (if $count_only is true).
* @throws Turba_Exception
*/
public function search(array $search_criteria, $sort_order = null,
$search_type = 'AND', array $return_fields = array(),
array $custom_strict = array(), $match_begin = false,
$count_only = false)
{
global $injector;
/* Add any fields that must match exactly for this source to the
* $strict_fields array. */
$strict_fields = $custom_strict_fields = array();
foreach ($this->strict as $strict_field) {
$strict_fields[$strict_field] = true;
}
/* Differentiate between provided $custom_strict fields - which honor
* the $search_type and $strict fields which are not
* explicitly requested as part of this search, and as such, are not
* constrained by the requested $search_type. */
foreach ($custom_strict as $strict_field) {
if (isset($this->map[$strict_field])) {
$custom_strict_fields[$this->map[$strict_field]] = true;
}
}
/* Translate the Turba attributes to driver-specific attributes. */
$fields = $this->makeSearch($search_criteria, $search_type,
$strict_fields, $match_begin, $custom_strict_fields);
/* If we are not using Horde_Share, enforce the requirement that the
* current user must be the owner of the addressbook. */
if (isset($this->map['__owner'])) {
$fields = array(
'AND' => array(
$fields,
array(
'field' => $this->toDriver('__owner'),
'op' => '=',
'test' => $this->getContactOwner()
)
)
);
}
if (in_array('email', $return_fields) &&
!in_array('emails', $return_fields)) {
$return_fields[] = 'emails';
}
if (count($return_fields)) {
$default_fields = array('__key', '__type', '__owner', '__members', 'name');
if ($this->alternativeName) {
$default_fields[] = $this->alternativeName;
}
$return_fields_pre = array_unique(array_merge($default_fields, $return_fields));
$return_fields = array();
foreach ($return_fields_pre as $field) {
$result = $this->toDriver($field);
if (is_array($result)) {
foreach ($result as $composite_field) {
$composite_result = $this->toDriver($composite_field);
if ($composite_result) {
$return_fields[] = $composite_result;
}
}
} elseif ($result) {
$return_fields[] = $result;
}
}
} else {
/* Need to force the array to be re-keyed for the (fringe) case
* where we might have 1 DB field mapped to 2 or more Turba
* fields */
$return_fields = array_values(
array_unique(array_values($this->fields)));
}
/* Retrieve the search results from the driver. */
$objects = $this->_search($fields, $return_fields, $this->toDriverKeys($this->getBlobs()), isset($search_criteria['tags']) ? false : $count_only);
/* Need some magic if we are searching tags */
$list = $this->_filterTags(
$objects,
!empty($search_criteria['tags']) ? $injector->getInstance('Turba_Tagger')->split($search_criteria['tags']) : array(),
$sort_order
);
if ($count_only) {
return $list->count();
}
return $list;
}
/**
* Returns a Turba_List object containing $objects filtered by $tags.
*
* @param array $objects A hash of objects, as returned by
* self::_search.
* @param array $tags An array of tags to filter by.
* @param Array $sort_order The sort order to pass to Turba_List::sort.
*
* @return Turba_List The filtered Turba_List object.
*/
protected function _filterTags($objects, $tags, $sort_order = null)
{
global $injector;
if (empty($tags)) {
return $this->_toTurbaObjects($objects, $sort_order);
}
$tag_results = $injector->getInstance('Turba_Tagger')
->search($tags, array('list' => $this->_name));
// Short circuit if we know we have no tag hits.
if (!$tag_results) {
return new Turba_List();
}
$list = $this->_toTurbaObjects($objects, $sort_order);
return $list->filter('__uid', $tag_results);
}
/**
* Searches the current address book for duplicate entries.
*
* Duplicates are determined by comparing email and name or last name and
* first name values.
*
* @return array A hash with the following format:
* <code>
* array('name' => array('John Doe' => Turba_List, ...), ...)
* </code>
* @throws Turba_Exception
*/
public function searchDuplicates()
{
return array();
}
/**
* Takes an array of object hashes and returns a Turba_List
* containing the correct Turba_Objects
*
* @param array $objects An array of object hashes (keyed to backend).
* @param array $sort_order Array of hashes describing sort fields. Each
* hash has the following fields:
* <pre>
* ascending - (boolean) Indicating sort direction.
* field - (string) Sort field.
* </pre>
*
* @return Turba_List A list object.
*/
protected function _toTurbaObjects(array $objects, array $sort_order = null)
{
$list = new Turba_List();
foreach ($objects as $object) {
/* Translate the driver-specific fields in the result back to the
* more generalized common Turba attributes using the map. */
$object = $this->toTurbaKeys($object);
$done = false;
if (!empty($object['__type']) &&
ucwords($object['__type']) != 'Object') {
$class = 'Turba_Object_' . ucwords($object['__type']);
if (class_exists($class)) {
$list->insert(new $class($this, $object, $this->_objectOptions));
$done = true;
}
}
if (!$done) {
$list->insert(new Turba_Object($this, $object, $this->_objectOptions));
}
}
$list->sort($sort_order);
/* Return the filtered (sorted) results. */
return $list;
}
/**
* Returns a list of birthday or anniversary hashes from this source for a
* certain period.
*
* @param Horde_Date $start The start date of the valid period.
* @param Horde_Date $end The end date of the valid period.
* @param string $category The timeObjects category to return.
*
* @return array A list of timeObject hashes.
* @throws Turba Exception
*/
public function listTimeObjects(
Horde_Date $start, Horde_Date $end, $category
)
{
global $attributes, $registry;
try {
$res = $this->getTimeObjectTurbaList($start, $end, $category);
} catch (Turba_Exception $e) {
/* Try the default implementation before returning an error */
$res = $this->_getTimeObjectTurbaListFallback(
$start, $end, $category
);
}
$t_objects = array();
while ($ob = $res->next()) {
$t_object = $ob->getValue($category);
if (empty($t_object)) {
continue;
}
try {
$t_object = new Horde_Date($t_object);
} catch (Horde_Date_Exception $e) {
continue;
}
if ($t_object->compareDate($end) > 0) {
continue;
}
$t_object_end = new Horde_Date($t_object);
++$t_object_end->mday;
$key = $ob->getValue('__key');
// Calculate the age of the time object
if ($start->year == $end->year ||
$end->year == 9999) {
$age = $start->year - $t_object->year;
} elseif ($t_object->month <= $end->month) {
// t_object must be in later year
$age = $end->year - $t_object->year;
} else {
// t_object must be in earlier year
$age = $start->year - $t_object->year;
}
// Generate thumbnail.
$img = null;
if (($imgdata = $ob->getValue('photo')) &&
!empty($imgdata['load']['data'])) {
$file = Horde::getTempFile('turba_', false);
if ($fd = fopen($file, 'w')) {
fwrite($fd, $imgdata['load']['data']);
fclose($fd);
$img = (string)Horde::url(
$registry->get('webroot', 'horde')
. '/services/images/view.php',
true
)->add(
array(
'f' => basename($file),
'a' => 'resize',
'v' => '25.25.1'
)
);
}
}
$title = sprintf(
_("%d. %s of %s"),
$age,
$attributes[$category]['label'],
$ob->getValue('name')
);
$t_objects[] = array(
'id' => $key,
'title' => $title,
'start' => sprintf(
'%d-%02d-%02dT00:00:00',
$t_object->year,
$t_object->month,
$t_object->mday
),
'end' => sprintf(
'%d-%02d-%02dT00:00:00',
$t_object_end->year,
$t_object_end->month,
$t_object_end->mday
),
'recurrence' => array(
'type' => Horde_Date_Recurrence::RECUR_YEARLY_DATE,
'interval' => 1
),
'params' => array('source' => $this->_name, 'key' => $key),
'link' => Horde::url('contact.php', true)
->add(array('source' => $this->_name, 'key' => $key))
->setRaw(true),
'icon' => $img,
);
}
return $t_objects;
}
/**
* Default implementation for obtaining a Turba_List to get TimeObjects
* out of.
*
* @param Horde_Date $start The starting date.
* @param Horde_Date $end The ending date.
* @param string $field The address book field containing the
* timeObject information (birthday,
* anniversary).
*
* @return Turba_List A list of objects.
* @throws Turba_Exception
*/
public function getTimeObjectTurbaList(Horde_Date $start, Horde_Date $end, $field)
{
return $this->_getTimeObjectTurbaListFallback($start, $end, $field);
}
/**
* Default implementation for obtaining a Turba_List to get TimeObjects
* out of.
*
* @param Horde_Date $start The starting date.
* @param Horde_Date $end The ending date.
* @param string $field The address book field containing the
* timeObject information (birthday,
* anniversary).
*
* @return Turba_List A list of objects.
* @throws Turba_Exception
*/
protected function _getTimeObjectTurbaListFallback(Horde_Date $start, Horde_Date $end, $field)
{
return $this->search(array(), null, 'AND', array('name', $field));
}
/**
* Retrieves a set of objects from the source.
*
* @param array $objectIds The unique ids of the objects to retrieve.
*
* @return array The array of retrieved objects (Turba_Objects).
* @throws Turba_Exception
* @throws Horde_Exception_NotFound
*/
public function getObjects(array $objectIds)
{
$objects = $this->_read($this->map['__key'], $objectIds,
$this->getContactOwner(),
array_values($this->fields),
$this->toDriverKeys($this->getBlobs()),
$this->toDriverKeys($this->getDateFields()));
if (!is_array($objects)) {
throw new Horde_Exception_NotFound();
}
$results = array();
foreach ($objects as $object) {
$object = $this->toTurbaKeys($object);
$done = false;
if (!empty($object['__type']) &&
ucwords($object['__type']) != 'Object') {
$class = 'Turba_Object_' . ucwords($object['__type']);
if (class_exists($class)) {
$results[] = new $class($this, $object, $this->_objectOptions);
$done = true;
}
}
if (!$done) {
$results[] = new Turba_Object($this, $object, $this->_objectOptions);
}
}
return $results;
}
/**
* Retrieves one object from the source.
*
* @param string $objectId The unique id of the object to retrieve.
*
* @return Turba_Object The retrieved object.
* @throws Turba_Exception
* @throws Horde_Exception_NotFound
*/
public function getObject($objectId)
{
$result = $this->getObjects(array($objectId));
if (empty($result[0])) {
throw new Horde_Exception_NotFound();
}
$result = $result[0];
if (!isset($this->map['__owner'])) {
$result->attributes['__owner'] = $this->getContactOwner();
}
return $result;
}
/**
* Adds a new entry to the contact source.
*
* @param array $attributes The attributes of the new object to add.