erp; $conf = $app->Conf; /** @var Logger $logger */ $logger = $app->Container->get('Logger'); $cronjobname = 'tickets'; /* $mutex = $app->DB->Select( "SELECT MAX(`mutex`) FROM `prozessstarter` WHERE (`parameter` = '".$cronjobname."')" ); if($mutex){ $app->DB->Update( "UPDATE `prozessstarter` SET `mutexcounter`=`mutexcounter`+1 WHERE `mutex` = 1 AND (`parameter` = '".$cronjobname."')" ); return; } $app->DB->Update( "UPDATE `prozessstarter` SET `mutex`='1', `mutexcounter` = 0 WHERE (`parameter` = '".$cronjobname."')" ); */ // get all email Accounts that have the ticket system active /** @var EmailAccountGateway $accountGateway */ $accountGateway = $app->Container->get('EmailAccountGateway'); $accounts = $accountGateway->getAccountsWithTicketActive(); // only load services if there is at least one account to import (performance) $ticketModule = null; $factory = null; $configProvider = null; $formatHelper = null; $importHelperFactory = null; if(!empty($accounts)){ /** @var Ticket $ticketModule */ $ticketModule = $app->erp->LoadModul('ticket'); /** @var MailClientFactory $factory */ $factory = $app->Container->get('MailClientFactory'); /** @var MailClientConfigProvider $configProvider */ $configProvider = $app->Container->get('MailClientConfigProvider'); /** @var TicketFormatter $formatHelper */ $formatHelper = $app->Container->get('TicketFormatter'); /** @var TicketImportHelperFactory $importHelperFactory */ $importHelperFactory = $app->Container->get('TicketImportHelperFactory'); } $totalEmailsImportCount = 0; foreach ($accounts as $account) { $logger->debug( 'Start imap ticket import for {email}', ['email' => $account->getEmailAddress(), 'account' => $account] ); // create mail client try { $mailConfig = $configProvider->createImapConfigFromAccount($account); $mailClient = $factory->createImapClient($mailConfig); } catch (Exception $e) { $logger->error('Failed to create email client', ['error' => (string)$e, 'account' => $account]); continue; } // connect mail client try { $mailClient->connect(); } catch (Exception $e) { $logger->error('Error during imap connection', ['error' => (string)$e, 'account' => $account]); continue; } // connet to INBOX folder try { $mailClient->selectFolder('INBOX'); } catch (Exception $e) { $logger->error('Failed to select INBOX folder', ['error' => (string)$e, 'account' => $account]); continue; } $projectId = $account->getProjectId() > 0 ? $account->getProjectId() : 1; $delete_msg = 0; $daysold = $account->getBackupDeleteAfterDays(); // determine search criteria for new messages $datet = '2012-12-24'; if ($account->getImportStartDateAsString() !== '0000-00-00') { $datesince = date('d-M-Y', strtotime($account->getImportStartDateAsString())); $criteria = 'UNSEEN SINCE ' . $datesince; } else { $criteria = 'UNSEEN'; } // search new messages try { $searchResult = $mailClient->searchMessages($criteria); } catch (Exception $e) { $logger->error('Error during imap search', ['exception' => $e]); continue; } $logger->debug('unread emails to import: {message_count}', ['message_count' => count($searchResult)]); // set mutex if there is more than 5 emails to import if (count($searchResult) > 5) { $app->DB->Update( "UPDATE `prozessstarter` SET `mutex`=1, `mutexcounter` = 0, `letzteausfuerhung` = NOW() WHERE (`parameter` = '".$cronjobname."')" ); } $importer = $importHelperFactory->create($mailClient, $account, $projectId); $insertedMailsCount = $importer->importMessages($searchResult); // TicketImportHelper->importMessages() $totalEmailsImportCount += $insertedMailsCount; // set mutex if the total amount of imported emails is more than 10 if ($totalEmailsImportCount > 10) { $app->DB->Update( "UPDATE `prozessstarter` SET `mutex`=1, `mutexcounter` = 0, `letzteausfuerhung` = NOW() WHERE (`parameter` = '".$cronjobname."')" ); } $mailClient->expunge(); $mailClient->disconnect(); if ( method_exists($app->erp, 'canRunCronjob') && !$app->erp->canRunCronjob(['supportmails', 'tickets']) ) { $logger->error('Tickets error'); return; } $app->DB->Update( "UPDATE `prozessstarter` SET `mutex`=1, `mutexcounter` = 0, `letzteausfuerhung` = NOW() WHERE (`parameter` = '".$cronjobname."')" ); } $app->DB->Update( "UPDATE `prozessstarter` SET `mutex`=0,`mutexcounter`=0 WHERE (`parameter` = '".$cronjobname."')" );