forked from practical-solutions/addressbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.php
1304 lines (1002 loc) · 44.1 KB
/
syntax.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
/**
* Addressbook Plugin
*
* @license GPL2
* @author Gero Gothe <[email protected]>
*
*/
// TS: 2022-04-04 - eigene Anpassung, um die Felder cfunction und tel2 als Straße und PLZ+Ort zu missbrauchen
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
define('ADRESSBOOK_SQL',false); # for development purposes
class syntax_plugin_addressbook extends DokuWiki_Syntax_Plugin {
public $editor = false; # Does user have editing rights for contacts?
public $loggedin = false; # User logged in?
private $instance = 0;
private $showCount = 0;
function getSort(){
return 158;
}
public function getType() {
return 'substition';
}
/**
* Paragraph Type
*/
public function getPType() {
return 'block';
}
function __construct(){
global $INFO;
global $ID;
$permissionlevel = auth_quickaclcheck($ID);
switch ($this->getConf('use ACL permissions')) {
case 'no':
if ($INFO['ismanager'] === true) $this->editor = true;
break;
case 'AUTH_NONE':
if ($permissionlevel >= AUTH_NONE) {
$this->editor = true;
}
break;
case 'AUTH_READ':
if ($permissionlevel >= AUTH_READ) {
$this->editor = true;
}
break;
case 'AUTH_EDIT':
if ($permissionlevel >= AUTH_EDIT) {
$this->editor = true;
}
break;
case 'AUTH_CREATE':
if ($permissionlevel >= AUTH_CREATE) {
$this->editor = true;
}
break;
case 'AUTH_UPLOAD':
if ($permissionlevel >= AUTH_UPLOAD) {
$this->editor = true;
}
break;
case 'AUTH_DELETE':
if ($permissionlevel >= AUTH_DELETE) {
$this->editor = true;
}
break;
case 'AUTH_ADMIN':
if ($permissionlevel >= AUTH_ADMIN) {
$this->editor = true;
}
break;
}
if (isset($INFO['userinfo'])) $this->loggedin = true;
}
/**
* @param string $mode
*/
public function connectTo($mode) {$this->Lexer->addEntryPattern('\[ADDRESSBOOK.*?', $mode, 'plugin_addressbook');}
public function postConnect() { $this->Lexer->addExitPattern('\]','plugin_addressbook'); }
/**
* Handler to prepare matched data for the rendering process
*/
public function handle($match, $state, $pos, Doku_Handler $handler) {
if ($state == DOKU_LEXER_UNMATCHED) {
if ($match==':') return 'LOOKUP';
if ($match[0]==':') $match = substr($match,1);
return trim($match);
}
return false;
}
/**
* @param $mode string output format being rendered
* @param $renderer Doku_Renderer the current renderer object
* @param $data array data created by handler()
* @return boolean rendered correctly?
*/
public function render($mode, Doku_Renderer $renderer, $data) {
switch ($mode) {
case "xhtml":
/* @var Doku_Renderer_xhtml $renderer */
// Calling the XHTML render function.
return $this->render_for_xhtml($renderer, $data);
case "odt":
/* @var renderer_plugin_odt_page $renderer */
// Calling the ODT render function.
return $this->render_for_odt($renderer, $data);
}
// Success. We did support the format.
return false;
}
/**
* @param $renderer Doku_Renderer the current renderer object
* @param $data array data created by handler()
* @return boolean rendered correctly?
*/
protected function render_for_xhtml (&$renderer, $data) {
// Generate XHTML content...
global $ID;
$renderer->info['cache'] = false;
# addressbook_debug_show();
if (isset($_REQUEST['Submit']) && $_REQUEST['Submit'] == $this->getLang('exec cancel')){
unset($_REQUEST);
unset ($cinfo);
$action = $data;
}
$cinfo = null;
# Main action given by tag data
if (!isset($_REQUEST['Submit'])) $action = $data;
/* Save a contact
*
* On fail: show edit form again
*/
if (isset($_REQUEST['Submit']) && $_REQUEST['Submit']==$this->getLang('exec save')) $action = "savedata";
# Certain actions could cause double saving, which is avoided by counting
if ($action=='savedata' && $this->saveOnce == 0 && $this->editor){
$this->saveOnce++;
$contact_id = $_REQUEST['editcontact'];
$cinfo = $this->loadFormData(); # Loads form data concerning the contact
$res = $this->saveData($cinfo);
if (!$res) {
$action = 'edit';
$contact_id = $_REQUEST['contactid'];
} else { # Clear all
unset($_REQUEST);
unset ($cinfo);
$action = $data;
}
}
# Delete a contact
if (isset($_REQUEST['erasecontact']) && $this->editor){
$this->deleteContact($_REQUEST['erasecontact']);
}
/* Directly show contact card by tag
*
* Cards are only display, when there is NO edit or save action
*/
if (substr($data,0,7) == 'contact' &&
!isset($_REQUEST['editcontact']) &&
$action != 'edit'
) {
$renderer->doc .= $this->showcontact(intval(substr($data,8)),$ID);
return; #no following actions
}
if (substr($data,0,5) == 'index' &&
!isset($_REQUEST['editcontact'])) {
# showcontact once before if necessary
if (isset($_REQUEST['showcontact']) && $this->showCount==0) {
$this->showCount++;
$out = $this->showcontact($_REQUEST['showcontact'],$ID);
if ($out !== false) $renderer->doc .= $out.'<br>';
}
# now show index
# keyword 'departments'
if (strpos($data,'departments') > 0) {
$list = $this->getList(false,'department,surname,firstname,cfunction');
$renderer->doc .= $this->buildIndex($list,'department',$ID);
} else $renderer->doc .= $this->buildIndex(false,false,$ID);
return true; # no following actions
}
if (substr($data,0,8) == 'BSNRlist') {
# keyword 'departments'
if (strpos($data,'departments') > 0) {
$list = $this->getList(false,'department,surname,firstname,cfunction');
$renderer->doc .= $this->buildBSNRList($list,'department',$ID);
} else $renderer->doc .= $this->buildBSNRList(false,false,$ID);
return true; # no following actions
}
# --------------------------------------------------------#
# only one instance beyond this point
$this->instance++;
if ($this->instance > 1) return;
# --------------------------------------------------------#
# Generate printable list
if (substr($data,0,5) == 'print') {
$pList = false;
$sep = false;
$params = $this->getValue($data,'');
if (isset($params['department'])) $sep = 'department';
# Select one department
if (isset($params['select'])) {
$pList = $this->getList(Array('department' => $params['select']));
$sep = false;
}
$renderer->doc .= $this->buildPrintList($pList,$sep);
return true;
}
/* Edit contact or add a new contact
*
* No futher actions are performed after an edit
*/
if ($action == 'addcontact' && $this->editor) { # Add a new contact. Can be overwritten by edit
$contact_id = 'new';
$action = 'edit'; # redefine action
}
if (isset($_REQUEST['editcontact']) && $this->editor) { # Override new contact if the action is to edit an existing one
$contact_id = $_REQUEST['editcontact'];
$cinfo = $this->getContactData($contact_id);
$action = 'edit';
}
if ($action == 'edit' && $this->editor) {
$out = $this->buildForm($contact_id,$cinfo);
$renderer->doc .= $out;
return; # no following actions
}
# Show search box
if ($action == 'search' || isset($_REQUEST['Submit']) && $_REQUEST['Submit'] == $this->getLang('exec search')) {
$out = $this->searchDialog();
$renderer->doc .= $out;
}
if (isset($_REQUEST['Submit']) && $_REQUEST['Submit'] == $this->getLang('exec search')) {
$list = $this->searchDB($_REQUEST['searchtext']);
if ($list != false){
if (count($list)<5) {
foreach ($list as $l) $renderer->doc .= $this->showcontact($l['id'],$ID);
} else $renderer->doc .= $this->buildIndex($list,false,$ID);
}
}
/* Show contact per request
* can only be shown once due to the instance count above
*
* placed below the searchbox
*/
if (isset($_REQUEST['showcontact']) && $this->showCount==0) {
$this->showCount++;
$out = $this->showcontact($_REQUEST['showcontact'],$ID);
if ($out !== false) $renderer->doc .= $out.'<br>';
}
return true;
}
/**
* @param $renderer Doku_Renderer the current renderer object
* @param $data array data created by handler()
* @return boolean rendered correctly?
*/
protected function render_for_odt (&$renderer, $data) {
global $ID;
// Return if installed ODT plugin version is too old.
if ( method_exists($renderer, 'getODTProperties') == false
|| method_exists($renderer, '_odtTableAddColumnUseProperties') == false
) {
return false;
}
# Generate printable list
if (substr($data,0,5) == 'print') {
$pList = false;
$sep = false;
$params = $this->getValue($data,'');
if (isset($params['department'])) $sep = 'department';
# Select one department
if (isset($params['select'])) {
$pList = $this->getList(Array('department' => $params['select']));
$sep = false;
}
return $this->buildPrintList4ODT($renderer,$pList,$sep);
}
return false;
}
function searchDialog(){
global $ID;
$out = '';
$out .= '<div class="plugin_addressbook_searchbox">';
$out .= '<form enctype="multipart/form-data" action="'.wl($ID).'" method="POST">';
$out .= '<span>'.$this->getLang('addressbook').'</span> <input type="text" name="searchtext" placeholder="'.$this->getLang('form search').'" value="'.$_REQUEST['searchtext'].'">';
$out .= '<input type="submit" name="Submit" value="'.$this->getLang('exec search').'" />';
$out .= '</form>';
$out .= '</div>';
return $out;
}
function buildForm($contact_id,$cinfo){
global $ID;
$out = '<div class="plugin_addressbook_editform">';
if ($contact_id == 'new') {$out .= '<h2>'.$this->getLang('header add').'</h2>';} else {$out .= '<h2>'.$this->getLang('header edit').'</h2>';}
$out .= '<br><form enctype="multipart/form-data" action="'.wl($ID).'" method="POST">';
$out .= '<input type="hidden" name="MAX_FILE_SIZE" value="2200000" />';
$out .= '<input type="hidden" name="contactid" value="'.$contact_id.'" />';
$out .= '<input type="text" name="department" placeholder="'.$this->getLang('form department').'" value="'.(isset($cinfo)?$cinfo['department']:'').'">';
/* // das Feld firstname nutzen wir nicht: $out .= '<input type="text" name="firstname" placeholder="'.$this->getLang('form firstname').'" value="'.(isset($cinfo)?$cinfo['firstname']:'').'">';*/
$out .= '<input type="text" name="surname" placeholder="'.$this->getLang('form surname').'" value="'.(isset($cinfo)?$cinfo['surname']:'').'"><br/>';
$out .= '<input type="text" name="tel2" placeholder="'.$this->getLang('form tel2').'" value="'.(isset($cinfo)?$cinfo['tel2']:'').'">';
$out .= '<input type="text" name="cfunction" placeholder="'.$this->getLang('form function').'" value="'.(isset($cinfo)?$cinfo['cfunction']:'').'"><br/>';
$out .= '<input type="text" name="tel1" placeholder="'.$this->getLang('form tel1').'" value="'.(isset($cinfo)?$cinfo['tel1']:'').'">';
$out .= '<input type="text" name="fax" placeholder="'.$this->getLang('form fax').'" value="'.(isset($cinfo)?$cinfo['fax']:'').'"><br>';
$out .= '<input type="text" name="email" placeholder="'.$this->getLang('form email').'" value="'.(isset($cinfo)?$cinfo['email']:'').'"><br>';
$out .= '<br>'.$this->getLang('form description').':<br><textarea name="description">'.(isset($cinfo)?$cinfo['description']:'').'</textarea><br><br>';
$out .= '<div class="photoupload">';
if (isset($_REQUEST['blob'])) $cinfo['photo'] = $_REQUEST['blob'];
if (isset($cinfo) && $cinfo['photo'] != false) {
$out .= "<img style='float:left;max-width:120px' src='data:image/jpg;base64,".($cinfo['photo'])."'>";
$out .= '<br><input type="checkbox" id="removephoto" name="removephoto" value="Remove photo"> ';
$out .= '<label for="removephoto">'.$this->getLang('form remove').'</label><br><br>';
$out .= $this->getLang('form upload info2').'.<br>';
$out .= '<input type="hidden" name="blob" value="'.($cinfo['photo']).'">';
}
$out .= '</div>';
$out .= $this->getLang('form upload').': <input name="photo" type="file" /> '.$this->getLang('form upload info').'.<br>';
$out .= '<br><input type="submit" name="Submit" value="'.$this->getLang('exec save').'" />';
$out .= '<input type="submit" name="Submit" value="'.$this->getLang('exec cancel').'" />';
$out .= '</form>';
$out .= "<div class='id'>ID: $contact_id</div>";
$out .= '</div>';
return $out;
}
function showcontact($cid,$target = false){
$r = $this->getContactData($cid);
//if ($res === false) return false;
$out ='';
$out .= '<div class="plugin_addressbook_singlecontact">';
$out .= '<div class="content">';
$out .= '<div class="data">';
# Name if existant
$out .= '<b>'.$r['surname'] .($r['firstname'] <> ''? ', '.$r['firstname']:'').'</b>';
if (strlen($r['surname'] .$r['firstname'])>0) $out .= '<br>';
# Function/department if existant
if ($r['surname'].$r['firstname'] == '') $out .= '<b>';
if ($r['department'] != '') $out .= $this->names(array($r['department']));
if (strlen($r['department'])>0) $out .= '<br>';
if ($r['surname'].$r['firstname'] == '') $out .= '</b>';
# Telephone
if ($r['tel1'] <> '') $out .= '<br>Tel.: '.$this->names(array($r['tel1']));
# TS: Adresse
if ($r['tel2'].$r['cfunction']<>'') $out .= '<br>'.$this->names(array($r['tel2'],$r['cfunction']));
# TS: BSNR
if ($r['fax']<>'') $out .= '<br>BSNR: '.$r['fax'];
# Mail
if ($r['email']<>'') $out .= '<br>Mail: <a href="mailto:'.$r['email'].'">'.$r['email'].'</a>';
$out .= '</div>';
if ($r['photo'] != false) $out .= "<img class='photo' src='data:image/jpg;base64,".($r['photo'])."'>";
$out .= '</div>';
if ($r['description']<>'') $out .= $r['description'];
if ($this->loggedin) {
$out .= '<div class="footer">';
$out .= 'Nr. '.$r['id'];
if ($this->editor && $target != false) {
$out .= '<span class="buttons">';
$out .= '<a href="'.wl($target,'editcontact='.$r['id']).'">'.$this->getLang('exec edit').'</a>';
$out .= '<a href="'.wl($target,'erasecontact='.$r['id']).'" onclick="return confirm(\'Sure?\');">'.$this->getLang('exec delete').'</a>';
$out .= '</span>';
}
$out .= '</div>';
}
$out .= '</div>';
return $out;
}
/* Get single contact data per id
*
* @param id: id in the database (unique)
*
* @return:
* - false if id is not found
* - array containing all data of the contact
*/
function getContactData($cid){
try {
$db_helper = plugin_load('helper', 'addressbook_db');
$sqlite = $db_helper->getDB();
} catch (Exception $e) {
msg($e->getMessage(), -1);
return false;
}
$sql = "SELECT * FROM addresslist WHERE id = $cid";
$query = $sqlite->query($sql);
if (ADRESSBOOK_SQL) msg("<code>$sql</code>",2);
if ($sqlite->res2count($query) ==0){
msg ("Contact not found (id: $cid)",-1);
return false;
}
$res = $sqlite->res2arr($query)[0];
return $res;
}
/*
* @return: Array - Contact data in the sequence they should be displayed
* ! result does not contain the photo and the id
*/
function getKeys(){
return Array('firstname','surname','department','cfunction','tel1','tel2','fax','email','description');
}
/* Loads the data from the $_REQUEST-Variables into an array
* the image is loaded as a blob
*
* @return array
*/
function loadFormData(){
$res = Array();
$keys = $this->getKeys();#Array('firstname','surname','cfunction','description');
foreach ($keys as $k) $res[$k] = $_REQUEST[$k];
# Validate and load photo data
if (isset($_FILES) && $_FILES['photo']['error'] == UPLOAD_ERR_OK && $_FILES['photo']['tmp_name']!='') {
if (filesize($_FILES['photo']['tmp_name']) > (2*1024*1024)) {
msg('Uploaded photo exceeds 2 MB. Not processed',-1);
$res['photo'] = false;
} elseif (exif_imagetype($_FILES['photo']['tmp_name']) != IMAGETYPE_JPEG){
msg('Image ist not *.jpg file. Not processed.',-1);
$res['photo'] = false;
} else {
$pic = $this->scaleJPG($_FILES['photo']['tmp_name']);
$res['photo'] = base64_encode($pic);
unset($pic);
}
} else {
$res['photo'] = false;
if ($_FILES['photo']['error'] != UPLOAD_ERR_OK && $_FILES['photo']['tmp_name']!='' ) msg('Image could not be uploaded. Error code: '.$_FILES['photo']['error'],-1);
}
return $res;
}
/* SaveData-Function: Saves Data to an existing contact or adds a new contact
*
* @param $info: Array containing the form data
*
* additions params used:
* $_REQUEST['blob'] - contains the blob of an existing id
* $_REQUEST['contactid'] - the id of the existing contact or "new" for a new one
*/
function saveData($info){
#msg(print_r($info,true));
if ($info['surname'] =='' && $info['cfunction']=='') {
msg('Please enter either a last name or a function.',-1);
return false;
} else {
try {
$db_helper = plugin_load('helper', 'addressbook_db');
$sqlite = $db_helper->getDB();
} catch (Exception $e) {
msg($e->getMessage(), -1);
return false;
}
$keys = array_keys($info);
# Use existing photo in existing id
if ($_REQUEST['contactid'] != 'new' && isset($_REQUEST['blob']) && $_REQUEST['removephoto'] != 'Remove photo' && $info['photo']== false) {
$info['photo'] = $_REQUEST['blob'];
# msg("Keep existing photo",2);
}
if ($info['photo']!== false) $blob = $info['photo'];
# Add new contact
if ($_REQUEST['contactid'] == 'new') {
#if ($info['photo']!== false) $blob = $info['photo'];
$sql = "INSERT INTO addresslist
(".implode(',',$keys).") VALUES
(";
foreach ($keys as $k) {
if ($k != 'photo') $sql .= "'".$info[$k]."',";
if ($k == 'photo') $sql .= "'$blob'";
}
$sql .= ')';
unset ($blob);
$res = $sqlite->query($sql);
if (ADRESSBOOK_SQL) msg("<code>$sql</code>",2);
msg("Added new contact",1);
return true;
} else {
# Update existing contact
$sql = "UPDATE addresslist SET ";
foreach ($keys as $k) {
if ($k != 'photo') $sql .= " $k = '".$info[$k]."', ";
if ($k == 'photo') $sql .= " $k = '$blob'";
}
$sql .= " WHERE id = ".$_REQUEST['contactid'];
if (ADRESSBOOK_SQL) msg("<code>$sql</code>",2);
$res = $sqlite->query($sql);
msg("Contact data updated",1);
return true;
}
}
}
function deleteContact($cid){
try {
$db_helper = plugin_load('helper', 'addressbook_db');
$sqlite = $db_helper->getDB();
} catch (Exception $e) {
msg($e->getMessage(), -1);
return false;
}
$sql = "DELETE FROM addresslist WHERE id = $cid";
$sqlite->query($sql);
if (ADRESSBOOK_SQL) msg("<code>$sql</code>",2);
msg('Contact deleted',1);
}
/* Scale image to a maximum size (either width or height maximum)
* from jpg to jpg
*
* @param $filename: name of source file
* @param $max: maximum width or heigth (depending on which is longer)
* @param $quality: compression rate
*
* return: blob with jpg
*/
function scaleJPG($filename,$max=120,$quality=70){
# calculate new dimensions
list($width, $height) = getimagesize($filename);
if ($width > $height) {$percent = $max/$width;} else {$percent = $max/$height;}
$newwidth = $width * $percent;
$newheight = $height * $percent;
# load image
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
# scale
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
# output
ob_start();
imagejpeg($thumb,NULL,$quality);
$result = ob_get_contents();
ob_end_clean();
return $result;
}
/* Searches the database for the occurence of search terms.
* The search uses AND-operator for multiple terms
*
* @param $text: search terms separated with a blank space
*
* return:
* - boolean false: no matches
* - array with matches
*/
function searchDB($text=false,$order='surname,firstname'){
if ($text == false || strlen($text) < 2) {msg("Invalid search.");return;}
try {
$db_helper = plugin_load('helper', 'addressbook_db');
$sqlite = $db_helper->getDB();
} catch (Exception $e) {
msg($e->getMessage(), -1);
return false;
}
$text = explode(" ",$text);
$sql = "select * from addresslist WHERE instr(lower(surname || firstname || tel1 || tel2 || fax || description || cfunction || department) ,'".$text[0]."') > 0";
for ($c=1;$c<count($text);$c++) $sql .= " AND instr(lower(' ' || surname || firstname || tel1 || tel2 || fax || description || cfunction || department) ,'".$text[$c]."') > 0";
$sql .= " ORDER BY $order";
$query = $sqlite->query($sql);
$res = $sqlite->res2arr($query);
if (ADRESSBOOK_SQL) msg("<code>$sql</code>",2);
if ($sqlite->res2count($sqlite->query($sql)) == 0) {
msg("No matches found",2);
return false;
}
return $res;
}
function getList($filters=false,$order="surname,firstname,cfunction,department"){
try {
$db_helper = plugin_load('helper', 'addressbook_db');
$sqlite = $db_helper->getDB();
} catch (Exception $e) {
msg($e->getMessage(), -1);
return false;
}
# Get all elements if no filters are set
if ($filters === false){
$sql = "select * from addresslist ORDER BY $order";
}
# Get one department
if (isset($filters['department'])){
$sql = "select * from addresslist WHERE UPPER(department) = UPPER('".$filters['department']."') ORDER BY $order";
}
# Get only entries with a value in 'fax'
if ($filters == 'BSNR'){
$sql = "select * from addresslist WHERE fax <> '' ORDER BY $order";
}
$query = $sqlite->query($sql);
$res = $sqlite->res2arr($query);
if (ADRESSBOOK_SQL) msg("<code>$sql</code>",2);
# return false if no matches are found
if ($sqlite->res2count($sqlite->query($sql)) == 0) {
msg("No matches found for list",2);
return false;
}
# Adjust content for sorting purposes when information is missing / not stated
foreach ($res as &$r) {
if ($r['surname'] == '' && $r['department'] == '') $r['department'] = $this->getLang('departments');
if ($r['surname'] != '' && $r['department'] == '') $r['department'] = $this->getLang('general');
}
return $res;
}
/* Creates a list of values from a column. Each element is listed
* once.
*
* @param string $column: choose column from which to get data
* @param boolean $htmlselect: creates output string containing html for a dropdown box
*
* @return:
* - array: elements founds
* - string: with html for checkbox
*/
function getCriteria($column,$htmlselect = true){
$allowed = array('cfunction','department');
if (!in_array($column,$allowed)) return false;
try {
$db_helper = plugin_load('helper', 'addressbook_db');
$sqlite = $db_helper->getDB();
} catch (Exception $e) {
msg($e->getMessage(), -1);
return false;
}
$sql = "SELECT DISTINCT $column FROM addresslist WHERE $column NOT LIKE '' ORDER by $column";
$query = $sqlite->query($sql);
$res = $sqlite->res2arr($query);
if (ADRESSBOOK_SQL) msg("<code>$sql</code>",2);
# return false if no matches are found
if ($sqlite->res2count($sqlite->query($sql)) == 0) {
#msg("No matches found for criteria",2);
return false;
}
foreach ($res as $r) $n[] =$r[$column];
if ($htmlselect){
$res = '';
$res .= ucfirst($column);
$res .= '<select name="column" id="column">';
$res .= "<option value='all'>All</option>";
foreach ($n as $s) $res .= "<option value='$s'>$s</option>";
$res .= '</select>';
return $res;
}
return $n;
}
/* Helper function, similar to implode
* Adds string elements to an array only if it is not empty and then implodes them
*/
function names($list,$symbol=', '){
$res = Array();
foreach ($list as $l) if ($l != '') $res[]=$l;
return implode($symbol,$res);
}
/* Helper function
* Return array with parameter=>value pairs
*
* @param string $params = parameters in form "<somet_text>?param1=value1¶m2=value2...paramN=valueN"
*
* @return array(parameter1 => value1, ...)
*/
function getValue($params){
$params = substr($params,strpos($params,'?')+1);
$opts = explode('&',$params);
foreach ($opts as $o) {
if (strpos($o,'=') == 0) {$res[$o] = false;} else {
$t = explode('=',$o);
$res[$t[0]] = $t[1];
}
}
return $res;
}
/* Build an index showing contacts
*
* @param $list: list of contacts
* @param $separator = db_field for which headers are created in the list.
*/
function buildIndex($list=false,$separator=false,$target=false){
# if no list ist stated, get all. If no entry in DB, return
if ($list===false){
$list =$this->getList();
if ($list === false) return;
}
# Sort by Surname or Function->if no surname ist stated
if (!$separator) usort($list,'contact_custom_double_sort');
$out = $this->getLang('elements').': '.count($list);
$out .= '<div class="addressbook_list" style="column-width:20em;margin-top:3px;">';
$sep = '';
foreach ($list as $r){
if ($separator !== false){
if ($sep != $r[$separator]) {
$sep = $r[$separator];
$out .= '<h3>'.$r[$separator].'</h3>';
}
}
$out .= '<span>';
if ($r['surname'].$r['firstname'] <> '') {$names = true;} else {$names = false;}
if ($target != false) $out .= '<a href="'.wl($target,'showcontact='.$r['id']).'">';
$out .= $r['surname'] .($r['firstname'] <> ''? ', '.$r['firstname']:'');
if (!$names) $out .= $this->names(array($r['cfunction'],$r['department']));
if ($target != false) $out .= '</a>';
if ($names && $r['department'] <>'') $out .= ' ('.$this->names(array($r['department'])).')';
if ($r['tel1'].$r['tel2'] <> '') $out .= ' Tel: '.$this->names(array($r['tel1']));
if ($r['tel2'].$r['cfunction'] <> '') $out .= ', '.$this->names(array($r['tel2'],$r['cfunction']));
$out .= '</span>';
}
$out .= '</div>';
return $out;
}
/* Build a list of contacts that have a BSNR (in db column 'fax') (written by TS)
*
* @param $list: list of contacts
* @param $separator = db_field for which headers are created in the list.
*/
function buildBSNRlist($list=false,$separator=false,$target=false){
# if no list ist stated, get all. If no entry in DB, return
if ($list===false){
$list =$this->getList('BSNR');
if ($list === false) return;
}
# Sort by Surname or Function->if no surname ist stated
if (!$separator) usort($list,'contact_custom_double_sort');
$out = '';//$this->getLang('elements').': '.count($list);
$out .= '<ul>';
$sep = '';
foreach ($list as $r){
if ($separator !== false){
if ($sep != $r[$separator]) {
if (strlen($sep) > 0) {
$out .= '</li></ul>';
}
$sep = $r[$separator];
$out .= '<li><strong>'.$r[$separator].'</strong>';
$out .= '<ul>';
}
}
if (isset($r['fax']) && strlen($r['fax']) > 0) {
$out .= '<li>';
if ($r['surname'].$r['firstname'] <> '') {$names = true;} else {$names = false;}
$out .= '<a href="'.wl(':quer:adresse','showcontact='.$r['id']).'">';
$out .= $r['surname'] .($r['firstname'] <> ''? ', '.$r['firstname']:'');
if (!$names) $out .= $this->names(array($r['cfunction'],$r['department']));
$out .= '</a>';
if ($names && $r['cfunction'] <>'') {
$tmp = $this->names(array($r['cfunction']));
// ziehe PLZ aus dem Namen heraus
$tmp = trim(preg_replace('/[0-9]+/', '', $tmp));
$out .= ', '.$tmp;
}
if ($names && $r['fax'] <>'') $out .= ' (BNSR: '.$this->names(array($r['fax'])).')';
$out .= '</li>';
}
}
if ($separator !== false){
$out .= '</li></ul>';
}
$out .= '</ul>';
return $out;
}
/* Builds a printable contact list
*
* @param array $list = list of contact entries in format array[1..n](db_field => value)
*
* @param string $separator = name of database field. This name is added as a
* header between the contact entries. Important: The
* entries should be sorted in first place according
* to this speparator, otherweise there will be as many
* headers as contacts!.
* Allowed separators: 'department'
*
* @param integer $entriesperpage = amount of list items per page, must be even!
*/
function buildPrintList($list=false,$separator = false,$entriesperpage = 80){
$pages = 0;
$amount = 0;
$this->preparePrintList($list,$separator,$entriesperpage,$pages,$amount);
$out = '';
for ($p=0;$p<$pages;$p++) {
$out .= '<table class="plugin_addressbook_print">';
$out .= '<tr><th>Praxis</th><th>Adresse</th><th>Telefon</th><th>BSNR</th></tr>';
for ($row=0;$row<$entriesperpage/2;$row++) {