DigitalPianism_ReviewCaptcha - Version 0.1.0

Version Notes

- Use observers instead of rewriting controller and block.

Download this release

Release Info

Developer Digital Pianism
Extension DigitalPianism_ReviewCaptcha
Version 0.1.0
Comparing to
See all releases


Code changes from version 0.0.4 to 0.1.0

app/code/community/DigitalPianism/ReviewCaptcha/Block/Form.php DELETED
@@ -1,18 +0,0 @@
1
- <?php
2
-
3
- /**
4
- * Class DigitalPianism_ReviewCaptcha_Block_Form
5
- */
6
- class DigitalPianism_ReviewCaptcha_Block_Form extends Mage_Review_Block_Form
7
- {
8
-
9
- /**
10
- * @return string
11
- */
12
- public function getAction()
13
- {
14
- $productId = Mage::app()->getRequest()->getParam('id', false);
15
- return Mage::getUrl('reviewcaptcha/index/post', array('id' => $productId));
16
- }
17
-
18
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/DigitalPianism/ReviewCaptcha/Model/Observer.php ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class DigitalPianism_ReviewCaptcha_Model_Observer
4
+ {
5
+ public function checkReviewCaptcha(Varien_Event_Observer $observer)
6
+ {
7
+ // Captcha check
8
+ $formId = "review_captcha";
9
+ $captchaModel = Mage::helper("captcha")->getCaptcha($formId);
10
+ if ($captchaModel->isRequired())
11
+ {
12
+ $controller = $observer->getControllerAction();
13
+ $word = $this->_getCaptchaString($controller->getRequest(), $formId);
14
+ if (!$captchaModel->isCorrect($word))
15
+ {
16
+ Mage::getSingleton('core/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
17
+ $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
18
+ if ($redirectUrl = Mage::getSingleton('review/session')->getRedirectUrl(true)) {
19
+ $controller->getResponse()->setRedirect($redirectUrl);
20
+ return;
21
+ }
22
+ $referrerUrl = Mage::helper('core/http')->getHttpReferer() ? Mage::helper('core/http')->getHttpReferer() : Mage::getUrl();
23
+ $controller->getResponse()->setRedirect($referrerUrl);
24
+ }
25
+ }
26
+ return $this;
27
+ }
28
+
29
+ /**
30
+ * @param $request
31
+ * @param $formId
32
+ * @return mixed
33
+ */
34
+ protected function _getCaptchaString($request, $formId)
35
+ {
36
+ $captchaParams = $request->getPost(Mage_Captcha_Helper_Data::INPUT_NAME_FIELD_VALUE);
37
+ return $captchaParams[$formId];
38
+ }
39
+ }
app/code/community/DigitalPianism/ReviewCaptcha/controllers/IndexController.php DELETED
@@ -1,107 +0,0 @@
1
- <?php
2
- require_once 'Mage/Review/controllers/ProductController.php';
3
-
4
- /**
5
- * Review controller
6
- *
7
- * @category Mage
8
- * @package Mage_Review
9
- * @author Magento Core Team <core@magentocommerce.com>
10
- */
11
- class DigitalPianism_ReviewCaptcha_IndexController extends Mage_Review_ProductController
12
- {
13
- protected $_realModuleName = 'Mage_Review';
14
- /**
15
- * Submit new review action
16
- *
17
- */
18
- public function postAction()
19
- {
20
- if ($data = Mage::getSingleton('review/session')->getFormData(true)) {
21
- $rating = array();
22
- if (isset($data['ratings']) && is_array($data['ratings'])) {
23
- $rating = $data['ratings'];
24
- }
25
- } else {
26
- $data = $this->getRequest()->getPost();
27
- $rating = $this->getRequest()->getParam('ratings', array());
28
- }
29
-
30
- if (($product = $this->_initProduct()) && !empty($data)) {
31
- $session = Mage::getSingleton('core/session');
32
- /* @var $session Mage_Core_Model_Session */
33
- $review = Mage::getModel('review/review')->setData($data);
34
- /* @var $review Mage_Review_Model_Review */
35
-
36
- $validate = $review->validate();
37
-
38
- // Captcha check
39
- $formId = "review_captcha";
40
- $captchaModel = Mage::helper("captcha")->getCaptcha($formId);
41
- if ($captchaModel->isRequired())
42
- {
43
- if (!$captchaModel->isCorrect($this->_getCaptchaString($this->getRequest(), $formId)))
44
- {
45
- $validate = false;
46
- $validate[] = $this->__('Incorrect CAPTCHA.');
47
- }
48
- }
49
-
50
- if ($validate === true) {
51
- try {
52
- $review->setEntityId($review->getEntityIdByCode(Mage_Review_Model_Review::ENTITY_PRODUCT_CODE))
53
- ->setEntityPkValue($product->getId())
54
- ->setStatusId(Mage_Review_Model_Review::STATUS_PENDING)
55
- ->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
56
- ->setStoreId(Mage::app()->getStore()->getId())
57
- ->setStores(array(Mage::app()->getStore()->getId()))
58
- ->save();
59
-
60
- foreach ($rating as $ratingId => $optionId) {
61
- Mage::getModel('rating/rating')
62
- ->setRatingId($ratingId)
63
- ->setReviewId($review->getId())
64
- ->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
65
- ->addOptionVote($optionId, $product->getId());
66
- }
67
-
68
- $review->aggregate();
69
- $session->addSuccess($this->__('Your review has been accepted for moderation.'));
70
- }
71
- catch (Exception $e) {
72
- $session->setFormData($data);
73
- $session->addError($this->__('Unable to post the review.'));
74
- }
75
- }
76
- else {
77
- $session->setFormData($data);
78
- if (is_array($validate)) {
79
- foreach ($validate as $errorMessage) {
80
- $session->addError($errorMessage);
81
- }
82
- }
83
- else {
84
- $session->addError($this->__('Unable to post the review.'));
85
- }
86
- }
87
- }
88
-
89
- if ($redirectUrl = Mage::getSingleton('review/session')->getRedirectUrl(true)) {
90
- $this->_redirectUrl($redirectUrl);
91
- return;
92
- }
93
- $this->_redirectReferer();
94
- }
95
-
96
- /**
97
- * @param $request
98
- * @param $formId
99
- * @return mixed
100
- */
101
- protected function _getCaptchaString($request, $formId)
102
- {
103
- $captchaParams = $request->getPost(Mage_Captcha_Helper_Data::INPUT_NAME_FIELD_VALUE);
104
- return $captchaParams[$formId];
105
- }
106
-
107
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/DigitalPianism/ReviewCaptcha/etc/config.xml CHANGED
@@ -2,31 +2,29 @@
2
  <config>
3
  <modules>
4
  <DigitalPianism_ReviewCaptcha>
5
- <version>0.0.4</version>
6
  </DigitalPianism_ReviewCaptcha>
7
  </modules>
8
 
9
  <global>
10
- <blocks>
11
- <review>
12
- <rewrite>
13
- <form>DigitalPianism_ReviewCaptcha_Block_Form</form>
14
- </rewrite>
15
- </review>
16
- </blocks>
 
 
 
 
 
 
 
 
17
  </global>
18
 
19
  <frontend>
20
- <routers>
21
- <review_captcha>
22
- <use>standard</use>
23
- <args>
24
- <module>DigitalPianism_ReviewCaptcha</module>
25
- <frontName>reviewcaptcha</frontName>
26
- </args>
27
- </review_captcha>
28
- </routers>
29
-
30
  <layout>
31
  <updates>
32
  <review_captcha>
2
  <config>
3
  <modules>
4
  <DigitalPianism_ReviewCaptcha>
5
+ <version>0.1.0</version>
6
  </DigitalPianism_ReviewCaptcha>
7
  </modules>
8
 
9
  <global>
10
+ <models>
11
+ <reviewcaptcha>
12
+ <class>DigitalPianism_ReviewCaptcha_Model</class>
13
+ </reviewcaptcha>
14
+ </models>
15
+ <events>
16
+ <controller_action_predispatch_review_product_post>
17
+ <observers>
18
+ <digitalpianism_reviewcaptcha_check>
19
+ <class>reviewcaptcha/observer</class>
20
+ <method>checkReviewCaptcha</method>
21
+ </digitalpianism_reviewcaptcha_check>
22
+ </observers>
23
+ </controller_action_predispatch_review_product_post>
24
+ </events>
25
  </global>
26
 
27
  <frontend>
 
 
 
 
 
 
 
 
 
 
28
  <layout>
29
  <updates>
30
  <review_captcha>
package.xml CHANGED
@@ -1,7 +1,7 @@
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>DigitalPianism_ReviewCaptcha</name>
4
- <version>0.0.4</version>
5
  <stability>stable</stability>
6
  <license uri="http://opensource.org/licenses/osl-3.0.php">OSL v3.0</license>
7
  <channel>community</channel>
@@ -44,11 +44,11 @@
44
  &lt;?php echo $this-&gt;getChildHtml('form.additional.info'); ?&gt;&#xD;
45
  &lt;/ul&gt;&#xD;
46
  &lt;/pre&gt;</description>
47
- <notes>- Original release</notes>
48
  <authors><author><name>Digital Pianism</name><user>digitalpianism</user><email>contact@digital-pianism.com</email></author></authors>
49
- <date>2015-11-16</date>
50
- <time>21:14:23</time>
51
- <contents><target name="magecommunity"><dir name="DigitalPianism"><dir name="ReviewCaptcha"><dir name="Block"><file name="Form.php" hash="cd6ec652a8fcebcf99c9c9cb6aeae35c"/></dir><dir name="controllers"><file name="IndexController.php" hash="2948058816f32ff8647e1c2145094083"/></dir><dir name="etc"><file name="config.xml" hash="651004f1c3d96394599ab72df3d4e7df"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="DigitalPianism_ReviewCaptcha.xml" hash="0e864d11c329f0c4b961dd29d0e19ecc"/></dir></target><target name="magedesign"><dir name="frontend"><dir name="base"><dir name="default"><dir name="layout"><dir name="digitalpianism"><file name="reviewcaptcha.xml" hash="f347bc64a1519b3d26c9a34fa63915fb"/></dir></dir></dir></dir></dir></target></contents>
52
  <compatible/>
53
  <dependencies><required><php><min>4.1.0</min><max>6.0.0</max></php></required></dependencies>
54
  </package>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>DigitalPianism_ReviewCaptcha</name>
4
+ <version>0.1.0</version>
5
  <stability>stable</stability>
6
  <license uri="http://opensource.org/licenses/osl-3.0.php">OSL v3.0</license>
7
  <channel>community</channel>
44
  &lt;?php echo $this-&gt;getChildHtml('form.additional.info'); ?&gt;&#xD;
45
  &lt;/ul&gt;&#xD;
46
  &lt;/pre&gt;</description>
47
+ <notes>- Use observers instead of rewriting controller and block.</notes>
48
  <authors><author><name>Digital Pianism</name><user>digitalpianism</user><email>contact@digital-pianism.com</email></author></authors>
49
+ <date>2015-12-09</date>
50
+ <time>20:57:14</time>
51
+ <contents><target name="magecommunity"><dir name="DigitalPianism"><dir name="ReviewCaptcha"><dir name="Model"><file name="Observer.php" hash="13702fac76e5b72bff8a3caf26c1c829"/></dir><dir name="etc"><file name="config.xml" hash="c4dd460eeaa038c661455ab320a3a6ff"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="DigitalPianism_ReviewCaptcha.xml" hash="0e864d11c329f0c4b961dd29d0e19ecc"/></dir></target><target name="magedesign"><dir name="frontend"><dir name="base"><dir name="default"><dir name="layout"><dir name="digitalpianism"><file name="reviewcaptcha.xml" hash="f347bc64a1519b3d26c9a34fa63915fb"/></dir></dir></dir></dir></dir></target></contents>
52
  <compatible/>
53
  <dependencies><required><php><min>4.1.0</min><max>6.0.0</max></php></required></dependencies>
54
  </package>