commit abb9a61d0ddc151151c4df4f45299a2b0002cb0f Author: Hayden Smith Date: Thu Jul 19 10:40:05 2007 +0000 Moved CMS module to open source path git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@39000 467b73ca-7a2a-4603-9d3b-597d59a354a9 diff --git a/_config.php b/_config.php new file mode 100644 index 00000000..99e92ca9 --- /dev/null +++ b/_config.php @@ -0,0 +1,19 @@ + 'BatchProcess_Controller', + 'silverstripe/' => '->admin/', + 'cms/' => '->admin/', + 'admin/security/$Action/$ID/$OtherID' => 'SecurityAdmin', + 'admin/help/$Action/$ID' => 'CMSHelp', + 'admin/newsletter/$Action/$ID' => 'NewsletterAdmin', + 'admin/reports/$Action/$ID' => 'ReportAdmin', + 'admin/assets/$Action/$ID' => 'AssetAdmin', + 'admin/ReportField/$Action/$ID/$Type/$OtherID' => 'ReportField_Controller', + 'admin/bulkload/$Action/$ID/$OtherID' => 'BulkLoaderAdmin', + 'admin/$Action/$ID/$OtherID' => 'CMSMain', + 'unsubscribe/$Email/$MailingList' => 'Unsubscribe_Controller', + 'membercontrolpanel/$Email' => 'MemberControlPanel' +)); + +?> diff --git a/code/AssetAdmin.php b/code/AssetAdmin.php new file mode 100755 index 00000000..7e866aaf --- /dev/null +++ b/code/AssetAdmin.php @@ -0,0 +1,660 @@ +currentPageID(); + } + + /** + * Return fake-ID "root" if no ID is found (needed to upload files into the root-folder) + */ + public function currentPageID() { + if(isset($_REQUEST['ID']) && is_numeric($_REQUEST['ID'])) { + return $_REQUEST['ID']; + } elseif (is_numeric($this->urlParams['ID'])) { + return $this->urlParams['ID']; + } elseif(is_numeric(Session::get("{$this->class}.currentPage"))) { + return Session::get("{$this->class}.currentPage"); + } else { + return "root"; + } + } + + /** + * Set up the controller, in particular, re-sync the File database with the assets folder./ + */ + function init() { + parent::init(); + + // needed for MemberTableField (Requirements not determined before Ajax-Call) + Requirements::javascript("sapphire/javascript/ComplexTableField.js"); + Requirements::css("jsparty/greybox/greybox.css"); + Requirements::css("sapphire/css/ComplexTableField.css"); + + Requirements::javascript("cms/javascript/AssetAdmin.js"); + Requirements::javascript("cms/javascript/AssetAdmin_left.js"); + Requirements::javascript("cms/javascript/AssetAdmin_right.js"); + + // Requirements::javascript('sapphire/javascript/TableListField.js'); + + // Include the right JS] + // Hayden: This didn't appear to be used at all + /*$fileList = new FileList("Form_EditForm_Files", null); + $fileList->setClick_AjaxLoad('admin/assets/getfile/', 'Form_SubForm'); + $fileList->FieldHolder();*/ + + Requirements::javascript("jsparty/greybox/AmiJS.js"); + Requirements::javascript("jsparty/greybox/greybox.js"); + Requirements::css("jsparty/greybox/greybox.css"); + } + + /** + * Display the upload form. Returns an iframe tag that will show admin/assets/uploadiframe. + */ + function getUploadIframe() { + return << + +HTML; + } + + function index() { + File::sync(); + return array(); + } + + /** + * Show the content of the upload iframe. The form is specified by a template. + */ + function uploadiframe() { + Requirements::clear(); + + Requirements::javascript("jsparty/prototype.js"); + Requirements::javascript("jsparty/loader.js"); + Requirements::javascript("jsparty/behaviour.js"); + Requirements::javascript("jsparty/prototype_improvements.js"); + Requirements::javascript("jsparty/layout_helpers.js"); + Requirements::javascript("cms/javascript/LeftAndMain.js"); + Requirements::javascript("jsparty/multifile/multifile.js"); + Requirements::css("jsparty/multifile/multifile.css"); + Requirements::css("cms/css/typography.css"); + Requirements::css("cms/css/layout.css"); + Requirements::css("cms/css/cms_left.css"); + Requirements::css("cms/css/cms_right.css"); + + if(isset($data['ID']) && $data['ID'] != 'root') $folder = DataObject::get_by_id("Folder", $data['ID']); + else $folder = singleton('Folder'); + + $canUpload = $folder->userCanEdit(); + + return array( 'CanUpload' => $canUpload ); + } + + /** + * Return the form object shown in the uploadiframe. + */ + function UploadForm() { + + return new Form($this,'UploadForm', new FieldSet( + new HiddenField("ID", "", $this->currentPageID()), + // needed because the button-action is triggered outside the iframe + new HiddenField("action_doUpload", "", "1"), + new FileField("Files[0]" , "Choose file "), + new LiteralField('UploadButton'," + + "), + new LiteralField('MultifileCode'," +

Files ready to upload:

+
+ + ") + ), new FieldSet( + )); + + } + + /** + * This method processes the results of the UploadForm. + * It will save the uploaded files to /assets/ and create new File objects as required. + */ + function doUpload($data, $form) { + foreach($data['Files'] as $param => $files) { + foreach($files as $key => $value) { + $processedFiles[$key][$param] = $value; + } + } + + if($data['ID'] && $data['ID'] != 'root') $folder = DataObject::get_by_id("Folder", $data['ID']); + else $folder = singleton('Folder'); + + $warnFiles = array(); + $fileSizeWarnings = ''; + + foreach($processedFiles as $file) { + if($file['tmp_name']) { + // check that the file can be uploaded and isn't too large + + $extensionIndex = strripos( $file['name'], '.' ); + $extension = strtolower( substr( $file['name'], $extensionIndex + 1 ) ); + + if( $extensionIndex !== FALSE ) + list( $maxSize, $warnSize ) = File::getMaxFileSize( $extension ); + else + list( $maxSize, $warnSize ) = File::getMaxFileSize(); + + // check that the file is not too large or that the current user is an administrator + if( $this->can('AdminCMS') || ( File::allowedFileType( $extension ) && (!isset($maxsize) || $file['size'] < $maxSize))) + $newFiles[] = $folder->addUploadToFolder($file); + elseif( !File::allowedFileType( $extension ) ) { + $fileSizeWarnings .= "alert( 'Only administrators can upload $extension files.' );"; + } else { + if( $file['size'] > 1048576 ) + $fileSize = "" . ceil( $file['size'] / 1048576 ) . "MB"; + elseif( $file['size'] > 1024 ) + $fileSize = "" . ceil( $file['size'] / 1024 ) . "KB"; + else + $fileSize = "" . ceil( $file['size'] ) . "B"; + + + $fileSizeWarnings .= "alert( '\\'" . $file['name'] . "\\' is too large ($fileSize). Files of this type cannot be larger than $warnSize ' );"; + } + } + } + + if($newFiles) { + $numFiles = sizeof($newFiles); + $statusMessage = "Uploaded $numFiles files"; + $status = "good"; + } else { + $statusMessage = "There was nothing to upload"; + $status = ""; + } + echo << + var form = parent.document.getElementById('Form_EditForm'); + form.getPageFromServer(form.elements.ID.value); + parent.statusMessage("{$statusMessage}","{$status}"); + $fileSizeWarnings + parent.document.getElementById('sitetree').getTreeNodeByIdx( "{$folder->ID}" ).getElementsByTagName('a')[0].className += ' contents'; + +HTML; + } + + /** + * Needs to be overridden to make sure an ID with value "0" is still valid (rootfolder) + */ + + + /** + * Return the form that displays the details of a folder, including a file list and fields for editing the folder name. + */ + function getEditForm($id) { + if($id && $id != "root") { + $record = DataObject::get_by_id("File", $id); + } else { + $record = singleton("Folder"); + } + + $fileList = new AssetTableField( + $this, + "Files", + "File", + array("Title" => "Title", "LinkedURL" => "Filename"), + "" + ); + $fileList->setFolder($record); + $fileList->setPopupCaption("View/Edit Asset"); + + if($record) { + $nameField = ($id != "root") ? new TextField("Name") : new HiddenField("Name"); + $fields = new FieldSet( + new HiddenField("Title"), + $nameField, + new TabSet("Root", + new Tab("Files", + $fileList + ), + new Tab("Details", + new ReadonlyField("URL"), + new ReadonlyField("ClassName", "Type"), + new ReadonlyField("Created", "First Uploaded"), + new ReadonlyField("LastEdited", "Last Updated") + ), + new Tab("Upload", + new LiteralField("UploadIframe", + $this->getUploadIframe() + ) + ) + ), + new HiddenField("ID") + ); + + $actions = new FieldSet(); + + if( $record->userCanEdit() ) { + $actions = new FieldSet( + new FormAction('deletemarked',"Delete files"), + new FormAction('movemarked',"Move files..."), + new FormAction('save',"Save") + ); + } + + $form = new Form($this, "EditForm", $fields, $actions); + if($record->ID) { + $form->loadDataFrom($record); + } else { + $form->loadDataFrom(array( + "ID" => "root", + "URL" => Director::absoluteBaseURL() . 'assets/', + )); + } + + // @todo: These workflow features aren't really appropriate for all projects + if( Member::currentUser()->_isAdmin() && project() == 'mot' ) { + $fields->addFieldsToTab( 'Root.Workflow', new DropdownField("Owner", "Owner", Member::map() ) ); + $fields->addFieldsToTab( 'Root.Workflow', new TreeMultiselectField("CanUse", "Content usable by") ); + $fields->addFieldsToTab( 'Root.Workflow', new TreeMultiselectField("CanEdit", "Content modifiable by") ); + } + + if( !$record->userCanEdit() ) + $form->makeReadonly(); + + return $form; + + } + } + + /** + * Returns the form used to specify options for the "move marked" action. + */ + public function MoveMarkedOptionsForm() { + $folderDropdown = new TreeDropdownField("DestFolderID", "Move files to", "Folder"); + $folderDropdown->setFilterFunction(create_function('$obj', 'return $obj->class == "Folder";')); + + return new CMSActionOptionsForm($this, "MoveMarkedOptionsForm", new FieldSet( + new HiddenField("ID"), + new HiddenField("FileIDs"), + $folderDropdown + ), + new FieldSet( + new FormAction("movemarked", "Move marked files") + )); + } + + /** + * Perform the "move marked" action. + * Called by ajax, with a JavaScript return. + */ + public function movemarked() { + if($_REQUEST['DestFolderID'] && is_numeric($_REQUEST['DestFolderID'])) { + $destFolderID = $_REQUEST['DestFolderID']; + $fileList = "'" . ereg_replace(' *, *',"','",trim(addslashes($_REQUEST['FileIDs']))) . "'"; + $numFiles = 0; + + if($fileList != "''") { + $files = DataObject::get("File", "`File`.ID IN ($fileList)"); + if($files) { + foreach($files as $file) { + $file->ParentID = $destFolderID; + $file->write(); + $numFiles++; + } + } else { + user_error("No files in $fileList could be found!", E_USER_ERROR); + } + } + + echo <<