post.php 11.6 KB
Newer Older
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
6
 *
Francois Marier's avatar
Francois Marier committed
7
8
9
10
 * 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 3 of the License, or
 * (at your option) any later version.
11
 *
Francois Marier's avatar
Francois Marier committed
12
13
14
15
 * 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.
16
 *
Francois Marier's avatar
Francois Marier committed
17
18
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
21
 *
 * @package    mahara
 * @subpackage artefact-blog
22
 * @author     Catalyst IT Ltd
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
25
26
27
28
 *
 */

define('INTERNAL', 1);
29
define('MENUITEM', 'content/blogs');
30
31
32
33
define('SECTION_PLUGINTYPE', 'artefact');
define('SECTION_PLUGINNAME', 'blog');
define('SECTION_PAGE', 'post');

34
35
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once('pieforms/pieform.php');
Martyn Smith's avatar
Martyn Smith committed
36

37
safe_require('artefact', 'blog');
38
safe_require('artefact', 'file');
39

Richard Mansfield's avatar
Richard Mansfield committed
40
41
42
43
44
/* 
 * 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.
 */
45
$blogpost = param_integer('blogpost', param_integer('id', 0));
46
if (!$blogpost) {
47
    $blog = param_integer('blog');
48
49
50
51
52
53
    if (!get_record('artefact', 'id', $blog, 'owner', $USER->get('id'))) {
        // Blog security is also checked closer to when blogs are added, this 
        // check ensures that malicious users do not even see the screen for 
        // adding a post to a blog that is not theirs
        throw new AccessDeniedException(get_string('youarenottheownerofthisblog', 'artefact.blog'));
    }
54
55
    $title = '';
    $description = '';
56
    $tags = array();
57
    $checked = '';
Nigel McNie's avatar
Nigel McNie committed
58
    $pagetitle = get_string('newblogpost', 'artefact.blog', get_field('artefact', 'title', 'id', $blog));
59
    $focuselement = 'title';
60
    $attachments = array();
61
    define('TITLE', $pagetitle);
62
63
}
else {
64
    $blogpostobj = new ArtefactTypeBlogPost($blogpost);
65
    $blogpostobj->check_permission();
66
67
68
    if ($blogpostobj->get('locked')) {
        throw new AccessDeniedException(get_string('submittedforassessment', 'view'));
    }
69
70
71
    $blog = $blogpostobj->get('parent');
    $title = $blogpostobj->get('title');
    $description = $blogpostobj->get('description');
72
    $tags = $blogpostobj->get('tags');
73
    $checked = !$blogpostobj->get('published');
Nigel McNie's avatar
Nigel McNie committed
74
    $pagetitle = get_string('editblogpost', 'artefact.blog');
75
    $focuselement = 'description'; // Doesn't seem to work with tinyMCE.
76
    $attachments = $blogpostobj->attachment_id_list();
77
    define('TITLE', get_string('editblogpost','artefact.blog'));
78
79
}

80
$folder = param_integer('folder', 0);
81
82
83
84
85
86
$browse = (int) param_variable('browse', 0);
$highlight = null;
if ($file = param_integer('file', 0)) {
    $highlight = array($file);
}

87

