srchub

srchub Git Source Tree


Root/indefero/src/IDF/Views/Api.php

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
128
129
130
131
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of InDefero, an open source project management application.
# Copyright (C) 2008-2011 Céondo Ltd and contributors.
#
# InDefero 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.
#
# InDefero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
/**
 * API views.
 *
 * These are just small wrappers around the "normal" views. The normal
 * views are called with a third parameters $api set to true to return
 * JSON instead of HTML.
 *
 * A special precondition is used to set the $request->user from the
 * _login, _hash and _salt parameters.
 */
class IDF_Views_Api
{
    /**
     * View list of issues for a given project.
     */
    public $issuesIndex_precond = array('IDF_Precondition::apiSetUser',
                                        'IDF_Precondition::accessIssues');
    public function issuesIndex($request, $match)
    {
        $views = new IDF_Views_Issue();
        $p = $views->index($request, $match, true);
        $out = array(
                     'project' => $request->project->shortname,
                     'open' => $p['open'],
                     'closed' => $p['closed'],
                     'issues' => $p['issues']->render_array(),
                     );
        return new Pluf_HTTP_Response_Json($out);
    }
 
    /**
     * Create a new issue.
     */
    public $issueCreate_precond = array('IDF_Precondition::apiSetUser',
                                        'Pluf_Precondition::loginRequired',
                                        'IDF_Precondition::accessIssues');
    public function issueCreate($request, $match)
    {
        $views = new IDF_Views_Issue();
        $p = $views->create($request, $match, true);
        $out = array();
        if ($request->method == 'GET') {
            // We give the details of the form
            $out['help'] = 'A POST request against this url will allow you to create a new issue.';
            if ($request->user->hasPerm('IDF.project-owner', $request->project)
                or $request->user->hasPerm('IDF.project-member', $request->project)) {
                $out['status'] = array();
                foreach (self::getTags($request->project) as $tag) {
                    $out['status'][] = $tag->name;
                }
            }
 
        } else {
            // We need to give back the results of the creation
            if (is_object($p) and 'IDF_Issue' == get_class($p)) {
                $out['message'] = 'success';
                $out['issue'] = $p->id;
            } else {
                $out['message'] = 'error';
                $out['errors'] = $p['form']->errors;
            }
        }
        return new Pluf_HTTP_Response_Json($out);
    }
 
    /**
     * List all the projects
     */
    public $projectIndex_precond = array('IDF_Precondition::apiSetUser');
 
    public function projectIndex($request, $match)
    {
        $projects = IDF_Views::getProjects($request->user);
 
        $data = array();
        foreach ($projects as $p) {
            $data[] = array("shortname" => $p->shortname, "name" => $p->name, "shortdesc" => $p->shortdesc, "private" => $p->private);
        }
 
        $out = array();
        $out['message'] = 'success';
        $out['projects'] = $data;
        return new Pluf_HTTP_Response_Json($out);
    }
 
    /**
     * Get the list of tags to give them to the end users when doing a
     * GET request against a form. That way it is possible for them to
     * know which tags/labels are available.
     *
     * @param IDF_Project Current project
     * @param string Which tags to get ('issue-open')
     * @return ArrayObject Tags
     */
 
    public static function getTags($project, $what='issue-open')
    {
        switch ($what) {
        case 'issue-open':
            $key = 'labels_issue_open';
            $default = IDF_Form_IssueTrackingConf::init_open;
            return $project->getTagsFromConfig($key, $default);
        case 'issue-closed':
            return $project->getTagIdsByStatus('closed');
        }
        return array();
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.21853s using 11 queries.