getEditUserURL()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Template hyperlinks helpers file
4
 *
5
 * @package EBloodBank
6
 * @since   1.0
7
 */
8
namespace EBloodBank;
9
10
use EBloodBank\Views\View;
11
use EBloodBank\Models\User;
12
use EBloodBank\Models\Donor;
13
use EBloodBank\Models\City;
14
use EBloodBank\Models\District;
15
16
/*** Home Template Tags *******************************************************/
17
18
/**
19
 * @return string
20
 * @since 1.0
21
 */
22
function getHomeURL($format = 'absolute')
23
{
24
    $url = '';
25
26
    if (empty($format)) {
27
        $format = 'absolute';
28
    }
29
30
    switch (strtolower($format)) {
31
32
        case 'absolute':
33
            if (defined('EBB_URL')) {
34
                $url = EBB_URL;
0 ignored issues
show
Bug introduced by
The constant EBloodBank\EBB_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
35
            } else {
36
                $url = Options::getOption('site_url');
37
            }
38
            if (empty($url)) {
39
                $url = guessHomeURL(); // A fallback URl.
40
            }
41
            break;
42
43
        case 'relative':
44
            $url = (string) parse_url(getHomeURL(), PHP_URL_PATH);
45
            break;
46
47
    }
48
49
    return $url;
50
}
51
52
/**
53
 * @return string
54
 * @since 1.0
55
 */
56
function getSiteURL($path = '', $format = 'absolute')
57
{
58
    $url = getHomeURL($format);
59
60
    if (! empty($path)) {
61
        $url = trimTrailingSlash($url) . '/' . ltrim($path, '/');
62
    }
63
64
    return $url;
65
}
66
67
/**
68
 * @return string
69
 * @since 1.0
70
 */
71
function getHomeLink(array $args = [])
72
{
73
    $link = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $link is dead and can be removed.
Loading history...
74
75
    $args = array_merge([
76
        'content' => __('Home'),
77
        'atts' => [],
78
        'before' => '',
79
        'after' => '',
80
    ], $args);
81
82
    $args['atts']['href'] = getHomeURL();
83
84
    if (! isset($args['atts']['class'])) {
85
        $args['atts']['class'] = 'home-link';
86
    }
87
88
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
89
    return $args['before'] . $link . $args['after'];
90
}
91
92
/*** Theme Template Tags ******************************************************/
93
94
/**
95
 * @return string
96
 * @since 1.3
97
 */
98
function getThemeURL($path = '', $theme = '')
99
{
100
    $url = '';
101
102
    if (empty($theme)) {
103
        $theme = Themes::getCurrentTheme();
104
    } elseif (is_string($theme)) {
105
        $theme = Themes::findTheme($theme);
106
    }
107
108
    if (empty($theme) || ! $theme instanceof Theme) {
109
        return $url;
110
    }
111
112
    $path = ltrim($path, '/');
113
    $themeName = $theme->getName();
114
    $url = getSiteURL("/themes/{$themeName}/{$path}");
115
116
    return $url;
117
}
118
119
/*** Installer Template Tags **************************************************/
120
121
/**
122
 * @return string
123
 * @since 1.0
124
 */
125
function getInstallerURL()
126
{
127
    $url = getSiteURL('/install.php');
128
    return $url;
129
}
130
131
/*** Login\Logout\Signup Template Tags ****************************************/
132
133
/**
134
 * @return string
135
 * @since 1.0
136
 */
137
function getLoginURL()
138
{
139
    $url = getSiteURL('/login');
140
    return $url;
141
}
142
143
/**
144
 * @return string
145
 * @since 1.0
146
 */
147
function getLoginLink(array $args = [], View $context = null)
148
{
149
    $link = '';
150
151
    $args = array_merge([
152
        'content' => __('Log In'),
153
        'atts' => [],
154
        'before' => '',
155
        'after' => '',
156
    ], $args);
157
158
    if ($context) {
159
        if ($context->get('currentUser')) {
160
            return $link;
161
        }
162
    }
163
164
    $args['atts']['href'] = getLoginURL();
165
166
    if (! isset($args['atts']['class'])) {
167
        $args['atts']['class'] = 'login-link';
168
    }
169
170
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
171
    return $args['before'] . $link . $args['after'];
172
}
173
174
/**
175
 * @return string
176
 * @since 1.0
177
 */
178
function getLogoutURL()
179
{
180
    $url = getLoginURL();
181
    $url = addQueryArgs($url, [
182
        'action' => 'logout',
183
    ]);
184
    return $url;
185
}
186
187
/**
188
 * @return string
189
 * @since 1.0
190
 */
