View source
<?php
define('LINKIT_FILE_URL_TYPE_DIRECT', 'direct');
define('LINKIT_FILE_URL_TYPE_DOWNLOAD', 'download');
define('LINKIT_FILE_URL_TYPE_ENTITY', 'entity');
class LinkitSearchPluginFile extends LinkitSearchPluginEntity {
function ui_title() {
return t('Managed files');
}
function ui_description() {
return t('Extend Linkit with file support (Managed files).');
}
function createDescription($data) {
$description_array = array();
$imageinfo = image_get_info($data->uri);
if ($this->conf['image_extra_info']['thumbnail']) {
$image = $imageinfo ? theme_image_style(array(
'width' => $imageinfo['width'],
'height' => $imageinfo['height'],
'style_name' => 'linkit_thumb',
'path' => $data->uri,
)) : '';
}
if ($this->conf['image_extra_info']['dimensions'] && !empty($imageinfo)) {
$description_array[] = $imageinfo['width'] . 'x' . $imageinfo['height'] . 'px';
}
$description_array[] = parent::createDescription($data);
if ($this->conf['show_scheme']) {
$description_array[] = file_uri_scheme($data->uri) . '://';
}
$description = (isset($image) ? $image : '') . implode('<br />', $description_array);
return $description;
}
function createGroup($entity) {
$group = parent::createGroup($entity);
if ($this->conf['group_by_scheme']) {
$stream_wrapper = file_get_stream_wrappers();
$group .= ' - ' . $stream_wrapper[file_uri_scheme($entity->uri)]['name'];
}
return $group;
}
function createPath($entity) {
$url_type = isset($this->conf['url_type']) ? $this->conf['url_type'] : $this
->getDefaultUrlType();
if ($url_type == LINKIT_FILE_URL_TYPE_DOWNLOAD && !(module_exists('file_entity') && function_exists('file_entity_download_uri'))) {
$url_type = $this
->getDefaultUrlType();
}
switch ($url_type) {
case LINKIT_FILE_URL_TYPE_DIRECT:
$wrapper = file_stream_wrapper_get_instance_by_uri($entity->uri);
if ($wrapper instanceof DrupalPublicStreamWrapper) {
$path = $wrapper
->getDirectoryPath() . '/' . file_uri_target($entity->uri);
}
elseif ($wrapper instanceof DrupalPrivateStreamWrapper) {
$path = 'system/files/' . str_replace('\\', '/', file_uri_target($entity->uri));
}
else {
$path = file_create_url($entity->uri);
}
return linkit_get_insert_plugin_processed_path($this->profile, $path, array(
'language' => (object) array(
'language' => FALSE,
),
));
case LINKIT_FILE_URL_TYPE_DOWNLOAD:
$uri = file_entity_download_uri($entity);
if (isset($uri['options']['query']['token']) && $this->profile->data['insert_plugin']['url_method'] == LINKIT_URL_METHOD_RAW) {
return $uri['path'] . '?token=' . rawurlencode($uri['options']['query']['token']);
}
return linkit_get_insert_plugin_processed_path($this->profile, $uri['path'], $uri['options']);
case LINKIT_FILE_URL_TYPE_ENTITY:
return parent::createPath($entity);
}
}
function getQueryInstance() {
parent::getQueryInstance();
$this->query
->propertyCondition('status', FILE_STATUS_PERMANENT);
}
function buildSettingsForm() {
$form = parent::buildSettingsForm();
$form['entity:file']['show_scheme'] = array(
'#title' => t('Show file scheme'),
'#type' => 'checkbox',
'#default_value' => isset($this->conf['show_scheme']) ? $this->conf['show_scheme'] : '',
);
$form['entity:file']['group_by_scheme'] = array(
'#title' => t('Group files by scheme'),
'#type' => 'checkbox',
'#default_value' => isset($this->conf['group_by_scheme']) ? $this->conf['group_by_scheme'] : '',
);
$form['entity:file']['url_type'] = array(
'#title' => t('URL type'),
'#type' => 'radios',
'#options' => array(
LINKIT_FILE_URL_TYPE_DIRECT => t('Direct file link'),
LINKIT_FILE_URL_TYPE_DOWNLOAD => t('Download file link'),
LINKIT_FILE_URL_TYPE_ENTITY => t('Entity view page'),
),
'#default_value' => isset($this->conf['url_type']) ? $this->conf['url_type'] : $this
->getDefaultUrlType(),
);
if (!(module_exists('file_entity') && function_exists('file_entity_download_uri'))) {
unset($form['entity:file']['url_type']['#options'][LINKIT_FILE_URL_TYPE_DOWNLOAD]);
}
$image_extra_info_options = array(
'thumbnail' => t('Show thumbnails <em>(using the image style !linkit_thumb_link)</em>', array(
'!linkit_thumb_link' => l(t('linkit_thumb'), 'admin/config/media/image-styles/edit/linkit_thumb'),
)),
'dimensions' => t('Show pixel dimensions'),
);
$form['entity:file']['image_extra_info'] = array(
'#title' => t('Images'),
'#type' => 'checkboxes',
'#options' => $image_extra_info_options,
'#default_value' => isset($this->conf['image_extra_info']) ? $this->conf['image_extra_info'] : array(
'thumbnail',
'dimensions',
),
);
return $form;
}
protected function getDefaultUrlType() {
$info = entity_get_info('file');
$callback = $info['uri callback'];
if ($callback == 'entity_metadata_uri_file') {
return LINKIT_FILE_URL_TYPE_DIRECT;
}
else {
return LINKIT_FILE_URL_TYPE_ENTITY;
}
}
}