post.php 11.8 KB
Newer Older
1
2
3
4
5
<?php
/**
 *
 * @package    mahara
 * @subpackage artefact-blog
6
 * @author     Catalyst IT Ltd
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
9
10
11
12
 *
 */

define('INTERNAL', 1);
13
14
15
16
define('SECTION_PLUGINTYPE', 'artefact');
define('SECTION_PLUGINNAME', 'blog');
define('SECTION_PAGE', 'post');

17
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
18
require_once('license.php');
Martyn Smith's avatar
Martyn Smith committed
19

20
safe_require('artefact', 'blog');
21
safe_require('artefact', 'file');
22
if (!PluginArtefactBlog::is_active()) {
23
    throw new AccessDeniedException(get_string('plugindisableduser', 'mahara', get_string('Blog','artefact.blog')));
24
}
Aaron Wells's avatar
Aaron Wells committed
25
/*
Richard Mansfield's avatar
Richard Mansfield committed
26
27
28
29
 * For a new post, the 'blog' parameter will be set to the blog's
 * artefact id.  For an existing post, the 'blogpost' parameter will
 * be set to the blogpost's artefact id.
 */
30
$blogpost = param_integer('blogpost', param_integer('id', 0));
31
if (!$blogpost) {
32
33
34
35
/*
 *  For a new post, a tag can be set from tagged blogpost block
 */
    $tagselect = param_variable('tagselect', '');
36
    $blog = param_integer('blog');
37
    $blogobj = new ArtefactTypeBlog($blog);
38
    $blogobj->check_permission(true);
39

40
41
    $title = '';
    $description = '';
42
    $tags = array_map('trim', explode(",", $tagselect));
43
    $checked = '';
Nigel McNie's avatar
Nigel McNie committed
44
    $pagetitle = get_string('newblogpost', 'artefact.blog', get_field('artefact', 'title', 'id', $blog));
45
    $focuselement = 'title';
46
    $attachments = array();
47
    if ($blogobj->get('group')) {
48
        $group = get_group_by_id($blogobj->get('group'));
49
        define('TITLE', $group->name);
50
        define('SUBSECTIONHEADING', $pagetitle);
51
52
53
54
    }
    else {
        define('TITLE', $pagetitle);
    }
55
56
}
else {
57
    $blogpostobj = new ArtefactTypeBlogPost($blogpost);
58
    $blogpostobj->check_permission(true);
59
60
    $pagetitle = get_string('editblogpost', 'artefact.blog');
    if ($blogpostobj->get('group')) {
61
        $group = get_group_by_id($blogpostobj->get('group'));
62
63
64
65
66
67
        define('SUBSECTIONHEADING', $pagetitle);
        define('TITLE', $group->name);
    }
    else {
        define('TITLE', $pagetitle);
    }
68
69
70
    if ($blogpostobj->get('locked')) {
        throw new AccessDeniedException(get_string('submittedforassessment', 'view'));
    }
71
72
73
    $blog = $blogpostobj->get('parent');
    $title = $blogpostobj->get('title');
    $description = $blogpostobj->get('description');
74
    $tags = $blogpostobj->get('tags');
75
    $checked = !$blogpostobj->get('published');
76

77
    $focuselement = 'description'; // Doesn't seem to work with tinyMCE.
78
    $attachments = $blogpostobj->attachment_id_list();
79
80
}

81
$institution = $institutionname = $groupid = null;
82
83
84
85
86
$blogobj = new ArtefactTypeBlog($blog);
if ($blogobj->get('institution')) {
    $institution = true;
    $institutionname = $blogobj->get('institution');
}
87
88
89
90
else if ($blogobj->get('group')) {
    $groupid = $blogobj->get('group');
}
PluginArtefactBlog::set_blog_nav($institution, $institutionname, $groupid);
91

92
$folder = param_integer('folder', 0);
93
94
95
96
97
98
$browse = (int) param_variable('browse', 0);
$highlight = null;
if ($file = param_integer('file', 0)) {
    $highlight = array($file);
}

99