191
function getLogoutLink(array $args = [], View $context = null)
192
{
193
    $link = '';
194
195
    $args = array_merge([
196
        'content' => __('Log Out'),
197
        'atts' => [],
198
        'before' => '',
199
        'after' => '',
200
    ], $args);
201
202
    if ($context) {
203
        if (! $context->get('currentUser')) {
204
            return $link;
205
        }
206
    }
207
208
    $args['atts']['href'] = getLogoutURL();
209
210
    if (! isset($args['atts']['class'])) {
211
        $args['atts']['class'] = 'logout-link';
212
    }
213
214
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
215
    return $args['before'] . $link . $args['after'];
216
}
217
218
/**
219
 * @return string
220
 * @since 1.0
221
 */
222
function getSignupURL()
223
{
224
    $url = getSiteURL('/signup');
225
    return $url;
226
}
227
228
/**
229
 * @return string
230
 * @since 1.0
231
 */
232
function getSignupLink(array $args = [])
233
{
234
    $link = '';
235
236
    $args = array_merge([
237
        'content' => __('Sign Up'),
238
        'atts' => [],
239
        'before' => '',
240
        'after' => '',
241
    ], $args);
242
243
    if ('on' !== Options::getOption('self_registration')) {
244
        return $link;
245
    }
246
247
    $args['atts']['href'] = getSignupURL();
248
249
    if (! isset($args['atts']['class'])) {
250
        $args['atts']['class'] = 'signup-link';
251
    }
252
253
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
254
    return $args['before'] . $link . $args['after'];
255
}
256
257
/*** Users Template Tags ******************************************************/
258
259
/**
260
 * @return string
261
 * @since 1.0
262
 */
263
function getUsersURL()
264
{
265
    $url = getSiteURL('/users');
266
    return $url;
267
}
268
269
/**
270
 * @return string
271
 * @since 1.0
272
 */
273
function getAddUserURL()
274
{
275
    $url = getSiteURL('/add/user');
276
    return $url;
277
}
278
279
/**
280
 * @return string
281
 * @since 1.0
282
 */
283
function getEditUsersURL()
284
{
285
    $url = getSiteURL('/edit/users');
286
    return $url;
287
}
288
289
/**
290
 * @return string
291
 * @since 1.0
292
 */
293
function getEditUserURL($id)
294
{
295
    $url = '';
296
    $id = (int) $id;
297
298
    if (! isValidID($id)) {
299
        return $url;
300
    }
301
302
    $url = getSiteURL("/edit/user/{$id}");
303
    return $url;
304
}
305
306
/**
307
 * @return string
308
 * @since 1.0
309
 */
310
function getDeleteUserURL($id)
311
{
312
    $url = '';
313
    $id = (int) $id;
314
315
    if (! isValidID($id)) {
316
        return $url;
317
    }
318
319
    $url = getSiteURL("/delete/user/{$id}");
320
    return $url;
321
}
322
323
/**
324
 * @return string
325
 * @since 1.0
326
 */
327
function getActivateUserURL($id)
328
{
329
    $url = '';
330
    $id = (int) $id;
331
332
    if (! isValidID($id)) {
333
        return $url;
334
    }
335
336
    $url = getSiteURL("/activate/user/{$id}");
337
    return $url;
338
}
339
340
341
/**
342
 * @return string
343
 * @since 1.0
344
 */
345
function getUsersLink(array $args = [], View $context = null)
346
{
347
    $link = '';
348
349
    $args = array_merge([
350
        'content' => __('Users'),
351
        'atts' => [],
352
        'before' => '',
353
        'after' => '',
354
    ], $args);
355
356
    if ($context) {
357
        $currentUser = $context->get('currentUser');
358
359
        if (! $currentUser || ! $context->getAcl()->isUserAllowed($currentUser, 'User', 'read')) {
360
            return $link;
361
        }
362
    }
363
364
    $args['atts']['href'] = getUsersURL();
365
366
    if (! isset($args['atts']['class'])) {
367
        $args['atts']['class'] = 'view-link view-users-link';
368
    }
369
370
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
371
    return $args['before'] . $link . $args['after'];
372
}
373
374
/**
375
 * @return string
376
 * @since 1.0
377
 */
378
function getAddUserLink(array $args = [], View $context = null)
379
{
380
    $link = '';
381
382
    $args = array_merge([
383
        'content' => __('Add'),
384
        'atts' => [],
385
        'before' => '',
386
        'after' => '',
387
    ], $args);
388
389
    if ($context) {
390
        $currentUser = $context->get('currentUser');
391
392
        if (! $currentUser || ! $context->getAcl()->isUserAllowed($currentUser, 'User', 'add')) {
393
            return $link;
394
        }
395
    }
396
397
    $args['atts']['href'] = getAddUserURL();
398
399
    if (! isset($args['atts']['class'])) {
400
        $args['atts']['class'] = 'add-link add-user-link';
401
    }
402
403
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
404
    return $args['before'] . $link . $args['after'];
405
}
406
407
/**
408
 * @return string
409
 * @since 1.0
410
 */
