SoftwareService.php 3.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
/**
 * gredu_labs
 * 
 * @link https://github.com/eellak/gredu_labs for the canonical source repository
 * @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
 * @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
 */

namespace GrEduLabs\Schools\Service;

use RedBeanPHP\R;

class SoftwareService implements SoftwareServiceInterface
{

    public function createSoftwareCategory($name)
    {
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
19
        $software_category       = R::dispense('softwarecategory');
20
21
22
23
24
25
26
        $software_category->name = $name;
        R::store($software_category);
    }

    public function getSoftwareCategoryById($id)
    {
        $software_category = R::load('softwarecategory', $id);
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
27

28
29
30
31
32
33
        return $software_category->export();
    }

    public function getSoftwareCategories()
    {
        $software_categories = R::findAll('softwarecategory');
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
34
35
36
37

        return array_map(function ($bean) {
            return $bean->export();
        }, $software_categories);
38
39
40
41
    }

    public function updateSoftwareCategory($id, $data)
    {
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
42
        $software_category       = R::load('softwarecategory');
43
44
45
46
47
48
49
50
51
        $software_category->name = $name;
        R::store($software_category);
    }

    public function createSoftware(array $data)
    {
        unset($data['id']);
        $software = R::dispense('software');
        $this->persistSoftware($software, $data);
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
52
53

        return $this->export($software);
54
55
56
57
58
59
60
61
    }

    public function updateSoftware(array $data, $id)
    {
        $software = R::load('software', $id);
        if (!$software->id) {
            throw new \InvalidArgumentException('No software found');
        }
62
        $this->persistSoftware($software, $data);
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
63
64

        return $this->export($software);
65
66
67
68
    }

    private function persistSoftware($software, array $data)
    {
69
70
        if (!isset($data['lab_id']) || empty($data['lab_id'])) {
            $data['lab_id'] = null;
71
        }
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
72

73
        $software->softwarecategory_id  = $data['softwarecategory_id'];
74
75
76
77
78
        $software->school_id            = $data['school_id'];
        $software->lab_id               = $data['lab_id'];
        $software->title                = $data['title'];
        $software->vendor               = $data['vendor'];
        $software->url                  = $data['url'];
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
79

80
81
82
83
84
85
        R::store($software);
    }

    public function getSoftwareById($id)
    {
        $software = R::load('software', $id);
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
86
87

        return $this->export($software);
88
89
90
91
92
    }

    public function getSoftwareBySchoolId($id)
    {
        $software = R::findAll('software', 'school_id = ?', [$id]);
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
93

94
        return $this->exportAll($software);
95
96
97
98
99
    }

    public function getSoftwareByLabId($id)
    {
        $software = R::findAll('software', 'lab_id = ?', [$id]);
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
100

101
102
        return $software->exportAll();
    }
103

104
105
106
107
108
    public function removeSoftware($id)
    {
        R::trash('software', $id);
    }

Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
109
110
111
112
113
114
115
116
117
118
119
120
121
    private function export($bean)
    {
        $softwareCategory     = $bean->softwarecategory;
        $softwareCategoryName = ($softwareCategory) ? $softwareCategory->name : '';
        $lab                  = $bean->lab;
        $labName              = ($lab) ? $lab->name : '';

        return array_merge($bean->export(), [
            'softwarecategory' => $softwareCategoryName,
            'lab'              => $labName,
        ]);
    }

122
123
    private function exportAll($beans)
    {
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
124
        return array_map([$this, 'export'], $beans);
125
    }
126
}