Version: current

      1 <?php
      2 /**
      3  * A special kind of complex table field for manipulating assets.
      4  *
      5  * @package cms
      6  * @subpackage assets
      7  */
      8 class AssetTableField extends ComplexTableField {
      9
     10     protected $folder;
     11
     12     protected $template = "AssetTableField";
     13
     14     protected $permissions = array(
     15         "edit",
     16         "delete",
     17         //"export",
     18     );
     19
     20     /**
     21      * Indicates whether a search is being executed on this object
     22      */
     23     protected $searchingFor = null;
     24
     25     function __construct($controller, $name, $sourceClass, $fieldList, $detailFormFields = null, $sourceFilter = "", $sourceSort = "", $sourceJoin = "") {
     26         parent::__construct($controller, $name, $sourceClass, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin);
     27
     28         $SNG_file = singleton('File');
     29
     30         // If search was request, filter the results here
     31         $SQL_search = (!empty($_REQUEST['FileSearch'])) ? Convert::raw2sql($_REQUEST['FileSearch']) : null;
     32         if($SQL_search) {
     33             $searchFilters = array();
     34             foreach($SNG_file->searchableFields() as $fieldName => $fieldSpec) {
     35                 if(strpos($fieldName, '.') === false) $searchFilters[] = "\"$fieldName\" LIKE '%{$SQL_search}%'";
     36             }
     37             $this->sourceFilter = '(' . implode(' OR ', $searchFilters) . ')';
     38             $this->searchingFor = $_REQUEST['FileSearch'];
     39
     40             // @todo Integrate search form more closely and don't rely on deprecated
     41             //  $extraLinkParams.
     42             $this->extraLinkParams = array(
     43                 'FileSearch' => $SQL_search
     44             );
     45         }
     46
     47         $this->sourceSort = 'Title';
     48         $this->Markable = true;
     49         $this->pageSize = 20;
     50     }
     51
     52     /**
     53      * Creates the link to this form, including the search pattern
     54      *
     55      * @return string
     56      */
     57     function CurrentLink() {
     58         $link = parent::CurrentLink();
     59
     60         if(isset($_REQUEST['FileSearch']) ) {
     61             if ( strpos($link, '?')!==false )
     62                 $link .= "&";
     63             else
     64                 $link .= "/?";
     65
     66             $link .= "FileSearch=".urlencode($_REQUEST['FileSearch']);
     67         }
     68
     69         return $link;
     70     }
     71
     72     function FieldHolder() {
     73         $ret = parent::FieldHolder();
     74
     75         Requirements::javascript(CMS_DIR . '/javascript/AssetTableField.js');
     76         Requirements::javascript('cms/javascript/ImageEditor/Activator.js');
     77
     78         return $ret;
     79     }
     80
     81     function FirstLink() {
     82         $link = parent::FirstLink();
     83         if($link && isset($_REQUEST['FileSearch'])) {
     84             return $link . '&FileSearch=' . $_REQUEST['FileSearch'];
     85         }
     86         return $link;
     87     }
     88
     89     function PrevLink() {
     90         $link = parent::PrevLink();
     91         if($link && isset($_REQUEST['FileSearch'])) {
     92             return $link . '&FileSearch=' . $_REQUEST['FileSearch'];
     93         }
     94         return $link;
     95     }
     96
     97     function NextLink() {
     98         $link = parent::NextLink();
     99         if($link && isset($_REQUEST['FileSearch'])) {
    100             return $link . '&FileSearch=' . $_REQUEST['FileSearch'];
    101         }
    102         return $link;
    103     }
    104
    105     function LastLink() {
    106         $link = parent::LastLink();
    107         if($link && isset($_REQUEST['FileSearch'])) {
    108             return $link . '&FileSearch=' . $_REQUEST['FileSearch'];
    109         }
    110         return $link;
    111     }
    112
    113     function setFolder($folder) {
    114         $this->folder = $folder;
    115         $this->sourceFilter .= ($this->sourceFilter) ? " AND " : "";
    116
    117         // If you are searching for files then show all those from subfolders
    118         if($this->searchingFor) {
    119             $folderIDs = $nextIDSet = array($folder->ID);
    120             $folderClasses = "'" . implode("','", ClassInfo::subclassesFor("Folder")) . "'";
    121
    122             while($nextIDSet) {
    123                 // TO DO: In 2.4 this should be refactored to use the new data mapper.
    124                 $nextIDSet = DB::query("SELECT \"ID\" FROM \"File\" WHERE \"ParentID\" IN ("
    125                     . implode(", " , $nextIDSet) . ") AND \"ClassName\" IN ($folderClasses)")->column();
    126                 if($nextIDSet) $folderIDs = array_merge($folderIDs, $nextIDSet);
    127             }
    128
    129             $this->sourceFilter .= " \"ParentID\" IN (" . implode(", ", $folderIDs) . ") AND \"ClassName\" <> 'Folder'";
    130
    131         // Otherwise just show the direct contents
    132         } else {
    133             $this->sourceFilter .= " \"ParentID\" = '" . $folder->ID . "' AND \"ClassName\" <> 'Folder'";
    134         }
    135     }
    136
    137     function Folder() {
    138         return $this->folder;
    139     }
    140
    141     function sourceID() {
    142         if($this->folder) return $this->folder->ID;
    143     }
    144
    145     /**
    146      * Get the pop-up fields for the given record.
    147      */
    148     function getCustomFieldsFor($childData) {
    149         if(!$childData) {
    150             user_error("AssetTableField::DetailForm No record found");
    151             return null;
    152         }
    153
    154         if($childData->ParentID) {
    155             $folder = DataObject::get_by_id('File', $childData->ParentID );
    156         } else {
    157             $folder = singleton('Folder');
    158         }
    159
    160         $urlLink = "<div class='field readonly'>";
    161         $urlLink .= "<label class='left'>"._t('AssetTableField.URL','URL')."</label>";
    162         $urlLink .= "<span class='readonly'><a target=\"_blank\" href='{$childData->Link()}'>{$childData->RelativeLink()}</a></span>";
    163         $urlLink .= "</div>";
    164
    165         $detailFormFields = new FieldSet(
    166             new TabSet("BottomRoot",
    167                 $mainTab = new Tab('Main',
    168                     new TextField("Title", _t('AssetTableField.TITLE','Title')),
    169                     new TextField("Name", _t('AssetTableField.FILENAME','Filename')),
    170                     new LiteralField("AbsoluteURL", $urlLink),
    171                     new ReadonlyField("FileType", _t('AssetTableField.TYPE','Type')),
    172                     new ReadonlyField("Size", _t('AssetTableField.SIZE','Size'), $childData->getSize()),
    173                     new DropdownField("OwnerID", _t('AssetTableField.OWNER','Owner'), Member::mapInCMSGroups()),
    174                     new DateField_Disabled("Created", _t('AssetTableField.CREATED','First uploaded')),
    175                     new DateField_Disabled("LastEdited", _t('AssetTableField.LASTEDIT','Last changed'))
    176                 )
    177             )
    178         );
    179         $mainTab->setTitle(_t('AssetTableField.MAIN', 'Main'));
    180
    181         if(is_a($childData, 'Image')) {
    182             $tab = $detailFormFields->findOrMakeTab("BottomRoot.Image", _t('AssetTableField.IMAGE', 'Image'));
    183
    184             $big = $childData->URL;
    185             $formattedImage = $childData->getFormattedImage('AssetLibraryPreview');
    186             $thumbnail = $formattedImage ? $formattedImage->URL : '';
    187
    188             $tab->push(
    189                 new LiteralField("ImageFull",
    190                     "<img id='thumbnailImage' src='{$thumbnail}?r=" . rand(1,100000)  . "' alt='{$childData->Name}' />"
    191                 )
    192             );
    193             $tab->push(new ReadonlyField("Dimensions", _t('AssetTableField.DIM','Dimensions')));
    194         }
    195
    196         if(class_exists('Subsite')) Subsite::disable_subsite_filter(true);
    197         $links = false;
    198         $dataobjectLinks = false;
    199         if($childData) {
    200             if ($childData->hasMethod('BackLinkTracking')) {
    201                 $links = $childData->BackLinkTracking();
    202             }
    203             if ($childData->hasMethod('BackFileTracking')) {
    204                 $dataobjectLinks = $childData->BackFileTracking();
    205             }
    206         }
    207         if(class_exists('Subsite')) Subsite::disable_subsite_filter(false);
    208
    209         $backlinks = array();
    210         $pageTypeName = singleton('Page')->i18n_singular_name();
    211         if($links && $links->exists()) {
    212             foreach($links as $link) {
    213                 $backlinks[] = "<li class=\"linked-item\"><span class=\"linked-type\">{$pageTypeName}:</span> <a class=\"linked-title\" href=\"admin/show/$link->ID\">" . $link->Breadcrumbs(null,true). "</a></li>";
    214             }
    215         }
    216
    217         if($dataobjectLinks && $dataobjectLinks->exists()) {
    218             foreach($dataobjectLinks as $link) {
    219                 if ($linked = $link->LinkedObject()) {
    220                     $editLink = false; $objType = false; $objTitle = false; $objSubtitle = false;
    221                     $parent = ($linked->hasMethod('ParentObject')) ? $linked->ParentObject() : false;
    222                     if ($parent && $parent->ID) {
    223                         if (is_a($parent, 'Page')) {
    224                             $editLink = 'admin/show/' . $parent->ID;
    225                             $objType = $pageTypeName;
    226                             $objTitle = $parent->Breadcrumbs(null,true);
    227                             $objSubtitle = $linked->Title;
    228                         }
    229                         else {
    230                             $linked = $parent;
    231                         }
    232                     }
    233 /*
    234                     if (is_a($linked, 'Page')) {
    235                         $editLink = 'admin/show/' . $linked->ID;
    236                         $objType = $pageTypeName;
    237                         $objTitle = $linked->Breadcrumbs(null,true);
    238                     }
    239 */
    240                     if (!$editLink && $linked->hasMethod('EditLink')) $editLink = $linked->EditLink();
    241                     if (!$objType) $objType = $linked->i18n_singular_name();
    242                     if (!$objTitle) $objTitle = $linked->Title;
    243                     if ($objSubtitle) $objSubtitle = ' (<span class="linked-subtitle">' . $objSubtitle . '</span>)';
    244                     $backlinks[] = ($editLink) ?
    245                         "<li class=\"linked-item\"><span class=\"linked-type\">{$objType}:</span> <a target=\"_blank\" class=\"linked-title\" href=\"{$editLink}\">{$objTitle}</a>$objSubtitle</li>" :
    246                         "<li class=\"linked-item\"><span class=\"linked-type\">{$objType}:</span> <span class=\"linked-title\">{$objTitle}</span>$objSubtitle</li>"
    247                     ;
    248                 }
    249             }
    250         }
    251         if (count($backlinks)) {
    252             $backlinks = "<div class=\"linked-list\" style=\"clear:left\">". _t('AssetTableField.PAGESLINKING','The following pages link to this file:') ."<ul>" . implode("",$backlinks) . "</ul></div>";
    253         } else {
    254             $backlinks = "<p>". _t('AssetTableField.NOLINKS',"This file hasn't been linked to from any pages.") ."</p>";
    255         }
    256         $tab = $detailFormFields->findOrMakeTab("BottomRoot.Links", _t('AssetTableField.LINKS', 'Links'));
    257         $tab->push(new LiteralField("Backlinks", $backlinks));
    258
    259         // the ID field confuses the Controller-logic in finding the right view for ReferencedField
    260         $detailFormFields->removeByName('ID');
    261
    262         if($childData) $childData->extend('updateCMSFields', $detailFormFields);
    263
    264         return $detailFormFields;
    265     }
    266
    267     /**
    268      * Provide some HTML for a search form, to be
    269      * added above the AssetTable field, allowing
    270      * a user to filter the current table's files
    271      * by their filename.
    272      *
    273      * @return string HTML for search form
    274      */
    275     function SearchForm() {
    276         $searchFields = new FieldGroup(
    277             new TextField('FileSearch', _t('MemberTableField.SEARCH', 'Search'), $this->searchingFor),
    278             new HiddenField("ctf[ID]", '', $this->ID),
    279             new HiddenField('FileFieldName', '', $this->name)
    280         );
    281
    282         $actionFields = new LiteralField(
    283             'FileFilterButton',
    284             '<input type="submit" class="action" name="FileFilterButton" value="' . _t('MemberTableField.FILTER', 'Filter') . '" id="FileFilterButton"/>'
    285         );
    286
    287         $fieldContainer = new FieldGroup(
    288             $searchFields,
    289             $actionFields
    290         );
    291
    292         return $fieldContainer->FieldHolder();
    293     }
    294 }
    295
    296 ?>
    297

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -