From 57360d17238eaeed13d4f1c1574f370aad565de3 Mon Sep 17 00:00:00 2001
From: Reinhold Kainhofer <reinhold@kainhofer.com>
Date: Sat, 2 Apr 2016 23:06:14 +0200
Subject: [PATCH] Make it work with the new VM Framework

---
 controllers/eurecap.php     |  2 +-
 eurecap.php                 | 22 +++++++++++++++-------
 eurecap.xml                 |  2 +-
 models/eurecap.php          |  2 +-
 views/eurecap/view.html.php |  7 +++----
 5 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/controllers/eurecap.php b/controllers/eurecap.php
index dc28f32..eab0871 100644
--- a/controllers/eurecap.php
+++ b/controllers/eurecap.php
@@ -34,7 +34,7 @@ class VirtuemartControllerEuRecap extends VmController {
 	function __construct(){
 		parent::__construct();
 		// Add the proper view pathes...
-		$this->addViewPath(JPATH_PLUGINS.DS . 'vmextended' . DS . 'eurecap' . DS . 'views');
+		$this->addViewPath(JPATH_PLUGINS.DS . 'vmextended' . DS . 'eurecap' . DS . 'views' . DS . 'eurecap');
 	}
 
 	public function cancel(){
diff --git a/eurecap.php b/eurecap.php
index 6a0e84b..03e2254 100644
--- a/eurecap.php
+++ b/eurecap.php
@@ -24,7 +24,7 @@ class plgVmExtendedEuRecap extends vmExtendedPlugin {
 	public function __construct (&$subject, $config=array()) {
 		parent::__construct($subject, $config);
 		$this->_path = JPATH_PLUGINS.DS.'vmextended'.DS.$this->getName();
-		JPlugin::loadLanguage('plg_vmextended_'.$this->getName());
+		$this->loadLanguage('plg_vmextended_'.$this->getName());
 	}
 
 //     public function getVmPluginCreateTableSQL () {
@@ -45,13 +45,21 @@ class plgVmExtendedEuRecap extends vmExtendedPlugin {
 	 */
 	public function onVmAdminController ($controller) {
 		if ($controller == 'eurecap') {
-			VmModel::addIncludePath($this->_path . DS . 'models');
-
-			// TODO: Make sure the model exists. We probably should find a better way to load this automatically! 
-			//       Currently, some path config seems missing, so the model is not found by default.
-			require_once($this->_path.DS.'models'.DS.'eurecap.php');
-			require_once($this->_path.DS.'models'.DS.'eurecap_config.php');
+			VmModel::addIncludePath($this->_path . DS . 'models', 'VirtueMartModel');
+// 			require_once($this->_path.DS.'models'.DS.'eurecap.php');
+// 			require_once($this->_path.DS.'models'.DS.'eurecap_config.php');
 			require_once($this->_path.DS.'controllers'.DS.'eurecap.php');
+			
+			// In later VM versions, we can execute the controller here:
+			$_class = 'VirtueMartController'.ucfirst($controller);
+			if(!class_exists($_class)){
+				vmError('Serious Error could not find controller '.$_class,'Serious error, unable to find class');
+				$app = vFactory::getApplication();
+				$app->redirect('index.php?option=com_virtuemart');
+			}
+			$controller = new $_class();
+			$controller->execute(vRequest::getCmd('task', $controller));
+			$controller->redirect();
 			return true;
 		}
 	}
diff --git a/eurecap.xml b/eurecap.xml
index d0ed32e..f14cede 100644
--- a/eurecap.xml
+++ b/eurecap.xml
@@ -7,7 +7,7 @@
     <authorUrl>http://www.open-tools.net/</authorUrl>
     <copyright>Copyright (C) 2015 Reinhold Kainhofer. All rights reserved.</copyright>
     <license>http://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3</license>
-    <version>0.4</version>
+    <version>0.5</version>
     <releaseDate>2016-01-01</releaseDate>
     <releaseType>Update</releaseType>
     <downloadUrl>http://www.open-tools.net</downloadUrl>
diff --git a/models/eurecap.php b/models/eurecap.php
index 3a294d6..b7dc22a 100644
--- a/models/eurecap.php
+++ b/models/eurecap.php
@@ -252,7 +252,7 @@ vmDebug("SQL: SELECT ".$selectString.$joinedTables.$whereString.$groupBy.$orderB
         }
         sort($templates, SORT_STRING);
         $templates = array_unique($templates);
-        
+        $options = array();
         foreach ($templates as $tmpl) {
 			$value = substr($tmpl, 7, -4); // Cut off export_ and .php
 			$options[] = JHtml::_('select.option', vmText::_('VMEXT_EU_RECAP_EXPORT_' . strtoupper($value)), $value);
diff --git a/views/eurecap/view.html.php b/views/eurecap/view.html.php
index 75af80c..1e99669 100644
--- a/views/eurecap/view.html.php
+++ b/views/eurecap/view.html.php
@@ -34,9 +34,8 @@ if(!defined('VM_VERSION') or VM_VERSION < 3){
 class VirtuemartViewEuRecap extends VmViewAdmin {
 	function __construct(){
 		parent::__construct();
-		// Add the proper view pathes...
-		$this->_addPath('template', JPATH_PLUGINS.DS . 'vmextended' . DS . 'eurecap' . DS . 'views' . DS . $this->getName() . DS  . 'tmpl');
-// 		$this->_addPath('models', JPATH_PLUGINS.DS . 'vmextended' . DS . 'eurecap' . DS . 'models' );
+		$this->_name = "eurecap";
+		$this->addLayoutPath($this->getName(), JPATH_PLUGINS.DS . 'vmextended' . DS . $this->getName() . DS . 'views' . DS . $this->getName() . DS  . 'tmpl');
 	}
 
 	/**
@@ -47,7 +46,7 @@ class VirtuemartViewEuRecap extends VmViewAdmin {
 		if (!class_exists('VmHTML'))
 			require(VMPATH_ADMIN . DS . 'helpers' . DS . 'html.php');
 
-		$model		= VmModel::getModel();
+		$model		= VmModel::getModel('eurecap');
 		$this->addStandardDefaultViewLists($model);
 
 		vRequest::setvar('task','');
-- 
GitLab