/**
  * @see parent::doStore()
  */
 function doStore()
 {
     if (isset($_FILES['attachment'])) {
         $mail_id = CValue::post('mail_id');
         $mail = new CUserMail();
         $mail->load($mail_id);
         $files = array();
         foreach ($_FILES['attachment']['error'] as $key => $file_error) {
             if (isset($_FILES['attachment']['name'][$key])) {
                 $files[] = array('name' => $_FILES['attachment']['name'][$key], 'tmp_name' => $_FILES['attachment']['tmp_name'][$key], 'error' => $_FILES['attachment']['error'][$key], 'size' => $_FILES['attachment']['size'][$key]);
             }
         }
         foreach ($files as $_key => $_file) {
             if ($_file['error'] == UPLOAD_ERR_NO_FILE) {
                 continue;
             }
             if ($_file['error'] != 0) {
                 CAppUI::setMsg(CAppUI::tr("CFile-msg-upload-error-" . $_file["error"]), UI_MSG_ERROR);
                 continue;
             }
             $attachment = new CMailAttachments();
             $attachment->name = $_file['name'];
             $content_type = mime_content_type($_file['tmp_name']);
             $attachment->type = $attachment->getTypeInt($content_type);
             $attachment->bytes = $_file['size'];
             $attachment->mail_id = $mail_id;
             $content_type = explode('/', $content_type);
             $attachment->subtype = strtoupper($content_type[1]);
             $attachment->disposition = 'ATTACHMENT';
             $attachment->extension = substr(strrchr($attachment->name, '.'), 1);
             $attachment->part = $mail->countBackRefs('mail_attachments') + 1;
             $attachment->store();
             $file = new CFile();
             $file->setObject($attachment);
             $file->author_id = CAppUI::$user->_id;
             $file->file_name = $attachment->name;
             $file->file_date = CMbDT::dateTime();
             $file->fillFields();
             $file->updateFormFields();
             $file->doc_size = $attachment->bytes;
             $file->file_type = mime_content_type($_file['tmp_name']);
             $file->moveFile($_file, true);
             if ($msg = $file->store()) {
                 CAppUI::setMsg(CAppUI::tr('CMailAttachments-error-upload-file') . ':' . CAppUI::tr($msg), UI_MSG_ERROR);
                 CApp::rip();
             }
             $attachment->file_id = $file->_id;
             if ($msg = $attachment->store()) {
                 CAppUI::setMsg($msg, UI_MSG_ERROR);
                 CApp::rip();
             }
         }
         CAppUI::setMsg('CMailAttachments-msg-added', UI_MSG_OK);
     } else {
         parent::doStore();
     }
 }
 function createUserMail($user_id, $object = null, $apicrypt = false)
 {
     if (CModule::getActive('messagerie')) {
         $mail = new CUserMail();
         $mail->account_id = $this->_id;
         $mail->account_class = $this->_class;
         $mail->sent = 1;
         $mail->subject = $this->_mail->Subject;
         $mail->from = $this->_mail->From;
         $mail->to = $this->_to['address'];
         $mail->date_inbox = CMbDT::dateTime();
         if ($this->_mail->ContentType == 'text/html') {
             $mail->_text_html = $this->_mail->Body;
             if ($apicrypt) {
                 $mail->_is_apicrypt = 'html';
             }
             $mail->getHtmlText($user_id);
         } else {
             $mail->_text_plain = $this->_mail->Body;
             if ($apicrypt) {
                 $mail->_is_apicrypt = 'plain';
             }
             $mail->getPlainText($user_id);
         }
         $mail->store();
         if ($object) {
             $file = null;
             switch ($object->_class) {
                 case "CCompteRendu":
                     $file = $object->_ref_file;
                     break;
                 case "CFile":
                     $file = $object;
                     break;
             }
             if ($file && $file->_id) {
                 $attachment = new CMailAttachments();
                 $attachment->mail_id = $mail->_id;
                 list($type, $subtype) = explode('/', $file->file_type);
                 $attachment->type = $attachment->getTypeInt($type);
                 $attachment->part = 1;
                 $attachment->subtype = $subtype;
                 $attachment->bytes = $file->doc_size;
                 list($file_name, $extension) = explode('.', $file->file_name);
                 $attachment->name = $file_name;
                 $attachment->extension = $extension;
                 $attachment->file_id = $file->_id;
                 $attachment->store();
             }
         }
     }
 }