aboutsummaryrefslogtreecommitdiff
path: root/lib/models/resources/ResourceProperty.php
blob: 4f7a3299bc480e65d439b29e492259f7d5b68ddb (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
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
<?php

/**
 * ResourceProperty.php - model class for resource properties
 *
 * 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      Moritz Strohm <strohm@data-quest.de>
 * @copyright   2017
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @package     resources
 * @since       TODO
 *
 * @property array $id alias for pk
 * @property string $resource_id database column
 * @property string $property_id database column
 * @property string $state database column
 * @property int $mkdate database column
 * @property int $chdate database column
 * @property ResourcePropertyDefinition $definition belongs_to ResourcePropertyDefinition
 * @property Resource $resource belongs_to Resource
 * @property mixed $name additional field
 * @property-read mixed $fullname additional field
 * @property mixed $display_name additional field
 * @property mixed $type additional field
 * @property mixed $info_label additional field
 */
class ResourceProperty extends SimpleORMap
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'resource_properties';

        $config['belongs_to']['definition'] = [
            'class_name' => ResourcePropertyDefinition::class,
            'foreign_key' => 'property_id',
            'assoc_func' => 'find'
        ];

        $config['belongs_to']['resource'] = [
            'class_name' => Resource::class,
            'foreign_key' => 'resource_id',
            'assoc_func' => 'find'
        ];

        $config['additional_fields']['name'] = ['definition', 'name'];
        $config['additional_fields']['fullname'] = ['get' => 'getFullName'];
        $config['additional_fields']['display_name'] = ['definition', 'display_name'];
        $config['additional_fields']['type'] = ['definition', 'type'];
        $config['additional_fields']['info_label'] = ['definition', 'info_label'];

        parent::configure($config);
    }

    /**
     * Determines whether this resource property is requestable
     * by checking the requestable flag of the corresponding
     * resource category property.
     *
     * @return bool True, if this property is requestable, false otherwise.
     */
    public function isRequestable()
    {
        //We must only check if there is a ResourceCategoryProperty
        //object in the database which has the same category-ID
        //as the Resource of this ResourceProperty object,
        //the same definition-ID as this ResourceProperty and which has
        //furthermore the requestable column set to '1'.
        //In this case we return true, otherwise false.

        return ResourceCategoryProperty::countBySql(
            "category_id = :category_id
            AND property_id = :definition_id
            AND requestable = '1'
            ",
            [
                'category_id' => $this->resource->category_id,
                'definition_id' => $this->definition->id
            ]
        ) > 0;
    }


    /**
     * Creates a string containing the appropriate value
     * of this resource property.
     *
     * @return string The string representation of this resource property.
     */
    public function __toString()
    {
        $string = '';

        if ($this->type == 'position') {
            $string .= ResourceManager::getPositionString($this);
        } elseif ($this->type == 'bool') {
            $string .= $this->state ? _('ja') : _('nein');
        } elseif ($this->type == 'num') {
            $string .= $this->state;
        } elseif ($this->type == 'user') {
            $user = User::find($this->state);
            if ($user) {
                $string .= $user->getFullName();
            } else {
                $string .= _('unbekannt');
            }
        } else {
            $string .= $this->state;
        }
        return $string;
    }

    public function getFullName()
    {
        $name = (string)$this->definition->display_name ?: $this->definition->name;
        $category = trim(strstr($name, ':', true));
        if ($category) {
            $name = trim(strstr($name, ':'), ' :');
        }
        $value = (string)$this;
        return $name . ': ' . $value;
    }
}