Skip to content

Instantly share code, notes, and snippets.

@litzinger
Last active November 9, 2017 05:20
Show Gist options
  • Save litzinger/2846529 to your computer and use it in GitHub Desktop.
Save litzinger/2846529 to your computer and use it in GitHub Desktop.
How to use Assets or EE's File Manager anywhere in the CP
<?php
if ( array_key_exists('assets', $this->EE->addons->get_installed()) )
{
// Load Assets' assets
if ( ! isset($this->EE->session->cache['assets']['included_sheet_resources']))
{
if (! class_exists('Assets_helper'))
{
require PATH_THIRD.'assets/helper.php';
}
$assets_helper = new Assets_helper;
$assets_helper->include_sheet_resources();
}
foreach( $fields as $k => $field)
{
$script = '$("#thumbnail_trigger_'. $k .'").click(function(e){
var sheet = new Assets.Sheet({
filedirs: "all",
kinds: "any",
onSelect: function(file) {
// do something fun with the file object
myCoolFunction(file[0]);
}
});
e.preventDefault();
sheet.show();
});';
$this->EE->cp->add_to_foot('<script type="text/javascript">'. $script .'</script>');
}
}
else
{
$this->EE->load->library('filemanager');
$this->EE->load->library('file_field');
foreach( $fields as $k => $field)
{
$config = array(
'trigger' => '#thumbnail_trigger_'. $k,
'field_name' => '"#thumbnail_'. $k .'"',
'settings' => '{content_type: "img", directory: "all"}',
'callback' => 'function(file, e) {
// do something fun with the file object
myCoolFunction(file[0]);
}'
);
// Pass the config to the file_field, it is responsible for loading all JS assets.
$this->EE->file_field->browser($config);
}
}
Publisher.assets_choose_file = function(button, event)
{
var $container = button.closest(".file_field");
var $file_set = $container.find('.file_set');
var $input = $file_set.prev('input[type=hidden]');
var kind = "any"; //field.data("content-type");
var dirs = "all"; //field.data("directory");
var upload_prefs = $.parseJSON(EE.publisher.upload_prefs);
var sheet = new Assets.Sheet({
filedirs: dirs,
kinds: kind,
onSelect: function(file)
{
var file = file[0];
var file_parts = file.url.split('/');
var file_name = file_parts.slice(-1)[0];
var thumb_url = file.url.replace(file_name, '_thumbs/'+ file_name);
for(dir_id in upload_prefs)
{
path = upload_prefs[dir_id];
if(file.url.indexOf(path.url) !== -1)
{
var directory = dir_id;
var file_path = file.url.replace(path.url, '{filedir_'+ dir_id +'}');
break;
}
}
$file_set.removeClass("js_hide").find('.filename').append('<img src="'+ thumb_url +'" /><br />'+file_name);
$file_set.next('.no_file').addClass('js_hide');
$input.val(file_path);
$file_set.find('.remove_file').click(function(){
Publisher.assets_remove_file(button, event);
});
}
});
event.preventDefault();
sheet.show();
}
Publisher.assets_remove_file = function(button, event)
{
var $container = button.closest(".file_field");
var $file_set = $container.find('.file_set');
var $input = $file_set.prev('input[type=hidden]');
$input.val('');
$file_set.addClass("js_hide").find('.filename').html('');
$file_set.next('.no_file').removeClass('js_hide');
event.preventDefault();
}
<?php
// Here is a more specific example used in the Publisher module
public function load_file_manager()
{
if ( array_key_exists('assets', ee()->addons->get_installed()) )
{
// Load Assets' assets
if ( ! isset(ee()->session->cache['assets']['included_sheet_resources']))
{
if (! class_exists('Assets_helper'))
{
require PATH_THIRD.'assets/helper.php';
}
$assets_helper = new Assets_helper;
$assets_helper->include_sheet_resources();
}
$script = '$(".templateTable").delegate(".choose_file", "click", $.proxy(function(e){
Publisher.assets_choose_file($(e.target), e);
}, this));
$(".templateTable").delegate(".remove_file", "click", $.proxy(function(e){
Publisher.assets_remove_file($(e.target), e);
}, this));';
ee()->cp->add_to_foot('<script type="text/javascript">'. $script .'</script>');
}
else
{
ee()->load->library('filemanager');
ee()->load->library('file_field');
// Pass the config to the file_field, it is responsible for loading all JS assets.
ee()->file_field->browser();
ee()->cp->add_js_script(array(
'file' => array('cp/publish')
));
$date_fmt = (ee()->session->userdata('time_format') != '')
? ee()->session->userdata('time_format')
: ee()->config->item('time_format');
$this->_setup_file_list();
ee()->javascript->set_global(array(
'date.format' => $date_fmt,
'upload_directories' => $this->_file_manager['file_list'],
// Set a few vars to empty, otherwise the JS throws errors. We don't need valid values here.
'publish.markitup.fields' => '',
'user.can_edit_html_buttons' => '',
'publish.lang.edit_cateogry' => ''
));
}
}
/**
* Setup File List Actions
*
* @return void
*/
private function _setup_file_list()
{
ee()->load->model('file_upload_preferences_model');
$upload_directories = ee()->file_upload_preferences_model->get_file_upload_preferences(ee()->session->userdata('group_id'));
$this->_file_manager = array(
'file_list' => array(),
'upload_directories' => array(),
);
$fm_opts = array(
'id', 'name', 'url', 'pre_format', 'post_format',
'file_pre_format', 'file_post_format', 'properties',
'file_properties'
);
foreach($upload_directories as $id => $data)
{
$this->_file_manager['upload_directories'][$id] = $data['name'];
foreach($fm_opts as $prop)
{
$this->_file_manager['file_list'][$id][$prop] = $data[$prop];
}
}
}
@jimrepos
Copy link

Worth noting that, as per the above, the field_name parameter needs to be a jQuery selector for the field to populate, not just the field name like the documentation suggests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment