Browse code

Rewrite ajax functionality for Contao 4 compatibility

Benjamin Roth authored on22/03/2019 20:54:43
Showing4 changed files
... ...
@@ -30,11 +30,16 @@
30 30
 
31 31
 namespace legalAgeCheck;
32 32
 
33
+use Contao\Environment;
34
+use Contao\Input;
33 35
 use Contao\InsertTags;
34 36
 use Contao\PageModel;
35 37
 use Contao\LayoutModel;
36 38
 use Contao\PageRegular;
37
-use Contao\System;
39
+use Contao\FrontendTemplate;
40
+use Contao\Frontend;
41
+use Haste\Http\Response\JsonResponse;
42
+use Haste\Http\Response\Response;
38 43
 
39 44
 /**
40 45
  * Class LegalAgeCheck 
... ...
@@ -43,7 +48,7 @@ use Contao\System;
43 48
  * @author     Benjamin Roth <www.esales-media.de> 
44 49
  * @package    Controller
45 50
  */
46
-class LegalAgeCheck extends \Frontend
51
+class LegalAgeCheck extends Frontend
47 52
 {
48 53
 
49 54
 	/**
... ...
@@ -58,6 +63,16 @@ class LegalAgeCheck extends \Frontend
58 63
 	 */
59 64
 	public function performAgeCheck(PageModel $objPage, LayoutModel $objLayout, PageRegular $objPageRegular)
60 65
 	{
66
+	  // Trigger ajax cookie authentication
67
+    if (Environment::get('isAjaxRequest') && Input::get('do') == 'legalage_authentication') {
68
+      if (!is_null(Input::get('legalage_commit')))
69
+      {
70
+        static::sendResponse(array('status'=>(static::ajaxUnlockPage() ? 'OK' : 'FAILED')));
71
+      } else {
72
+        static::preconditionFailed();
73
+      }
74
+    }
75
+
61 76
 		// Skip age check if passed before
62 77
 		//if ($this->Session->get('legalAgeCheck_passed'))
63 78
 		if ($this->Input->cookie('legalAgeCheck_passed') || $objPage->es_ext_agecheck_ignorePage)
... ...
@@ -67,19 +82,19 @@ class LegalAgeCheck extends \Frontend
67 82
 		}
68 83
 
69 84
 		// Get root page
70
-		$objRootPage = \PageModel::findByPk($objPage->rootId);
85
+		$objRootPage = PageModel::findByPk($objPage->rootId);
71 86
 		
72 87
 		// Cancel verification if age check is disabled					
73 88
 		if (is_null($objRootPage) || !$objRootPage->es_ext_agecheck || $objRootPage->es_ext_agecheck_exitPage == $objPage->id)
74 89
 			return;
75 90
 		
76 91
 		// Get exit page
77
-		$objExitPage = \PageModel::findWithDetails($objRootPage->es_ext_agecheck_exitPage);
92
+		$objExitPage = PageModel::findWithDetails($objRootPage->es_ext_agecheck_exitPage);
78 93
 		if (is_null($objExitPage) || !$objExitPage->published)
79 94
 			return;
80 95
 		
81 96
 		// Load dialog template
82
-		$objTemplate = new \FrontendTemplate($this->strTemplate);
97
+		$objTemplate = new FrontendTemplate($this->strTemplate);
83 98
 		
84 99
 		// Load modal box template
85 100
 		//$objModalBoxTemplate = new FrontendTemplate('agecheck_dialog_template');
... ...
@@ -89,8 +104,10 @@ class LegalAgeCheck extends \Frontend
89 104
 		$objTemplate->text = str_replace(array("\r", "\n"),array("\\r","\\n"),$objRootPage->es_ext_agecheck_text);
90 105
 		$objTemplate->btn_over_18 = $GLOBALS['TL_LANG']['MSC']['es_legalagecheck']['over_18'];
91 106
 		$objTemplate->btn_under_18 = $GLOBALS['TL_LANG']['MSC']['es_legalagecheck']['under_18'];
92
-		$objTemplate->exitPageURL = $this->generateFrontendUrl($objExitPage->row(), null, null, true);
93
-		$objTemplate->commitURL = 'system/modules/legalAgeCheck/ajax/Ajax.php?do=legalage_authentication&legalage_commit='.$this->createToken();
107
+//		$objTemplate->exitPageURL = $this->generateFrontendUrl($objExitPage->row(), null, null, true);
108
+		$objTemplate->exitPageURL = $objExitPage->getAbsoluteUrl();
109
+//		$objTemplate->commitURL = 'system/modules/legalAgeCheck/ajax/Ajax.php?do=legalage_authentication&legalage_commit='.$this->createToken();
110
+		$objTemplate->commitURL = Frontend::addToUrl('do=legalage_authentication&legalage_commit='.$this->createToken());
94 111
 		//$objTemplate->modalBoxTemplate = addcslashes($objModalBoxTemplate->parse(), "\\'\"&\n\r");
95 112
 		
96 113
 		// Add dialog code to page
... ...
@@ -138,4 +155,16 @@ class LegalAgeCheck extends \Frontend
138 155
 		}
