aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/ObjectdisplayHelper.php
blob: 1d0bbc35f40d6c0c0ee562a209e8124459958ab6 (plain)
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
<?php

/**
 * ObjectdisplayHelper - utilityfunctions for object display
 *
 * Helps to output name with a link to the object.
 * Works for User and Course Objects
 *
 * ::link($object) produces the name with a link
 * ::avatarlink($object) produces the avatar and the name with a link
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Florian Bieringer <florian.bieringer@uni-passau.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 */
class ObjectdisplayHelper {

    /**
     * Produces the name with a link to the given object
     * @param User/Course The given object
     * @return string html code
     */
    public static function link($object) {
        return '<a href="' . self::map($object, 'link') . '">' . self::map($object, 'name') . '</a>';
    }

    /**
     * Produces the avatar and the name with a link to the given object
     * @param User/Course The given object
     * @return string html code
     */
    public static function avatarlink($object) {
        return '<a href="' . self::map($object, 'link') . '">' . self::map($object, 'avatar') . " " . self::map($object, 'name') . '</a>';
    }

    /**
     * Mapping function where to find what
     * @param type $object the object
     * @param type $function the called function
     * @return string output
     */
    private static function map($object, $function) {

        /**
         * If you want to add an object to the helper simply add to this array
         */
        $mapping = [
            'User' => [
                'link' => function($obj) {
            return URLHelper::getLink('dispatch.php/profile', ['username' => $obj->username]);
        },
                'name' => function($obj) {
            return htmlReady($obj->getFullname());
        },
                'avatar' => function($obj) {
            return Avatar::getAvatar($obj->id, $obj->username)->getImageTag(Avatar::SMALL,['title' => $obj->getFullname('no_title')]);
        }
            ],
            'Course' => [
                'link' => function($obj) {
            return URLHelper::getLink('seminar_main.php', ['auswahl' => $obj->id]);
        },
                'name' => function($obj) {
            return htmlReady($obj->name);
        },
                'avatar' => function($obj) {
            return CourseAvatar::getAvatar($obj->id)->getImageTag($size = CourseAvatar::SMALL,['title' => $obj->name]);
        }
            ]
        ];

        /*
         * Some php magic to call the right function if it exists
         */
        if ($object && $mapping[get_class($object)]) {
            return $mapping[get_class($object)][$function]($object);
        }
        return "";
    }

}