'handleImport' ); public static $upgrade_audio = true; public static $upgrade_video = true; public static $upgrade_image = true; public $view; public $default_view = "grid"; protected $allowedFileTypes; protected $limitFileTypes; protected $uploadLimit = "20"; protected $allowUploadFolderSelection = false; protected $enableUploadDebugging = false; public $hasDataObject = true; public $importClass = "File"; protected $permissions = array( "add", "edit", "show", "delete", "upload", "import" ); public $popupClass = "FileDataObjectManager_Popup"; public $itemClass = "FileDataObjectManager_Item"; public $template = "FileDataObjectManager"; public $templatePopup = "DataObjectManager_popup"; public $gridLabelField; public $pluralTitle; public $browseButtonText = "Upload files"; public $uploadFolder = "Uploads"; public function __construct($controller, $name = null, $sourceClass = null, $fileFieldName = null, $fieldList = null, $detailFormFields = null, $sourceFilter = "", $sourceSort = "Created DESC", $sourceJoin = "") { if(!class_exists("SWFUploadField")) die(""._t('DataObjectManager.ERROR','Error').": "._t('FileDataObjectManager.SWFUPLOAD','DataObjectManager requires the SWFUpload module.')); parent::__construct($controller, $name, $sourceClass, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin); // Intelligent constructor for fileFieldName $SNG = singleton($this->sourceClass()); if($fileFieldName === null) { if($has_ones = $SNG->stat('has_one')) { foreach($has_ones as $relation => $value) { if(is_subclass_of($value,"File")) { $fileFieldName = $relation; $fileClassName = $value; break; } } } } if(isset($_REQUEST['ctf'][$this->Name()])) { $this->view = $_REQUEST['ctf'][$this->Name()]['view']; } if($this->sourceClass() == "File" || is_subclass_of($this->sourceClass(), "File")) { $this->hasDataObject = false; $this->fileFieldName = $name; $this->fileClassName = $this->sourceClass(); $this->dataObjectFieldName = null; } else { $this->dataObjectFieldName = $name; $this->fileFieldName = $fileFieldName; $this->fileClassName = $SNG->has_one($this->fileFieldName); if(!$this->fileClassName) die("FileDataObjectManager::__construct():"._t('FileDataObjectManager.FILERELATION','Could not determine file relationship')); } $this->controllerClassName = $controller->class; if($key = array_search($this->controllerClassName, $SNG->stat('has_one'))) $this->controllerFieldName = $key; else $this->controllerFieldName = $this->controllerClassName; $this->controllerID = $controller->ID; // Check for allowed file types if($types = Object::get_static($this->fileClassName,'allowed_file_types')) $this->setAllowedFileTypes($types); } public function getQueryString($params = array()) { $view = isset($params['view'])? $params['view'] : $this->view; return parent::getQueryString($params)."&ctf[{$this->Name()}][view]={$view}"; } public function setGridLabelField($fieldName) { $this->gridLabelField = $fieldName; } public function GridLink() { return $this->RelativeLink(array('view' => 'grid')); } public function ListLink() { return $this->RelativeLink(array('view' => 'list')); } public function GridView() { return $this->ListStyle() == "grid"; } public function ListView() { return $this->ListStyle() == "list"; } public function ListStyle() { return $this->view ? $this->view : $this->default_view; } public function ImportDropdown() { return new DropdownField('ImportFolder','',$this->getImportFolderHierarchy(0),null, null, "-- "._t('DataObjectManager.SELECTFOLDER', 'Select a folder')." --"); } protected function importLinkFor($file) { return $this->BaseLink()."/import/$file->ID"; } protected function getImportFolderHierarchy($parentID, $level = 0) { $options = array(); if($children = DataObject::get("Folder", "ParentID = $parentID")) { foreach($children as $child) { $indent=""; for($i=0;$i<$level;$i++) $indent .= "  "; $files = DataObject::get($this->importClass, "ClassName != 'Folder' AND ParentID = $child->ID"); $count = $files ? $files->Count() : "0"; $options[$this->importLinkFor($child)] = $indent.$child->Title . " ($count files)"; $options += $this->getImportFolderHierarchy($child->ID, $level+1); } } return $options; } protected function getUploadFolderHierarchy($parentID, $level = 0) { $options = array(); if($children = DataObject::get("Folder", "ParentID = $parentID")) { foreach($children as $child) { $indent=""; for($i=0;$i<$level;$i++) $indent .= "  "; $options[$child->ID] = empty($child->Title) ? "$indent Untitled" : $indent.$child->Title; $options += $this->getUploadFolderHierarchy($child->ID, $level+1); } } return $options; } public function setAllowedFileTypes($types = array()) { foreach($types as $type) { if(is_array($this->limitFileTypes) && !in_array(strtolower(str_replace(".","",$type)), $this->limitFileTypes)) // To-do: get user_error working. die("".$this->class . "::setAllowedFileTypes() -- Only files of type " . implode(", ", $this->limitFileTypes) . " are allowed."); } $this->allowedFileTypes = $types; } public function getAllowedFileTypes() { return $this->allowedFileTypes; } public function setUploadLimit($num) { $this->uploadLimit = $num; } public function getUploadLimit() { return $this->uploadLimit; } public function setBrowseButtonText($text) { $this->browseButtonText = $text; } public function getBrowseButtonText() { return $this->browseButtonText; } public function ButtonAddTitle() { return $this->addTitle ? $this->addTitle : $this->PluralTitle(); } public function allowUploadFolderSelection() { $this->allowUploadFolderSelection = true; } public function enableUploadDebugging() { $this->enableUploadDebugging = true; } public function setDefaultView($type) { $this->default_view = $type; } public function upload() { if(!$this->can('add')) return; $form = $this->UploadForm(); if(is_string($form)) return $this->customise(array( 'String' => true, 'NestedController' => $this->isNested, 'DetailForm' => $this->UploadForm(), ))->renderWith($this->templatePopup); else { $form = $this->UploadForm(); return $this->customise(array( 'String' => is_string($form), 'DetailForm' => $form ))->renderWith($this->templatePopup); } } public function UploadLink() { return Controller::join_links($this->BaseLink(),'/upload'); } protected function getUploadFields() { $fields = new FieldSet( new HeaderField($title = sprintf(_t('DataObjectManager.ADDITEM', 'Add %s'),$this->PluralTitle()), $headingLevel = 2), new HeaderField($title = _t('DataObjectManager.UPLOADFROMPC', 'Upload from my computer'), $headingLevel = 3), new SWFUploadField( "UploadForm", "Upload", "", array( 'file_upload_limit' => $this->getUploadLimit(), // how many files can be uploaded 'file_queue_limit' => $this->getUploadLimit(), // how many files can be in the queue at once 'browse_button_text' => $this->getBrowseButtonText(), 'upload_url' => Director::absoluteURL('FileDataObjectManager_Controller/handleswfupload'), 'required' => 'true' ) ) ); if($this->allowUploadFolderSelection) $fields->insertBefore(new DropdownField('UploadFolder','',$this->getUploadFolderHierarchy(0),null, null, "-- Select a folder --"),"Upload"); return $fields; } public function UploadForm() { // Sync up the DB // singleton('Folder')->syncChildren(); $className = $this->sourceClass(); $childData = new $className(); $validator = $this->getValidatorFor($childData); if($this->Can('upload')) { SWFUploadConfig::addPostParams(array( 'dataObjectClassName' => $this->sourceClass(), 'dataObjectFieldName' => $this->dataObjectFieldName, 'fileFieldName' => $this->fileFieldName, 'fileClassName' => $this->fileClassName, 'parentIDName' => $this->getParentIdName( $this->getParentClass(), $this->sourceClass() ), 'controllerID' => $this->controllerID, 'OverrideUploadFolder' => $this->getUploadFolder(), 'hasDataObject' => $this->hasDataObject ? 1 : 0 )); if($this->allowUploadFolderSelection) SWFUploadConfig::addDynamicPostParam('UploadFolder',$this->popupClass.'_UploadForm_UploadFolder'); if($this->getAllowedFileTypes()) SWFUploadConfig::addFileTypes($this->getAllowedFileTypes()); if($this->enableUploadDebugging) SWFUploadConfig::set_var('debug','true'); } $fields = $this->Can('upload') ? $this->getUploadFields() : new FieldSet( new HeaderField($title = sprintf(_t('DataObjectManager.ADD', 'Add %s'),$this->PluralTitle()), $headingLevel = 2) ); $form = Object::create( $this->popupClass, $this, 'UploadForm', $fields, $validator, false, $childData ); $action = $this->Can('upload') ? new FieldSet(new FormAction('saveUploadForm', 'Upload')) : new FieldSet(); $form->setActions($action); if($this->Can('import')) { $header = new HeaderField($title = _t('DataObjectManager.IMPORTFROMFOLDER', 'Import from an existing folder'), $headingLevel = 3); $holder = new LiteralField("holder","
"); if(!isset($_POST['uploaded_files'])) return $form->forTemplate() . $header->Field() . $this->ImportDropdown()->Field() . $holder->Field(); else return $form; } return $form; } public function saveUploadForm() { if(isset($_POST['uploaded_files']) && is_array($_POST['uploaded_files'])) { $form = $this->EditUploadedForm(); return $this->customise(array( 'String' => is_string($form), 'DetailForm' => $form ))->renderWith($this->templatePopup); } } protected function getChildDataObj() { $class = $this->sourceClass(); return new $class(); } public function getPreviewFieldFor($fileObject, $size = 150) { if($fileObject instanceof Image) { $URL = $fileObject->getHeight() > $size ? $fileObject->SetHeight($size)->URL : $fileObject->URL ; return new LiteralField("icon", "

$fileObject->Filename

" ); } else { $URL = $fileObject->Icon(); return new LiteralField("icon", "

$fileObject->Filename

" ); } } protected function closePopup() { Requirements::clear(); if($this->isNested) Requirements::customScript("parent.jQuery('#iframe_".$this->id()." a').click();"); else { Requirements::customScript(" var container = parent.jQuery('#".$this->id()."'); parent.jQuery('#facebox').fadeOut(function() { parent.jQuery('#facebox .content').removeClass().addClass('content'); parent.jQuery('#facebox_overlay').remove(); parent.jQuery('#facebox .loading').remove(); parent.refresh(container, container.attr('href')); });"); } return $this->customise(array( 'String' => true, 'DetailForm' => 'Closing...' ))->renderWith($this->templatePopup); } public function EditUploadedForm() { if(!$this->hasDataObject) return $this->closePopup(); $childData = $this->getChildDataObj(); $validator = $this->getValidatorFor($childData); $fields = $this->getFieldsFor($childData); $fields->removeByName($this->fileFieldName); $total = isset($_POST['totalsize']) ? $_POST['totalsize'] : sizeof($_POST['uploaded_files']); $index = isset($_POST['index']) ? $_POST['index'] + 1 : 1; $fields->push(new HiddenField('totalsize','',$total)); $fields->push(new HiddenField('index','',$index)); if(isset($_POST['uploaded_files']) && is_array($_POST['uploaded_files'])) { $remaining_files = $_POST['uploaded_files']; $current = $remaining_files[0]; $dataObject = DataObject::get_by_id($this->sourceClass(), $current); $fileObject = $dataObject->obj($this->fileFieldName); $fields->push(new HiddenField('current','',$current)); unset($remaining_files[0]); if(!$fields->loaded) { foreach($remaining_files as $id) $fields->push(new LiteralField("u-$id","")); $first = $fields->First()->Name(); $fields->insertBefore(new HeaderField("Header","Editing file $index of $total",2), $first); $fields->insertBefore($this->getPreviewFieldFor($fileObject), $first); } } $form = Object::create( $this->popupClass, $this, 'EditUploadedForm', $fields, $validator, false, $childData ); $form->setActions(new FieldSet(new FormAction("saveEditUploadedForm", $index == $total ? "Finish" : "Next"))); if(isset($dataObject) && $dataObject) $form->loadDataFrom($dataObject); $fields->loaded = true; return $form; } function saveEditUploadedForm($data, $form) { $obj = DataObject::get_by_id($this->sourceClass(), $data['current']); $form->saveInto($obj); $obj->write(); if(isset($data['uploaded_files']) && is_array($data['uploaded_files'])) { $form = $this->EditUploadedForm(); return $this->customise(array( 'String' => is_string($form), 'DetailForm' => $form ))->renderWith($this->templatePopup); } else { return $this->closePopup(); } } public function handleImport($request) { $this->importFolderID = $request->param('ID'); die($this->ImportForm($this->importFolderID)->forTemplate()); } protected function getImportFields() { return new FieldSet( new HiddenField('dataObjectClassName','',$this->sourceClass()), new HiddenField('fileFieldName','', $this->fileFieldName), new HiddenField('parentIDName','', $this->getParentIdName( $this->getParentClass(), $this->sourceClass() )), new HiddenField('controllerID','',$this->controllerID) ); } protected function ImportForm($folder_id = null) { $folder_id = isset($_POST['folder_id']) ? $_POST['folder_id'] : $this->importFolderID;; if($files = DataObject::get($this->importClass, "ClassName != 'Folder' AND ParentID = $folder_id")) $fields = $this->getImportFields(); $fields->push(new HiddenField('folder_id','',$folder_id)); $fields->push(new LiteralField('select','
Select: all | none
')); $fields->push(new LiteralField("ul","