summaryrefslogtreecommitdiff
path: root/app/Api/Project.php
blob: faf2a3da92e83e44f9f4c2666844ac9c6cb86dcd (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
<?php

namespace Api;

/**
 * Project API controller
 *
 * @package  api
 * @author   Frederic Guillot
 */
class Project extends Base
{
    public function getProjectById($project_id)
    {
        return $this->formatProject($this->project->getById($project_id));
    }

    public function getProjectByName($name)
    {
        return $this->formatProject($this->project->getByName($name));
    }

    public function getAllProjects()
    {
        return $this->formatProjects($this->project->getAll());
    }

    public function removeProject($project_id)
    {
        return $this->project->remove($project_id);
    }

    public function enableProject($project_id)
    {
        return $this->project->enable($project_id);
    }

    public function disableProject($project_id)
    {
        return $this->project->disable($project_id);
    }

    public function enableProjectPublicAccess($project_id)
    {
        return $this->project->enablePublicAccess($project_id);
    }

    public function disableProjectPublicAccess($project_id)
    {
        return $this->project->disablePublicAccess($project_id);
    }

    public function getProjectActivities(array $project_ids)
    {
        return $this->projectActivity->getProjects($project_ids);
    }

    public function getProjectActivity($project_id)
    {
        return $this->projectActivity->getProject($project_id);
    }

    public function createProject($name, $description = null)
    {
        $values = array(
            'name' => $name,
            'description' => $description
        );

        list($valid,) = $this->project->validateCreation($values);
        return $valid ? $this->project->create($values) : false;
    }

    public function updateProject($id, $name, $description = null)
    {
        $values = array(
            'id' => $id,
            'name' => $name,
            'description' => $description
        );

        list($valid,) = $this->project->validateModification($values);
        return $valid && $this->project->update($values);
    }

    private function formatProject($project)
    {
        if (! empty($project)) {
            $project['url'] = array(
                'board' => $this->helper->url->base().$this->helper->url->to('board', 'show', array('project_id' => $project['id'])),
                'calendar' => $this->helper->url->base().$this->helper->url->to('calendar', 'show', array('project_id' => $project['id'])),
                'list' => $this->helper->url->base().$this->helper->url->to('listing', 'show', array('project_id' => $project['id'])),
            );
        }

        return $project;
    }

    private function formatProjects($projects)
    {
        if (! empty($projects)) {
            foreach ($projects as &$project) {
                $project = $this->formatProject($project);
            }
        }

        return $projects;
    }
}