diff --git a/migration/19.04/migrateSendmail.php b/migration/19.04/migrateSendmail.php index b144ed62764807dbe0873caaca8827fa30180a33..613fb8c09a70ecd93274b6e601e543e1e9f1feb0 100644 --- a/migration/19.04/migrateSendmail.php +++ b/migration/19.04/migrateSendmail.php @@ -43,31 +43,110 @@ foreach ($customs as $custom) { $migrated++; } -// $sendmails = \SrcCore\models\DatabaseModel::select([ -// 'select' => ['*'], -// 'table' => ['sendmail'], -// 'order_by' => ['creation_date'] -// ]); -// -// foreach ($sendmails as $sendmail) { -// -// $user = \User\models\UserModel::getByLogin(['login' => $sendmail['user_id'], 'select' => ['id']]); -// -// \Email\models\EmailModel::create([ -// 'userId' => $user['id'], -// 'sender' => json_encode($data['sender']), -// 'recipients' => json_encode($data['recipients']), -// 'cc' => empty($data['cc']) ? '[]' : json_encode($data['cc']), -// 'cci' => empty($data['cci']) ? '[]' : json_encode($data['cci']), -// 'object' => $data['object'], -// 'body' => $data['body'], -// 'document' => empty($data['document']) ? null : json_encode($data['document']), -// 'isHtml' => $data['isHtml'] ? 'true' : 'false', -// 'status' => $data['status'] == 'DRAFT' ? 'DRAFT' : 'WAITING', -// 'messageExchangeId' => $data['messageExchangeId'] -// ]); -// } + $sendmails = \SrcCore\models\DatabaseModel::select([ + 'select' => ['*'], + 'table' => ['sendmail'], + 'order_by' => ['creation_date'] + ]); + foreach ($sendmails as $sendmail) { + + $user = \User\models\UserModel::getByLogin(['login' => $sendmail['user_id'], 'select' => ['id']]); + $sender = explode(',', $sendmail['sender_email']); + if (empty($sender[1])) { + $sender = ['email' => $sender[0]]; + } else { + $sender = ['email' => $sender[1], 'entityId' => $sender[0]]; + } + $recipients = explode(',', $sendmail['to_list']); + $cc = explode(',', $sendmail['cc_list']); + $cc = empty($cc[0]) ? [] : $cc; + $cci = explode(',', $sendmail['cci_list']); + $cci = empty($cci[0]) ? [] : $cci; + + $document = [ + 'id' => $sendmail['res_id'], + 'isLinked' => $sendmail['is_res_master_attached'] == 'Y', + 'original' => false, + ]; + + $attachments = []; + + $rawAttachments = explode(',', $sendmail['res_attachment_id_list']); + if (!empty($rawAttachments[0])) { + foreach ($rawAttachments as $rawAttachment) { + $id = $rawAttachment; + $original = true; + if (strpos($rawAttachment, '#') !== false) { + $id = substr($rawAttachment, 0, strpos($rawAttachment, '#')); + $original = false; + } + $attachments[] = [ + 'id' => (int)$id, + 'isVersion' => false, + 'original' => $original, + ]; + } + } + + $rawVersionAttachments = explode(',', $sendmail['res_version_att_id_list']); + if (!empty($rawVersionAttachments[0])) { + foreach ($rawVersionAttachments as $rawAttachment) { + $id = $rawAttachment; + $original = true; + if (strpos($rawAttachment, '#') !== false) { + $id = substr($rawAttachment, 0, strpos($rawAttachment, '#')); + $original = false; + } + $attachments[] = [ + 'id' => (int)$id, + 'isVersion' => true, + 'original' => $original, + ]; + } + } + if (!empty($attachments)) { + $document['attachments'] = $attachments; + } + $notes = explode(',', $sendmail['note_id_list']); + if (!empty($notes[0])) { + $document['notes'] = []; + foreach ($notes as $note) { + $document['notes'][] = (int)$note; + } + } + + if ($sendmail['email_status'] == 'S') { + $status = 'SENT'; + } elseif ($sendmail['email_status'] == 'D') { + $status = 'DRAFT'; + } elseif ($sendmail['email_status'] == 'W') { + $status = 'WAITING'; + } else { + $status = 'ERROR'; + } + + \SrcCore\models\DatabaseModel::insert([ + 'table' => 'emails', + 'columnsValues' => [ + 'user_id' => $user['id'], + 'sender' => json_encode($sender), + 'recipients' => json_encode($recipients), + 'cc' => empty($cc) ? '[]' : json_encode($cc), + 'cci' => empty($cci) ? '[]' : json_encode($cci), + 'object' => empty($sendmail['email_object']) ? null : $sendmail['email_object'], + 'body' => empty($sendmail['email_body']) ? null : $sendmail['email_body'], + 'document' => empty($document) ? null : json_encode($document), + 'is_html' => $sendmail['is_html'] == 'Y' ? 'true' : 'false', + 'status' => $status, + 'message_exchange_id' => empty($sendmail['messageExchangeId']) ? null : $sendmail['messageExchangeId'], + 'creation_date' => $sendmail['creation_date'], + 'send_date' => empty($sendmail['send_date']) ? null : $sendmail['send_date'] + ] + ]); + } + + printf(count($sendmails) . " email(s) migré(s) du custom {$custom} vers la nouvelle table.\n"); } printf($migrated . " custom(s) avec une configuration sendmail trouvé(s) et migré(s).\n"); diff --git a/sql/develop.sql b/sql/develop.sql index 91dc00840e498c2aee6b608ad60b19bd906a1174..643862edec210b56283a2c654523433b8ba4be39 100755 --- a/sql/develop.sql +++ b/sql/develop.sql @@ -65,7 +65,7 @@ sender json DEFAULT '{}' NOT NULL, recipients json DEFAULT '[]' NOT NULL, cc json DEFAULT '[]' NOT NULL, cci json DEFAULT '[]' NOT NULL, -object character varying(256) NOT NULL, +object character varying(256), body text, document json, is_html boolean NOT NULL DEFAULT TRUE, diff --git a/sql/structure.sql b/sql/structure.sql index d6a7abdfd11acd13c457b4ef07df02e30327eb78..a185fadfc5290f7b284a56da32560c210037a3ae 100755 --- a/sql/structure.sql +++ b/sql/structure.sql @@ -2169,7 +2169,7 @@ sender json DEFAULT '{}' NOT NULL, recipients json DEFAULT '[]' NOT NULL, cc json DEFAULT '[]' NOT NULL, cci json DEFAULT '[]' NOT NULL, -object character varying(256) NOT NULL, +object character varying(256), body text, document json, is_html boolean NOT NULL DEFAULT TRUE, diff --git a/src/app/email/controllers/EmailController.php b/src/app/email/controllers/EmailController.php index 7d4707a61a2f43bc65fdf39a7594f84cc8db6673..8f3e1b5bb862661df83ae1b3991f72b2ad29a7b9 100644 --- a/src/app/email/controllers/EmailController.php +++ b/src/app/email/controllers/EmailController.php @@ -241,8 +241,6 @@ class EmailController return ['errors' => 'Data sender email is not set', 'code' => 400]; } elseif (!Validator::arrayType()->notEmpty()->validate($args['data']['recipients'])) { return ['errors' => 'Data recipients is not an array or empty', 'code' => 400]; - } elseif (!Validator::stringType()->notEmpty()->validate($args['data']['object'])) { - return ['errors' => 'Data object is not a string or empty', 'code' => 400]; } elseif (!Validator::boolType()->validate($args['data']['isHtml'])) { return ['errors' => 'Data isHtml is not a boolean or empty', 'code' => 400]; } elseif (!Validator::stringType()->notEmpty()->validate($args['data']['status'])) { diff --git a/src/app/email/models/EmailModel.php b/src/app/email/models/EmailModel.php index 6f91f7a1a32fcfa19ae0243c73577bd38e1297b8..c86d2d07a6a8cfe54baf547bcd0953590c0a0bda 100644 --- a/src/app/email/models/EmailModel.php +++ b/src/app/email/models/EmailModel.php @@ -41,7 +41,7 @@ class EmailModel public static function create(array $aArgs) { - ValidatorModel::notEmpty($aArgs, ['userId', 'sender', 'recipients', 'cc', 'cci', 'object', 'isHtml', 'status']); + ValidatorModel::notEmpty($aArgs, ['userId', 'sender', 'recipients', 'cc', 'cci', 'isHtml', 'status']); ValidatorModel::intVal($aArgs, ['userId']); ValidatorModel::stringType($aArgs, ['sender', 'recipients', 'cc', 'cci', 'object', 'body', 'messageExchangeId', 'document', 'isHtml', 'status']); @@ -56,7 +56,7 @@ class EmailModel 'recipients' => $aArgs['recipients'], 'cc' => $aArgs['cc'], 'cci' => $aArgs['cci'], - 'object' => $aArgs['object'], + 'object' => empty($aArgs['object']) ? null : $aArgs['object'], 'body' => empty($aArgs['body']) ? null : $aArgs['body'], 'document' => $aArgs['document'], 'is_html' => $aArgs['isHtml'],