2007-07-19 12:40:05 +02:00
< ? php
2008-02-25 03:10:37 +01:00
/**
* A special kind of complex table field for manipulating assets .
2008-06-15 15:33:53 +02:00
*
2008-02-25 03:10:37 +01:00
* @ package cms
* @ subpackage assets
*/
2007-07-19 12:40:05 +02:00
class AssetTableField extends ComplexTableField {
protected $folder ;
protected $template = " AssetTableField " ;
2007-10-02 23:34:57 +02:00
protected $permissions = array (
" edit " ,
" delete " ,
//"export",
);
2009-04-29 03:44:28 +02:00
/**
* Indicates whether a search is being executed on this object
*/
protected $searchingFor = null ;
2007-07-19 12:40:05 +02:00
function __construct ( $controller , $name , $sourceClass , $fieldList , $detailFormFields , $sourceFilter = " " , $sourceSort = " " , $sourceJoin = " " ) {
parent :: __construct ( $controller , $name , $sourceClass , $fieldList , $detailFormFields , $sourceFilter , $sourceSort , $sourceJoin );
2008-11-04 00:09:49 +01:00
$SNG_file = singleton ( 'File' );
// If search was request, filter the results here
$SQL_search = ( ! empty ( $_REQUEST [ 'FileSearch' ])) ? Convert :: raw2sql ( $_REQUEST [ 'FileSearch' ]) : null ;
if ( $SQL_search ) {
$searchFilters = array ();
foreach ( $SNG_file -> searchableFields () as $fieldName => $fieldSpec ) {
2008-11-23 01:31:13 +01:00
if ( strpos ( $fieldName , '.' ) === false ) $searchFilters [] = " \" $fieldName\ " LIKE '%{$SQL_search}%' " ;
2008-11-04 00:09:49 +01:00
}
$this -> sourceFilter = '(' . implode ( ' OR ' , $searchFilters ) . ')' ;
2009-04-29 03:44:28 +02:00
$this -> searchingFor = $_REQUEST [ 'FileSearch' ];
2009-07-09 07:41:00 +02:00
// @todo Integrate search form more closely and don't rely on deprecated
// $extraLinkParams.
$this -> extraLinkParams = array (
'FileSearch' => $SQL_search
);
2009-10-21 06:32:57 +02:00
}
2008-11-04 00:09:49 +01:00
$this -> sourceSort = 'Title' ;
2007-07-19 12:40:05 +02:00
$this -> Markable = true ;
}
2009-10-21 06:39:50 +02:00
/**
* Creates the link to this form , including the search pattern
*
* @ return string
*/
function CurrentLink () {
$link = parent :: CurrentLink ();
if ( isset ( $_REQUEST [ 'FileSearch' ]) ) {
if ( strpos ( $link , '?' ) !== false )
$link .= " & " ;
else
$link .= " /? " ;
$link .= " FileSearch= " . urlencode ( $_REQUEST [ 'FileSearch' ]);
}
return $link ;
}
2008-11-12 05:31:53 +01:00
function FieldHolder () {
2009-04-29 03:44:28 +02:00
$ret = parent :: FieldHolder ();
Requirements :: javascript ( CMS_DIR . '/javascript/AssetTableField.js' );
2008-11-12 05:31:53 +01:00
2009-04-29 03:44:28 +02:00
return $ret ;
2008-11-12 05:31:53 +01:00
}
2009-10-21 06:32:57 +02:00
function FirstLink () {
$link = parent :: FirstLink ();
if ( $link && isset ( $_REQUEST [ 'FileSearch' ])) {
return $link . '&FileSearch=' . $_REQUEST [ 'FileSearch' ];
}
return $link ;
}
function PrevLink () {
$link = parent :: PrevLink ();
if ( $link && isset ( $_REQUEST [ 'FileSearch' ])) {
return $link . '&FileSearch=' . $_REQUEST [ 'FileSearch' ];
}
return $link ;
}
function NextLink () {
$link = parent :: NextLink ();
if ( $link && isset ( $_REQUEST [ 'FileSearch' ])) {
return $link . '&FileSearch=' . $_REQUEST [ 'FileSearch' ];
}
return $link ;
}
function LastLink () {
$link = parent :: LastLink ();
if ( $link && isset ( $_REQUEST [ 'FileSearch' ])) {
return $link . '&FileSearch=' . $_REQUEST [ 'FileSearch' ];
}
return $link ;
}
2007-07-19 12:40:05 +02:00
function setFolder ( $folder ) {
$this -> folder = $folder ;
$this -> sourceFilter .= ( $this -> sourceFilter ) ? " AND " : " " ;
2009-04-29 03:44:28 +02:00
// If you are searching for files then show all those from subfolders
if ( $this -> searchingFor ) {
$folderIDs = $nextIDSet = array ( $folder -> ID );
$folderClasses = " ' " . implode ( " ',' " , ClassInfo :: subclassesFor ( " Folder " )) . " ' " ;
while ( $nextIDSet ) {
// TO DO: In 2.4 this should be refactored to use the new data mapper.
2009-09-01 00:24:23 +02:00
$nextIDSet = DB :: query ( " SELECT \" ID \" FROM \" File \" WHERE \" ParentID \" IN ( "
. implode ( " , " , $nextIDSet ) . " ) AND \" ClassName \" IN ( $folderClasses ) " ) -> column ();
2009-04-29 03:44:28 +02:00
if ( $nextIDSet ) $folderIDs = array_merge ( $folderIDs , $nextIDSet );
}
2009-09-01 00:24:23 +02:00
$this -> sourceFilter .= " \" ParentID \" IN ( " . implode ( " , " , $folderIDs ) . " ) AND \" ClassName \" <> 'Folder' " ;
2009-04-29 03:44:28 +02:00
// Otherwise just show the direct contents
} else {
2009-09-01 00:24:23 +02:00
$this -> sourceFilter .= " \" ParentID \" = ' " . $folder -> ID . " ' AND \" ClassName \" <> 'Folder' " ;
2009-04-29 03:44:28 +02:00
}
2007-07-19 12:40:05 +02:00
}
function Folder () {
return $this -> folder ;
}
function sourceID () {
2007-10-02 23:34:57 +02:00
if ( $this -> folder ) return $this -> folder -> ID ;
2007-07-19 12:40:05 +02:00
}
2008-08-18 02:52:15 +02:00
/**
* Get the pop - up fields for the given record .
*/
function getCustomFieldsFor ( $childData ) {
2007-07-19 12:40:05 +02:00
if ( ! $childData ) {
user_error ( " AssetTableField::DetailForm No record found " );
return null ;
}
if ( $childData -> ParentID ) {
$folder = DataObject :: get_by_id ( 'File' , $childData -> ParentID );
} else {
$folder = singleton ( 'Folder' );
}
$urlLink = " <div class='field readonly'> " ;
2008-02-25 03:10:37 +01:00
$urlLink .= " <label class='left'> " . _t ( 'AssetTableField.URL' , 'URL' ) . " </label> " ;
2007-07-19 12:40:05 +02:00
$urlLink .= " <span class='readonly'><a href=' { $childData -> Link () } '> { $childData -> RelativeLink () } </a></span> " ;
$urlLink .= " </div> " ;
$detailFormFields = new FieldSet (
new TabSet ( " BottomRoot " ,
2009-01-05 07:17:59 +01:00
$mainTab = new Tab ( 'Main' ,
2007-09-16 18:33:05 +02:00
new TextField ( " Title " , _t ( 'AssetTableField.TITLE' , 'Title' )),
new TextField ( " Name " , _t ( 'AssetTableField.FILENAME' , 'Filename' )),
2007-07-19 12:40:05 +02:00
new LiteralField ( " AbsoluteURL " , $urlLink ),
2007-09-16 18:33:05 +02:00
new ReadonlyField ( " FileType " , _t ( 'AssetTableField.TYPE' , 'Type' )),
new ReadonlyField ( " Size " , _t ( 'AssetTableField.SIZE' , 'Size' ), $childData -> getSize ()),
2008-11-11 21:45:11 +01:00
new DropdownField ( " OwnerID " , _t ( 'AssetTableField.OWNER' , 'Owner' ), Member :: mapInCMSGroups ()),
2007-09-16 18:33:05 +02:00
new DateField_Disabled ( " Created " , _t ( 'AssetTableField.CREATED' , 'First uploaded' )),
new DateField_Disabled ( " LastEdited " , _t ( 'AssetTableField.LASTEDIT' , 'Last changed' ))
2007-07-19 12:40:05 +02:00
)
)
);
2009-01-05 07:17:59 +01:00
$mainTab -> setTitle ( _t ( 'AssetTableField.MAIN' , 'Main' ));
2009-10-21 06:33:51 +02:00
if ( is_a ( $childData , 'Image' )) {
2009-01-05 07:17:59 +01:00
$tab = $detailFormFields -> findOrMakeTab ( " BottomRoot.Image " , _t ( 'AssetTableField.IMAGE' , 'Image' ));
2007-07-19 12:40:05 +02:00
$big = $childData -> URL ;
2009-10-21 06:33:51 +02:00
$formattedImage = $childData -> getFormattedImage ( 'AssetLibraryPreview' );
$thumbnail = $formattedImage ? $formattedImage -> URL : '' ;
2007-07-19 12:40:05 +02:00
2008-02-25 03:10:37 +01:00
// Hmm this required the translated string to be appended to BottomRoot to add this to the Main tab
2009-01-05 07:17:59 +01:00
$detailFormFields -> addFieldToTab ( 'BottomRoot.Main' ,
new ReadonlyField ( " Dimensions " , _t ( 'AssetTableField.DIM' , 'Dimensions' ))
2007-07-19 12:40:05 +02:00
);
2009-01-05 07:17:59 +01:00
$tab -> push (
new LiteralField ( " ImageFull " ,
2009-10-21 06:33:51 +02:00
" <img id='thumbnailImage' src=' { $thumbnail } ?r= " . rand ( 1 , 100000 ) . " ' alt=' { $childData -> Name } ' /> "
)
2007-07-19 12:40:05 +02:00
);
2007-09-20 06:06:50 +02:00
}
2009-01-05 07:17:59 +01:00
2007-07-19 12:40:05 +02:00
if ( $childData && $childData -> hasMethod ( 'BackLinkTracking' )) {
2010-04-12 11:42:36 +02:00
if ( class_exists ( 'Subsite' )) Subsite :: disable_subsite_filter ( true );
2007-07-19 12:40:05 +02:00
$links = $childData -> BackLinkTracking ();
2010-04-12 11:42:36 +02:00
if ( class_exists ( 'Subsite' )) Subsite :: disable_subsite_filter ( false );
2007-07-19 12:40:05 +02:00
if ( $links -> exists ()) {
foreach ( $links as $link ) {
$backlinks [] = " <li><a href= \" admin/show/ $link->ID\ " > " . $link->Breadcrumbs (null,true). " </ a ></ li > " ;
}
2009-12-13 06:24:18 +01:00
$backlinks = " <div style= \" clear:left \" > " . _t ( 'AssetTableField.PAGESLINKING' , 'The following pages link to this file:' ) . " <ul> " . implode ( " " , $backlinks ) . " </ul></div> " ;
2007-07-19 12:40:05 +02:00
}
2007-09-16 18:33:05 +02:00
if ( ! isset ( $backlinks )) $backlinks = " <p> " . _t ( 'AssetTableField.NOLINKS' , " This file hasn't been linked to from any pages. " ) . " </p> " ;
2007-07-19 12:40:05 +02:00
$detailFormFields -> addFieldToTab ( " BottomRoot.Links " , new LiteralField ( " Backlinks " , $backlinks ));
}
// the ID field confuses the Controller-logic in finding the right view for ReferencedField
$detailFormFields -> removeByName ( 'ID' );
2008-11-10 05:07:03 +01:00
if ( $childData ) $childData -> extend ( 'updateCMSFields' , $detailFormFields );
2008-08-18 02:52:15 +02:00
return $detailFormFields ;
2007-07-19 12:40:05 +02:00
}
2008-11-04 00:09:49 +01:00
/**
* Provide some HTML for a search form , to be
* added above the AssetTable field , allowing
* a user to filter the current table ' s files
* by their filename .
*
* @ return string HTML for search form
*/
function SearchForm () {
$fieldContainer = new FieldGroup (
2009-11-21 04:20:11 +01:00
new FieldGroup (
new TextField (
'FileSearch' ,
_t ( 'MemberTableField.SEARCH' , 'Search' ),
$this -> searchingFor
)
),
new FieldGroup (
$btnFilter = new InlineFormAction (
'FileFilterButton' ,
_t ( 'MemberTableField.FILTER' , 'Filter' )
),
$btnClear = new InlineFormAction (
'FileFilterClearButton' ,
_t ( 'AssetTableField.CLEAR' , 'Clear' )
)
)
2008-11-04 00:09:49 +01:00
);
2009-11-21 04:20:11 +01:00
$btnFilter -> includeDefaultJS ( false );
$btnClear -> includeDefaultJS ( false );
2008-11-04 00:09:49 +01:00
return $fieldContainer -> FieldHolder ();
}
2007-07-19 12:40:05 +02:00
2009-11-21 04:20:03 +01:00
/**
* @ return FormField | null
*/
function DeleteMarkedButton () {
if ( ! $this -> isReadonly () ) {
$deleteButton = new InlineFormAction (
'deletemarked' ,
_t ( 'Folder.DELSELECTED' , 'Delete selected files' ),
'delete'
);
$deleteButton -> includeDefaultJS ( false );
return $deleteButton ;
}
}
/**
* @ return string HTML
*/
public function deletemarked ( $request ) {
$fileIDs = $request -> requestVar ( $this -> Name ());
$numFiles = 0 ;
$brokenPageList = '' ;
if ( $fileIDs && count ( $fileIDs )) {
$files = DataObject :: get (
" File " ,
sprintf ( " \" File \" . \" ID \" IN (%s) " , Convert :: raw2sql ( implode ( ',' , $fileIDs )))
);
if ( $files ) {
2010-10-15 05:40:11 +02:00
$brokenPages = array ();
2009-11-21 04:20:03 +01:00
foreach ( $files as $file ) {
2010-10-15 05:40:11 +02:00
$brokenPages = array_merge ( $brokenPages , $file -> BackLinkTracking () -> toArray ());
2009-11-21 04:20:03 +01:00
$file -> delete ();
$numFiles ++ ;
}
2010-10-15 05:40:11 +02:00
if ( $brokenPages ) {
2009-11-21 04:20:03 +01:00
$brokenPageList = " " . _t ( 'AssetAdmin.NOWBROKEN' , 'These pages now have broken links:' ) . '<ul>' ;
foreach ( $brokenPages as $brokenPage ) {
$brokenPageList .= " <li style="font-size: 65%"> " . $brokenPage -> Breadcrumbs ( 3 , true ) . '</li>' ;
}
$brokenPageList .= '</ul>' ;
Notifications :: notifyByEmail ( " BrokenLink " , " Page_BrokenLinkEmail " );
} else {
$brokenPageList = '' ;
}
}
}
$response = $this -> form -> Controller () -> getResponse ();
$response -> addHeader (
'X-Status' ,
sprintf (
_t ( 'AssetAdmin.DELETEDX' , " Deleted %s file(s) %s " ),
$numFiles ,
$brokenPageList
)
);
2009-11-21 04:20:11 +01:00
}
public function movemarked ( $request ) {
$fileIDs = $request -> requestVar ( $this -> Name ());
$folderId = $request -> requestVar ( 'DestFolderID' );
$numFiles = 0 ;
2009-11-21 04:20:03 +01:00
2009-11-21 04:20:11 +01:00
if ( $folderId && ( is_numeric ( $folderId ) || ( $folderId ) == 'root' )) {
if ( $folderId == 'root' ) $folderId = 0 ;
if ( $fileIDs && count ( $fileIDs )) {
$files = DataObject :: get (
" File " ,
sprintf ( " \" File \" . \" ID \" IN (%s) " , Convert :: raw2sql ( implode ( ',' , $fileIDs )))
);
if ( $files ) {
foreach ( $files as $file ) {
$file -> ParentID = $folderId ;
$file -> write ();
$numFiles ++ ;
}
}
}
$response = $this -> form -> Controller () -> getResponse ();
$response -> addHeader (
'X-Status' ,
sprintf (
sprintf ( _t ( 'AssetAdmin.MOVEDX' , 'Moved %s files' ), $numFiles ),
$numFiles
)
);
}
2009-11-21 04:20:03 +01:00
}
2007-07-19 12:40:05 +02:00
}
2008-12-04 23:38:58 +01:00
2009-04-29 03:44:28 +02:00
?>