139 156
 		return $_SESSION['legalAgeCheck_token'];
140 157
 	}
158
+
159
+  private static function sendResponse($content)
160
+  {
161
+    $objResponse = new JsonResponse($content);
162
+    $objResponse->send();
163
+  }
164
+
165
+  private static function preconditionFailed()
166
+  {
167
+    $objResponse = new Response('Precondition failed',412);
168
+    $objResponse->send();
169
+  }
141 170
 }
142 171
\ No newline at end of file
... ...
@@ -12,8 +12,10 @@
12 12
   ],
13 13
   "require":{
14 14
     "php": "^5.6 || ^7.0",
15
+    "ext-json": "*",
15 16
     "contao/core-bundle": "^3.5 || ^4.4",
16
-    "contao-community-alliance/composer-plugin": "~2.4 || ~3.0"
17
+    "contao-community-alliance/composer-plugin": "~2.4 || ~3.0",
18
+    "codefog/contao-haste": "4.*"
17 19
   },
18 20
   "extra":{
19 21
     "contao": {
... ...
@@ -70,7 +70,11 @@ $GLOBALS['TL_DCA']['tl_page']['fields']['es_ext_agecheck_title'] = array
70 70
   'label'                   => &$GLOBALS['TL_LANG']['tl_page']['es_ext_agecheck_title'],
71 71
   'exclude'                 => true,
72 72
   'inputType'               => 'text',
73
-  'eval'                    => array('mandatory'=>true, 'rgxp'=>'extnd'),
73
+  'load_callback'            => array
74
+  (
75
+    array('tl_page_legalAgeCheck', 'setDefaultLegalAgeTitle')
76
+  ),
77
+  'eval'                    => array('mandatory'=>true, 'rgxp'=>'extnd','alwaysSave'=>true),
74 78
   'sql'                     => "varchar(255) NOT NULL default ''"
75 79
 );
76 80
 
... ...
@@ -109,4 +113,21 @@ class tl_page_legalAgeCheck extends \Backend
109 113
 
110 114
     return $varValue;
111 115
   }
116
+
117
+  /**
118
+   * Return default title if value is empty
119
+   *
120
+   * @param $varValue
121
+   * @param \DataContainer $dc
122
+   * @return String
123
+   */
124
+  public function setDefaultLegalAgeTitle($varValue, \DataContainer $dc)
125
+  {
126
+    if (!$varValue)
127
+    {
128
+      return $GLOBALS['TL_LANG']['MSC']['es_legalagecheck']['title'];
129
+    }
130
+
131
+    return $varValue;
132
+  }
112 133
 }
113 134
\ No newline at end of file
... ...
@@ -5,12 +5,12 @@ $GLOBALS['TL_CSS'][] = 'system/modules/legalAgeCheck/assets/css/vex-theme-defaul
5 5
 <script src="system/modules/legalAgeCheck/assets/js/vex.combined.min.js"></script>
6 6
 <script>
7 7
 
8
-	var comPromo = vex.dialog.open({
8
+	var legalAgeCheck = vex.dialog.open({
9 9
 		unsafeMessage: '<h1 class="vex-header"><?php echo $this->title; ?></h1>' +
10 10
 		'<?php echo $this->text; ?>',
11 11
 		overlayClosesOnClick: false,
12 12
 		escapeButtonCloses: false,
13
-		buttons: [{text: '<?php echo $this->btn_over_18; ?>',type: 'button',className: 'vex-dialog-button-primary',click: function(vexContent, event) {
13
+		buttons: [{text: '<?php echo $this->btn_over_18; ?>',type: 'button',className: 'vex-dialog-button-primary',click: function(event) {
14 14
 			$.ajax({
15 15
 				url: '<?php echo $this->commitURL; ?>',
16 16
 				type: 'GET',
... ...
@@ -18,11 +18,11 @@ $GLOBALS['TL_CSS'][] = 'system/modules/legalAgeCheck/assets/css/vex-theme-defaul
18 18
 				success: function (data) {
19 19
 					if (data.status == 'OK')
20 20
 					{
21
-						return vex.close(vexContent.data().vex.id);
21
+            return vex.close(legalAgeCheck);
22 22
 					}
23 23
 				}
24 24
 			});
25
-		}}, {text: '<?php echo $this->btn_under_18; ?>',type: 'button',className: 'vex-dialog-button-secondary',click: function(vexContent, event) { window.location = '<?php echo $this->exitPageURL; ?>'}}]
25
+		}}, {text: '<?php echo $this->btn_under_18; ?>',type: 'button',className: 'vex-dialog-button-secondary',click: function(event) { window.location = '<?php echo $this->exitPageURL; ?>'}}]
26 26
 	});
27 27
 
28 28