Skip to content

Commit

Permalink
LP: Plugin: Allow to show PDF files with onlyoffice when enabled - re…
Browse files Browse the repository at this point in the history
…fs BT#22195 #5917
  • Loading branch information
AngelFQC committed Nov 14, 2024
1 parent 54d55b5 commit 8bcf1c1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
56 changes: 23 additions & 33 deletions main/lp/learnpath.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3720,43 +3720,33 @@ public function get_link($type = 'http', $item_id = 0, $provided_toc = false)
'odt',
'dot',
];
$officeExtensions = [
'ppt',
'pptx',
'odp',
'xls',
'xlsx',
'ods',
'csv',
'doc',
'docx',
'odt',
];

if (in_array($extension, $extensionsToDownload)) {
$found = false;
if (in_array($extension, $officeExtensions)) {
$onlyOffice = OnlyofficePlugin::create();
if ($onlyOffice->isEnabled()) {
$lpItem = $this->getItem($item_id);
if ($lpItem->get_type() == 'document') {
$docId = $lpItem->get_path();
if (method_exists('OnlyofficeTools', 'getPathToView')) {
$pathToView = OnlyofficeTools::getPathToView($docId, false);
// getPathView returns empty on error, so if this is the case,
// fallback to normal viewer/downloader
if (!empty($pathToView)) {
$file = $pathToView;
$found = true;
}
}
$onlyofficeEditable = false;

if (OnlyofficePlugin::create()->isEnabled()) {
$lpItem = $this->getItem($item_id);

if ($lpItem->get_type() == 'document'
&& OnlyofficePlugin::isExtensionAllowed($extension)
) {
$docId = $lpItem->get_path();

if (method_exists('OnlyofficeTools', 'getPathToView')) {
$pathToView = OnlyofficeTools::getPathToView($docId, false);
// getPathView returns empty on error, so if this is the case,
// fallback to normal viewer/downloader
if (!empty($pathToView)) {
$file = $pathToView;
$onlyofficeEditable = true;
}
}
}
if (false === $found) {
$file = api_get_path(WEB_CODE_PATH).
'lp/embed.php?type=download&source=file&lp_item_id='.$item_id.'&'.api_get_cidreq();
}
}

if (in_array($extension, $extensionsToDownload) && false === $onlyofficeEditable) {
$file = api_get_path(WEB_CODE_PATH)
.'lp/embed.php?type=download&source=file&lp_item_id='.$item_id.'&'
.api_get_cidreq();
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions plugin/onlyoffice/lib/onlyofficePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,23 @@ public function getPluginName()
{
return $this->pluginName;
}

public static function isExtensionAllowed(string $extension): bool
{
$officeExtensions = [
'ppt',
'pptx',
'odp',
'xls',
'xlsx',
'ods',
'csv',
'doc',
'docx',
'odt',
'pdf',
];

return in_array($extension, $officeExtensions);
}
}

0 comments on commit 8bcf1c1

Please sign in to comment.