function handleCreateCase($email, $userId) { global $current_user, $mod_strings, $current_language; $mod_strings = return_module_language($current_language, "Emails"); $GLOBALS['log']->debug('In handleCreateCase'); $c = new aCase(); $this->getCaseIdFromCaseNumber($email->name, $c); if (!$this->handleCaseAssignment($email) && $this->isMailBoxTypeCreateCase()) { // create a case $GLOBALS['log']->debug('retrieveing email'); $email->retrieve($email->id); $c = new aCase(); $c->description = $email->description; $c->assigned_user_id = $userId; $c->name = $email->name; $c->status = 'New'; $c->priority = 'P1'; if (!empty($email->reply_to_email)) { $contactAddr = $email->reply_to_email; } else { $contactAddr = $email->from_addr; } $GLOBALS['log']->debug('finding related accounts with address ' . $contactAddr); if ($accountIds = $this->getRelatedId($contactAddr, 'accounts')) { if (sizeof($accountIds) == 1) { $c->account_id = $accountIds[0]; $acct = new Account(); $acct->retrieve($c->account_id); $c->account_name = $acct->name; } // if } // if $c->save(true); $caseId = $c->id; $c = new aCase(); $c->retrieve($caseId); if ($c->load_relationship('emails')) { $c->emails->add($email->id); } // if if ($contactIds = $this->getRelatedId($contactAddr, 'contacts')) { if (!empty($contactIds) && $c->load_relationship('contacts')) { $c->contacts->add($contactIds); } // if } // if $c->email_id = $email->id; $email->parent_type = "Cases"; $email->parent_id = $caseId; // assign the email to the case owner $email->assigned_user_id = $c->assigned_user_id; $email->name = str_replace('%1', $c->case_number, $c->getEmailSubjectMacro()) . " " . $email->name; $email->save(); $GLOBALS['log']->debug('InboundEmail created one case with number: ' . $c->case_number); $createCaseTemplateId = $this->get_stored_options('create_case_email_template', ""); if (!empty($this->stored_options)) { $storedOptions = unserialize(base64_decode($this->stored_options)); } if (!empty($createCaseTemplateId)) { $fromName = ""; $fromAddress = ""; if (!empty($this->stored_options)) { $fromAddress = $storedOptions['from_addr']; $fromName = from_html($storedOptions['from_name']); $replyToName = !empty($storedOptions['reply_to_name']) ? from_html($storedOptions['reply_to_name']) : $fromName; $replyToAddr = !empty($storedOptions['reply_to_addr']) ? $storedOptions['reply_to_addr'] : $fromAddress; } // if $defaults = $current_user->getPreferredEmail(); $fromAddress = !empty($fromAddress) ? $fromAddress : $defaults['email']; $fromName = !empty($fromName) ? $fromName : $defaults['name']; $to[0]['email'] = $contactAddr; // handle to name: address, prefer reply-to if (!empty($email->reply_to_name)) { $to[0]['display'] = $email->reply_to_name; } elseif (!empty($email->from_name)) { $to[0]['display'] = $email->from_name; } $et = new EmailTemplate(); $et->retrieve($createCaseTemplateId); if (empty($et->subject)) { $et->subject = ''; } if (empty($et->body)) { $et->body = ''; } if (empty($et->body_html)) { $et->body_html = ''; } $et->subject = "Re:" . " " . str_replace('%1', $c->case_number, $c->getEmailSubjectMacro() . " " . $c->name); $html = trim($email->description_html); $plain = trim($email->description); $email->email2init(); $email->from_addr = $email->from_addr_name; $email->to_addrs = $email->to_addrs_names; $email->cc_addrs = $email->cc_addrs_names; $email->bcc_addrs = $email->bcc_addrs_names; $email->from_name = $email->from_addr; $email = $email->et->handleReplyType($email, "reply"); $ret = $email->et->displayComposeEmail($email); $ret['description'] = empty($email->description_html) ? str_replace("\n", "\n<BR/>", $email->description) : $email->description_html; $reply = new Email(); $reply->type = 'out'; $reply->to_addrs = $to[0]['email']; $reply->to_addrs_arr = $to; $reply->cc_addrs_arr = array(); $reply->bcc_addrs_arr = array(); $reply->from_name = $fromName; $reply->from_addr = $fromAddress; $reply->reply_to_name = $replyToName; $reply->reply_to_addr = $replyToAddr; $reply->name = $et->subject; $reply->description = $et->body . "<div><hr /></div>" . $email->description; if (!$et->text_only) { $reply->description_html = $et->body_html . "<div><hr /></div>" . $email->description; } $GLOBALS['log']->debug('saving and sending auto-reply email'); //$reply->save(); // don't save the actual email. $reply->send(); } // if } else { if (!empty($email->reply_to_email)) { $contactAddr = $email->reply_to_email; } else { $contactAddr = $email->from_addr; } $this->handleAutoresponse($email, $contactAddr); } }
function syncticket($session, $data) { $GLOBALS['log']->error("syncticket"); if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', 'Cases', 'write', 'no_access', new SoapError())) { echo "No access"; return; } $admin = new Administration(); $admin->retrieveSettings('system'); $ticket = new SimpleXMLElement(str_replace('!AMP!', '&', $data)); $GLOBALS['log']->error("find or create case"); $case = new aCase(); $nice_id = (int) $ticket->{'nice-id'}; if ($case = $case->retrieve_by_string_fields(array('zendesk_ticket_id_c' => $nice_id))) { $GLOBALS['log']->error("found case " . $case->id); } else { $GLOBALS['log']->error("creating new case"); $case = new aCase(); } $case->zendesk_ticket_id_c = $nice_id; $case->name = $ticket->subject; if ($admin->settings['system_zendesk_status_map']) { $mapped_to = $admin->settings['system_zendesk_status_map']; $mapped_field = 'system_zendesk_status_map_' . $ticket->{'status-id'}; $case->{$mapped_to} = $admin->settings[$mapped_field]; } if ($admin->settings['system_zendesk_priority_map']) { $mapped_to = $admin->settings['system_zendesk_priority_map']; $mapped_field = 'system_zendesk_priority_map_' . $ticket->{'priority-id'}; $case->{$mapped_to} = $admin->settings[$mapped_field]; } if ($admin->settings['system_zendesk_type_map']) { $mapped_to = $admin->settings['system_zendesk_type_map']; $mapped_field = 'system_zendesk_type_map_' . $ticket->{'ticket-type-id'}; $case->{$mapped_to} = $admin->settings[$mapped_field]; } $requester = $ticket->requester; if ($requester->organization) { $account = new Account(); if ($account = $account->retrieve_by_string_fields(array('name' => $requester->organization->name))) { $GLOBALS['log']->error("found account " . $account->id); } else { $account = new Account(); $account->name = $requester->organization->name; $account->save(); $GLOBALS['log']->error("created account " . $account->id); } $case->account_id = $account->id; } $GLOBALS['log']->error("save case with zendesk_ticket_id_c " . $case->zendesk_ticket_id_c); $case->save(); $GLOBALS['log']->error("associate contacts"); if ($case->load_relationship('contacts')) { $ea = new EmailAddress(); $ids = $ea->getRelatedId($requester->email, 'contacts'); if (!empty($ids)) { $case->contacts->add($ids); } else { $contact = new Contact(); $contact->email1 = $requester->email; $names = split(' ', $requester->name); $contact->last_name = $names[count($names) - 1]; $contact->first_name = join(' ', array_slice($names, 0, -1)); $contact->save(); $case->contacts->add(array($contact->id)); } } $GLOBALS['log']->error("syncticket done"); echo "OK " . $case->id; }