88
89
90
91
92
93
$form = pieform(array(
    'name'               => 'editpost',
    'method'             => 'post',
    'autofocus'          => $focuselement,
    'jsform'             => true,
    'newiframeonsubmit'  => true,
94
95
    'jssuccesscallback'  => 'editpost_callback',
    'jserrorcallback'    => 'editpost_callback',
96
97
98
    'plugintype'         => 'artefact',
    'pluginname'         => 'blog',
    'configdirs'         => array(get_config('libroot') . 'form/', get_config('docroot') . 'artefact/file/form/'),
99
    'elements' => array(
100
        'blog' => array(
101
102
103
            'type' => 'hidden',
            'value' => $blog,
        ),
104
        'blogpost' => array(
105
            'type' => 'hidden',
106
            'value' => $blogpost,
107
108
109
110
111
112
113
        ),
        'title' => array(
            'type' => 'text',
            'title' => get_string('posttitle', 'artefact.blog'),
            'rules' => array(
                'required' => true
            ),
114
            'defaultvalue' => $title,
115
116
117
        ),
        'description' => array(
            'type' => 'wysiwyg',
118
            'rows' => 20,
119
            'cols' => 70,
120
121
122
            'title' => get_string('postbody', 'artefact.blog'),
            'description' => get_string('postbodydesc', 'artefact.blog'),
            'rules' => array(
123
                'maxlength' => 65536,
124
125
                'required' => true
            ),
126
            'defaultvalue' => $description,
127
        ),
128
129
130
131
132
        'tags'       => array(
            'defaultvalue' => $tags,
            'type'         => 'tags',
            'title'        => get_string('tags'),
            'description'  => get_string('tagsdesc'),
133
            'help' => true,
134
        ),
135
136
137
138
        'filebrowser' => array(
            'type'         => 'filebrowser',
            'title'        => get_string('attachments', 'artefact.blog'),
            'folder'       => $folder,
139
140
            'highlight'    => $highlight,
            'browse'       => $browse,
141
            'page'         => get_config('wwwroot') . 'artefact/blog/post.php?' . ($blogpost ? ('id=' . $blogpost) : ('blog=' . $blog)) . '&browse=1',
142
            'browsehelp'   => 'browsemyfiles',
143
144
            'config'       => array(
                'upload'          => true,
145
                'uploadagreement' => get_config_plugin('artefact', 'file', 'uploadagreement'),
146
147
148
149
                'createfolder'    => false,
                'edit'            => false,
                'select'          => true,
            ),
150
151
            'defaultvalue'       => $attachments,
            'selectlistcallback' => 'artefact_get_records_by_id',
152
153
            'selectcallback'     => 'add_attachment',
            'unselectcallback'   => 'delete_attachment',
154
155
156
157
158
159
160
161
        ),
        'draft' => array(
            'type' => 'checkbox',
            'title' => get_string('draft', 'artefact.blog'),
            'description' => get_string('thisisdraftdesc', 'artefact.blog'),
            'defaultvalue' => $checked,
            'help' => true,
        ),
162
163
164
165
166
167
        'allowcomments' => array(
            'type'         => 'checkbox',
            'title'        => get_string('allowcomments','artefact.comment'),
            'description'  => get_string('allowcommentsonpost','artefact.blog'),
            'defaultvalue' => $blogpost ? $blogpostobj->get('allowcomments') : 1,
        ),
168
169
170
171
172
        'submitpost' => array(
            'type' => 'submitcancel',
            'value' => array(get_string('savepost', 'artefact.blog'), get_string('cancel')),
            'goto' => get_config('wwwroot') . 'artefact/blog/view/index.php?id=' . $blog,
        )
173
174
175
    )
));

176

Richard Mansfield's avatar
Richard Mansfield committed
177
178
179
180
/*
 * Javascript specific to this page.  Creates the list of files
 * attached to the blog post.
 */
181
182
$wwwroot = get_config('wwwroot');
$noimagesmessage = json_encode(get_string('noimageshavebeenattachedtothispost', 'artefact.blog'));
183
$javascript = <<<EOF
184

185

Richard Mansfield's avatar
Richard Mansfield committed
186

187
// Override the image button on the tinyMCE editor.  Rather than the
188
189
190
// normal image popup, open up a modified popup which allows the user
// to select an image from the list of image files attached to the
// post.
191
192

// Get all the files in the attached files list that have been
193
194
// recognised as images.  This function is called by the the popup
// window, but needs access to the attachment list on this page
195
function attachedImageList() {
196
197
198
199
    var images = [];
    var attachments = editpost_filebrowser.selecteddata;
    for (var a in attachments) {
        if (attachments[a].artefacttype == 'image') {
200
201
202
203
204
            images.push({
                'id': attachments[a].id,
                'name': attachments[a].title,
                'description': attachments[a].description ? attachments[a].description : ''
            });
205
206
207
        }
    }
    return images;
208
209
210
211
}


function imageSrcFromId(imageid) {
212
    return config.wwwroot + 'artefact/file/download.php?file=' + imageid;
213
214
}

215
216
217
218
function imageIdFromSrc(src) {
    var artefactstring = 'download.php?file=';
    var ind = src.indexOf(artefactstring);
    if (ind != -1) {
219
        return src.substring(ind+artefactstring.length, src.length);
220
221
222
223
    }
    return '';
}

224
var imageList = {};
225

226
227
function blogpostImageWindow(ui, v) {
    var t = tinyMCE.activeEditor;
228

229
    imageList = attachedImageList();
230

231
    var template = new Array();
232

233
    template['file'] = '{$wwwroot}artefact/blog/image_popup.php';
234
235
    template['width'] = 355;
    template['height'] = 275 + (tinyMCE.isMSIE ? 25 : 0);
236

237
238
239
240
    // Language specific width and height addons
    template['width'] += t.getLang('lang_insert_image_delta_width', 0);
    template['height'] += t.getLang('lang_insert_image_delta_height', 0);
    template['inline'] = true;
241

242
    t.windowManager.open(template);
243
}
244

245
246
function editpost_callback(form, data) {
    editpost_filebrowser.callback(form, data);
247
248
};

249
EOF;
250

251
$smarty = smarty(array(), array(), array(), array(
252
    'tinymcesetup' => "ed.addCommand('mceImage', blogpostImageWindow);",
253
254
255
256
257
    'sideblocks' => array(
        array(
            'name'   => 'quota',
            'weight' => -10,
            'data'   => array(),
258
        ),
259
    ),
260
));
261
$smarty->assign('INLINEJAVASCRIPT', $javascript);
262
$smarty->assign_by_ref('form', $form);
263
$smarty->assign('PAGEHEADING', $pagetitle);
264
265
$smarty->display('artefact:blog:editpost.tpl');

266
267


268
269
270
271
272
/** 
 * This function get called to cancel the form submission. It returns to the
 * blog list.
 */
function editpost_cancel_submit() {
273
274
275
276
277
    global $blog;
    redirect(get_config('wwwroot') . 'artefact/blog/view/?id=' . $blog);
}

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

280
281
282
283
284
285
    db_begin();
    $postobj = new ArtefactTypeBlogPost($blogpost, null);
    $postobj->set('title', $values['title']);
    $postobj->set('description', $values['description']);
    $postobj->set('tags', $values['tags']);
    $postobj->set('published', !$values['draft']);
286
    $postobj->set('allowcomments', (int) $values['allowcomments']);
287
288
289
290
291
292
293
294
295
296
297
298
299
300
    if (!$blogpost) {
        $postobj->set('parent', $blog);
        $postobj->set('owner', $USER->id);
    }
    $postobj->commit();
    $blogpost = $postobj->get('id');

    // Attachments
    $old = $postobj->attachment_id_list();
    // $new = is_array($values['filebrowser']['selected']) ? $values['filebrowser']['selected'] : array();
    $new = is_array($values['filebrowser']) ? $values['filebrowser'] : array();
    if (!empty($new) || !empty($old)) {
        foreach ($old as $o) {
            if (!in_array($o, $new)) {
301
302
303
304
                try {
                    $postobj->detach($o);
                }
                catch (ArtefactNotFoundException $e) {}
305
306
            }
        }
307
308
        foreach ($new as $n) {
            if (!in_array($n, $old)) {
309
310
311
312
                try {
                    $postobj->attach($n);
                }
                catch (ArtefactNotFoundException $e) {}
313
            }
314
315
        }
    }
316
    db_commit();
317

318
319
    $result = array(
        'error'   => false,
320
321
        'message' => get_string('blogpostsaved', 'artefact.blog'),
        'goto'    => get_config('wwwroot') . 'artefact/blog/view/index.php?id=' . $blog,
322
    );
323
324
325
326
    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);
327
    }
328
    $form->reply(PIEFORM_OK, $result);
329
330
}

331
332
333
334
335
336
337
338
339
340
341
342
343
function add_attachment($attachmentid) {
    global $blogpostobj;
    if ($blogpostobj) {
        $blogpostobj->attach($attachmentid);
    }
}

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