Hi team,
Using a combination of Peter Bacon Darwin's Associated Folder module and a useful bit of code from Uncle Cheese, I've built a nice wee Resources page that allows FileDataObjects to be uploaded along with a Title and Description (using Uploadify) and displayed in a table for people to download. All is working fine.
Can anyone show me how I can enable users to upload new Resources from the front end as well as through the CMS?
Here's my ResourcesPage.php:
<?php
class ResourcePage extends Page
{
static $db = array(
'PageHeading' => 'Varchar(200)',
);
static $has_many = array (
'Resources' => 'Resource'
);
static $icon = "themes/tdg/images/treeicons/bricks";
public function getCMSFields()
{
$f = parent::getCMSFields();
$manager = new FileDataObjectManager(
$this, // Controller
'Resources', // Source name
'Resource', // Source class
'Attachment', // File name on DataObject
array(
'Name' => 'Name',
'Description' => 'Description',
'Category' => 'Category'
), // Headings
'getCMSFields_forPopup' // Detail fields (function name or FieldSet object)
// Filter clause
// Sort clause
// Join clause
);
/*$manager->setUploadFolder('assets/', $this->AssociatedFolder);*/
$manager->setUploadFolder(str_replace('assets/','',$this->AssociatedFolder()->Filename));
$manager->allowUploadFolderSelection();
$manager->setFilter(
'Category', // Name of field to filter
'Filter by Category', // Label for filter
singleton('Resource')->dbObject('Category')->enumValues() // Map for filter (could be $dataObject->toDropdownMap(), e.g.)
);
// If undefined, all types are allowed. Pass with or without a leading "."
$manager->setAllowedFileTypes(array());
// Label for the upload button in the popup
$manager->setBrowseButtonText("Upload");
// In grid view, what field will appear underneath the icon. If left out, it defaults to the file title.
$manager->setGridLabelField('Name');
// Plural form of the objects being managed. Used on the "Add" button.
// If left out, this defaults to [MyObjectName]s
$manager->setPluralTitle('Resources');
$f->addFieldToTab("Root.Content.Resources", $manager);
$f->addFieldToTab('Root.Content.Main', new TextField('PageHeading', 'Page Heading Text'), 'Content');
return $f;
}
}
Object::add_extension('ResourcePage', 'AssociatedFolderDecorator');
class ResourcePage_Controller extends Page_Controller
{
}
?>
Here's the html from my page template that displays the Resources:
<table class="resourceTable">
<tr>
<th class="resourceName">File Name</th>
<th class="resourceDesc">Description</th>
<th class="resourceCat">Category</th>
<th class="resourceType">File Type</th>
<th class="resourceSize">File Size</th>
</tr>
<% control Resources %>
<tr>
<td>
<a href="$Attachment.URL">$Name</a>
</td>
<td>
$Description
</td>
<td>
$Category
</td>
<td>
$Attachment.FileType
</td>
<td>
$Attachment.Size
</td>
</tr>
<% end_control %>
</table>
Let me know if it would be useful to give you the Resource.php code (for the Resource DataObject).
Many thanks,
David