post.php 13.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
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
5
 *
Francois Marier's avatar
Francois Marier committed
6
7
8
9
 * 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.
10
 *
Francois Marier's avatar
Francois Marier committed
11
12
13
14
 * 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.
15
 *
Francois Marier's avatar
Francois Marier committed
16
17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20
 *
 * @package    mahara
 * @subpackage artefact-blog
21
 * @author     Catalyst IT Ltd
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
23
 * @copyright  (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
24
25
26
27
 *
 */

define('INTERNAL', 1);
28
define('MENUITEM', 'myportfolio/blogs');
29
30
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once('pieforms/pieform.php');
Martyn Smith's avatar
Martyn Smith committed
31

32
safe_require('artefact', 'blog');
33
safe_require('artefact', 'file');
34

Richard Mansfield's avatar
Richard Mansfield committed
35
36
37
38
39
/* 
 * 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.
 */
40
$blogpost = param_integer('blogpost', param_integer('id', 0));
41
if (!$blogpost) {
42
    $blog = param_integer('blog');
43
44
45
46
47
48
    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'));
    }
49
50
    $title = '';
    $description = '';
51
    $tags = array();
52
    $checked = '';
Nigel McNie's avatar
Nigel McNie committed
53
    $pagetitle = get_string('newblogpost', 'artefact.blog', get_field('artefact', 'title', 'id', $blog));
54
    $focuselement = 'title';
55
    define('TITLE', $pagetitle);
56
57
}
else {
58
    $blogpostobj = new ArtefactTypeBlogPost($blogpost);
59
    $blogpostobj->check_permission();
60
61
62
    $blog = $blogpostobj->get('parent');
    $title = $blogpostobj->get('title');
    $description = $blogpostobj->get('description');
63
    $tags = $blogpostobj->get('tags');
64
    $checked = !$blogpostobj->get('published');
Nigel McNie's avatar
Nigel McNie committed
65
    $pagetitle = get_string('editblogpost', 'artefact.blog');
66
    $focuselement = 'description'; // Doesn't seem to work with tinyMCE.
67
    define('TITLE', get_string('editblogpost','artefact.blog'));
68
69
}

70

71
$folder = ArtefactTypeBlogpost::blogfiles_folder_id();
72

73
74
75
76
77
78
79
80
81
82
$form = pieform(array(
    'name'               => 'editpost',
    'method'             => 'post',
    'autofocus'          => $focuselement,
    'jsform'             => true,
    'newiframeonsubmit'  => true,
    'jssuccesscallback'  => 'editpost_success',
    'plugintype'         => 'artefact',
    'pluginname'         => 'blog',
    'configdirs'         => array(get_config('libroot') . 'form/', get_config('docroot') . 'artefact/file/form/'),
83
    'elements' => array(
84
        'blog' => array(
85
86
87
            'type' => 'hidden',
            'value' => $blog,
        ),
88
        'blogpost' => array(
89
            'type' => 'hidden',
90
            'value' => $blogpost,
91
92
93
94
95
96
97
        ),
        'title' => array(
            'type' => 'text',
            'title' => get_string('posttitle', 'artefact.blog'),
            'rules' => array(
                'required' => true
            ),
98
            'defaultvalue' => $title,
99
100
101
        ),
        'description' => array(
            'type' => 'wysiwyg',
102
            'rows' => 20,
103
            'cols' => 70,
104
105
106
107
108
            'title' => get_string('postbody', 'artefact.blog'),
            'description' => get_string('postbodydesc', 'artefact.blog'),
            'rules' => array(
                'required' => true
            ),
109
            'defaultvalue' => $description,
110
        ),
111
112
113
114
115
        'tags'       => array(
            'defaultvalue' => $tags,
            'type'         => 'tags',
            'title'        => get_string('tags'),
            'description'  => get_string('tagsdesc'),
116
            'help' => true,
117
        ),
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
        'filebrowser' => array(
            'type'         => 'filebrowser',
            'title'        => get_string('attachments', 'artefact.blog'),
            'group'        => null,
            'institution'  => null,
            'folder'       => $folder,
            'highlight'    => null,
            'config'       => array(
                'upload'          => true,
                'uploadagreement' => true,
                'createfolder'    => false,
                'edit'            => false,
                'select'          => true,
            ),
            'selectlistcallback' => 'load_attachments',
        ),
        'draft' => array(
            'type' => 'checkbox',
            'title' => get_string('draft', 'artefact.blog'),
            'description' => get_string('thisisdraftdesc', 'artefact.blog'),
            'defaultvalue' => $checked,
            'help' => true,
        ),
        '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,
        )
146
147
148
    )
));

