Skip to content

Commit

Permalink
Work on email collector
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Nov 9, 2018
1 parent f128757 commit e86e3ec
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 49 deletions.
4 changes: 2 additions & 2 deletions htdocs/admin/emailcollector_card.php
Expand Up @@ -175,9 +175,9 @@
{
dol_include_once('/emailcollector/class/emailcollector.class.php');

$res = $object->doCollect();
$res = $object->doCollectOneCollector();

if ($res == 0)
if ($res > 0)
{
setEventMessages($object->output, null, 'mesgs');
}
Expand Down
5 changes: 1 addition & 4 deletions htdocs/core/modules/modEmailCollector.class.php
Expand Up @@ -174,11 +174,8 @@ public function __construct($db)
// Cronjobs (List of cron jobs entries to add when module is enabled)
// unit_frequency must be 60 for minute, 3600 for hour, 86400 for day, 604800 for week
$this->cronjobs = array(
0=>array('label'=>'Email collector', 'jobtype'=>'method', 'class'=>'/emailcollector/class/emailcollector.class.php', 'objectname'=>'EmailCollector', 'method'=>'doCollect', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>5, 'unitfrequency'=>60, 'status'=>1, 'test'=>'$conf->emailcollector->enabled')
0=>array('label'=>'Email collector', 'priority'=>50, 'jobtype'=>'method', 'class'=>'/emailcollector/class/emailcollector.class.php', 'objectname'=>'EmailCollector', 'method'=>'doCollect', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>5, 'unitfrequency'=>60, 'status'=>1, 'test'=>'$conf->emailcollector->enabled')
);
// Example: $this->cronjobs=array(0=>array('label'=>'My label', 'jobtype'=>'method', 'class'=>'/dir/class/file.class.php', 'objectname'=>'MyClass', 'method'=>'myMethod', 'parameters'=>'param1, param2', 'comment'=>'Comment', 'frequency'=>2, 'unitfrequency'=>3600, 'status'=>0, 'test'=>true),
// 1=>array('label'=>'My label', 'jobtype'=>'command', 'command'=>'', 'parameters'=>'param1, param2', 'comment'=>'Comment', 'frequency'=>1, 'unitfrequency'=>3600*24, 'status'=>0, 'test'=>true)
// );


// Permissions
Expand Down
2 changes: 1 addition & 1 deletion htdocs/cron/list.php
Expand Up @@ -192,7 +192,7 @@
$result = 0;
if ($massaction == 'disable') $result = $tmpcron->setStatut(Cronjob::STATUS_DISABLED);
elseif ($massaction == 'enable') $result = $tmpcron->setStatut(Cronjob::STATUS_ENABLED);
else dol_print_error($db, 'Bad value for massaction');
//else dol_print_error($db, 'Bad value for massaction');
if ($result < 0) setEventMessages($tmpcron->error, $tmpcron->errors, 'errors');
}
else
Expand Down
76 changes: 40 additions & 36 deletions htdocs/emailcollector/class/emailcollector.class.php
Expand Up @@ -85,7 +85,7 @@ class EmailCollector extends CommonObject
'host' => array('type'=>'varchar(255)', 'label'=>'EMailHost', 'visible'=>1, 'enabled'=>1, 'position'=>100, 'notnull'=>1, 'searchall'=>1, 'comment'=>"IMAP server", 'help'=>'Example: imap.gmail.com'),
'user' => array('type'=>'varchar(128)', 'label'=>'Login', 'visible'=>1, 'enabled'=>1, 'position'=>101, 'notnull'=>1, 'index'=>1, 'comment'=>"IMAP login", 'help'=>'Example: myacount@gmail.com'),
'password' => array('type'=>'password', 'label'=>'Password', 'visible'=>-1, 'enabled'=>1, 'position'=>102, 'notnull'=>1, 'comment'=>"IMAP password"),
'source_directory' => array('type'=>'varchar(255)', 'label'=>'MailboxSourceDirectory', 'visible'=>-1, 'enabled'=>1, 'position'=>103, 'notnull'=>1, 'default' => 'Inbox'),
'source_directory' => array('type'=>'varchar(255)', 'label'=>'MailboxSourceDirectory', 'visible'=>-1, 'enabled'=>1, 'position'=>103, 'notnull'=>1, 'default' => 'Inbox', 'help'=>'Example: INBOX'),
//'filter' => array('type'=>'text', 'label'=>'Filter', 'visible'=>1, 'enabled'=>1, 'position'=>105),
//'actiontodo' => array('type'=>'varchar(255)', 'label'=>'ActionToDo', 'visible'=>1, 'enabled'=>1, 'position'=>106),
'target_directory' => array('type'=>'varchar(255)', 'label'=>'MailboxTargetDirectory', 'visible'=>1, 'enabled'=>1, 'position'=>110, 'notnull'=>0, 'comment'=>"Where to store messages once processed"),
Expand Down Expand Up @@ -318,33 +318,9 @@ public function fetchAll(User $user, $activeOnly = 0, $sortfield = 's.rowid', $s

$socid = $user->societe_id ? $user->societe_id : '';

// If the internal user must only see his customers, force searching by him
if (! $user->rights->societe->client->voir && !$socid) {
$search_sale = $user->id;
}
$sql = "SELECT s.rowid";
//if ((!$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
// $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
//}
$sql.= " FROM ".MAIN_DB_PREFIX."emailcollector as s";

//if ((!$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
// $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
//}
//$sql.= ", ".MAIN_DB_PREFIX."c_stcomm as st";
//$sql.= " WHERE s.fk_stcomm = st.id";

// Example of use $mode
//if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
//if ($mode == 2) $sql.= " AND s.client IN (2, 3)";

$sql.= " FROM ".MAIN_DB_PREFIX."emailcollector_emailcollector as s";
$sql.= ' WHERE s.entity IN ('.getEntity('emailcollector').')';
//if ((!$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
// $sql.= " AND s.fk_soc = sc.fk_soc";
//}
//if ($socid) {
// $sql.= " AND s.fk_soc = ".$socid;
//}
if ($activeOnly) {
$sql.= " AND s.status = 1";
}
Expand All @@ -359,10 +335,10 @@ public function fetchAll(User $user, $activeOnly = 0, $sortfield = 's.rowid', $s
}

$result = $this->db->query($sql);

if ($result) {
$num = $this->db->num_rows($result);
while ($i < $num) {
while ($i < $num)
{
$obj = $this->db->fetch_object($result);
$emailcollector_static = new EmailCollector($this->db);
if ($emailcollector_static->fetch($obj->rowid)) {
Expand All @@ -371,11 +347,12 @@ public function fetchAll(User $user, $activeOnly = 0, $sortfield = 's.rowid', $s
$i++;
}
} else {
dol_syslog(__METHOD__.':: Error when retrieve emailcollector list', LOG_ERR);
$this->errors[] = 'EmailCollector::fetchAll Error when retrieve emailcollector list';
dol_syslog('EmailCollector::fetchAll Error when retrieve emailcollector list', LOG_ERR);
$ret = -1;
}
if (! count($obj_ret)) {
dol_syslog(__METHOD__.':: No emailcollector found', LOG_DEBUG);
dol_syslog('EmailCollector::fetchAll No emailcollector found', LOG_DEBUG);
}

return $obj_ret;
Expand Down Expand Up @@ -676,14 +653,40 @@ public function fetchActions()
*
* @return int 0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
*/
//public function doScheduledJob($param1, $param2, ...)
public function doCollect()
{
global $user;

$nberror = 0;

$arrayofcollectors = $this->fetchAll($user, 1);

// Loop on each collector
foreach($arrayofcollectors as $emailcollector)
{
$result = $emailcollector->doCollectOneCollector();
dol_syslog("doCollect result = ".$result." for emailcollector->id = ".$emailcollector->id);

$this->error.='EmailCollector ID '.$emailcollector->id.':'.$emailcollector->error.'<br>';
if (! empty($emailcollector->errors)) $this->error.=join('<br>', $emailcollector->errors);
$this->output.='EmailCollector ID '.$emailcollector->id.': '.$emailcollector->output.'<br>';
}

return $nberror;
}

/**
* Execute collect for current collector loaded previously with fetch.
*
* @return int <0 if KO, >0 if OK
*/
public function doCollectOneCollector()
{
global $conf, $langs, $user;

//$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log';

dol_syslog("EmailCollector::doCollect start", LOG_DEBUG);
dol_syslog("EmailCollector::doCollectOneCollector start", LOG_DEBUG);

$error = 0;
$this->output = '';
Expand Down Expand Up @@ -803,11 +806,11 @@ public function doCollect()
// Move email
if (! $errorforactions && $targetdir)
{
dol_syslog("EmailCollector::doCollect 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);
if ($res == false) {
$error++;
$this->errors = imap_last_error();
$this->error = imap_last_error();
dol_syslog(imap_last_error());
}
}
Expand All @@ -830,11 +833,12 @@ public function doCollect()

$this->datelastresult = $now;
$this->lastresult = $this->output;
$this->codelastresult = ($error ? 'KO' : 'OK');
$this->update($user);

dol_syslog("EmailCollector::doCollect end", LOG_DEBUG);
dol_syslog("EmailCollector::doCollectOneCollector end", LOG_DEBUG);

return $error;
return $error?-1:1;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions htdocs/emailcollector/class/emailcollectorfilter.class.php
Expand Up @@ -159,10 +159,10 @@ public function create(User $user, $notrigger = false)
$this->errors[]=$langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type"));
return -1;
}
if (empty($this->rulevalue))
if (! in_array($this->type, array('seen','unseen')) && empty($this->rulevalue))
{
$langs->load("errors");
$this->errors[]=$langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("RuleValue"));
$this->errors[]=$langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("StringToFilter"));
return -1;
}

Expand Down
2 changes: 1 addition & 1 deletion htdocs/langs/en_US/admin.lang
Expand Up @@ -1821,7 +1821,7 @@ NewEmailCollector=New Email Collector
EMailHost=Host of email IMAP server
MailboxSourceDirectory=Mailbox source directory
MailboxTargetDirectory=Mailbox target directory
EmailcollectorOperations=Operations done by collector
EmailcollectorOperations=Operations to do by collector
CollectNow=Collect now
DateLastResult=Date last collect
LastResult=Last result
Expand Down
Expand Up @@ -197,10 +197,10 @@ public function __construct($db)
// Cronjobs (List of cron jobs entries to add when module is enabled)
// unit_frequency must be 60 for minute, 3600 for hour, 86400 for day, 604800 for week
$this->cronjobs = array(
0=>array('label'=>'MyJob label', 'jobtype'=>'method', 'class'=>'/mymodule/class/myobject.class.php', 'objectname'=>'MyObject', 'method'=>'doScheduledJob', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>2, 'unitfrequency'=>3600, 'status'=>0, 'test'=>'$conf->mymodule->enabled')
0=>array('label'=>'MyJob label', 'jobtype'=>'method', 'class'=>'/mymodule/class/myobject.class.php', 'objectname'=>'MyObject', 'method'=>'doScheduledJob', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>2, 'unitfrequency'=>3600, 'status'=>0, 'test'=>'$conf->mymodule->enabled', 'priority'=>50)
);
// Example: $this->cronjobs=array(0=>array('label'=>'My label', 'jobtype'=>'method', 'class'=>'/dir/class/file.class.php', 'objectname'=>'MyClass', 'method'=>'myMethod', 'parameters'=>'param1, param2', 'comment'=>'Comment', 'frequency'=>2, 'unitfrequency'=>3600, 'status'=>0, 'test'=>'$conf->mymodule->enabled'),
// 1=>array('label'=>'My label', 'jobtype'=>'command', 'command'=>'', 'parameters'=>'param1, param2', 'comment'=>'Comment', 'frequency'=>1, 'unitfrequency'=>3600*24, 'status'=>0, 'test'=>'$conf->mymodule->enabled')
// Example: $this->cronjobs=array(0=>array('label'=>'My label', 'jobtype'=>'method', 'class'=>'/dir/class/file.class.php', 'objectname'=>'MyClass', 'method'=>'myMethod', 'parameters'=>'param1, param2', 'comment'=>'Comment', 'frequency'=>2, 'unitfrequency'=>3600, 'status'=>0, 'test'=>'$conf->mymodule->enabled', 'priority'=>50),
// 1=>array('label'=>'My label', 'jobtype'=>'command', 'command'=>'', 'parameters'=>'param1, param2', 'comment'=>'Comment', 'frequency'=>1, 'unitfrequency'=>3600*24, 'status'=>0, 'test'=>'$conf->mymodule->enabled', 'priority'=>50)
// );


Expand Down

0 comments on commit e86e3ec

Please sign in to comment.