Beispiel #1
0
 /**
  * 添加接收人
  */
 public function foreignAddAction()
 {
     $tuduId = $this->_request->getPost('tid');
     $to = $this->_request->getPost('to');
     $cc = $this->_request->getPost('cc');
     // 参数:图度ID必须存在
     if (!$tuduId) {
         return $this->json(false, $this->lang['invalid_tuduid']);
     }
     $fromTudu = $this->manager->getTuduById($tuduId, $this->_user->uniqueId);
     // 图度必须存在
     if (null === $fromTudu) {
         return $this->json(false, $this->lang['tudu_not_exists']);
     }
     // 必须是发起人才具备此操作
     if ($fromTudu->sender != $this->_user->userName) {
         return $this->json(false, $this->lang['perm_deny_add_foreign']);
     }
     // 检查外发用户email格式,外发用户是否存在,不存在则创建
     $address = array('to' => $this->_formatRecipients($to), 'cc' => $this->_formatRecipients($cc));
     $tuduAddr = array('to' => array(), 'cc' => array());
     $sendTo = array();
     foreach ($fromTudu->to as $k => $item) {
         $tuduAddr['to'][] = implode(' ', array(!is_numeric($k) ? $k : '', $item[0]));
     }
     foreach ($fromTudu->cc as $k => $item) {
         $tuduAddr['cc'][] = implode(' ', array(!is_int($k) ? $k : '', $item[0]));
     }
     /* @var $addressBook Tudu_AddressBook */
     $addressBook = Tudu_AddressBook::getInstance();
     $receivers = $this->manager->getTuduUsers($tuduId, array('isforeign' => 1));
     foreach ($address as $k => $arr) {
         foreach ($arr as $email => $name) {
             if (!$email && !$name) {
                 continue;
             }
             $email = preg_replace('/^#+/', '', $email);
             // 本系统用户不参与外发
             if (false !== strpos($email, '@') && null !== $addressBook->searchUser($this->_user->orgId, $email)) {
                 return $this->json(false, $this->lang['deny_add_user_to_foreign']);
             }
             // 新添加的外发用户是否已存在
             foreach ($receivers as $receiver) {
                 if ($email && $email == $receiver['email']) {
                     $display = $email ? $email : $name;
                     return $this->json(false, sprintf($this->lang['foreign_user_already_exists'], $display));
                 }
             }
             // 不存在联系人时创建
             if (!$addressBook->searchContact($this->_user->uniqueId, $email, $name)) {
                 $this->manager->createContact($this->_user->uniqueId, $email, $name);
             }
             $sendTo[] = $email;
             $tuduAddr[$k][] = implode(' ', array($email, $name));
         }
     }
     /* @var $deliver Tudu_Tudu_Deliver */
     $deliver = Tudu_Tudu_Deliver::getInstance();
     /* @var $storage Tudu_Tudu_Storage */
     $storage = Tudu_Tudu_Storage::getInstance();
     $tudu = $storage->prepareTudu(array('to' => $to, 'cc' => $cc), $fromTudu);
     $recipients = $deliver->prepareRecipients($this->_user->uniqueId, $this->_user->userId, $tudu);
     // 添加用户
     if (!$deliver->sendTudu($tudu, $recipients)) {
         return $this->json(false, $this->lang['add_foreign_failure']);
     }
     // 更新图度用户
     $this->manager->updateTudu($tuduId, array('to' => implode("\n", $tuduAddr['to']), 'cc' => implode("\n", $tuduAddr['cc'])));
     $config = $this->bootstrap->getOption('httpsqs');
     // 发送邮件
     $httpsqs = new Oray_Httpsqs($config['host'], $config['port'], $config['chartset'], $config['name']);
     // 发送外部邮件(如果有),处理联系人
     $data = implode(' ', array('send', 'tudu', '', http_build_query(array('tsid' => $this->_user->tsId, 'tuduid' => $tuduId, 'uniqueid' => $this->_user->uniqueId, 'to' => implode(',', $sendTo)))));
     $httpsqs->put($data, 'send');
     return $this->json(true, $this->lang['add_foreign_success']);
 }