Skip to content

Commit

Permalink
NEW Generated files are now indexed in database
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Aug 18, 2017
1 parent 097b660 commit 260beed
Show file tree
Hide file tree
Showing 42 changed files with 186 additions and 56 deletions.
51 changes: 25 additions & 26 deletions htdocs/core/actions_massactions.inc.php
Expand Up @@ -440,26 +440,26 @@
$outputlangs->setDefaultLang($newlang);
}

if(!empty($conf->global->USE_PDFTK_FOR_PDF_CONCAT)) {
if (!empty($conf->global->USE_PDFTK_FOR_PDF_CONCAT))
{
// Create output dir if not exists
dol_mkdir($diroutputmassaction);

// Defined name of merged file
$filename=strtolower(dol_sanitizeFileName($langs->transnoentities($objectlabel)));
$filename=preg_replace('/\s/','_',$filename);

// Save merged file
if ($filter=='paye:0')
{
if ($option=='late') $filename.='_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid"))).'_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Late")));
else $filename.='_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid")));
}
if ($year) $filename.='_'.$year;
if ($month) $filename.='_'.$month;
dol_mkdir($diroutputmassaction);
// Defined name of merged file
$filename=strtolower(dol_sanitizeFileName($langs->transnoentities($objectlabel)));
$filename=preg_replace('/\s/','_',$filename);
// Save merged file
if ($filter=='paye:0')
{
if ($option=='late') $filename.='_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid"))).'_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Late")));
else $filename.='_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid")));
}
if ($year) $filename.='_'.$year;
if ($month) $filename.='_'.$month;

if (count($files)>0)
{

$now=dol_now();
$file=$diroutputmassaction.'/'.$filename.'_'.dol_print_date($now,'dayhourlog').'.pdf';

Expand All @@ -474,14 +474,13 @@
if (! empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK));

$langs->load("exports");
setEventMessages($langs->trans('FileSuccessfullyBuilt',$filename.'_'.dol_print_date($now,'dayhourlog')), null, 'mesgs');
$langs->load("exports");
setEventMessages($langs->trans('FileSuccessfullyBuilt',$filename.'_'.dol_print_date($now,'dayhourlog')), null, 'mesgs');
}
else
{
setEventMessages($langs->trans('NoPDFAvailableForDocGenAmongChecked'), null, 'errors');
}

}
else {
// Create empty PDF
Expand Down Expand Up @@ -519,18 +518,18 @@
// Save merged file
if ($filter=='paye:0')
{
if ($option=='late') $filename.='_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid"))).'_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Late")));
else $filename.='_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid")));
if ($option=='late') $filename.='_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid"))).'_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Late")));
else $filename.='_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid")));
}
if ($year) $filename.='_'.$year;
if ($month) $filename.='_'.$month;
if ($pagecount)
{
$now=dol_now();
$file=$diroutputmassaction.'/'.$filename.'_'.dol_print_date($now,'dayhourlog').'.pdf';
$pdf->Output($file,'F');
if (! empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK));
$now=dol_now();
$file=$diroutputmassaction.'/'.$filename.'_'.dol_print_date($now,'dayhourlog').'.pdf';
$pdf->Output($file,'F');
if (! empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK));

$langs->load("exports");
setEventMessages($langs->trans('FileSuccessfullyBuilt',$filename.'_'.dol_print_date($now,'dayhourlog')), null, 'mesgs');
Expand Down
62 changes: 58 additions & 4 deletions htdocs/core/class/commonobject.class.php
Expand Up @@ -3876,7 +3876,7 @@ function __clone()
*
* @param string $modelspath Relative folder where generators are placed
* @param string $modele Generator to use. Caller must set it to obj->modelpdf or GETPOST('modelpdf') for example.
* @param Translate $outputlangs Language to use
* @param Translate $outputlangs Output language to use
* @param int $hidedetails 1 to hide details. 0 by default
* @param int $hidedesc 1 to hide product description. 0 by default
* @param int $hideref 1 to hide product reference. 0 by default
Expand All @@ -3885,7 +3885,7 @@ function __clone()
*/
protected function commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
{
global $conf, $langs;
global $conf, $langs, $user;

$srctemplatepath='';

Expand All @@ -3903,7 +3903,6 @@ protected function commonGenerateDocument($modelspath, $modele, $outputlangs, $h
$srctemplatepath=$tmp[1];
}


// Search template files
$file=''; $classname=''; $filefound=0;
$dirmodels=array('/');
Expand Down Expand Up @@ -3995,7 +3994,10 @@ protected function commonGenerateDocument($modelspath, $modele, $outputlangs, $h
$arrayofrecords = array(); // The write_file of templates of adherent class need this
$resultwritefile = $obj->write_file($this, $outputlangs, $srctemplatepath, 'member', 1, $moreparams);
}
else $resultwritefile = $obj->write_file($this, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref, $moreparams);
else
{
$resultwritefile = $obj->write_file($this, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref, $moreparams);
}

if ($resultwritefile > 0)
{
Expand All @@ -4005,6 +4007,58 @@ protected function commonGenerateDocument($modelspath, $modele, $outputlangs, $h
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
dol_delete_preview($this);

// Index file in database
if (! empty($obj->result['fullpath']))
{
$destfull = $obj->result['fullpath'];
$upload_dir = dirname($destfull);
$destfile = basename($destfull);
$rel_dir = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $upload_dir);

if (! preg_match('/[\\/]temp[\\/]|[\\/]thumbs|\.meta$/', $rel_dir)) // If not a tmp dir
{
$filename = basename($destfile);
$rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
$rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);

include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
$ecmfile=new EcmFiles($this->db);
$result = $ecmfile->fetch(0, '', ($rel_dir?$rel_dir.'/':'').$filename);
if ($result > 0)
{
$ecmfile->label = md5_file(dol_osencode($destfull));
$ecmfile->fullpath_orig = '';
$ecmfile->gen_or_uploaded = 'generated';
$ecmfile->description = ''; // indexed content
$ecmfile->keyword = ''; // keyword content
$result = $ecmfile->update($user);
if ($result < 0)
{
setEventMessages($ecmfile->error, $ecmfile->errors, 'warnings');
}
}
else
{
$ecmfile->filepath = $rel_dir;
$ecmfile->filename = $filename;
$ecmfile->label = md5_file(dol_osencode($destfull));
$ecmfile->fullpath_orig = '';
$ecmfile->gen_or_uploaded = 'generated';
$ecmfile->description = ''; // indexed content
$ecmfile->keyword = ''; // keyword content
$result = $ecmfile->create($user);
if ($result < 0)
{
setEventMessages($ecmfile->error, $ecmfile->errors, 'warnings');
}
}
}
}
else
{
dol_syslog('Method ->write_file was called on object '.get_class($obj).' and return a success but the return array ->result["fullpath"] was not set.', LOG_WARNING);
}

// Success in building document. We build meta file.
dol_meta_create($this);

Expand Down
33 changes: 19 additions & 14 deletions htdocs/core/lib/files.lib.php
Expand Up @@ -328,7 +328,7 @@ function completeFileArrayWithDatabaseInfo(&$filearray, $relativedir)
$filearray[$key]['acl']='';

$rel_filename = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $filearray[$key]['fullname']);
if (! preg_match('/(\/temp\/|\/thumbs|\.meta$)/', $rel_filetorenameafter)) // If not a tmp file
if (! preg_match('/([\\/]temp[\\/]|[\\/]thumbs|\.meta$)/', $rel_filetorenameafter)) // If not a tmp file
{
dol_syslog("list_of_documents We found a file called '".$filearray[$key]['name']."' not indexed into database. We add it");
include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
Expand Down Expand Up @@ -826,7 +826,7 @@ function dol_move($srcfile, $destfile, $newmask=0, $overwriteifexists=1, $testvi
// Rename entry into ecm database
$rel_filetorenamebefore = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $srcfile);
$rel_filetorenameafter = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $destfile);
if (! preg_match('/(\/temp\/|\/thumbs|\.meta$)/', $rel_filetorenameafter)) // If not a tmp file
if (! preg_match('/([\\/]temp[\\/]|[\\/]thumbs|\.meta$)/', $rel_filetorenameafter)) // If not a tmp file
{
$rel_filetorenamebefore = preg_replace('/^[\\/]/', '', $rel_filetorenamebefore);
$rel_filetorenameafter = preg_replace('/^[\\/]/', '', $rel_filetorenameafter);
Expand Down Expand Up @@ -1324,16 +1324,17 @@ function dol_delete_preview($object)

/**
* Create a meta file with document file into same directory.
* This should allow "grep" search.
* This feature is enabled only if option MAIN_DOC_CREATE_METAFILE is set.
* This make "grep" search possible.
* This feature to generate the meta file is enabled only if option MAIN_DOC_CREATE_METAFILE is set.
*
* @param CommonObject $object Object
* @return int 0 if we did nothing, >0 success, <0 error
* @return int 0 if do nothing, >0 if we update meta file too, <0 if KO
*/
function dol_meta_create($object)
{
global $conf;

// Create meta file
if (empty($conf->global->MAIN_DOC_CREATE_METAFILE)) return 0; // By default, no metafile.

// Define parent dir of elements
Expand All @@ -1351,9 +1352,9 @@ function dol_meta_create($object)
{
$object->fetch_thirdparty();

$facref = dol_sanitizeFileName($object->ref);
$dir = $dir . "/" . $facref;
$file = $dir . "/" . $facref . ".meta";
$objectref = dol_sanitizeFileName($object->ref);
$dir = $dir . "/" . $objectref;
$file = $dir . "/" . $objectref . ".meta";

if (! is_dir($dir))
{
Expand All @@ -1368,15 +1369,15 @@ function dol_meta_create($object)
DATE=\"" . dol_print_date($object->date,'') . "\"
NB_ITEMS=\"" . $nblignes . "\"
CLIENT=\"" . $client . "\"
TOTAL_HT=\"" . $object->total_ht . "\"
TOTAL_TTC=\"" . $object->total_ttc . "\"\n";
AMOUNT_WO_TAX=\"" . $object->total_ht . "\"
AMOUNT_INC_TAX=\"" . $object->total_ttc . "\"\n";

for ($i = 0 ; $i < $nblignes ; $i++)
{
//Pour les articles
$meta .= "ITEM_" . $i . "_QUANTITY=\"" . $object->lines[$i]->qty . "\"
ITEM_" . $i . "_TOTAL_HT=\"" . $object->lines[$i]->total_ht . "\"
ITEM_" . $i . "_TVA=\"" .$object->lines[$i]->tva_tx . "\"
ITEM_" . $i . "_AMOUNT_WO_TAX=\"" . $object->lines[$i]->total_ht . "\"
ITEM_" . $i . "_VAT=\"" .$object->lines[$i]->tva_tx . "\"
ITEM_" . $i . "_DESCRIPTION=\"" . str_replace("\r\n","",nl2br($object->lines[$i]->desc)) . "\"
";
}
Expand All @@ -1390,6 +1391,10 @@ function dol_meta_create($object)

return 1;
}
else
{
dol_syslog('FailedToDetectDirInDolMetaCreateFor'.$object->element, LOG_WARNING);
}

return 0;
}
Expand Down Expand Up @@ -1428,7 +1433,7 @@ function dol_init_file_process($pathtoscan='', $trackid='')


/**
* Get and save an upload file (for example after submitting a new file a mail form).
* Get and save an upload file (for example after submitting a new file a mail form). Database index of file is also updated if donotupdatesession is set.
* All information used are in db, conf, langs, user and _FILES.
* Note: This function can be used only into a HTML page context.
*
Expand Down Expand Up @@ -1522,7 +1527,7 @@ function dol_add_file_process($upload_dir, $allowoverwrite=0, $donotupdatesessio
{
$rel_dir = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $upload_dir);

if (! preg_match('/[\\/]temp[\\/]/', $rel_dir)) // If not a tmp dir
if (! preg_match('/[\\/]temp[\\/]|[\\/]thumbs|\.meta$/', $rel_dir)) // If not a tmp dir
{
$filename = basename($destfile);
$rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
Expand Down
2 changes: 2 additions & 0 deletions htdocs/core/modules/action/rapport.pdf.php
Expand Up @@ -178,6 +178,8 @@ function write_file($socid = 0, $catid = 0, $outputlangs='')
if (! empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK));

$this->result = array('fullpath'=>$file);

return 1;
}
}
Expand Down
2 changes: 2 additions & 0 deletions htdocs/core/modules/bank/doc/pdf_ban.modules.php
Expand Up @@ -249,6 +249,8 @@ function write_file($object,$outputlangs)
if (! empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK));

$this->result = array('fullpath'=>$file);

return 1; // Pas d'erreur
}
else
Expand Down
2 changes: 2 additions & 0 deletions htdocs/core/modules/bank/doc/pdf_sepamandate.modules.php
Expand Up @@ -396,6 +396,8 @@ function write_file($object, $outputlangs, $srctemplatepath='', $hidedetails=0,
if (! empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK));

$this->result = array('fullpath'=>$file);

return 1; // Pas d'erreur
}
else
Expand Down
2 changes: 2 additions & 0 deletions htdocs/core/modules/cheque/doc/pdf_blochet.class.php
Expand Up @@ -193,6 +193,8 @@ function write_file($object, $_dir, $number, $outputlangs)
if (! empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK));

$this->result = array('fullpath'=>$file);

$outputlangs->charset_output=$sav_charset_output;
return 1; // Pas d'erreur
}
Expand Down
Expand Up @@ -467,6 +467,8 @@ function write_file($object,$outputlangs,$srctemplatepath,$hidedetails=0,$hidede

$odfHandler=null; // Destroy object

$this->result = array('fullpath'=>$file);

return 1; // Success
}
else
Expand Down
2 changes: 2 additions & 0 deletions htdocs/core/modules/commande/doc/pdf_einstein.modules.php
Expand Up @@ -603,6 +603,8 @@ function write_file($object,$outputlangs,$srctemplatepath='',$hidedetails=0,$hid
if (! empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK));

$this->result = array('fullpath'=>$file);

return 1; // Pas d'erreur
}
else
Expand Down
Expand Up @@ -454,6 +454,8 @@ function write_file($object,$outputlangs,$srctemplatepath,$hidedetails=0,$hidede

$odfHandler=null; // Destroy object

$this->result = array('fullpath'=>$file);

return 1; // Success
}
else
Expand Down
2 changes: 2 additions & 0 deletions htdocs/core/modules/contract/doc/pdf_strato.modules.php
Expand Up @@ -396,6 +396,8 @@ function write_file($object,$outputlangs,$srctemplatepath='',$hidedetails=0,$hid
if (! empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK));

$this->result = array('fullpath'=>$file);

return 1;
}
else
Expand Down
2 changes: 2 additions & 0 deletions htdocs/core/modules/dons/html_cerfafr.modules.php
Expand Up @@ -259,6 +259,8 @@ function write_file($don,$outputlangs,$currency='')
if (! empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK));

$this->result = array('fullpath'=>$file);

return 1;
}
else
Expand Down
Expand Up @@ -510,6 +510,8 @@ function write_file($object,$outputlangs,$srctemplatepath,$hidedetails=0,$hidede

$odfHandler=null; // Destroy object

$this->result = array('fullpath'=>$file);

return 1; // Success
}
else
Expand Down
2 changes: 2 additions & 0 deletions htdocs/core/modules/expedition/doc/pdf_merou.modules.php
Expand Up @@ -359,6 +359,8 @@ function write_file(&$object,$outputlangs,$srctemplatepath='',$hidedetails=0,$hi
if (! empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK));

$this->result = array('fullpath'=>$file);

return 1;
}
else
Expand Down
2 changes: 2 additions & 0 deletions htdocs/core/modules/expedition/doc/pdf_rouget.modules.php
Expand Up @@ -567,6 +567,8 @@ function write_file($object,$outputlangs,$srctemplatepath='',$hidedetails=0,$hid
if (! empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK));

$this->result = array('fullpath'=>$file);

return 1; // No error
}
else
Expand Down

0 comments on commit 260beed

Please sign in to comment.