Completed
Pull Request — master (#203)
by
unknown
40:36
created

PostAttachment   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 0
loc 59
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A canDelete() 0 8 3
A canEdit() 0 8 3
A download() 0 16 3
1
<?php
2
namespace SilverStripe\Forum\Models;
3
4
use SilverStripe\Assets\File;
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Control\HTTPResponse;
7
use SilverStripe\Core\Convert;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\Security\Member;
10
11
/**
12
 * Attachments for posts (one post can have many attachments)
13
 *
14
 * @package forum
15
 * @method Post Post
16
 */
17
class PostAttachment extends File
18
{
19
20
    private static $has_one = array(
21
        "Post" => "Post"
22
    );
23
24
    private static $defaults = array(
25
        'ShowInSearch' => 0
26
    );
27
28
    /**
29
     * Can a user delete this attachment
30
     *
31
     * @return bool
32
     */
33
    public function canDelete($member = null)
34
    {
35
        if (!$member) {
36
            $member = Member::currentUser();
37
        }
38
39
        return ($this->Post()) ? $this->Post()->canDelete($member) : true;
40
    }
41
42
    /**
43
     * Can a user edit this attachement
44
     *
45
     * @return bool
46
     */
47
    public function canEdit($member = null)
48
    {
49
        if (!$member) {
50
            $member = Member::currentUser();
51
        }
52
53
        return ($this->Post()) ? $this->Post()->canEdit($member) : true;
54
    }
55
56
    /**
57
     * Allows the user to download a file without right-clicking
58
     */
59
    public function download()
60
    {
61
        if (isset($this->urlParams['ID'])) {
62
            $id = Convert::raw2sql($this->urlParams['ID']);
63
64
            if (is_numeric($id)) {
65
                /** @var File $file */
66
                $file     = File::get()->byID($id);
67
                $response = HTTPRequest::send_file(file_get_contents($file->getFilename()), $file->Name);
68
                $response->output();
69
            }
70
        }
71
        
72
        // todo
73
        return $this->redirectBack();
74
    }
75
}
76