protected function _toHtml() { $storeId = Mage::app()->getStore()->getId(); if (AW_Productquestions_Helper_Data::isModuleOutputDisabled($storeId)) { return ''; } if (!AW_Productquestions_Helper_Data::checkIfGuestsAllowed($storeId)) { return Mage::helper('productquestions')->getPleaseRegisterMessage($storeId); } if (!$this->getQuestionAuthorName() || !$this->getQuestionAuthorEmail()) { $customer = Mage::getSingleton('customer/session')->getCustomer(); if ($customer && $customer->getId()) { if (!$this->getQuestionAuthorName()) { // add logged in customer name as nickname $this->setQuestionAuthorName($customer->getFirstname()); } if (!$this->getQuestionAuthorEmail()) { // add logged in customer email $this->setQuestionAuthorEmail($customer->getEmail()); } } } $product = Mage::helper('productquestions')->getCurrentProduct(); if (!$product instanceof Mage_Catalog_Model_Product) { return ''; } $this->setProduct($product); $data = Mage::getSingleton('core/session')->getProductquestionsData(true); if (is_array($data)) { $this->setData(array_merge($this->getData(), $data)); } return parent::_toHtml(); }
public function postAction() { $productId = $this->getRequest()->getParam('id'); $url = Mage::getUrl('productquestions/index', array('id' => $productId)); if (!AW_Productquestions_Helper_Data::checkIfGuestsAllowed()) { return $this->_redirectReferer(); } try { $this->_initProduct(); } catch (Exception $ex) { Mage::getSingleton('core/session')->addError($ex->getMessage()); } $data = $this->getRequest()->getPost(); if ($this->_product && !empty($data)) { $session = Mage::getSingleton('core/session'); $question = Mage::getModel('productquestions/productquestions')->setData($data); $validate = $question->validate(); if ($validate === true) { $store = Mage::app()->getStore(); $storeId = $store->getId(); try { if (Mage::getStoreConfig('productquestions/interface/customer_status', $storeId) && isset($data['question_status'])) { $question->setQuestionStatus(intval(@$data['question_status'])); } elseif (Mage::getStoreConfig('productquestions/interface/customer_status')) { $question->setQuestionStatus(AW_Productquestions_Model_Status::STATUS_PRIVATE); } $question->setQuestionProductId($this->_product->getId())->setQuestionAuthorName($data['question_author_name'])->setQuestionAuthorEmail($data['question_author_email'])->setQuestionProductName($this->_product->getName())->setQuestionText($data['question_text'])->setQuestionDate(now())->setQuestionStoreId($storeId)->setQuestionStoreIds($storeId)->save(); $session->addSuccess($this->__('Your question has been accepted for moderation')); $session->setProductquestionsData(false); if (Mage::getStoreConfig(AW_Productquestions_Model_Source_Config_Path::EMAIL_RECIPIENT)) { /* Now send email to admin about new question */ $mailTemplate = Mage::getModel('core/email_template'); try { $sender = Mage::helper('productquestions')->getSender(); if (method_exists($mailTemplate->getMail(), 'setReplyTo')) { $mailTemplate->getMail()->setReplyTo($sender['mail'], $sender['name']); } else { $mailTemplate->getMail()->addHeader('Reply-To', $sender['mail']); } } catch (Exception $ex) { } $mailTemplate->setDesignConfig(array('area' => 'adminhtml', 'store' => Mage_Core_Model_App::ADMIN_STORE_ID))->sendTransactional(Mage::getStoreConfig(AW_Productquestions_Model_Source_Config_Path::EMAIL_ADMIN_TEMPLATE), Mage::getStoreConfig(AW_Productquestions_Model_Source_Config_Path::EMAIL_SENDER), Mage::getStoreConfig(AW_Productquestions_Model_Source_Config_Path::EMAIL_RECIPIENT), null, array('data' => $question), $storeId); if (!$mailTemplate->getSentSuccess()) { //throw new Exception(); Mage::log($this->__('An error occured while sending Product Questions email from \'%s\' to admin \'%s\' using template \'%s\', asked by \'%s\', the question is \'%s\'', Mage::getStoreConfig(AW_Productquestions_Model_Source_Config_Path::EMAIL_SENDER), Mage::getStoreConfig(AW_Productquestions_Model_Source_Config_Path::EMAIL_RECIPIENT), Mage::getStoreConfig(AW_Productquestions_Model_Source_Config_Path::EMAIL_ADMIN_TEMPLATE), $data['question_author_email'], $question->getQuestionText())); } } // processing standard Newsletter subscription if (isset($data['subscribe_newsletter']) && $data['subscribe_newsletter']) { Mage::helper('productquestions')->subscribeCustomer($data['question_author_email']); } // processing Advanced Newsletter segment subscription if (isset($data['anl_segments'])) { Mage::helper('productquestions')->subscribeAdvancedNewsletterSegment($data['question_author_email'], $data['question_author_name'], $data['anl_segments']); } // sending a reply to customer if (Mage::getStoreConfig('productquestions/autorespond/status')) { $mailTemplate = Mage::getModel('core/email_template'); try { $sender = Mage::helper('productquestions')->getSender(); if (method_exists($mailTemplate->getMail(), 'setReplyTo')) { $mailTemplate->getMail()->setReplyTo($sender['mail'], $sender['name']); } else { $mailTemplate->getMail()->addHeader('Reply-To', $sender['mail']); } $mailTemplate->setFromEmail($sender['mail']); } catch (Exception $ex) { } $categoryId = $this->getRequest()->getParam('category_id'); $mailVariables = array('data' => $question, 'question_text' => $data['question_text'], 'customer_name' => $data['question_author_name'], 'customer_email' => $data['question_author_email'], 'date_asked' => Mage::app()->getLocale()->date($question->getQuestionDate(), Varien_Date::DATETIME_INTERNAL_FORMAT)->toString(Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM)), 'store' => $store, 'product' => $this->_product, 'product_name' => $this->_product->getName(), 'category' => Mage::getModel('catalog/category')->load($categoryId), 'customer' => Mage::getModel('customer/customer')->setWebsiteId(Mage::app()->getWebsite()->getId())->loadByEmail($data['question_author_email']), 'customer_subscribed' => isset($data['subscribe_newsletter']) && $data['subscribe_newsletter'], 'product_url' => $this->_product->getProductUrl()); $mailTemplate->setDesignConfig(array('area' => 'frontend', 'store' => $store))->sendTransactional(Mage::getStoreConfig('productquestions/autorespond/email_template'), Mage::getStoreConfig(AW_Productquestions_Model_Source_Config_Path::EMAIL_SENDER), $data['question_author_email'], $data['question_author_name'], $mailVariables, $storeId); if (!$mailTemplate->getSentSuccess()) { //throw new Exception(); $session->addError($this->__('An error occured, while sending a reply message to you.')); } } } catch (Exception $e) { Mage::logException($e); Mage::getSingleton('core/session')->setProductquestionsData($data); Mage::log($e); $session->addError($this->__('Unable to post question. Please, try again later.')); } } else { Mage::getSingleton('core/session')->setProductquestionsData($data); if (is_array($validate)) { foreach ($validate as $errorMessage) { $session->addError($errorMessage); } } else { $session->addError($this->__('Unable to post question. Please, try again later.')); } } } if (Mage::getVersion() == '1.4.2.0') { $this->_redirectUrl(Mage::helper('productquestions')->getQuestionsLink($productId, $url)); } else { $this->_redirectReferer(); } }