editpost.php 8.93 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
28
29
30
31
32
33
<?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     Alastair Pharo <alastair@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);
define('MENUITEM', 'myblogs');

require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once('pieforms/pieform.php');
safe_require('artefact', 'blog');

Richard Mansfield's avatar
Richard Mansfield committed
34
35
36
37
38
39
/*
 * Files uploaded to blog posts will be stored temporarily in the
 * artefact/blog directory under the dataroot until the blog post is
 * saved.  This createid is used to ensure that all of these newly
 * uploaded files get unique filenames.
 */
40
41
42
43
44
45
$createid = $SESSION->get('createid');
if (empty($createid)) {
    $createid = 0;
}
$SESSION->set('createid', $createid + 1);

46

Richard Mansfield's avatar
Richard Mansfield committed
47
48
49
50
51
/* 
 * 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.
 */
52
53
$blogpost = param_integer('blogpost', 0);
if (!$blogpost) {
54
55
56
57
58
59
60
    $blog = param_integer('blog');
    $title = '';
    $description = '';
    $checked = '';
    $pagetitle = 'newblogpost';
}
else {
61
62
    $blogpostobj = new ArtefactTypeBlogPost($blogpost);
    if ($blogpostobj->get('owner') != $USER->get('id')) {
63
64
        return;
    }
65
66
67
68
    $blog = $blogpostobj->get('parent');
    $title = $blogpostobj->get('title');
    $description = $blogpostobj->get('description');
    $checked = !$blogpostobj->get('published');
69
70
71
    $pagetitle = 'editblogpost';
}

72

73

Richard Mansfield's avatar
Richard Mansfield committed
74
75
76
77
78
/*
 * The main form has the text inputs and no submit button.  The submit
 * and cancel buttons are in their own form at the bottom of the page,
 * with the file upload form appearing in between.
 */
79
80
81
82
83
84
85
86
87
88
89
$form = pieform(array(
    'name' => 'editpost',
    'method' => 'post',
    'action' => '',
    'elements' => array(
        'parent' => array(
            'type' => 'hidden',
            'value' => $blog,
        ),
        'id' => array(
            'type' => 'hidden',
90
            'value' => $blogpost,
91
92
93
94
95
96
97
98
99
100
101
102
103
        ),
        'title' => array(
            'type' => 'text',
            'title' => get_string('posttitle', 'artefact.blog'),
            'description' => get_string('posttitledesc', 'artefact.blog'),
            'rules' => array(
                'required' => true
            ),
            'defaultvalue' => $title
        ),
        'description' => array(
            'type' => 'wysiwyg',
            'rows' => 10,
104
            'cols' => 70,
105
106
107
108
109
110
111
112
113
114
115
            'title' => get_string('postbody', 'artefact.blog'),
            'description' => get_string('postbodydesc', 'artefact.blog'),
            'rules' => array(
                'required' => true
            ),
            'defaultvalue' => $description
        ),
        'thisisdraft' => array(
            'type' => 'checkbox',
            'title' => get_string('thisisdraft', 'artefact.blog'),
            'description' => get_string('thisisdraftdesc', 'artefact.blog'),
116
            'defaultvalue' => $checked
117
118
119
120
        ),
    )
));

121
122


Richard Mansfield's avatar
Richard Mansfield committed
123
124
125
/*
 * Strings used in the inline javascript for this page.
 */
126
127
128
$getstring = quotestrings(array(
    'mahara' => array(
    ),
129
130
131
    'artefact.file' => array(
        'myfiles',
    ),
132
    'artefact.blog' => array(
133
        'attach',
134
        'blogpost',
135
        'browsemyfiles',
136
        'nofilesattachedtothispost',
137
        'remove',
138
139
    )));

Richard Mansfield's avatar
Richard Mansfield committed
140
141
142
143


// These variables are needed by file.js.  They should really be set
// automatically when file.js is included.
144
$copyright = get_field('site_content', 'content', 'name', 'uploadcopyright');
145
$wwwroot = get_config('wwwroot');
146
147


Richard Mansfield's avatar
Richard Mansfield committed
148
149
150
151
152

/*
 * Javascript specific to this page.  Creates the list of files
 * attached to the blog post.
 */
153
$javascript = <<<EOF
154

155

Richard Mansfield's avatar
Richard Mansfield committed
156

157
// The file uploader uploads files to the list of blog post attachments
158
159
var copyrightnotice = '{$copyright}';
var uploader = new FileUploader('uploader', 'upload.php', {$getstring['blogpost']}, false, 
Richard Mansfield's avatar
Richard Mansfield committed
160
                                attachtopost, fileattached);
161
162
uploader.createid = {$createid};

163
164


165
166
167
168
169
170
// File browser instance allows users to attach files from the my files area
var browser = null;
var browsebutton = INPUT({'id':'browsebutton', 'type':'button', 'value':{$getstring['browsemyfiles']},
                          'onclick':browsemyfiles});