411
function getEditUsersLink(array $args = [], View $context = null)
412
{
413
    $link = '';
414
415
    $args = array_merge([
416
        'content' => __('Edit'),
417
        'atts' => [],
418
        'before' => '',
419
        'after' => '',
420
    ], $args);
421
422
    if ($context) {
423
        $currentUser = $context->get('currentUser');
424
425
        if (! $currentUser || ! $context->getAcl()->isUserAllowed($currentUser, 'User', 'edit')) {
426
            return $link;
427
        }
428
    }
429
430
    $args['atts']['href'] = getEditUsersURL();
431
432
    if (! isset($args['atts']['class'])) {
433
        $args['atts']['class'] = 'edit-link edit-users-link';
434
    }
435
436
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
437
    return $args['before'] . $link . $args['after'];
438
}
439
440
/**
441
 * @return string
442
 * @since 1.0
443
 */
444
function getEditUserLink(array $args, View $context = null)
445
{
446
    $link = '';
447
448
    $args = array_merge([
449
        'fallbackContent' => false,
450
        'content' => __('Edit'),
451
        'atts' => [],
452
        'before' => '',
453
        'after' => '',
454
        'user' => null,
455
    ], $args);
456
457
    if (! $args['user'] instanceof User) {
458
        return $args['fallbackContent'] ? $args['content'] : $link;
459
    }
460
461
    if ($context) {
462
        $currentUser = $context->get('currentUser');
463
464
        if (! $currentUser || ! $context->getAcl()->canEditEntity($currentUser, $args['user'])) {
465
            return $args['fallbackContent'] ? $args['content'] : $link;
466
        }
467
    }
468
469
    $args['atts']['href'] = getEditUserURL($args['user']->get('id'));
470
471
    if (! isset($args['atts']['class'])) {
472
        $args['atts']['class'] = 'edit-link edit-user-link';
473
    }
474
475
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
476
    return $args['before'] . $link . $args['after'];
477
}
478
479
/**
480
 * @return string
481
 * @since 1.0
482
 */
483
function getDeleteUserLink(array $args, View $context = null)
484
{
485
    $link = '';
486
487
    $args = array_merge([
488
        'content' => __('Delete'),
489
        'atts' => [],
490
        'before' => '',
491
        'after' => '',
492
        'user' => null,
493
    ], $args);
494
495
    if (! $args['user'] instanceof User) {
496
        return $link;
497
    }
498
499
    if ($context) {
500
        $currentUser = $context->get('currentUser');
501
502
        if (! $currentUser || ! $context->getAcl()->canDeleteEntity($currentUser, $args['user'])) {
503
            return $link;
504
        }
505
    }
506
507
    $args['atts']['href'] = getDeleteUserURL($args['user']->get('id'));
508
509
    if (! isset($args['atts']['class'])) {
510
        $args['atts']['class'] = 'delete-link delete-user-link';
511
    }
512
513
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
514
    return $args['before'] . $link . $args['after'];
515
}
516
517
/**
518
 * @return string
519
 * @since 1.0
520
 */
521
function getActivateUserLink(array $args, View $context = null)
522
{
523
    $link = '';
524
525
    $args = array_merge([
526
        'content' => __('Activate'),
527
        'atts' => [],
528
        'before' => '',
529
        'after' => '',
530
        'user' => null,
531
    ], $args);
532
533
    if (! $args['user'] instanceof User || ! $args['user']->isPending()) {
534
        return $link;
535
    }
536
537
    if ($context) {
538
        $currentUser = $context->get('currentUser');
539
540
        if (! $currentUser || ! $context->getAcl()->canActivateUser($currentUser, $args['user'])) {
541
            return $link;
542
        }
543
    }
544
545
    $args['atts']['href'] = getActivateUserURL($args['user']->get('id'));
546
547
    if (! isset($args['atts']['class'])) {
548
        $args['atts']['class'] = 'activate-link activate-user-link';
549
    }
550
551
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
552
    return $args['before'] . $link . $args['after'];
553
}
554
555
/*** Donors Template Tags *****************************************************/
556
557
/**
558
 * @return string
559
 * @since 1.0
560
 */
561
function getDonorsURL()
562
{
563
    $url = getSiteURL('/donors');
564
    return $url;
565
}
566
567
/**
568
 * @return string
569
 * @since 1.1
570
 */
571
function getDonorURL($id)
572
{
573
    $url = '';
574
    $id = (int) $id;
575
576
    if (! isValidID($id)) {
577
        return $url;
578
    }
579
580
    $url = getSiteURL("/donor/{$id}");
581
    return $url;
582
}
583
584
/**
585
 * @return string
586
 * @since 1.0
587
 */
588
function getAddDonorURL()
589
{
590
    $url = getSiteURL('/add/donor');
591
    return $url;
592
}
593
594
/**
595
 * @return string
596
 * @since 1.0
597
 */
598
function getEditDonorsURL()
599
{
600
    $url = getSiteURL('/edit/donors');
601
    return $url;
602
}
603
604
/**
605
 * @return string
606
 * @since 1.0
607
 */
608
function getEditDonorURL($id)
609
{
610
    $url = '';
611
    $id = (int) $id;
612
613
    if (! isValidID($id)) {
614
        return $url;
615
    }
616
617
    $url = getSiteURL("/edit/donor/{$id}");
618
    return $url;
619
}
620
621
/**
622
 * @return string
623
 * @since 1.0
624
 */
625
function getDeleteDonorURL($id)
626
{
627
    $url = '';
628
    $id = (int) $id;
629
630
    if (! isValidID($id)) {
631
        return $url;
632
    }
633
634
    $url = getSiteURL("/delete/donor/{$id}");
635
    return $url;
636
}
637
638
/**
639
 * @return string
640
 * @since 1.0
641
 */
642
function getApproveDonorURL($id)
643
{
644
    $url = '';
645
    $id = (int) $id;
646
647
    if (! isValidID($id)) {
648
        return $url;
649
    }
650
651
    $url = getSiteURL("/approve/donor/{$id}");
652
    return $url;
653
}
654
655
656
/**
657
 * @return string
658
 * @since 1.0
659
 */
660
function getDonorsLink(array $args = [], View $context = null)
661
{
662
    $link = '';
663
664
    $args = array_merge([
665
        'content' => __('Donors'),
666
        'atts' => [],
667
        'before' => '',
668
        'after' => '',
669
    ], $args);
670
671
    if ($context) {
672
        $currentUser = $context->get('currentUser');
673
674
        if ('on' !== Options::getOption('site_publication') && (! $currentUser || ! $context->getAcl()->isUserAllowed($currentUser, 'Donor', 'read'))) {
675
            return $link;
676
        }
677
    }
678
679
    $args['atts']['href'] = getDonorsURL();
680
681
    if (! isset($args['atts']['class'])) {
682
        $args['atts']['class'] = 'view-link view-donors-link';
683
    }
684
685
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
686
    return $args['before'] . $link . $args['after'];
687
}
688
689
/**
690
 * @return string
691
 * @since 1.0
692
 */
693
function getDonorLink(array $args = [], View $context = null)
694
{
695
    $link = '';
696
697
    $args = array_merge([
698
        'content' => '',
699
        'atts' => [],
700
        'before' => '',
701
        'after' => '',
702
        'donor' => null,
703
    ], $args);
704
705
    if (! $args['donor'] instanceof Donor) {
706
        return $link;
707
    }
708
709
    if ($context) {
710
        $currentUser = $context->get('currentUser');
711
712
        if ('on' !== Options::getOption('site_publication') && (! $currentUser || ! $context->getAcl()->canReadEntity($currentUser, $args['donor']))) {
713
            return $link;
714
        }
715
    }
716
717
    if (empty($args['content'])) {
718
        $args['content'] = escHTML($args['donor']->get('name'));
719
    }
720
721
    $args['atts']['href'] = getDonorURL($args['donor']->get('id'));
722
723
    if (! isset($args['atts']['class'])) {
724
        $args['atts']['class'] = 'view-link view-donor-link';
725
    }
726
727
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
728
    return $args['before'] . $link . $args['after'];
729
}
730
731
/**
732
 * @return string
733
 * @since 1.0
734
 */
735
function getAddDonorLink(array $args = [], View $context = null)
736
{
737
    $link = '';
738
739
    $args = array_merge([
740
        'content' => __('Add'),
741
        'atts' => [],
742
        'before' => '',
743
        'after' => '',
744
    ], $args);
745
746
    if ($context) {
747
        $currentUser = $context->get('currentUser');
748
749
        if (! $currentUser || ! $context->getAcl()->isUserAllowed($currentUser, 'Donor', 'add')) {
750
            return $link;
751
        }
752
    }
753
754
    $args['atts']['href'] = getAddDonorURL();
755
756
    if (! isset($args['atts']['class'])) {
757
        $args['atts']['class'] = 'add-link add-donor-link';
758
    }
759
760
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
761
    return $args['before'] . $link . $args['after'];
762
}
763
764
/**
765
 * @return string
766
 * @since 1.0
767
 */
768
function getEditDonorsLink(array $args = [], View $context = null)
769
{
770
    $link = '';
771
772
    $args = array_merge([
773
        'content' => __('Edit'),
774
        'atts' => [],
775
        'before' => '',
776
        'after' => '',
777
    ], $args);
778
779
    if ($context) {
780
        $currentUser = $context->get('currentUser');
781
782
        if (! $currentUser || ! $context->getAcl()->isUserAllowed($currentUser, 'Donor', 'edit')) {
783
            return $link;
784
        }
785
    }
786
787
    $args['atts']['href'] = getEditDonorsURL();
788
789
    if (! isset($args['atts']['class'])) {
790
        $args['atts']['class'] = 'edit-link edit-donors-link';
791
    }
792
793
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
794
    return $args['before'] . $link . $args['after'];
795
}
796
797
/**
798
 * @return string
799
 * @since 1.0
800
 */
801
function getEditDonorLink(array $args, View $context = null)
802
{
803
    $link = '';
804
805
    $args = array_merge([
806
        'fallbackContent' => false,
807
        'content' => __('Edit'),
808
        'atts' => [],
809
        'before' => '',
810
        'after' => '',
811
        'donor' => null,
812
    ], $args);
813
814
    if (! $args['donor'] instanceof Donor) {
815
        return $args['fallbackContent'] ? $args['content'] : $link;
816
    }
817
818
    if ($context) {
819
        $currentUser = $context->get('currentUser');
820
821
        if (! $currentUser || ! $context->getAcl()->canEditEntity($currentUser, $args['donor'])) {
822
            return $args['fallbackContent'] ? $args['content'] : $link;
823
        }
824
    }
825
826
    $args['atts']['href'] = getEditDonorURL($args['donor']->get('id'));
827
828
    if (! isset($args['atts']['class'])) {
829
        $args['atts']['class'] = 'edit-link edit-donor-link';
830
    }
831
832
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
833
    return $args['before'] . $link . $args['after'];
834
}
835
836
/**
837
 * @return string
838
 * @since 1.0
839
 */
840
function getDeleteDonorLink(array $args, View $context = null)
841
{
842
    $link = '';
843
844
    $args = array_merge([
845
        'content' => __('Delete'),
846
        'atts' => [],
847
        'before' => '',
848
        'after' => '',
849
        'donor' => null,
850
    ], $args);
851
852
    if (! $args['donor'] instanceof Donor) {
853
        return $link;
854
    }
855
856
    if ($context) {
857
        $currentUser = $context->get('currentUser');
858
859
        if (! $currentUser || ! $context->getAcl()->canDeleteEntity($currentUser, $args['donor'])) {
860
            return $link;
861
        }
862
    }
863
864
    $args['atts']['href'] = getDeleteDonorURL($args['donor']->get('id'));
865
866
    if (! isset($args['atts']['class'])) {
867
        $args['atts']['class'] = 'delete-link delete-donor-link';
868
    }
869
870
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
871
    return $args['before'] . $link . $args['after'];
872
}
873
874
/**
875
 * @return string
876
 * @since 1.0
877
 */
878
function getApproveDonorLink(array $args, View $context = null)
879
{
880
    $link = '';
881
882
    $args = array_merge([
883
        'content' => __('Approve'),
884
        'atts' => [],
885
        'before' => '',
886
        'after' => '',
887
        'donor' => null,
888
    ], $args);
889
890
    if (! $args['donor'] instanceof Donor || ! $args['donor']->isPending()) {
891
        return $link;
892
    }
893
894
    if ($context) {
895
        $currentUser = $context->get('currentUser');
896
897
        if (! $currentUser || ! $context->getAcl()->canApproveDonor($currentUser, $args['donor'])) {
898
            return $link;
899
        }
900
    }
901
902
    $args['atts']['href'] = getApproveDonorURL($args['donor']->get('id'));
903
904
    if (! isset($args['atts']['class'])) {
905
        $args['atts']['class'] = 'approve-link approve-donor-link';
906
    }
907
908
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
909
    return $args['before'] . $link . $args['after'];
910
}
911
912
/*** Cities Template Tags *****************************************************/
913
914
/**
915
 * @return string
916
 * @since 1.0
917
 */
918
function getCitiesURL()
919
{
920
    $url = getSiteURL('/cities');
921
    return $url;
922
}
923
924
/**
925
 * @return string
926
 * @since 1.0
927
 */
928
function getAddCityURL()
929
{
930
    $url = getSiteURL('/add/city');
931
    return $url;
932
}
933
934
/**
935
 * @return string
936
 * @since 1.0
937
 */
938
function getEditCitiesURL()
939
{
940
    $url = getSiteURL('/edit/cities');
941
    return $url;
942
}
943
944
/**
945
 * @return string
946
 * @since 1.0
947
 */
948
function getEditCityURL($id)
949
{
950
    $url = '';
951
    $id = (int) $id;
952
953
    if (! isValidID($id)) {
954
        return $url;
955
    }
956
957
    $url = getSiteURL("/edit/city/{$id}");
958
    return $url;
959
}
960
961
/**
962
 * @return string
963
 * @since 1.0
964
 */
965
function getDeleteCityURL($id)
966
{
967
    $url = '';
968
    $id = (int) $id;
969
970
    if (! isValidID($id)) {
971
        return $url;
972
    }
973
974
    $url = getSiteURL("/delete/city/{$id}");
975
    return $url;
976
}
977
978
/**
979
 * @return string
980
 * @since 1.0
981
 */
982
function getCitiesLink(array $args = [], View $context = null)
983
{
984
    $link = '';
985
986
    $args = array_merge([
987
        'content' => __('Cities'),
988
        'atts' => [],
989
        'before' => '',
990
        'after' => '',
991
    ], $args);
992
993
    if ($context) {
994
        $currentUser = $context->get('currentUser');
995
996
        if ('on' !== Options::getOption('site_publication') && (! $currentUser || ! $context->getAcl()->isUserAllowed($currentUser, 'City', 'read'))) {
997
            return $link;
998
        }
999
    }
1000
1001
    $args['atts']['href'] = getCitiesURL();
1002
1003
    if (! isset($args['atts']['class'])) {
1004
        $args['atts']['class'] = 'view-link view-cities-link';
1005
    }
1006
1007
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
1008
    return $args['before'] . $link . $args['after'];
1009
}
1010
1011
/**
1012
 * @return string
1013
 * @since 1.0
1014
 */
1015
function getAddCityLink(array $args = [], View $context = null)
1016
{
1017
    $link = '';
1018
1019
    $args = array_merge([
1020
        'content' => __('Add'),
1021
        'atts' => [],
1022
        'before' => '',
1023
        'after' => '',
1024
    ], $args);
1025
1026
    if ($context) {
1027
        $currentUser = $context->get('currentUser');
1028
1029
        if (! $currentUser || ! $context->getAcl()->isUserAllowed($currentUser, 'City', 'add')) {
1030
            return $link;
1031
        }
1032
    }
1033
1034
    $args['atts']['href'] = getAddCityURL();
1035
1036
    if (! isset($args['atts']['class'])) {
1037
        $args['atts']['class'] = 'add-link add-city-link';
1038
    }
1039
1040
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
1041
    return $args['before'] . $link . $args['after'];
1042
}
1043
1044
/**
1045
 * @return string
1046
 * @since 1.0
1047
 */
1048
function getEditCitiesLink(array $args = [], View $context = null)
1049
{
1050
    $link = '';
1051
1052
    $args = array_merge([
1053
        'content' => __('Edit'),
1054
        'atts' => [],
1055
        'before' => '',
1056
        'after' => '',
1057
    ], $args);
1058
1059
    if ($context) {
1060
        $currentUser = $context->get('currentUser');
1061
1062
        if (! $currentUser || ! $context->getAcl()->isUserAllowed($currentUser, 'City', 'edit')) {
1063
            return $link;
1064
        }
1065
    }
1066
1067
    $args['atts']['href'] = getEditCitiesURL();
1068
1069
    if (! isset($args['atts']['class'])) {
1070
        $args['atts']['class'] = 'edit-link edit-cities-link';
1071
    }
1072
1073
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
1074
    return $args['before'] . $link . $args['after'];
1075
}
1076
1077
/**
1078
 * @return string
1079
 * @since 1.0
1080
 */
1081
function getEditCityLink(array $args, View $context = null)
1082
{
1083
    $link = '';
1084
1085
    $args = array_merge([
1086
        'fallbackContent' => false,
1087
        'content' => __('Edit'),
1088
        'atts' => [],
1089
        'before' => '',
1090
        'after' => '',
1091
        'city' => null,
1092
    ], $args);
1093
1094
    if (! $args['city'] instanceof City) {
1095
        return $args['fallbackContent'] ? $args['content'] : $link;
1096
    }
1097
1098
    if ($context) {
1099
        $currentUser = $context->get('currentUser');
1100
1101
        if (! $currentUser || ! $context->getAcl()->canEditEntity($currentUser, $args['city'])) {
1102
            return $args['fallbackContent'] ? $args['content'] : $link;
1103
        }
1104
    }
1105
1106
    $args['atts']['href'] = getEditCityURL($args['city']->get('id'));
1107
1108
    if (! isset($args['atts']['class'])) {
1109
        $args['atts']['class'] = 'edit-link edit-city-link';
1110
    }
1111
1112
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
1113
    return $args['before'] . $link . $args['after'];
1114
}
1115
1116
/**
1117
 * @return string
1118
 * @since 1.0
1119
 */
