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; }
public function questionSaveAfter(Varien_Event_Observer $observer) { if (!Mage::helper('aw_hdu3')->isModuleOutputEnabled() || !Mage::helper('aw_hdu3/config')->isEnabled() || !Mage::helper('aw_hdu3/config')->isPQEnabled()) { return $this; } $question = $observer->getEvent()->getQuestion(); if (!$question->isObjectNew()) { return $this; } $departmentCollection = Mage::getModel('aw_hdu3/department')->getCollection(); $departmentCollection->sortByOrder()->addActiveFilter()->addFilterByStoreId(Mage::app()->getStore()->getId()); $department = $departmentCollection->getFirstItem(); if (null === $department->getId()) { return $this; } try { $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(Mage::helper('aw_hdu3')->stripTags($question->getAuthorName()))->setCustomerEmail($question->getAuthorEmail())->setSubject(Mage::helper('core/string')->truncate(Mage::helper('aw_hdu3')->__("PQ on %s: %s", Mage::helper('aw_hdu3')->stripTags($question->getProduct()->getName()), $question->getContent()), 80))->setStoreId(Mage::app()->getStore()->getId())->save(); $content = Mage::helper('aw_hdu3')->__('<a href="%s" target="_blank">Edit Product Question entry</a></br>%s: <a href="%s" target="_blank">frontend</a> | <a href="%s" target="_blank">backend</a>', Mage::getSingleton('adminhtml/url')->getUrl('aw_pq2_admin/adminhtml_question/edit', array('id' => $question->getId())), Mage::helper('aw_hdu3')->stripTags($question->getProduct()->getName()), $question->getProduct()->getProductUrl(), Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/catalog_product/edit', array('id' => $question->getProductId()))); $ticket->addHistory(AW_Helpdesk3_Model_Ticket_History_Event_Message::TYPE, array('content' => $question->getContent(), 'attachments' => array())); $ticket->addHistory(AW_Helpdesk3_Model_Ticket_History_Event_Note::TYPE, array('content' => $content, 'attachments' => array())); } catch (Exception $e) { AW_Lib_Helper_Log::log($e->getMessage(), AW_Lib_Helper_Log::SEVERITY_ERROR); } 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; }
protected function _convertMessageToMail($message, $messageUid) { $messageNumber = $this->_getMessageNumberByUid($messageUid); $currentDate = new Zend_Date(); $mail = Mage::getModel('aw_hdu3/gateway_mail'); $mail->setUid($this->getMailUidByMessageUid($messageUid))->setGatewayId($this->getId())->setFrom($this->_getMessageFrom($message))->setTo($this->getEmail())->setStatus(AW_Helpdesk3_Model_Gateway_Mail::STATUS_UNPROCESSED)->setBody($this->_getMessageBody($message))->setHeaders($this->_getMessageHeadersByNumber($messageNumber))->setSubject($this->_getMessageSubject($message))->setContentType(strtok($this->_getMessageContentType($message), ';'))->setCreatedAt($currentDate->toString(Varien_Date::DATETIME_INTERNAL_FORMAT)); try { $mail->save(); AW_Lib_Helper_Log::log(Mage::helper('aw_hdu3')->__('Message saved with UID[%s]', $mail->getUid())); } catch (Exception $e) { AW_Lib_Helper_Log::log($e->getMessage(), AW_Lib_Helper_Log::SEVERITY_ERROR); } if ($this->getIsAllowAttachment()) { $attachments = $this->_getMessageAttachment($message); if (is_array($attachments)) { foreach ($attachments as $attach) { if (is_array($attach)) { AW_Lib_Helper_Log::log(Mage::helper('aw_hdu3')->__('Got new attachment (%s)', $attach['filename'])); try { $mail->addAttachmentFromArray($attach); } catch (Exception $e) { AW_Lib_Helper_Log::log($e->getMessage(), AW_Lib_Helper_Log::SEVERITY_ERROR); } } } } } //remove mail from gateway if ($this->getDeleteEmails()) { $this->_removeMessageFromServerByNumber($messageNumber); } return $this; }