149

Richard Mansfield's avatar
Richard Mansfield committed
150
151
152
153
/*
 * Javascript specific to this page.  Creates the list of files
 * attached to the blog post.
 */
154
155
$wwwroot = get_config('wwwroot');
$noimagesmessage = json_encode(get_string('noimageshavebeenattachedtothispost', 'artefact.blog'));
156
$javascript = <<<EOF
157

158

Richard Mansfield's avatar
Richard Mansfield committed
159

160
// Override the image button on the tinyMCE editor.  Rather than the
161
162
163
// 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.
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

// The contents of this function is stolen straight out of the tinyMCE
// code in tinymce/themes/advanced/editor_template_src.js
function getSelectedImgAttributes (editorid) {
    var src = "", alt = "", border = "", hspace = "", vspace = "", width = "", height = "", align = "";
    var title = "", onmouseover = "", onmouseout = "", action = "insert";
    var img = tinyMCE.imgElement;
    var inst = tinyMCE.getInstanceById(editorid);

    if (tinyMCE.selectedElement != null && tinyMCE.selectedElement.nodeName.toLowerCase() == "img") {
        img = tinyMCE.selectedElement;
        tinyMCE.imgElement = img;
    }

    if (img) {
        // Is it a internal MCE visual aid image, then skip this one.
        if (tinyMCE.getAttrib(img, 'name').indexOf('mce_') == 0)
            return true;

        src = tinyMCE.getAttrib(img, 'src');
        alt = tinyMCE.getAttrib(img, 'alt');

        // Try polling out the title
        if (alt == "")
            alt = tinyMCE.getAttrib(img, 'title');

        // Fix width/height attributes if the styles is specified
        if (tinyMCE.isGecko) {
            var w = img.style.width;
            if (w != null && w != "")
                img.setAttribute("width", w);

            var h = img.style.height;
            if (h != null && h != "")
                img.setAttribute("height", h);
        }

        border = tinyMCE.getAttrib(img, 'border');
        hspace = tinyMCE.getAttrib(img, 'hspace');
        vspace = tinyMCE.getAttrib(img, 'vspace');
        width = tinyMCE.getAttrib(img, 'width');
        height = tinyMCE.getAttrib(img, 'height');
        align = tinyMCE.getAttrib(img, 'align');
        onmouseover = tinyMCE.getAttrib(img, 'onmouseover');
        onmouseout = tinyMCE.getAttrib(img, 'onmouseout');
        title = tinyMCE.getAttrib(img, 'title');

        // Is realy specified?
        if (tinyMCE.isMSIE) {
            width = img.attributes['width'].specified ? width : "";
            height = img.attributes['height'].specified ? height : "";
        }

        src = eval(tinyMCE.settings['urlconverter_callback'] + "(src, img, true);");

        // Use mce_src if defined
        mceRealSrc = tinyMCE.getAttrib(img, 'mce_src');
        if (mceRealSrc != "") {
            src = mceRealSrc;

            if (tinyMCE.getParam('convert_urls'))
                src = eval(tinyMCE.settings['urlconverter_callback'] + "(src, img, true);");
        }

        action = "update";
    }
    return {'src' : src, 'alt' : alt, 'border' : border, 'hspace' : hspace, 'vspace' : vspace, 
            'width' : width, 'height' : height, 'align' : align, 'title' : title, 
            'onmouseover' : onmouseover, 'onmouseout' : onmouseout, 'action' : action};
}



// Get all the files in the attached files list that have been
238
239
// recognised as images.  This function is called by the the popup
// window, but needs access to the attachment list on this page
240
function attachedImageList() {
241
242
243
244
245
246
247
248
    var images = [];
    var attachments = editpost_filebrowser.selecteddata;
    for (var a in attachments) {
        if (attachments[a].artefacttype == 'image') {
            images.push({'id': attachments[a].id, 'name': attachments[a].title});
        }
    }
    return images;
249
250
251
252
}


function imageSrcFromId(imageid) {
253
    return config.wwwroot + 'artefact/file/download.php?file=' + imageid;
254
255
}

