public function createMailFromNewMessage() { if ($this->_isLocked() || !Mage::helper('aw_hdu3/config')->isEnabled()) { return $this; } $departmentCollection = Mage::getModel('aw_hdu3/department')->getCollection(); $departmentCollection->addActiveFilter()->sortByOrder(); foreach ($departmentCollection as $department) { /** @var AW_Helpdesk3_Model_Gateway $gateway */ $gateway = $department->getGateway(); if (!$gateway->getIsActive()) { continue; } AW_Lib_Helper_Log::start(Mage::helper('aw_hdu3')->__('Start process gateway "%s".', $gateway->getTitle())); try { $gateway->load($gateway->getId()); $gateway->process(); } catch (Exception $e) { AW_Lib_Helper_Log::log($e->getMessage(), AW_Lib_Helper_Log::SEVERITY_ERROR); } AW_Lib_Helper_Log::stop(Mage::helper('aw_hdu3')->__('Stop process gateway "%s".', $gateway->getTitle())); } //remove lock Mage::app()->removeCache(self::LOCK_CACHE_ID); return $this; }
/** * Create tickets from mail * * @return $this */ public function createTicketFromMail() { if ($this->_isLocked(self::LOCK_CACHE_CREATE_TICKET_ID) || !Mage::helper('aw_hdu3/config')->isEnabled()) { return $this; } $mailCollection = Mage::getModel('aw_hdu3/gateway_mail')->getCollection(); //get only unprocessed mail $mailCollection->addPendingFilter(); $mailCollection->setPageSize(self::MAIL_LIMIT); AW_Lib_Helper_Log::start(Mage::helper('aw_hdu3')->__('Create tickets from saved email messages.')); foreach ($mailCollection as $mail) { //check mail on reject pattern if ($mail->isCanConvertToTicket()) { $gateway = Mage::getModel('aw_hdu3/gateway')->load($mail->getGatewayId()); $department = Mage::getModel('aw_hdu3/department')->load($gateway->getDepartmentId()); if ($gateway->getId() && $department->getId()) { //get exist or new ticket by mail subject $ticket = $this->_getTicketByMailSubject($mail->getSubject()); $agentId = null; if ($ticket->getId()) { //ticket exist if ($this->_parseEmail($mail->getFrom()) == $ticket->getDepartmentAgent()->getEmail()) { $agentId = $ticket->getDepartmentAgentId(); $ticket->setStatus(AW_Helpdesk3_Model_Source_Ticket_Status::WAITING_VALUE); } else { if ($ticket->getStatus() != AW_Helpdesk3_Model_Source_Ticket_Status::NEW_VALUE) { $ticket->setStatus(AW_Helpdesk3_Model_Source_Ticket_Status::OPEN_VALUE); AW_Lib_Helper_Log::log(Mage::helper('aw_hdu3')->__('Ticket UID[%s] changed status to "Open"', $ticket->getUid())); } } $ticket->setIsReply(true); } else { //ticket new //check option: Create new tickets from incoming emails $isAllowIncomingFromGateway = Mage::helper('aw_hdu3/config')->isAllowCreateNewTicketsFromIncomingEmails(array_shift($department->getStoreIds())); if (!$isAllowIncomingFromGateway) { $mail->setStatus(AW_Helpdesk3_Model_Gateway_Mail::STATUS_PROCESSED)->save(); AW_Lib_Helper_Log::log(Mage::helper('aw_hdu3')->__('Message UID[%s] skipped. Create new tickets from incoming emails denied for this gateway', $mail->getUid())); continue; } $email = $this->_parseEmail($mail->getFrom()); $customer = Mage::getModel('customer/customer')->setStore(Mage::app()->getStore(array_shift($department->getStoreIds())))->loadByEmail($email); $name = $this->_parseCustomerName($mail->getFrom()); if ($customer && $customer->getId()) { $name = $customer->getName(); } $ticket->setDepartmentAgentId($department->getPrimaryAgentId())->setDepartmentId($department->getId())->setStatus(AW_Helpdesk3_Model_Source_Ticket_Status::NEW_VALUE)->setPriority(AW_Helpdesk3_Model_Source_Ticket_Priority::TODO_VALUE)->setCustomerName($name)->setCustomerEmail($email)->setSubject($mail->getSubject())->setStoreId(array_shift($department->getStoreIds())); } try { //save ticket $ticket->save(); if ($ticket->isObjectNew()) { AW_Lib_Helper_Log::log(Mage::helper('aw_hdu3')->__('New ticket UID[%s]', $ticket->getUid())); } } catch (Exception $e) { AW_Lib_Helper_Log::log($e->getMessage(), AW_Lib_Helper_Log::SEVERITY_ERROR); } try { //add ticket history - Message $eventData = array('content' => $mail->getBody(), 'attachments' => $mail->getAttachmentCollection()->getItems()); if ($agentId) { //reply of admin $eventData['agent_id'] = $agentId; $eventData['content'] = nl2br(Mage::helper('aw_hdu3')->escapeHtml($mail->getBody())); } $ticket->addHistory(AW_Helpdesk3_Model_Ticket_History_Event_Message::TYPE, $eventData); } catch (Exception $e) { AW_Lib_Helper_Log::log($e->getMessage(), AW_Lib_Helper_Log::SEVERITY_ERROR); } } //set status processed $mail->setStatus(AW_Helpdesk3_Model_Gateway_Mail::STATUS_PROCESSED)->save(); } else { AW_Lib_Helper_Log::log(Mage::helper('aw_hdu3')->__('Message UID[%s] rejected.', $mail->getUid()), AW_Lib_Helper_Log::SEVERITY_WARNING); } } AW_Lib_Helper_Log::stop(Mage::helper('aw_hdu3')->__('Complete.')); //remove lock Mage::app()->removeCache(self::LOCK_CACHE_CREATE_TICKET_ID); return $this; }
public function createFromOrder() { if (Mage::app()->getRequest()->getParam('create_ticket')) { $order = Mage::getModel('sales/order')->load(Mage::app()->getRequest()->getParam('order_id')); $history = Mage::app()->getRequest()->getPost(); $history = @$history['history']; $body = Mage::app()->getResponse()->getBody(); if (!array_key_exists('comment', $history) || empty($history['comment'])) { $message = Mage::helper('aw_hdu3')->__('Ticket has not been saved. Comment is empty'); $message = Zend_Json::encode($message); $body .= "<script type='text/javascript'>orderTicket.showMessage({$message},'error-msg')</script>"; Mage::app()->getResponse()->setBody($body); return $this; } $department = null; if (Mage::helper('aw_hdu3/config')->isPrimaryDepartmentActive(Mage::app()->getStore()->getId()) && ($defaultDepartmentId = Mage::helper('aw_hdu3/config')->getDefaultDepartmentId(Mage::app()->getStore()->getId()))) { $department = Mage::getModel('aw_hdu3/department')->load($defaultDepartmentId); } if (!$department || !$department->getId()) { $departmentCollection = Mage::getModel('aw_hdu3/department')->getCollection(); $departmentCollection->sortByOrder()->addActiveFilter()->addFilterByStoreId(Mage::app()->getStore()->getId()); $department = $departmentCollection->getFirstItem(); } if (!$department || !$department->getId()) { $message = Mage::helper('aw_hdu3')->__('Ticket has not been saved. There are no Primary Department or any Help Desk Departments configured'); $message = Zend_Json::encode($message); $body .= "<script type='text/javascript'>orderTicket.showMessage({$message},'error-msg')</script>"; Mage::app()->getResponse()->setBody($body); return $this; } $ticket = Mage::getModel('aw_hdu3/ticket'); $ticket->setDepartmentAgentId($department->getPrimaryAgentId())->setDepartmentId($department->getId())->setStatus(AW_Helpdesk3_Model_Source_Ticket_Status::NEW_VALUE)->setPriority(AW_Helpdesk3_Model_Source_Ticket_Priority::TODO_VALUE)->setCustomerName($order->getCustomerName())->setCustomerEmail($order->getCustomerEmail())->setOrderIncrementId($order->getIncrementId())->setSubject(Mage::helper('aw_hdu3')->__('Order #%s', $order->getIncrementId()))->setStoreId($order->getStoreId())->save(); AW_Lib_Helper_Log::start(Mage::helper('aw_hdu3')->__('Got new ticket from order page.')); AW_Lib_Helper_Log::stop(Mage::helper('aw_hdu3')->__('Ticket UID[%s]', $ticket->getUid())); $ticket->addHistory(AW_Helpdesk3_Model_Ticket_History_Event_Message::TYPE, array('content' => trim(@$history['comment']))); $ticketUrl = Mage::helper('adminhtml')->getUrl('helpdesk_admin/adminhtml_ticket/edit', array('id' => $ticket->getId())); $ticketUrlHtml = Mage::helper('aw_hdu3')->__('Ticket %s has been successfully saved', "<a href='{$ticketUrl}'>" . $ticket->getUid() . "</a>"); $ticketUrlHtml = Zend_Json::encode($ticketUrlHtml); $body .= "<script type='text/javascript'>orderTicket.showMessage({$ticketUrlHtml},'success-msg')</script>"; Mage::app()->getResponse()->setBody($body); } }
/** * @return $this */ protected function _prepareAndSaveNewMails() { try { $mailboxUIDs = $this->_getNewMessageUids(); } catch (Exception $e) { $mailboxUIDs = array(); } AW_Lib_Helper_Log::log(Mage::helper('aw_hdu3')->__('Got (%s) new message(s).', count($mailboxUIDs))); if (count($mailboxUIDs) > 0) { AW_Lib_Helper_Log::start(Mage::helper('aw_hdu3')->__('Save messages.')); foreach ($mailboxUIDs as $messageUid) { $message = $this->_getMessageByUid($messageUid); $this->_convertMessageToMail($message, $messageUid); } AW_Lib_Helper_Log::stop(Mage::helper('aw_hdu3')->__('Messages have been successfully saved.')); } return $this; }