*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/**
* \file htdocs/ecm/file_card.php
* \ingroup ecm
* \brief Card of a file for ECM module
*/
require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/ecm.lib.php';
// Load translation files required by page
$langs->loadLangs(array('ecm', 'companies', 'other', 'users', 'orders', 'propal', 'bills', 'contracts', 'categories'));
$action = GETPOST('action', 'aZ09');
$cancel = GETPOST('cancel', 'alpha');
if (!$user->rights->ecm->setup) accessforbidden();
// Get parameters
$socid = GETPOST("socid", "int");
// Security check
if ($user->societe_id > 0)
{
$action = '';
$socid = $user->societe_id;
}
$sortfield = GETPOST("sortfield", 'alpha');
$sortorder = GETPOST("sortorder", 'alpha');
$page = GETPOST("page", 'int');
if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
$offset = $conf->liste_limit * $page;
$pageprev = $page - 1;
$pagenext = $page + 1;
if (! $sortorder) $sortorder="ASC";
if (! $sortfield) $sortfield="label";
$section=GETPOST("section", 'alpha');
if (! $section)
{
dol_print_error('', 'Error, section parameter missing');
exit;
}
$urlfile=GETPOST("urlfile");
if (! $urlfile)
{
dol_print_error('', "ErrorParamNotDefined");
exit;
}
// Load ecm object
$ecmdir = new EcmDirectory($db);
$result=$ecmdir->fetch(GETPOST("section", 'alpha'));
if (! $result > 0)
{
dol_print_error($db, $ecmdir->error);
exit;
}
$relativepath=$ecmdir->getRelativePath();
$upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
$fullpath=$conf->ecm->dir_output.'/'.$relativepath.$urlfile;
$file = new stdClass();
$file->section_id=$ecmdir->id;
$file->label=$urlfile;
$relativetodocument = 'ecm/'.$relativepath; // $relativepath is relative to ECM dir, we need relative to document
$filepath=$relativepath.$file->label;
$filepathtodocument=$relativetodocument.$file->label;
// Try to load object from index
$object = new ECMFiles($db);
$result=$object->fetch(0, '', $filepathtodocument);
if ($result < 0)
{
dol_print_error($db, $object->error, $object->errors);
exit;
}
/*
* Actions
*/
if ($cancel)
{
$action ='';
if ($backtopage)
{
header("Location: ".$backtopage);
exit;
}
else
{
header('Location: '.$_SERVER["PHP_SELF"].'?urlfile='.urlencode($urlfile).'§ion='.urlencode($section).($module?'&module='.urlencode($module):''));
exit;
}
}
// Rename file
if ($action == 'update')
{
$error=0;
$oldlabel=GETPOST('urlfile', 'alpha');
$newlabel=GETPOST('label', 'alpha');
$shareenabled = GETPOST('shareenabled', 'alpha');
//$db->begin();
$olddir=$ecmdir->getRelativePath(0); // Relative to ecm
$olddirrelativetodocument = 'ecm/'.$olddir; // Relative to document
$newdirrelativetodocument = 'ecm/'.$olddir;
$olddir=$conf->ecm->dir_output.'/'.$olddir;
$newdir=$olddir;
$oldfile=$olddir.$oldlabel;
$newfile=$newdir.$newlabel;
// Now we update index of file
$db->begin();
//print $oldfile.' - '.$newfile;
if ($newlabel != $oldlabel)
{
$result=dol_move($oldfile, $newfile); // This include update of database
if (! $result)
{
$langs->load('errors');
setEventMessages($langs->trans('ErrorFailToRenameFile', $oldfile, $newfile), null, 'errors');
$error++;
}
// Reload object after the move
$result=$object->fetch(0, '', $newdirrelativetodocument.$newlabel);
if ($result < 0)
{
dol_print_error($db, $object->error, $object->errors);
exit;
}
}
if (! $error)
{
if ($shareenabled)
{
require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
$object->share = getRandomPassword(true);
}
else
{
$object->share = '';
}
if ($object->id > 0)
{
// Call update to set the share key
$result = $object->update($user);
if ($result < 0)
{
setEventMessages($object->error, $object->errors, 'warnings');
}
}
else
{
// Call create to insert record
$object->entity = $conf->entity;
$object->filepath = preg_replace('/[\\/]+$/', '', $newdirrelativetodocument);
$object->filename = $newlabel;
$object->label = md5_file(dol_osencode($newfile)); // hash of file content
$object->fullpath_orig = '';
$object->gen_or_uploaded = 'unknown';
$object->description = ''; // indexed content
$object->keyword = ''; // keyword content
$result = $object->create($user);
if ($result < 0)
{
setEventMessages($object->error, $object->errors, 'warnings');
}
}
}
if (!$error)
{
$db->commit();
$urlfile=$newlabel;
header('Location: '.$_SERVER["PHP_SELF"].'?urlfile='.urlencode($urlfile).'§ion='.urlencode($section));
exit;
}
else
{
$db->rollback();
}
}
/*
* View
*/
$form=new Form($db);
llxHeader();
$head = ecm_file_prepare_head($file);
if ($action == 'edit')
{
print '
';
}
// Confirmation de la suppression d'une ligne categorie
if ($action == 'delete_file')
{
print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.urlencode($section), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile', $urlfile), 'confirm_deletefile', '', 1, 1);
}
if ($action != 'edit')
{
// Actions buttons
print '';
}
// End of page
llxFooter();
$db->close();