256
257
258
259
function imageIdFromSrc(src) {
    var artefactstring = 'download.php?file=';
    var ind = src.indexOf(artefactstring);
    if (ind != -1) {
260
        return src.substring(ind+artefactstring.length, src.length);
261
262
263
264
    }
    return '';
}

265
var imageList = {};
266

267
268
269
270
function blogpostExecCommandHandler(editor_id, elm, command, user_interface, value) {
    var linkElm, imageElm, inst;
    switch (command) {
    case "mceImage":
271
        a = getSelectedImgAttributes(editor_id);
272
273
274

        imageList = attachedImageList();
        if (imageList.length == 0) {
275
            alert({$noimagesmessage});
276
277
            return true;
        }
278
279
280
281
282
283
284
285
286
287
288
289
290

        var template = new Array();

        template['file'] = '{$wwwroot}artefact/blog/image_popup.php?src=\{\$src\}';
        template['width'] = 355;
        template['height'] = 265 + (tinyMCE.isMSIE ? 25 : 0);

        // Language specific width and height addons
        template['width'] += tinyMCE.getLang('lang_insert_image_delta_width', 0);
        template['height'] += tinyMCE.getLang('lang_insert_image_delta_height', 0);

        a.inline = "yes";
        tinyMCE.openWindow(template, a);
291

292
293
294
295
        return true;
    }
    return false;
}
296

Richard Mansfield's avatar
Richard Mansfield committed
297

298
299
300
function editpost_success(form, data) {
    editpost_filebrowser.success(form, data);
};
301

302
EOF;
303

304
305
306
307
308
309
310
$smarty = smarty(array(), array(), array(), array(
    'tinymcecommandcallback' => 'blogpostExecCommandHandler',
    'sideblocks' => array(
        array(
            'name'   => 'quota',
            'weight' => -10,
            'data'   => array(),
311
        ),
312
313
314
315
316
    ),
    'themepaths' => array(
        'images/file.gif',
        'images/image.gif'
    ),
317
));
318
$smarty->assign('INLINEJAVASCRIPT', $javascript);
319
$smarty->assign_by_ref('form', $form);
320
321
322
$smarty->assign('pagetitle', $pagetitle);
$smarty->display('artefact:blog:editpost.tpl');

323
324


325
326
327
328
329
/** 
 * This function get called to cancel the form submission. It returns to the
 * blog list.
 */
function editpost_cancel_submit() {
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
    global $blog;
    redirect(get_config('wwwroot') . 'artefact/blog/view/?id=' . $blog);
}

function editpost_submit(Pieform $form, $values) {
    global $SESSION, $USER, $blogpost, $blog;

    // save the post if the user clicked submit or has no js
    $submitted = !empty($values['submitpost']);
    if ($submitted || !$form->submitted_by_js()) {
        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']);
        if (!$blogpost) {
            $postobj->set('parent', $blog);
            $postobj->set('owner', $USER->id);
        }
        $postobj->commit();
        $blogpost = $postobj->get('id');

        // Attachments
        if (isset($values['filebrowser']['selected'])) {
            $old = $postobj->attachment_id_list();
            foreach ($old as $o) {
                if (!in_array($o, $values['filebrowser']['selected'])) {
                    $postobj->detach($o);
                }
            }
            foreach ($values['filebrowser']['selected'] as $a) {
                if (!in_array($a, $old)) {
                    $postobj->attach($a);
                }
            }
            $filebrowser['selectedlist'] = $postobj->get_attachments(true);
        }
        db_commit();
        $result['error'] = false;
        $result['message'] = get_string('blogpostsaved', 'artefact.blog');
        $result['goto'] = get_config('wwwroot') . 'artefact/blog/view/index.php?id=' . $blog;
    }

    // Filebrowser actions should only come through to here if it's not a js submission.
    if (!empty($values['filebrowser']['action']) && !$form->submitted_by_js()) {
        if (isset($values['filebrowser']['folder'])) {
            $params['folder'] = $values['filebrowser']['folder'];
        }
        $result = pieform_element_filebrowser_submit($form, $values['filebrowser']);
        $result['goto'] = get_config('wwwroot') . 'artefact/blog/post.php?id=' . $blogpost;
    }

    $form->reply(empty($result['error']) ? PIEFORM_OK : PIEFORM_ERR, $result);
}

function load_attachments() {
    global $blogpostobj;
    if ($blogpostobj) {
        return $blogpostobj->get_attachments(true);
    }
    return array();
392
393
394
}
 
?>