* Add option to merge documents as PDF

This commit is contained in:
nathan 2021-09-28 17:00:55 -06:00
parent 2a4faf0137
commit 707c57ecf0
3 changed files with 125 additions and 73 deletions

View File

@ -733,6 +733,49 @@ export abstract class EgwApp
this.et2_view.close = destroy;
}
/**
* Merge selected entries into template document
*
* @param {egwAction} _action
* @param {egwActionObject[]} _selected
*/
merge(_action : egwAction, _selected : egwActionObject[])
{
// Find what we need
let nm = null;
let action = _action;
let as_pdf = false;
// Find Select all
while(nm == null && action != null)
{
if(action.data != null && action.data.nextmatch)
{
nm = action.data.nextmatch;
}
action = action.parent;
}
let all = nm?.getSelection().all || false;
as_pdf = action.getActionById('as_pdf')?.checked || false;
// Get list of entry IDs
let ids = [];
for(let i = 0; !all && i < _selected.length; i++)
{
let split = _selected[i].id.split("::");
ids.push(split[1]);
}
let vars = {
..._action.data.merge_data,
pdf: as_pdf,
select_all: all,
id: JSON.stringify(ids)
};
egw.open_link(egw.link('/index.php', vars), '_blank');
}
/**
* Initializes actions and handlers on sidebox (delete)
*

View File

@ -16,6 +16,7 @@ namespace EGroupware\Api\Storage;
use DOMDocument;
use EGroupware\Api;
use EGroupware\Api\Vfs;
use EGroupware\Collabora\Conversion;
use EGroupware\Stylite;
use tidy;
use uiaccountsel;
@ -2115,7 +2116,6 @@ abstract class Merge
$export_limit=null)
{
$documents = array();
$editable_mimes = array();
if ($export_limit == null) $export_limit = self::getExportLimit(); // check if there is a globalsetting
try {
@ -2200,13 +2200,6 @@ abstract class Merge
}
foreach($files as $file)
{
$edit_attributes = array(
'menuaction' => $GLOBALS['egw_info']['flags']['currentapp'].'.'.get_called_class().'.merge_entries',
'document' => $file['path'],
'merge' => get_called_class(),
'id' => '$id',
'select_all' => '$select_all'
);
if (count($dircount) > 1)
{
$name_arr = explode('/', $file['name']);
@ -2224,14 +2217,8 @@ abstract class Merge
switch($count)
{
case (count($name_arr) - 1):
$current_level[$prefix.$file['name']] = array(
'icon' => Api\Vfs::mime_icon($file['mime']),
'caption' => Api\Vfs::decodePath($name_arr[$count]),
'group' => 2,
'postSubmit' => true, // download needs post submit (not Ajax) to work,
'target' => '_blank',
'url' => urldecode(http_build_query($edit_attributes))
);
$current_level[$prefix . $file['name']];
self::document_editable_action($current_level[$prefix . $file['name']], $file);
if($file['mime'] == 'message/rfc822')
{
self::document_mail_action($current_level[$prefix . $file['name']], $file);
@ -2264,12 +2251,8 @@ abstract class Merge
'children' => array(),
);
}
$documents[$file['mime']]['children'][$prefix.$file['name']] = array(
'caption' => Api\Vfs::decodePath($file['name']),
'target' => '_blank',
'postSubmit' => true, // download needs post submit (not Ajax) to work
);
$documents[$file['mime']]['children'][$prefix.$file['name']]['url'] = urldecode(http_build_query($edit_attributes));
$documents[$file['mime']]['children'][$prefix . $file['name']] = array();
self::document_editable_action($documents[$file['mime']]['children'][$prefix . $file['name']], $file);
if($file['mime'] == 'message/rfc822')
{
self::document_mail_action($documents[$file['mime']]['children'][$prefix . $file['name']], $file);
@ -2277,13 +2260,8 @@ abstract class Merge
}
else
{
$documents[$prefix.$file['name']] = array(
'icon' => Api\Vfs::mime_icon($file['mime']),
'caption' => Api\Vfs::decodePath($file['name']),
'group' => 2,
'target' => '_blank'
);
$documents[$prefix.$file['name']]['url'] = urldecode(http_build_query($edit_attributes));
$documents[$prefix . $file['name']] = array();
self::document_editable_action($documents[$prefix . $file['name']], $file);
if($file['mime'] == 'message/rfc822')
{
self::document_mail_action($documents[$prefix . $file['name']], $file);
@ -2291,13 +2269,19 @@ abstract class Merge
}
}
// Add PDF checkbox
$documents['as_pdf'] = array(
'caption' => 'As PDF',
'checkbox' => true,
);
return array(
'icon' => 'etemplate/merge',
'caption' => $caption,
'children' => $documents,
// disable action if no document or export completly forbidden for non-admins
'enabled' => (boolean)$documents && (self::hasExportLimit($export_limit, 'ISALLOWED') || self::is_export_limit_excepted()),
'hideOnDisabled' => true, // do not show 'Insert in document', if no documents defined or no export allowed
'hideOnDisabled' => true,
// do not show 'Insert in document', if no documents defined or no export allowed
'group' => $group,
);
}
@ -2348,15 +2332,24 @@ abstract class Merge
*/
private static function document_editable_action(Array &$action, $file)
{
unset($action['postSubmit']);
static $action_base = array(
// The same for every file
'group' => 2,
// Overwritten for every file
'icon' => '', //Api\Vfs::mime_icon($file['mime']),
'caption' => '', //Api\Vfs::decodePath($name_arr[$count]),
);
$edit_attributes = array(
'menuaction' => 'collabora.EGroupware\\collabora\\Ui.merge_edit',
'menuaction' => $GLOBALS['egw_info']['flags']['currentapp'] . '.' . get_called_class() . '.merge_entries',
'document' => $file['path'],
'merge' => get_called_class(),
'id' => '$id',
'select_all' => '$select_all'
);
$action['url'] = urldecode(http_build_query($edit_attributes));
$action = array_merge($action_base, $action, array(
'icon' => Api\Vfs::mime_icon($file['mime']),
'caption' => Api\Vfs::decodePath($file['name']),
'onExecute' => 'javaScript:app.' . $GLOBALS['egw_info']['flags']['currentapp'] . '.merge',
'merge_data' => $edit_attributes
));
}
/**
@ -2397,12 +2390,13 @@ abstract class Merge
* Merge the selected IDs into the given document, save it to the VFS, then
* either open it in the editor or have the browser download the file.
*
* @param String[]|null $ids Allows extending classes to process IDs in their own way. Leave null to pull from request.
* @param string[]|null $ids Allows extending classes to process IDs in their own way. Leave null to pull from request.
* @param Merge|null $document_merge Already instantiated Merge object to do the merge.
* @param boolean|null $pdf Convert result to PDF
* @throws Api\Exception
* @throws Api\Exception\AssertionFailed
*/
public static function merge_entries(array $ids = null, Merge &$document_merge = null)
public static function merge_entries(array $ids = null, Merge &$document_merge = null, $pdf = null)
{
if(is_null($document_merge) && class_exists($_REQUEST['merge']) && is_subclass_of($_REQUEST['merge'], 'EGroupware\\Api\\Storage\\Merge'))
{
@ -2428,6 +2422,11 @@ abstract class Merge
$ids = self::get_all_ids($document_merge);
}
if(is_null($pdf))
{
$pdf = (boolean)$_REQUEST['pdf'];
}
$filename = $document_merge->get_filename($_REQUEST['document']);
$result = $document_merge->merge_file($_REQUEST['document'], $ids, $filename, '', $header);
@ -2462,7 +2461,16 @@ abstract class Merge
// ignore failed discovery
unset($e);
}
if($editable_mimes[Vfs::mime_content_type($target)])
// PDF conversion
if($editable_mimes[Vfs::mime_content_type($target)] && $pdf)
{
$error = '';
$convert = new Conversion();
$convert->convert($target, $target, 'pdf', $error);
}
if($editable_mimes[Vfs::mime_content_type($target)] &&
!in_array(Vfs::mime_content_type($target), explode(',', $GLOBALS['egw_info']['user']['preferences']['filemanager']['collab_excluded_mimes'])))
{
\Egroupware\Api\Egw::redirect_link('/index.php', array(
'menuaction' => 'collabora.EGroupware\\Collabora\\Ui.editor',

View File

@ -2396,14 +2396,15 @@ class Vfs extends Vfs\Base
*/
static function make_unique($path)
{
$filename = Vfs::basename($path);
$dupe_count = 0;
while(is_file(Vfs::PREFIX . $path))
{
$dupe_count++;
$path = Vfs::dirname($path) . '/' .
pathinfo($path, PATHINFO_FILENAME) .
pathinfo($filename, PATHINFO_FILENAME) .
' (' . ($dupe_count + 1) . ')' . '.' .
pathinfo($path, PATHINFO_EXTENSION);
pathinfo($filename, PATHINFO_EXTENSION);
}
return $path;
}