function sbp_attach_sbp_details in Search by Page 6
Same name and namespace in other branches
- 7 sbp_attach.module \sbp_attach_sbp_details()
Implementation of Search by Page hook_sbp_details().
Returns details for a particular file ID, for particular search keys.
File
- ./
sbp_attach.module, line 278 - Module file for Search by Page Attachments, a sub-module for Search by Page.
Code
function sbp_attach_sbp_details($id, $environment, $keys = NULL) {
$id = intval($id);
if (!$id) {
// no ID given to us
return NULL;
}
// Find out where this file came from.
$info = db_fetch_object(db_query('SELECT * FROM {sbpa_files} WHERE fid = %d', $id));
if (!$info) {
// We have no record of this file ID
return NULL;
}
// Get information about the file: node ID, file name, description
$nid = 0;
$fname = '';
$desc = '';
if ($info->source == 'upload') {
// This file came from the Upload module -- get information
$res = db_fetch_object(db_query('SELECT f.filepath, u.description, u.nid FROM {upload} u LEFT JOIN {files} f ON u.fid = f.fid WHERE f.fid = %d AND u.nid = %d AND u.vid = %d', $id, $info->nid, $info->vid));
if (!$res) {
// Upload has no record of this file ID
return NULL;
}
$nid = $res->nid;
$fname = $res->filepath;
$desc = $res->description;
}
else {
// This file came from the CCK module -- get information
if (!function_exists('content_fields') || !function_exists('content_database_info')) {
// CCK module not loaded
return NULL;
}
// First extract db info from CCK
$field = content_fields($info->fieldname, $info->typename);
if (!$field) {
// CCK has no record of the field we recorded this came from
return NULL;
}
$dbinfo = content_database_info($field);
$fidcol = $dbinfo['columns']['fid']['column'];
if (!$fidcol) {
// The CCK field we think this is has no fid column
return NULL;
}
// Optional 'data' column -- might have a Description field inside
// the 'data' array, if this is actually a FileField
$datacol = '';
if ($dbinfo['columns']['data']['column']) {
$datacol = ', ' . $dbinfo['columns']['data']['column'] . ' AS datacol';
}
// Now get node/file info from the database
$res = db_fetch_object(db_query('SELECT f.filepath, u.nid' . $datacol . ' FROM {' . $dbinfo['table'] . '} u LEFT JOIN {files} f ON u.' . $fidcol . ' = f.fid WHERE f.fid = %d AND u.nid = %d AND u.vid = %d', $id, $info->nid, $info->vid));
if (!$res) {
// This file is apparently no longer attached to this content type field
return NULL;
}
$nid = $res->nid;
$fname = $res->filepath;
$data = unserialize($res->datacol);
if (!is_array($data)) {
// Sometimes this needs to be unserialized again, due to bugs in
// CCK/FileField.
$data = unserialize($data);
}
$desc = $data['description'];
}
if (!$nid || !$fname) {
// Ended up with a null NID or file name
return NULL;
}
$node = node_load($nid, NULL, TRUE);
$fileinfo = pathinfo($fname);
$fullpath = file_create_path($fname);
if (!$fullpath || !is_file($fullpath)) {
// File was not in an acceptable location or doesn't exist.
return NULL;
}
// Construct the information for this file.
$ret = array();
// Title.
$use_desc = search_by_page_setting_get('sbp_attach_use_description', $environment, 0);
$prepend_node_title = search_by_page_setting_get('sbp_attach_prepend_node_title', $environment, 0);
$ret['title'] = $fileinfo['basename'];
if ($use_desc && $desc) {
$ret['title'] = $desc;
}
if ($prepend_node_title) {
$sep = search_by_page_setting_get('sbp_attach_title_sep', $environment, '/');
if ($node->title) {
$ret['title'] = $node->title . $sep . $ret['title'];
}
}
$ret['title'] = check_plain($ret['title']);
// Set the content to an empty string, in case we cannot extract it.
// In this case, we'll just return the title we've constructed.
$ret['content'] = '';
// Extract file content, using helper function
$helpers = search_files_get_helpers();
$extension = strtolower($fileinfo['extension']);
$help = isset($helpers[$extension]) ? $helpers[$extension] : FALSE;
if (!$help) {
// no text extraction function for this file extension defined
return $ret;
}
$quoted_file_path = '"' . escapeshellcmd($fullpath) . '"';
$helper_command = preg_replace('/%file%/', $quoted_file_path, $help);
$content = search_files_convert_to_utf8(shell_exec($helper_command));
if (!$content) {
// no text in file
return $ret;
}
if (!$keys) {
// Return file content, if there are no search keys
$ret['content'] = $content;
}
else {
// If there are search keys, return a snippet, user name, date, type, etc.
$ret['user'] = theme('username', $node);
$ret['date'] = $node->changed;
$ret['snippet'] = search_by_page_excerpt($keys, $content);
$ret['related_node'] = $node;
}
return $ret;
}