1120
function getDeleteCityLink(array $args, View $context = null)
1121
{
1122
    $link = '';
1123
1124
    $args = array_merge([
1125
        'content' => __('Delete'),
1126
        'atts' => [],
1127
        'before' => '',
1128
        'after' => '',
1129
        'city' => null,
1130
    ], $args);
1131
1132
    if (! $args['city'] instanceof City) {
1133
        return $link;
1134
    }
1135
1136
    if ($context) {
1137
        $currentUser = $context->get('currentUser');
1138
1139
        if (! $currentUser || ! $context->getAcl()->canDeleteEntity($currentUser, $args['city'])) {
1140
            return $link;
1141
        }
1142
    }
1143
1144
    $args['atts']['href'] = getDeleteCityURL($args['city']->get('id'));
1145
1146
    if (! isset($args['atts']['class'])) {
1147
        $args['atts']['class'] = 'delete-link delete-city-link';
1148
    }
1149
1150
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
1151
    return $args['before'] . $link . $args['after'];
1152
}
1153
1154
/*** Districts Template Tags ***************************************************/
1155
1156
/**
1157
 * @return string
1158
 * @since 1.0
1159
 */
1160
function getDistrictsURL()
1161
{
1162
    $url = getSiteURL('/districts');
1163
    return $url;
1164
}
1165
1166
/**
1167
 * @return string
1168
 * @since 1.0
1169
 */
1170
function getAddDistrictURL()
1171
{
1172
    $url = getSiteURL('/add/district');
1173
    return $url;
1174
}
1175
1176
/**
1177
 * @return string
1178
 * @since 1.0
1179
 */
1180
function getEditDistrictsURL()
1181
{
1182
    $url = getSiteURL('/edit/districts');
1183
    return $url;
1184
}
1185
1186
/**
1187
 * @return string
1188
 * @since 1.0
1189
 */
1190
function getEditDistrictURL($id)
1191
{
1192
    $url = '';
1193
    $id = (int) $id;
1194
1195
    if (! isValidID($id)) {
1196
        return $url;
1197
    }
1198
1199
    $url = getSiteURL("/edit/district/{$id}");
1200
    return $url;
1201
}
1202
1203
/**
1204
 * @return string
1205
 * @since 1.0
1206
 */
1207
function getDeleteDistrictURL($id)
1208
{
1209
    $url = '';
1210
    $id = (int) $id;
1211
1212
    if (! isValidID($id)) {
1213
        return $url;
1214
    }
1215
1216
    $url = getSiteURL("/delete/district/{$id}");
1217
    return $url;
1218
}
1219
1220
/**
1221
 * @return string
1222
 * @since 1.0
1223
 */
1224
function getDistrictsLink(array $args = [], View $context = null)
1225
{
1226
    $link = '';
1227
1228
    $args = array_merge([
1229
        'content' => __('Districts'),
1230
        'atts' => [],
1231
        'before' => '',
1232
        'after' => '',
1233
    ], $args);
1234
1235
    if ($context) {
1236
        $currentUser = $context->get('currentUser');
1237
1238
        if ('on' !== Options::getOption('site_publication') && (! $currentUser || ! $context->getAcl()->isUserAllowed($currentUser, 'District', 'read'))) {
1239
            return $link;
1240
        }
1241
    }
1242
1243
    $args['atts']['href'] = getDistrictsURL();
1244
1245
    if (! isset($args['atts']['class'])) {
1246
        $args['atts']['class'] = 'view-link view-districts-link';
1247
    }
1248
1249
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
1250
    return $args['before'] . $link . $args['after'];
1251
}
1252
1253
/**
1254
 * @return string
1255
 * @since 1.0
1256
 */
1257
function getAddDistrictLink(array $args = [], View $context = null)
1258
{
1259
    $link = '';
1260
1261
    $args = array_merge([
1262
        'content' => __('Add'),
1263
        'atts' => [],
1264
        'before' => '',
1265
        'after' => '',
1266
    ], $args);
1267
1268
    if ($context) {
1269
        $currentUser = $context->get('currentUser');
1270
1271
        if (! $currentUser || ! $context->getAcl()->isUserAllowed($currentUser, 'District', 'add')) {
1272
            return $link;
1273
        }
1274
    }
1275
1276
    $args['atts']['href'] = getAddDistrictURL();
1277
1278
    if (! isset($args['atts']['class'])) {
1279
        $args['atts']['class'] = 'add-link add-district-link';
1280
    }
1281
1282
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
1283
    return $args['before'] . $link . $args['after'];
1284
}
1285
1286
/**
1287
 * @return string
1288
 * @since 1.0
1289
 */
1290
function getEditDistrictsLink(array $args = [], View $context = null)
1291
{
1292
    $link = '';
1293
1294
    $args = array_merge([
1295
        'content' => __('Edit'),
1296
        'atts' => [],
1297
        'before' => '',
1298
        'after' => '',
1299
    ], $args);
1300
1301
    if ($context) {
1302
        $currentUser = $context->get('currentUser');
1303
1304
        if (! $currentUser || ! $context->getAcl()->isUserAllowed($currentUser, 'District', 'edit')) {
1305
            return $link;
1306
        }
1307
    }
1308
1309
    $args['atts']['href'] = getEditDistrictsURL();
1310
1311
    if (! isset($args['atts']['class'])) {
1312
        $args['atts']['class'] = 'edit-link edit-districts-link';
1313
    }
1314
1315
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
1316
    return $args['before'] . $link . $args['after'];
1317
}
1318
1319
/**
1320
 * @return string
1321
 * @since 1.0
1322
 */
1323
function getEditDistrictLink(array $args, View $context = null)
1324
{
1325
    $link = '';
1326
1327
    $args = array_merge([
1328
        'fallbackContent' => false,
1329
        'content' => __('Edit'),
1330
        'atts' => [],
1331
        'before' => '',
1332
        'after' => '',
1333
        'district' => null,
1334
    ], $args);
1335
1336
    if (! $args['district'] instanceof District) {
1337
        return $args['fallbackContent'] ? $args['content'] : $link;
1338
    }
1339
1340
    if ($context) {
1341
        $currentUser = $context->get('currentUser');
1342
1343
        if (! $currentUser || ! $context->getAcl()->canEditEntity($currentUser, $args['district'])) {
1344
            return $args['fallbackContent'] ? $args['content'] : $link;
1345
        }
1346
    }
1347
1348
    $args['atts']['href'] = getEditDistrictURL($args['district']->get('id'));
1349
1350
    if (! isset($args['atts']['class'])) {
1351
        $args['atts']['class'] = 'edit-link edit-district-link';
1352
    }
1353
1354
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
1355
    return $args['before'] . $link . $args['after'];
1356
}
1357
1358
/**
1359
 * @return string
1360
 * @since 1.0
1361
 */
1362
function getDeleteDistrictLink(array $args, View $context = null)
1363
{
1364
    $link = '';
1365
1366
    $args = array_merge([
1367
        'content' => __('Delete'),
1368
        'atts' => [],
1369
        'before' => '',
1370
        'after' => '',
1371
        'district' => null,
1372
    ], $args);
1373
1374
    if (! $args['district'] instanceof District) {
1375
        return $link;
1376
    }
1377
1378
    if ($context) {
1379
        $currentUser = $context->get('currentUser');
1380
1381
        if (! $currentUser || ! $context->getAcl()->canDeleteEntity($currentUser, $args['district'])) {
1382
            return $link;
1383
        }
1384
    }
1385
1386
    $args['atts']['href'] = getDeleteDistrictURL($args['district']->get('id'));
1387
1388
    if (! isset($args['atts']['class'])) {
1389
        $args['atts']['class'] = 'delete-link delete-district-link';
1390
    }
1391
1392
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
1393
    return $args['before'] . $link . $args['after'];
1394
}
1395
1396
/*** Settings Template Tags ***************************************************/
1397
1398
/**
1399
 * @return string
1400
 * @since 1.0
1401
 */
1402
function getSettingsURL()
1403
{
1404
    $url = getSiteURL('/settings');
1405
    return $url;
1406
}
1407
1408
/**
1409
 * @return string
1410
 * @since 1.0
1411
 */
1412
function getSettingsLink(array $args = [], View $context = null)
1413
{
1414
    $link = '';
1415
1416
    $args = array_merge([
1417
        'content' => __('Settings'),
1418
        'atts' => [],
1419
        'before' => '',
1420
        'after' => '',
1421
    ], $args);
1422
1423
    if ($context) {
1424
        $currentUser = $context->get('currentUser');
1425
1426
        if (! $currentUser || ! $context->getAcl()->isUserAllowed($currentUser, 'Setting', 'edit')) {
1427
            return $link;
1428
        }
1429
    }
1430
1431
    $args['atts']['href'] = getSettingsURL();
1432
1433
    if (! isset($args['atts']['class'])) {
1434
        $args['atts']['class'] = 'settings-link';
1435
    }
1436
1437
    $link = '<a' . toAttributes($args['atts']) . '>' . $args['content'] . '</a>';
1438
    return $args['before'] . $link . $args['after'];
1439
}
1440
1441
/*** General Template Tags ****************************************************/
1442
1443
/**
1444
 * @return array
1445
 * @since 1.0
1446
 */
1447
function getPaginationURLs(array $args)
1448
{
1449
    $urls = [];
1450
1451
    $args = array_merge([
1452
        'total'    => 1,
1453
        'base_url' => '',
1454
        'page_url' => '',
1455
    ], $args);
1456
1457
    $args['total'] = (int) $args['total'];
1458
1459
    if ($args['total'] <= 1) {
1460
        return $urls;
1461
    }
1462
1463
    $args['page_url'] = urldecode($args['page_url']);
1464
1465
    for ($n = 1; $n <= $args['total']; $n++) {
1466
        if (1 === $n) {
1467
            $urls[$n] = $args['base_url'];
1468
        } else {
1469
            $urls[$n] = str_replace('%#%', $n, $args['page_url']);
1470
        }
1471
    }
1472
1473
    return $urls;
1474
}
1475