protected function _toHtml()
 {
     if (AW_Productquestions_Helper_Data::isModuleOutputDisabled()) {
         return '';
     }
     $storeId = Mage::app()->getStore()->getId();
     $route = $this->getRequest()->getModuleName();
     if ($route != 'productquestions') {
         $this->_showLastX = (int) Mage::getStoreConfig('productquestions/interface/show_last_x', $storeId);
     }
     $this->setShowPager('productquestions' == $this->getRequest()->getModuleName() || !$this->_showLastX);
     if (false === $this->_prepareCollection()) {
         return '';
     }
     if ($sorter = $this->getLayout()->getBlock('productquestions_sorter')) {
         list($sortOrder, $sortDir) = $sorter->getCurrentSorting();
         if ($sortOrder) {
             $this->_collection = $this->_collection->applySorting($sortOrder, $sortDir);
         }
     }
     if ($this->getShowPager() && ($pager = $this->getLayout()->getBlock($this->_pagerName))) {
         $qid = $this->getQuestionId();
         if (null !== $qid && ($allIds = $this->_collection->getAllIdsFiltered())) {
             $pos = array_search($qid, $allIds);
             if (false !== $pos && ($pageSize = $pager->getLimit()) && $pageSize < count($allIds) && ($pageNum = 1 + (int) ($pos / $pageSize))) {
                 $this->getRequest()->setParam($pager->getPageVarName(), $pageNum);
             }
         }
         $this->_collection = $pager->setCollection($this->_collection)->getCollection();
     }
     $this->setVotingAllowed(Mage::getStoreConfig('productquestions/interface/guests_allowed_to_vote', $storeId) || Mage::getSingleton('customer/session')->isLoggedIn());
     return parent::_toHtml();
 }
 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 saveAction()
 {
     $session = Mage::getSingleton('adminhtml/session');
     if ($dataRequested = $this->getRequest()->getPost()) {
         $data = $dataRequested;
         $id = $this->getRequest()->getParam('id');
         try {
             $data['question_store_ids'] = implode(',', $data['question_store_ids']);
             $locale = Mage::app()->getLocale();
             $model = Mage::getModel('productquestions/productquestions')->setData($data)->setQuestionDate($locale->date($data['question_date'], $locale->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT), null, false)->addTime(substr($data['question_datetime'], 10))->toString(Varien_Date::DATETIME_INTERNAL_FORMAT))->setId($id)->save();
             if ($this->getRequest()->getParam('sendEmail')) {
                 $model->setQuestionReplyText(AW_Productquestions_Helper_Data::parseURLsIntoLinks($model->getQuestionReplyText()));
                 $storeId = $model->getQuestionStoreId();
                 $store = Mage::app()->getStore($storeId);
                 $product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($model->getQuestionProductId());
                 $mailTemplate = Mage::getModel('core/email_template');
                 try {
                     $mailTemplate->setReplyTo(Mage::getStoreConfig(AW_Productquestions_Model_Source_Config_Path::EMAIL_SENDER, $storeId));
                 } catch (Exception $ex) {
                 }
                 $mailTemplate->setDesignConfig(array('area' => 'frontend', 'store' => $store))->sendTransactional(Mage::getStoreConfig(AW_Productquestions_Model_Source_Config_Path::EMAIL_CUSTOMER_TEMPLATE, $storeId), Mage::getStoreConfig(AW_Productquestions_Model_Source_Config_Path::EMAIL_SENDER, $storeId), $model->getQuestionAuthorEmail(), null, array('data' => $model, 'question_text' => $model->getQuestionText(), 'reply_text' => $model->getQuestionReplyText(), 'customer_name' => $model->getQuestionAuthorName(), 'customer_email' => $model->getQuestionAuthorEmail(), 'date_asked' => Mage::app()->getLocale()->date($model->getQuestionDate(), Varien_Date::DATETIME_INTERNAL_FORMAT)->toString(Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM)), 'product' => $product, 'product_name' => $product->getName(), 'product_url' => $product->getProductUrl(), 'store' => $store, 'store_url' => $store->getUrl(), 'customer' => Mage::getModel('customer/customer')->setWebsiteId($store->getWebsiteId())->loadByEmail($model->getQuestionAuthorEmail())), $storeId);
                 if (!$mailTemplate->getSentSuccess()) {
                     throw new Exception('Message was successfull saved, but the email was not sent');
                 } else {
                     $session->addSuccess($this->__('Email was sent successfully'));
                 }
             }
             $session->addSuccess($this->__('Question was successfully saved'));
             $session->setProductquestionsData(false);
             if ($this->getRequest()->getParam('back', false)) {
                 $this->_redirectReferer();
             } else {
                 $this->_redirect('*/*/');
             }
             return;
         } catch (Exception $e) {
             $session->addError($e->getMessage());
             Mage::logException($e);
             $session->setProductquestionsData($dataRequested);
             $this->_redirectReferer();
             return;
         }
     }
     $session->addError($this->__('Unable to find a data to save'));
     $this->_redirect('*/*/');
 }
 protected function _toHtml()
 {
     if (AW_Productquestions_Helper_Data::isModuleOutputDisabled()) {
         return '';
     }
     $product = Mage::helper('productquestions')->getCurrentProduct(true);
     if (!$product instanceof Mage_Catalog_Model_Product) {
         return '';
     }
     $productId = $product->getId();
     $category = Mage::registry('current_category');
     if ($category instanceof Mage_Catalog_Model_Category) {
         $categoryId = $category->getId();
     } else {
         $categoryId = false;
     }
     $questionCount = Mage::getResourceModel('productquestions/productquestions_collection')->addProductFilter($productId)->addVisibilityFilter()->addAnsweredFilter()->addStoreFilter()->getSize();
     $params = array('id' => $productId);
     if ($categoryId) {
         $params['category'] = $categoryId;
     }
     $suffix = Mage::getStoreConfig('catalog/seo/category_url_suffix');
     /*if($urlKey = $product->getUrlKey())
       {
           $requestString = ltrim(Mage::app()->getFrontController()->getRequest()->getRequestString(), '/');
           
           $pqSuffix = $urlKey.$suffix;
           if($pqSuffix == substr($requestString, strlen($requestString)-strlen($pqSuffix)))
           {
               $requestString = substr($requestString, 0, strlen($requestString)-strlen($suffix));
               $this->setQuestionsPageUrl($this->getBaseUrl().$requestString.AW_Productquestions_Model_Urlrewrite::SEO_SUFFIX.$suffix);
           }
       }*/
     if (Mage::getStoreConfig('productquestions/seo/enable_url_rewrites')) {
         $productUrl = $product->getProductUrl();
         $fileExtentionPos = strrpos($productUrl, $suffix);
         $this->setQuestionsPageUrl(substr($productUrl, 0, $fileExtentionPos) . AW_Productquestions_Model_Urlrewrite::SEO_SUFFIX . $suffix);
     } else {
         $this->setQuestionsPageUrl(Mage::getUrl('productquestions/index/index/', $params));
     }
     $this->setQuestionCount($questionCount);
     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();
     }
 }