Completed
Push — master ( fb8428...d8c42f )
by James
02:14
created

FilePage_Controller::Listing()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 28
rs 8.439
cc 6
eloc 17
nc 13
nop 1
1
<?php
2
3
class FilePage extends Page
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
        
6
    public static $db = array(
7
        "FilesHeading" => "Text"
8
    );
9
    
10
    public static $has_one = array(
11
        "Folder" => "Folder"
12
    );
13
    
14
    public static $defaults = array(
15
        "FilesHeading" => "Downloads"
16
    );
17
    
18
    public static $description = "Lists files contained within a specific assets folder, for downloading/viewing.";
19
    public static $singular_name = "File Download Page";
20
    public static $plural_name = "File Download Pages";
21
    public static $icon = "file-listing/images/download";
22
    
23
    public function getCMSFields()
24
    {
25
        $fields = parent::getCMSFields();
26
        
27
        if ($this->FolderID) {
28
            $filescount = File::get()->filter(array("ParentID"=>$this->FolderID))->count();
29
            $fields->addFieldToTab('Root.Main',
30
                new LiteralField("addnew", "<p><a href='/admin/assets/show/".$this->FolderID."' class='ss-ui-button ss-ui-action-constructive ui-button' style='font-size:130%' data-icon=add''>Manage Files (".$filescount.")</span></a></p>"), 'Title');
31
        }
32
        
33
        $fields->insertAfter(new TextField("FilesHeading", "Files Heading"), 'Content');
34
        
35
        $folders = class_exists('FileVersion') ? Folder::get()->exclude("Filename:PartialMatch", "_versions")->map("ID", "Title") : Folder::get()->map("ID", "Title");
36
        $dropdown = new DropdownField("FolderID", "Folder", $folders);
37
        $this->FolderID ? $dropdown->setEmptyString("Clear list") : $dropdown->setEmptyString(" ");
38
        $fields->insertAfter($dropdown, 'FilesHeading');
39
40
        return $fields;
41
    }
42
}
43
44
class FilePage_Controller extends Page_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
45
{
46
    
47
    public function init()
48
    {
49
        if (Director::fileExists(project() . "/css/files.css")) {
50
            Requirements::css(project() . "/css/files.css");
51
        } else {
52
            Requirements::css("file-listing/css/files.css");
53
        }
54
        parent::init();
55
    }
56
		
57
		// Gets the current folder ID from query string and validates it
58
		public function getCurrentFolderID()
59
		{
60
			$folderID = $this->request->getVar('fid');
61
			
62
			if ($folderID) {
63
				return filter_var($folderID, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
64
			} else {
65
				return false;
66
			}
67
		}
68
    
69
    // Returns files/folders for the current folder
70
    public function Listing($ParentID = null)
71
    {
72
        if (!$this->FolderID) {
73
            return false;
74
        }
75
        
76
        $currentFolderID = $this->getCurrentFolderID();
77
        
78
        if ($currentFolderID) {
79
            if (DataObject::get("File", "ID = ".$currentFolderID)) {
80
                $ParentID = $currentFolderID;
81
            }
82
        } else {
83
            $ParentID = $this->FolderID;
84
        }
85
        
86
        if ($ParentID == $this->FolderID) {
87
            $list = DataObject::get("File", "ParentID = ".$ParentID, "Title ASC");
88
        } else {
89
            $list = DataObject::get("File", "ParentID = ".$ParentID, "Created DESC");
90
        }
91
        
92
        if (class_exists('FileVersion')) {
93
            return $list->exclude("Filename:PartialMatch", "_versions");
94
        } else {
95
            return $list;
96
        }
97
    }
98
    
99
    // Checks if not at the root folder
100
    public function NotRoot()
101
    {
102
				$currentFolderID = $this->getCurrentFolderID();
103
			
104
				if ($currentFolderID) {
105
            if (DataObject::get("File", "ID = ".$currentFolderID)) {
106
                return true;
107
            }
108
        }
109
        return false;
110
    }
111
    
112
    // Gets current folder from $_GET['fid']
113
    public function CurrentFolder()
114
    {
115
				$currentFolderID = $this->getCurrentFolderID();
116
				
117
				if ($currentFolderID) {
118
            return DataObject::get_by_id("File", $currentFolderID);
119
        }
120
        return false;
121
    }
122
    
123
    // Creates link to go back to parent folder
124
    public function BackLink()
125
    {
126
        if ($this->CurrentFolder()) {
127
            if ($this->CurrentFolder()->ParentID != $this->FolderID) {
128
                return "?fid=".$this->CurrentFolder()->ParentID;
129
            } else {
130
                return "?";
131
            }
132
        } else {
133
            return false;
134
        }
135
    }
136
}
137