100
101
102
103
104
105
$form = pieform(array(
    'name'               => 'editpost',
    'method'             => 'post',
    'autofocus'          => $focuselement,
    'jsform'             => true,
    'newiframeonsubmit'  => true,
106
107
    'jssuccesscallback'  => 'editpost_callback',
    'jserrorcallback'    => 'editpost_callback',
108
109
110
    'plugintype'         => 'artefact',
    'pluginname'         => 'blog',
    'configdirs'         => array(get_config('libroot') . 'form/', get_config('docroot') . 'artefact/file/form/'),
111
    'elements' => array(
112
        'blog' => array(
113
114
115
            'type' => 'hidden',
            'value' => $blog,
        ),
116
        'blogpost' => array(
117
            'type' => 'hidden',
118
            'value' => $blogpost,
119
120
121
122
123
124
125
        ),
        'title' => array(
            'type' => 'text',
            'title' => get_string('posttitle', 'artefact.blog'),
            'rules' => array(
                'required' => true
            ),
126
            'defaultvalue' => $title,
127
128
129
        ),
        'description' => array(
            'type' => 'wysiwyg',
130
            'rows' => 20,
131
            'cols' => 70,
132
133
134
            'title' => get_string('postbody', 'artefact.blog'),
            'description' => get_string('postbodydesc', 'artefact.blog'),
            'rules' => array(
135
                'maxlength' => 65536,
136
137
                'required' => true
            ),
138
            'defaultvalue' => $description,
139
        ),
140
141
142
143
144
        'tags'       => array(
            'defaultvalue' => $tags,
            'type'         => 'tags',
            'title'        => get_string('tags'),
            'description'  => get_string('tagsdesc'),
145
            'help' => true,
146
            'institution'  => $institutionname,
147
        ),
148
149
        'license' => license_form_el_basic(isset($blogpostobj) ? $blogpostobj : null),
        'licensing_advanced' => license_form_el_advanced(isset($blogpostobj) ? $blogpostobj : null),
150
151
152
153
        'filebrowser' => array(
            'type'         => 'filebrowser',
            'title'        => get_string('attachments', 'artefact.blog'),
            'folder'       => $folder,
154
            'highlight'    => $highlight,
155
            'institution'  => $institutionname,
156
            'group'        => $groupid,
157
            'browse'       => $browse,
158
            'page'         => get_config('wwwroot') . 'artefact/blog/post.php?' . ($blogpost ? ('id=' . $blogpost) : ('blog=' . $blog)) . '&browse=1',
159
            'browsehelp'   => 'browsemyfiles',
160
161
            'config'       => array(
                'upload'          => true,
162
                'uploadagreement' => get_config_plugin('artefact', 'file', 'uploadagreement'),
163
164
                'resizeonuploaduseroption' => get_config_plugin('artefact', 'file', 'resizeonuploaduseroption'),
                'resizeonuploaduserdefault' => $USER->get_account_preference('resizeonuploaduserdefault'),
165
166
167
168
                'createfolder'    => false,
                'edit'            => false,
                'select'          => true,
            ),
169
170
            'defaultvalue'       => $attachments,
            'selectlistcallback' => 'artefact_get_records_by_id',
171
172
            'selectcallback'     => 'add_attachment',
            'unselectcallback'   => 'delete_attachment',
173
174
        ),
        'draft' => array(
175
            'type' => 'switchbox',
176
177
178
179
180
            'title' => get_string('draft', 'artefact.blog'),
            'description' => get_string('thisisdraftdesc', 'artefact.blog'),
            'defaultvalue' => $checked,
            'help' => true,
        ),
181
        'allowcomments' => array(
182
            'type'         => 'switchbox',
183
184
185
186
            'title'        => get_string('allowcomments','artefact.comment'),
            'description'  => get_string('allowcommentsonpost','artefact.blog'),
            'defaultvalue' => $blogpost ? $blogpostobj->get('allowcomments') : 1,
        ),
187
188
        'submitpost' => array(
            'type' => 'submitcancel',
189
            'class' => 'btn-primary',
190
191
192
            'value' => array(get_string('savepost', 'artefact.blog'), get_string('cancel')),
            'goto' => get_config('wwwroot') . 'artefact/blog/view/index.php?id=' . $blog,
        )
193
194
195
    )
));

Richard Mansfield's avatar
Richard Mansfield committed
196
197
198
199
/*
 * Javascript specific to this page.  Creates the list of files
 * attached to the blog post.
 */
200
201
$wwwroot = get_config('wwwroot');
$noimagesmessage = json_encode(get_string('noimageshavebeenattachedtothispost', 'artefact.blog'));
202

203
$javascript = <<<EOF
204
205
function editpost_callback(form, data) {
    editpost_filebrowser.callback(form, data);
206
};
207
EOF;
208

209
$smarty = smarty(array(), array(), array(), array(
210
211
212
    'tinymceconfig' => '
        image_filebrowser: "editpost_filebrowser",
    ',
213
214
215
216
217
    'sideblocks' => array(
        array(
            'name'   => 'quota',
            'weight' => -10,
            'data'   => array(),
218
        ),
219
    ),
220
));
221
$smarty->assign('INLINEJAVASCRIPT', $javascript);
222
$smarty->assign('form', $form);
223
224
$smarty->display('artefact:blog:editpost.tpl');

