示例#1
0
 private function _link($params, \GO\Base\Mail\Message $message, $model = false, $tags = array())
 {
     $autoLinkContacts = false;
     if (!$model) {
         if (!empty($params['link'])) {
             $linkProps = explode(':', $params['link']);
             $model = GO::getModel($linkProps[0])->findByPk($linkProps[1]);
         }
         $autoLinkContacts = GO::modules()->addressbook && GO::modules()->savemailas && !empty(GO::config()->email_autolink_contacts);
     } else {
         //don't link the same model twice on sent. It parses the new autolink tag
         //and handles the link to field.
         $linkProps = explode(':', $params['link']);
         if ($linkProps[0] == $model->className() && $linkProps[1] == $model->id) {
             return false;
         }
     }
     if ($model || $autoLinkContacts || count($tags)) {
         $path = 'email/' . date('mY') . '/sent_' . time() . '.eml';
         $file = new \GO\Base\Fs\File(GO::config()->file_storage_path . $path);
         $file->parent()->create();
         $fbs = new \Swift_ByteStream_FileByteStream($file->path(), true);
         $message->toByteStream($fbs);
         if (!$file->exists()) {
             throw new \Exception("Failed to save email to file!");
         }
         $attributes = array();
         $alias = \GO\Email\Model\Alias::model()->findByPk($params['alias_id']);
         $attributes['from'] = (string) \GO\Base\Mail\EmailRecipients::createSingle($alias->email, $alias->name);
         if (isset($params['to'])) {
             $attributes['to'] = $params['to'];
         }
         if (isset($params['cc'])) {
             $attributes['cc'] = $params['cc'];
         }
         if (isset($params['bcc'])) {
             $attributes['bcc'] = $params['bcc'];
         }
         $attributes['subject'] = !empty($params['subject']) ? $params['subject'] : GO::t('no_subject', 'email');
         //
         $attributes['path'] = $path;
         $attributes['time'] = $message->getDate();
         $attributes['uid'] = $alias->email . '-' . $message->getDate();
         $linkedModels = array();
         if ($model) {
             $attributes['acl_id'] = $model->findAclId();
             $linkedEmail = \GO\Savemailas\Model\LinkedEmail::model()->findSingleByAttributes(array('uid' => $attributes['uid'], 'acl_id' => $attributes['acl_id']));
             if (!$linkedEmail) {
                 $linkedEmail = new \GO\Savemailas\Model\LinkedEmail();
                 $linkedEmail->setAttributes($attributes);
                 try {
                     $linkedEmail->save();
                 } catch (\GO\Base\Exception\AccessDenied $e) {
                     throw new \Exception(GO::t('linkMustHavePermissionToWrite', 'email'));
                 }
             }
             $linkedEmail->link($model);
             $linkedModels[] = $model;
             GO::debug('1');
         }
         //process tags in the message body
         while ($tag = array_shift($tags)) {
             $linkModel = GO::getModel($tag['model'])->findByPk($tag['model_id'], false, true);
             if ($linkModel && !$linkModel->equals($linkedModels) && $linkModel->checkPermissionLevel(\GO\Base\Model\Acl::WRITE_PERMISSION)) {
                 $attributes['acl_id'] = $linkModel->findAclId();
                 $linkedEmail = \GO\Savemailas\Model\LinkedEmail::model()->findSingleByAttributes(array('uid' => $attributes['uid'], 'acl_id' => $attributes['acl_id']));
                 if (!$linkedEmail) {
                     $linkedEmail = new \GO\Savemailas\Model\LinkedEmail();
                     $linkedEmail->setAttributes($attributes);
                     $linkedEmail->save();
                 }
                 $linkedEmail->link($linkModel);
                 $linkedModels[] = $linkModel;
             }
         }
         if ($autoLinkContacts) {
             $to = new \GO\Base\Mail\EmailRecipients($params['to'] . "," . $params['bcc']);
             $to = $to->getAddresses();
             //					var_dump($to);
             foreach ($to as $email => $name) {
                 //$contact = \GO\Addressbook\Model\Contact::model()->findByEmail($email, \GO\Base\Db\FindParams::newInstance()->permissionLevel(Acl::WRITE_PERMISSION)->single());
                 $stmt = \GO\Addressbook\Model\Contact::model()->findByEmail($email, \GO\Base\Db\FindParams::newInstance()->permissionLevel(Acl::WRITE_PERMISSION)->limit(1));
                 $contact = $stmt->fetch();
                 if ($contact && !$contact->equals($linkedModels)) {
                     $attributes['acl_id'] = $contact->findAclId();
                     $linkedEmail = \GO\Savemailas\Model\LinkedEmail::model()->findSingleByAttributes(array('uid' => $attributes['uid'], 'acl_id' => $attributes['acl_id']));
                     if (!$linkedEmail) {
                         $linkedEmail = new \GO\Savemailas\Model\LinkedEmail();
                         $linkedEmail->setAttributes($attributes);
                         $linkedEmail->save();
                     }
                     $linkedEmail->link($contact);
                     // Also link the company to the email if the contact has a company attached to it.
                     if (!empty(GO::config()->email_autolink_companies) && !empty($contact->company_id)) {
                         $company = $contact->company;
                         if ($company && !$company->equals($linkedModels)) {
                             $linkedEmail->link($company);
                         }
                     }
                 }
             }
         }
     }
 }