function browsemyfiles() {
    hideElement('browsebutton');
171
    insertSiblingNodesAfter('browsebutton', H3(null, {$getstring['myfiles']}));
Richard Mansfield's avatar
Richard Mansfield committed
172
    showElement('filebrowser');
173
174
175
176
177
178
179
180
181
    browser = new FileBrowser('filebrowser', '{$wwwroot}artefact/file/myfiles.json.php', 
                              function () {}, {$getstring['attach']}, attachtopost);
    browser.init();
}
addLoadEvent(function () {insertSiblingNodesBefore('filebrowser', browsebutton);});



// List of attachments to the blog post
182
183
184
185
186
187
var attached = new TableRenderer(
    'attachedfiles',
    'attachedfiles.json.php',
    [
     'title',
     'description',
188
189
190
191
     function (r) { 
         return TD(null, INPUT({'type':'button', 'value':{$getstring['remove']},
                                'onclick':"removefrompost('attached_old:"+r.id+"')"}));
     }
192
193
194
195
196
197
    ]
);
attached.emptycontent = {$getstring['nofilesattachedtothispost']};
attached.paginate = false;
attached.blogpost = {$blogpost};
attached.statevars.push('blogpost');
198
attached.rowfunction = function (r) { return TR({'id':'attached_old:' + r.id}); };
199
attached.updateOnLoad();
200

201

202
203
204
205
206
207
208
209
210
211
212
213
// Show/hide the 'no attachments' message if there are no/some attachments
function checknoattachments() {
    if (attached.tbody.hasChildNodes()) {
        hideElement(attached.table.previousSibling);
        showElement(attached.table);
    }
    else {
        showElement(attached.table.previousSibling);
        hideElement(attached.table);
    }
}

214
215

// Add a newly uploaded file to the attached files list.
Richard Mansfield's avatar
Richard Mansfield committed
216
217
218
219
220

// Currently this function does not check whether names of files
// attached from my files clash with files already in the attached
// files list.  This should be done here if names of attached files
// need to be unique.
221
function attachtopost(data) {
222
    var rowid = data.uploadnumber ? 'uploaded:' + data.uploadnumber : 'existing:' + data.id;
Richard Mansfield's avatar
Richard Mansfield committed
223
224
225
    if (fileattached_id(rowid)) {
        return;
    }
226
    appendChildNodes(attached.tbody,
227
                     TR({'id':rowid},
228
229
230
                        map(partial(TD,null), 
                            [data.title, data.description,
                             INPUT({'type':'button', 'value':{$getstring['remove']},
231
                                'onclick':"removefrompost('"+rowid+"')"})])));
232
233
234
235
236
237
238
239
    checknoattachments();
}


// Remove a row from the attached files list.
function removefrompost(rowid) {
    removeElement(rowid);
    checknoattachments();
240
241
}

242
243

// Check if there's already a file attached to the post with the given name
Richard Mansfield's avatar
Richard Mansfield committed
244
245
246
function fileattached(filename) {
    return some(map(function (e) { return e.firstChild; }, attached.tbody.childNodes),
                function (cell) { return scrapeText(cell) == filename; });
247
248
}

249

Richard Mansfield's avatar
Richard Mansfield committed
250
251
252
253
254
255
256
257
// Check if there's already a file attached to the post with the given id
function fileattached_id(id) {
    return some(attached.tbody.childNodes, function (r) { return getNodeAttribute(r,'id') == id; });
}



// Save the blog post.
258
259
260

EOF;

Richard Mansfield's avatar
Richard Mansfield committed
261
262


263
$smarty = smarty(array('tablerenderer', 'artefact/file/js/file.js'));
264
$smarty->assign('INLINEJAVASCRIPT', $javascript);
265
$smarty->assign_by_ref('textinputform', $form);
266
267
268
$smarty->assign('pagetitle', $pagetitle);
$smarty->display('artefact:blog:editpost.tpl');

269
270


271
272
273
274
275
276
/**
 * This function gets called to create a new blog post, and publish it
 * simultaneously.
 *
 * @param array
 */
277
function editpost_submit(Pieform $form, array $values) {
278
279
280
281
282
283
284
285
286
287
288
289
    global $USER;

    $values['published'] = !$values['thisisdraft'];
    if ((!empty($values['id']) && ArtefactTypeBlogPost::edit_post($USER, $values))
        || (empty($values['id']) && ArtefactTypeBlogPost::new_post($USER, $values))) {
        // Redirect to the blog page.
        redirect(get_config('wwwroot') . 'artefact/blog/view/?id=' . $values['parent']);
    }

    redirect(get_config('wwwroot') . 'artefact/blog/list/');
}

290
291


292
293
294
295
296
297
298
299
300
301
/** 
 * This function get called to cancel the form submission. It returns to the
 * blog list.
 */
function editpost_cancel_submit() {
    $blog = param_integer('parent');
    redirect(get_config('wwwroot') . 'artefact/blog/view/?id=' . $blog);
}
 
?>