225
226


Aaron Wells's avatar
Aaron Wells committed
227
/**
228
229
230
231
 * This function get called to cancel the form submission. It returns to the
 * blog list.
 */
function editpost_cancel_submit() {
232
    global $blog;
233
234
235
236
    $blogobj = new ArtefactTypeBlog($blog);
    if ($blogobj->get('institution')) {
        redirect(get_config('wwwroot') . 'artefact/blog/view/index.php?id=' . $blog . '&institution=' . $blogobj->get('institution'));
    }
237
238
239
    else if ($blogobj->get('group')) {
        redirect(get_config('wwwroot') . 'artefact/blog/view/index.php?id=' . $blog . '&group=' . $blogobj->get('group'));
    }
240
241
242
    else {
        redirect(get_config('wwwroot') . 'artefact/blog/view/index.php?id=' . $blog);
    }
243
244
245
}

function editpost_submit(Pieform $form, $values) {
Richard Mansfield's avatar
Richard Mansfield committed
246
    global $USER, $SESSION, $blogpost, $blog;
247

248
    require_once('embeddedimage.php');
249
250
251
252
253
    db_begin();
    $postobj = new ArtefactTypeBlogPost($blogpost, null);
    $postobj->set('title', $values['title']);
    $postobj->set('description', $values['description']);
    $postobj->set('tags', $values['tags']);
254
255
256
257
258
    if (get_config('licensemetadata')) {
        $postobj->set('license', $values['license']);
        $postobj->set('licensor', $values['licensor']);
        $postobj->set('licensorurl', $values['licensorurl']);
    }
259
    $postobj->set('published', !$values['draft']);
260
    $postobj->set('allowcomments', (int) $values['allowcomments']);
261
262
    if (!$blogpost) {
        $postobj->set('parent', $blog);
263
264
265
266
        $blogobj = new ArtefactTypeBlog($blog);
        if ($blogobj->get('institution')) {
            $postobj->set('institution', $blogobj->get('institution'));
        }
267
268
269
        else if ($blogobj->get('group')) {
            $postobj->set('group', $blogobj->get('group'));
        }
270
271
272
        else {
            $postobj->set('owner', $USER->id);
        }
273
274
275
276
    }
    $postobj->commit();
    $blogpost = $postobj->get('id');

277
278
279
    // Need to wait until post is saved in case we are a new blogpost before we can sort out embedded images as we need an id
    $postobj->set('description', EmbeddedImage::prepare_embedded_images($values['description'], 'blogpost', $postobj->get('id')));

280
281
282
283
    // Attachments
    $old = $postobj->attachment_id_list();
    // $new = is_array($values['filebrowser']['selected']) ? $values['filebrowser']['selected'] : array();
    $new = is_array($values['filebrowser']) ? $values['filebrowser'] : array();
284
285
286
287
288
289
290
    // only allow the attaching of files that exist and are editable by user
    foreach ($new as $key => $fileid) {
        $file = artefact_instance_from_id($fileid);
        if (!($file instanceof ArtefactTypeFile) || !$USER->can_publish_artefact($file)) {
            unset($new[$key]);
        }
    }
291
292
293
    if (!empty($new) || !empty($old)) {
        foreach ($old as $o) {
            if (!in_array($o, $new)) {
294
295
296
297
                try {
                    $postobj->detach($o);
                }
                catch (ArtefactNotFoundException $e) {}
298
299
            }
        }
300
301
        foreach ($new as $n) {
            if (!in_array($n, $old)) {
302
303
304
305
                try {
                    $postobj->attach($n);
                }
                catch (ArtefactNotFoundException $e) {}
306
            }
307
308
        }
    }
309
    $postobj->commit();
310
    db_commit();
311

312
313
    $result = array(
        'error'   => false,
314
315
        'message' => get_string('blogpostsaved', 'artefact.blog'),
        'goto'    => get_config('wwwroot') . 'artefact/blog/view/index.php?id=' . $blog,
316
    );
317
318
319
320
    if ($form->submitted_by_js()) {
        // Redirect back to the blog page from within the iframe
        $SESSION->add_ok_msg($result['message']);
        $form->json_reply(PIEFORM_OK, $result, false);
321
    }
322
    $form->reply(PIEFORM_OK, $result);
323
324
}

325
326
327
328
329
330
331
332
333
334
335
336
337
function add_attachment($attachmentid) {
    global $blogpostobj;
    if ($blogpostobj) {
        $blogpostobj->attach($attachmentid);
    }
}

function delete_attachment($attachmentid) {
    global $blogpostobj;
    if ($blogpostobj) {
        $blogpostobj->detach($attachmentid);
    }
}