post.php 11.4 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', 'myportfolio/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
123
124
            'title' => get_string('postbody', 'artefact.blog'),
            'description' => get_string('postbodydesc', 'artefact.blog'),
            'rules' => array(
                'required' => true
            ),
125
            'defaultvalue' => $description,
126
        ),
127
128
129
130
131
        'tags'       => array(
            'defaultvalue' => $tags,
            'type'         => 'tags',
            'title'        => get_string('tags'),
            'description'  => get_string('tagsdesc'),
132
            'help' => true,
133
        ),
134
135
136
137
        'filebrowser' => array(
            'type'         => 'filebrowser',
            'title'        => get_string('attachments', 'artefact.blog'),
            'folder'       => $folder,
138
139
            'highlight'    => $highlight,
            'browse'       => $browse,
140
            'page'         => get_config('wwwroot') . 'artefact/blog/post.php?' . ($blogpost ? ('id=' . $blogpost) : ('blog=' . $blog)) . '&browse=1',
141
            'browsehelp'   => 'browsemyfiles',
142
143
            'config'       => array(
                'upload'          => true,
144
                'uploadagreement' => get_config_plugin('artefact', 'file', 'uploadagreement'),
145
146
147
148
                'createfolder'    => false,
                'edit'            => false,
                'select'          => true,
            ),
149
150
            'defaultvalue'       => $attachments,
            'selectlistcallback' => 'artefact_get_records_by_id',
151
152
            'selectcallback'     => 'add_attachment',
            'unselectcallback'   => 'delete_attachment',
153
154
155
156
157
158
159
160
        ),
        'draft' => array(
            'type' => 'checkbox',
            'title' => get_string('draft', 'artefact.blog'),
            'description' => get_string('thisisdraftdesc', 'artefact.blog'),
            'defaultvalue' => $checked,
            'help' => true,
        ),
161
162
163
164
165
166
        'allowcomments' => array(
            'type'         => 'checkbox',
            'title'        => get_string('allowcomments','artefact.comment'),
            'description'  => get_string('allowcommentsonpost','artefact.blog'),
            'defaultvalue' => $blogpost ? $blogpostobj->get('allowcomments') : 1,
        ),
167
168
169
170
171
        '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,
        )
172
173
174
    )
));

175

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

184

Richard Mansfield's avatar
Richard Mansfield committed
185

186
// Override the image button on the tinyMCE editor.  Rather than the
187
188
189
// 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.
190
191

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


function imageSrcFromId(imageid) {
207
    return config.wwwroot + 'artefact/file/download.php?file=' + imageid;
208
209
}

210
211
212
213
function imageIdFromSrc(src) {
    var artefactstring = 'download.php?file=';
    var ind = src.indexOf(artefactstring);
    if (ind != -1) {
214
        return src.substring(ind+artefactstring.length, src.length);
215
216
217
218
    }
    return '';
}

219
var imageList = {};
220

221
222
function blogpostImageWindow(ui, v) {
    var t = tinyMCE.activeEditor;
223

224
    imageList = attachedImageList();
225

226
    var template = new Array();
227

228
    template['file'] = '{$wwwroot}artefact/blog/image_popup.php';
229
230
    template['width'] = 355;
    template['height'] = 275 + (tinyMCE.isMSIE ? 25 : 0);
231

232
233
234
235
    // 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;
236

237
    t.windowManager.open(template);
238
}
239

240
241
function editpost_callback(form, data) {
    editpost_filebrowser.callback(form, data);
242
243
};

244
EOF;
245

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

261
262


263
264
265
266
267
/** 
 * This function get called to cancel the form submission. It returns to the
 * blog list.
 */
function editpost_cancel_submit() {
268
269
270
271
272
    global $blog;
    redirect(get_config('wwwroot') . 'artefact/blog/view/?id=' . $blog);
}

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

275
276
277
278
279
280
    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']);
281
    $postobj->set('allowcomments', (int) $values['allowcomments']);
282
283
284
285
286
287
288
289
290
291
292
293
294
295
    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)) {
296
297
298
299
                try {
                    $postobj->detach($o);
                }
                catch (ArtefactNotFoundException $e) {}
300
301
            }
        }
302
303
        foreach ($new as $n) {
            if (!in_array($n, $old)) {
304
305
306
307
                try {
                    $postobj->attach($n);
                }
                catch (ArtefactNotFoundException $e) {}
308
            }
309
310
        }
    }
311
    db_commit();
312

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

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

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