saveblogpost.json.php 3.47 KB
Newer Older
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
<?php
/**
 * This program is part of Mahara
 *
 *  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.
 *
 *  This program 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
 *
 * @package    mahara
 * @subpackage artefact-blog
 * @author     Richard Mansfield <richard.mansfield@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

define('INTERNAL', 1);
28
define('JSON', 1);
29
30
31
32

require(dirname(dirname(dirname(__FILE__))) . '/init.php');
global $USER;

33
34
json_headers();

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
$title      = param_variable('title');
$draft      = param_boolean('draft');
$createid   = param_integer('createid');
$blog       = param_integer('blog');
$blogpost   = param_integer('blogpost');
$uploads    = json_decode(param_variable('uploads'));
$artefacts  = json_decode(param_variable('artefacts'));
$body       = param_variable('body');

log_debug($title    );
log_debug($draft    );
log_debug($createid );
log_debug($blog     );
log_debug($blogpost );
log_debug($uploads  );
log_debug($artefacts);
log_debug($body     );

$userid = $USER->get('id');

safe_require('artefact', 'blog');

$postobj = new ArtefactTypeBlogPost($blogpost, null);
$postobj->set('title', $title);
$postobj->set('description', $body);
$postobj->set('published', !$draft);
if (!$blogpost) {
    $postobj->set('parent', $blog);
    $postobj->set('owner', $userid);
}
else if ($postobj->get('owner') != $userid) {
    json_reply('local', get_string('notowner'));
}
$postobj->commit();
$blogpost = $postobj->get('id');



// Delete old attachments in the db that no longer appear in the list
// of artefacts

if (!$old = get_column('artefact_blog_blogpost_file', 'file', 'blogpost', $blogpost)) {
    $old = array();
}

foreach ($old as $o) {
    if (!in_array($o, $artefacts)) {
        log_debug('old not in new ' . $o);
        delete_records('artefact_blog_blogpost_file', 'blogpost', $blogpost, 'file', $o);
    }
}



// Add new artefacts as attachments

foreach ($artefacts as $a) {
    if (!in_array($a, $old)) {
        $data = new StdClass;
        $data->blogpost = $blogpost;
        $data->file = $a;
        insert_record('artefact_blog_blogpost_file', $data);
    }
}



// Add the newly uploaded files to myfiles and then to the blog post.

if (!empty($uploads)) {

    // Create the blogfiles folder if it doesn't exist yet.
    $blogfilesid = ArtefactTypeBlogPost::blogfiles_folder_id();
    if (!$blogfilesid) {
        json_reply('local', get_string('erroraccessingblogfilesfolder', 'artefact.blog'));
    }

    // Turn all the uploaded files into artefacts.
    foreach ($uploads as $upload) {
        if (!$postobj->save_attachment(session_id() . $createid, $upload->id,
                                       $upload->title, $upload->description)) {
            json_reply('local', get_string('errorsavingattachments', 'artefact.blog'));
        }
    }
}



json_reply(false, 'foo');

125
?>