Last active
December 27, 2015 07:59
-
-
Save chocnut/7292700 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class GridFieldSelectColumn implements GridField_ColumnProvider { | |
public function __construct($useToggle = true, $targetFragment = 'before') { | |
$this->targetFragment = $targetFragment; | |
$this->useToggle = $useToggle; | |
} | |
public function augmentColumns($field, &$cols) { | |
if(!in_array('Select', $cols)) $cols[] = 'Select'; | |
} | |
public function getColumnsHandled($field) { | |
return array('Select'); | |
} | |
public function getColumnContent($field, $record, $col) { | |
if($record->canView()) { | |
$data = new ArrayData(array( | |
'ID' => $record->ID | |
)); | |
return '<input id="item_'.$record->ID.'" class="checkbox no-change-track" name="selected" type="checkbox" value="'.$record->ID.'" />'; | |
} | |
} | |
public function getColumnAttributes($field, $record, $col) { | |
return array('class' => 'col-select'); | |
} | |
public function getColumnMetadata($gridField, $col) { | |
return array( | |
'title' => '<input id="item_select_all" class="checkbox no-change-track" name="item_select_all" type="checkbox" value="" /> <label for="item_select_all">Select All</label>' | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @package framework | |
* @subpackage gridfield | |
*/ | |
/** | |
* Adds a "Download" button to the bottom of a GridField. | |
*/ | |
class GridFieldSelectedDownload implements GridField_HTMLProvider, GridField_ActionProvider { | |
/** | |
* Fragment to write the button to | |
*/ | |
protected $targetFragment; | |
/** | |
* @param string $targetFragment The HTML fragment to write the button into | |
*/ | |
public function __construct($targetFragment = "after") { | |
$this->targetFragment = $targetFragment; | |
} | |
/** | |
* Place the export button in a <p> tag below the field | |
*/ | |
public function getHTMLFragments($gridField) { | |
Requirements::javascript(BULK_DOWNLOAD_TOOLS_PATH . '/javascript/FileAdmin_GridField.js'); | |
Requirements::add_i18n_javascript(BULK_DOWNLOAD_TOOLS_PATH . '/javascript/lang'); | |
$button = new GridField_FormAction( | |
$gridField, | |
'downloadSelected', | |
'Download', | |
'downloadSelected', | |
null | |
); | |
$button->setAttribute('data-icon', 'network-cloud'); | |
$button->addExtraClass('no-ajax'); | |
return array( | |
$this->targetFragment => '<span class="grid-download-button">' . $button->Field() . '</span>', | |
); | |
} | |
public function getActions($gridField) { | |
return array('downloadSelected'); | |
} | |
public function handleAction(GridField $gridField, $actionName, $arguments, $data) { | |
if ($actionName == 'downloadselected') { | |
return $this->downloadRecords($gridField, $data); | |
} | |
} | |
/** | |
* it is also a URL | |
*/ | |
public function getURLHandlers($gridField) { | |
return array( | |
'downloadSelected' => 'downloadRecords', | |
); | |
} | |
public function downloadRecords($gridField, $request = null) { | |
if (is_array($request) && isset($request['selected'])) { | |
$ids = array($request['selected']); | |
foreach ($ids as $id) { | |
$this->downloadRecord($id); | |
} | |
} | |
} | |
public function downloadRecord($id) { | |
$file = File::get()->byID($id); | |
$file_path = Director::absoluteBaseURL() . $file->Filename; | |
return SS_HTTPRequest::send_file(file_get_contents($file_path), $file->Filename, 'image/jepg'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment