NEW Add a button "Test collect" in email collector

This commit is contained in:
Laurent Destailleur 2022-09-16 14:50:25 +02:00
parent a0abad5586
commit 111e5ba3f0
3 changed files with 643 additions and 613 deletions

View File

@ -235,10 +235,25 @@ if ($action == 'deleteoperation') {
} }
} }
if ($action == 'collecttest') {
dol_include_once('/emailcollector/class/emailcollector.class.php');
$res = $object->doCollectOneCollector(2);
if ($res > 0) {
$debuginfo = $object->debuginfo;
setEventMessages($object->lastresult, null, 'mesgs');
} else {
$debuginfo = $object->debuginfo;
setEventMessages($object->error, null, 'errors');
}
$action = '';
}
if ($action == 'confirm_collect') { if ($action == 'confirm_collect') {
dol_include_once('/emailcollector/class/emailcollector.class.php'); dol_include_once('/emailcollector/class/emailcollector.class.php');
$res = $object->doCollectOneCollector(); $res = $object->doCollectOneCollector(0);
if ($res > 0) { if ($res > 0) {
$debuginfo = $object->debuginfo; $debuginfo = $object->debuginfo;
setEventMessages($object->lastresult, null, 'mesgs'); setEventMessages($object->lastresult, null, 'mesgs');
@ -784,6 +799,8 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=clone&token='.newToken().'&object=order">'.$langs->trans("ToClone").'</a></div>'; print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=clone&token='.newToken().'&object=order">'.$langs->trans("ToClone").'</a></div>';
// Collect now // Collect now
print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=collecttest&token='.newToken().'">'.$langs->trans("TestCollectNow").'</a></div>';
if (count($object->actions) > 0) { if (count($object->actions) > 0) {
print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=collect&token='.newToken().'">'.$langs->trans("CollectNow").'</a></div>'; print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=collect&token='.newToken().'">'.$langs->trans("CollectNow").'</a></div>';
} else { } else {

View File

@ -126,8 +126,8 @@ class EmailCollector extends CommonObject
'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'showoncombobox'=>1, 'index'=>1, 'position'=>10, 'searchall'=>1, 'help'=>'Example: MyCollector1', 'csslist'=>'tdoverflowmax150'), 'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'showoncombobox'=>1, 'index'=>1, 'position'=>10, 'searchall'=>1, 'help'=>'Example: MyCollector1', 'csslist'=>'tdoverflowmax150'),
'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'visible'=>1, 'enabled'=>1, 'position'=>30, 'notnull'=>-1, 'searchall'=>1, 'help'=>'Example: My Email collector', 'csslist'=>'tdoverflowmax150'), 'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'visible'=>1, 'enabled'=>1, 'position'=>30, 'notnull'=>-1, 'searchall'=>1, 'help'=>'Example: My Email collector', 'csslist'=>'tdoverflowmax150'),
'description' => array('type'=>'text', 'label'=>'Description', 'visible'=>-1, 'enabled'=>1, 'position'=>60, 'notnull'=>-1, 'csslist'=>'small'), 'description' => array('type'=>'text', 'label'=>'Description', 'visible'=>-1, 'enabled'=>1, 'position'=>60, 'notnull'=>-1, 'csslist'=>'small'),
'host' => array('type'=>'varchar(255)', 'label'=>'EMailHost', 'visible'=>1, 'enabled'=>1, 'position'=>90, 'notnull'=>1, 'searchall'=>1, 'comment'=>"IMAP server", 'help'=>'Example: imap.gmail.com', 'csslist'=>'tdoverflow125'), 'host' => array('type'=>'varchar(255)', 'label'=>'EMailHost', 'visible'=>1, 'enabled'=>1, 'position'=>90, 'notnull'=>1, 'searchall'=>1, 'comment'=>"IMAP server", 'help'=>'Example: imap.gmail.com', 'csslist'=>'tdoverflowmax125'),
'port' => array('type'=>'varchar(10)', 'label'=>'EMailHostPort', 'visible'=>1, 'enabled'=>1, 'position'=>91, 'notnull'=>1, 'searchall'=>0, 'comment'=>"IMAP server port", 'help'=>'Example: 993', 'csslist'=>'tdoverflow125', 'default'=>'993'), 'port' => array('type'=>'varchar(10)', 'label'=>'EMailHostPort', 'visible'=>1, 'enabled'=>1, 'position'=>91, 'notnull'=>1, 'searchall'=>0, 'comment'=>"IMAP server port", 'help'=>'Example: 993', 'csslist'=>'tdoverflowmax50', 'default'=>'993'),
'hostcharset' => array('type'=>'varchar(16)', 'label'=>'HostCharset', 'visible'=>-1, 'enabled'=>1, 'position'=>92, 'notnull'=>0, 'searchall'=>0, 'comment'=>"IMAP server charset", 'help'=>'Example: "UTF-8" (May be "US-ASCII" with some Office365)', 'default'=>'UTF-8'), 'hostcharset' => array('type'=>'varchar(16)', 'label'=>'HostCharset', 'visible'=>-1, 'enabled'=>1, 'position'=>92, 'notnull'=>0, 'searchall'=>0, 'comment'=>"IMAP server charset", 'help'=>'Example: "UTF-8" (May be "US-ASCII" with some Office365)', 'default'=>'UTF-8'),
'acces_type' => array('type'=>'integer', 'label'=>'accessType', 'visible'=>-1, 'enabled'=>"getDolGlobalInt('MAIN_IMAP_USE_PHPIMAP')", 'position'=>101, 'notnull'=>1, 'index'=>1, 'comment'=>"IMAP login type", 'arrayofkeyval'=>array('0'=>'loginPassword', '1'=>'oauthToken'), 'default'=>'0', 'help'=>''), 'acces_type' => array('type'=>'integer', 'label'=>'accessType', 'visible'=>-1, 'enabled'=>"getDolGlobalInt('MAIN_IMAP_USE_PHPIMAP')", 'position'=>101, 'notnull'=>1, 'index'=>1, 'comment'=>"IMAP login type", 'arrayofkeyval'=>array('0'=>'loginPassword', '1'=>'oauthToken'), 'default'=>'0', 'help'=>''),
'login' => array('type'=>'varchar(128)', 'label'=>'Login', 'visible'=>-1, 'enabled'=>1, 'position'=>102, 'notnull'=>-1, 'index'=>1, 'comment'=>"IMAP login", 'help'=>'Example: myaccount@gmail.com'), 'login' => array('type'=>'varchar(128)', 'label'=>'Login', 'visible'=>-1, 'enabled'=>1, 'position'=>102, 'notnull'=>-1, 'index'=>1, 'comment'=>"IMAP login", 'help'=>'Example: myaccount@gmail.com'),
@ -1001,9 +1001,10 @@ class EmailCollector extends CommonObject
/** /**
* Execute collect for current collector loaded previously with fetch. * Execute collect for current collector loaded previously with fetch.
* *
* @param int $mode 0=Mode production, 1=Mode test (read IMAP and try SQL update then rollback), 2=Mode test with no SQL updates
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
public function doCollectOneCollector() public function doCollectOneCollector($mode = 0)
{ {
global $db, $conf, $langs, $user; global $db, $conf, $langs, $user;
global $hookmanager; global $hookmanager;
@ -1019,6 +1020,7 @@ class EmailCollector extends CommonObject
$error = 0; $error = 0;
$this->output = ''; $this->output = '';
$this->error = ''; $this->error = '';
$this->debuginfo = '';
$now = dol_now(); $now = dol_now();
@ -1950,6 +1952,7 @@ class EmailCollector extends CommonObject
} }
// Do operations // Do operations
if ($mode < 2) {
foreach ($this->actions as $operation) { foreach ($this->actions as $operation) {
$errorforthisaction = 0; $errorforthisaction = 0;
@ -2737,11 +2740,12 @@ class EmailCollector extends CommonObject
$nbactiondoneforemail++; $nbactiondoneforemail++;
} }
} }
}
// Error for email or not ? // Error for email or not ?
if (!$errorforactions) { if (!$errorforactions) {
if ($targetdir && empty($mode)) {
if (empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) { if (empty($conf->global->MAIN_IMAP_USE_PHPIMAP)) {
if ($targetdir) {
dol_syslog("EmailCollector::doCollectOneCollector move message ".$imapemail." to ".$connectstringtarget, LOG_DEBUG); dol_syslog("EmailCollector::doCollectOneCollector move message ".$imapemail." to ".$connectstringtarget, LOG_DEBUG);
$res = imap_mail_move($connection, $imapemail, $targetdir, 0); $res = imap_mail_move($connection, $imapemail, $targetdir, 0);
if ($res == false) { if ($res == false) {
@ -2751,9 +2755,10 @@ class EmailCollector extends CommonObject
dol_syslog(imap_last_error()); dol_syslog(imap_last_error());
} }
} else { } else {
dol_syslog("EmailCollector::doCollectOneCollector message ".$imapemail." to ".$connectstringtarget." was set to read", LOG_DEBUG); // TODO Move mail using PHP-IMAP
// TODO Make the move
} }
} else {
dol_syslog("EmailCollector::doCollectOneCollector message ".$imapemail." to ".$connectstringtarget." was set to read", LOG_DEBUG);
} }
} else { } else {
$errorforemail++; $errorforemail++;
@ -2771,7 +2776,11 @@ class EmailCollector extends CommonObject
$nbactiondone += $nbactiondoneforemail; $nbactiondone += $nbactiondoneforemail;
$nbemailok++; $nbemailok++;
if (empty($mode)) {
$this->db->commit(); $this->db->commit();
} else {
$this->db->rollback();
}
// Stop the loop to process email if we reach maximum collected per collect // Stop the loop to process email if we reach maximum collected per collect
if ($this->maxemailpercollect > 0 && $nbemailok >= $this->maxemailpercollect) { if ($this->maxemailpercollect > 0 && $nbemailok >= $this->maxemailpercollect) {
@ -2802,12 +2811,12 @@ class EmailCollector extends CommonObject
$this->datelastresult = $now; $this->datelastresult = $now;
$this->lastresult = $output; $this->lastresult = $output;
$this->debuginfo = 'IMAP search string used : '.$search; $this->debuginfo .= 'IMAP search string used : '.$search;
if ($searchhead) { if ($searchhead) {
$this->debuginfo .= '<br>Then search string into email header : '.$searchhead; $this->debuginfo .= '<br>Then search string into email header : '.$searchhead;
} }
if (!$error) { if (empty($error) && empty($mode)) {
$this->datelastok = $now; $this->datelastok = $now;
} }
@ -2815,11 +2824,14 @@ class EmailCollector extends CommonObject
$this->lastresult .= "<br>".join("<br>", $this->errors); $this->lastresult .= "<br>".join("<br>", $this->errors);
} }
$this->codelastresult = ($error ? 'KO' : 'OK'); $this->codelastresult = ($error ? 'KO' : 'OK');
if (empty($mode)) {
$this->update($user); $this->update($user);
}
dol_syslog("EmailCollector::doCollectOneCollector end", LOG_DEBUG); dol_syslog("EmailCollector::doCollectOneCollector end", LOG_DEBUG);
return $error ?-1 : 1; return $error ? -1 : 1;
} }

View File

@ -2081,6 +2081,7 @@ MailboxTargetDirectory=Mailbox target directory
EmailcollectorOperations=Operations to do by collector EmailcollectorOperations=Operations to do by collector
EmailcollectorOperationsDesc=Operations are executed from top to bottom order EmailcollectorOperationsDesc=Operations are executed from top to bottom order
MaxEmailCollectPerCollect=Max number of emails collected per collect MaxEmailCollectPerCollect=Max number of emails collected per collect
TestCollectNow=Test collect
CollectNow=Collect now CollectNow=Collect now
ConfirmCloneEmailCollector=Are you sure you want to clone the Email collector %s? ConfirmCloneEmailCollector=Are you sure you want to clone the Email collector %s?
DateLastCollectResult=Date of latest collect try DateLastCollectResult=Date of latest collect try