From f553c8c11f0a8b4cf97fb2fd5a1a001f274a5c40 Mon Sep 17 00:00:00 2001 From: Reinhold Kainhofer <reinhold@kainhofer.com> Date: Tue, 18 Dec 2012 20:26:31 +0100 Subject: [PATCH] V0.2: Basics work, trigger might be improved --- Makefile | 2 +- ....plg_system_vmAutoParentCategories.sys.ini | 48 ++++ ...stem_vmProductAutoParentCategories.sys.ini | 38 --- ....plg_system_vmAutoParentCategories.sys.ini | 48 ++++ ...stem_vmProductAutoParentCategories.sys.ini | 33 --- vmAutoParentCategories.php | 267 ++++++++++++++++++ vmAutoParentCategories.xml | 61 ++++ vmProductAutoParentCategories.php | 201 ------------- vmProductAutoParentCategories.xml | 59 ---- 9 files changed, 425 insertions(+), 332 deletions(-) create mode 100644 de-DE.plg_system_vmAutoParentCategories.sys.ini delete mode 100644 de-DE.plg_system_vmProductAutoParentCategories.sys.ini create mode 100644 en-GB.plg_system_vmAutoParentCategories.sys.ini delete mode 100644 en-GB.plg_system_vmProductAutoParentCategories.sys.ini create mode 100644 vmAutoParentCategories.php create mode 100644 vmAutoParentCategories.xml delete mode 100644 vmProductAutoParentCategories.php delete mode 100644 vmProductAutoParentCategories.xml diff --git a/Makefile b/Makefile index e6b177b..e7ec3bf 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -BASE=vmProductAutoParentCategories +BASE=vmAutoParentCategories PLUGINTYPE=system VERSION=0.1 diff --git a/de-DE.plg_system_vmAutoParentCategories.sys.ini b/de-DE.plg_system_vmAutoParentCategories.sys.ini new file mode 100644 index 0000000..78c38de --- /dev/null +++ b/de-DE.plg_system_vmAutoParentCategories.sys.ini @@ -0,0 +1,48 @@ +; @plugin VmAutoParentCategories +; @copyright Copyright (C) 2012 Reinhold Kainhofer. +; @Website : http://kainhofer.com +; @license - http://www.gnu.org/licenses/gpl.html GNU/GPL + +VMAUTOCATEGORIES_DESCRIPTION="<h2>Virtuemart 2 automatische Produktkategorien Plugin</h2><p style='font-weight: normal'>Dieses Plugin kann den VirtueMart Produkten automatische alle übergeordneten Kategorien des Produkts zuweisen, oder all übergeordneten Kategorien entfernen. Ebenso können bei Produktvarianten die Kategorien des übergeordneten Produkts zugeordnet, oder alle Kategorien entfernt werden.</p><p style='font-weight: normal'>Um das Plugin manuell auszuführen, <a href="_QQ_"?vmAutoParentCategories=run"_QQ_">klicken Sie hier</a>.</p>" + +VMAUTOCATEGORIES_RUN="Führe Plugin aus:" +VMAUTOCATEGORIES_RUN_DESC="Wählen Sie aus, wann das Plugin ausgeführt wird und die Kategorien der VirtueMart Produkte modifiziert." + +VMAUTOCATEGORIES_RUN_NEVER="Nie" +VMAUTOCATEGORIES_RUN_BACKEND_LOGIN="Joomla Backend Login" +VMAUTOCATEGORIES_RUN_VM_STARTPAGE="VirtueMart Startseite (Backend)" +VMAUTOCATEGORIES_RUN_VM_PRODUCTPAGE="VirtueMart Produktliste (Backend)" + +VMAUTOCATEGORIES_NORMAL_PRODUCTS="Normale Produkts" +VMAUTOCATEGORIES_NORMAL_PRODUCTS_DESC="Wählen Sie, wie normale Produkts (d.h. keine Produktvarianten) modifiziert werden sollen." +VMAUTOCATEGORIES_CHILD_PRODUCTS="Unterprodukte" +VMAUTOCATEGORIES_CHILD_PRODUCTS_DESC="Wählen Sie, wie die Kategorien von Produktvarianten modifiziert werden sollen." +VMAUTOCATEGORIES_DO_NOTHING="Nicht modifizieren" +VMAUTOCATEGORIES_ADD_PARENTS="Produkt zu allen Elternkategorien hinzufügen" +VMAUTOCATEGORIES_ADD_ONE_PARENT="Produkt nur zur unmittelbaren Elternkategorie hinzufügen." +VMAUTOCATEGORIES_REMOVE_EXCEPT_LEAF="Etferne alle Elternkategorien." +VMAUTOCATEGORIES_COPY_CATEGORIES_FROM_PARENT="Dupliziere Kategorien des übergeordneten Produkts." +VMAUTOCATEGORIES_REMOVE_ALL="Alle Kategorien entfernen." + +VMAUTOCATEGORIES_DEBUG="Debug-Meldungen" +VMAUTOCATEGORIES_DEBUG_DESC="Wählen Sie, wie viel Information das Plugin ausgeben soll." +VMAUTOCATEGORIES_DBG_NO_OUTPUT="Keine Meldung" +VMAUTOCATEGORIES_DBG_REPORT_CHANGES="Änderungen melden" +VMAUTOCATEGORIES_DBG_REPORT_ALWAYS="Anzeigen, auch wenn nichts geändert wurde" +VMAUTOCATEGORIES_DBG_REPORT_NO_CHANGES="Änderungen melden, aber nicht anwenden" +VMAUTOCATEGORIES_DBG_DEBUG="Ausführliche Debugmeldungen" +VMAUTOCATEGORIES_DBG_DEBUG_NO_CHANGES="Ausführliche Debugmeldungen, Änderungen nicht anwenden" + +VMAUTOCATEGORIES_PROGRESS_ARTICLES_MODIFIED="Kategorien von %d Top-Level Produkten modifiziert." +VMAUTOCATEGORIES_PROGRESS_ARTICLES_NO_MODIFICATIONS="Keine Modifikation der Kategorien von Top-Level Produkten nötig." +VMAUTOCATEGORIES_PROGRESS_CHILDREN_MODIFIED="Kategorien von %d Produktvarianten modifiziert." +VMAUTOCATEGORIES_PROGRESS_CHILDREN_NO_MODIFICATIONS="Keine Modifikation der Kategorien von Produktvarianten nötig." +VMAUTOCATEGORIES_PROGRESS_NOTAPPLIED="Änderungen aufgrund der Plugin-Einstellungen nicht angewendet." + +VMAUTOCATEGORIES_DEBUG_LOADCATS="%s Kategorien geladen." +VMAUTOCATEGORIES_DEBUG_LOADPRODUCTS="%s Produkte geladen." +VMAUTOCATEGORIES_DEBUG_ARTICLE_MODIFIED="Produkt "_QQ_"%s"_QQ_" (SKU %s) modifiziert: %d Kategorien hinzugefügt, %d Kategorien entfernt." +VMAUTOCATEGORIES_DEBUG_ARTICLE_NOT_MODIFIED="Produkt "_QQ_"%s"_QQ_" (SKU %s) nicht modifiziert." +VMAUTOCATEGORIES_DEBUG_CHILD_MODIFIED="Produktvariante "_QQ_"%s"_QQ_" (SKU %s) modifiziert: %d Kategorien hinzugefügt, %d Kategorien entfernt." +VMAUTOCATEGORIES_DEBUG_CHILD_NOT_MODIFIED="Produktvariante "_QQ_"%s"_QQ_" (SKU %s) nicht modifiziert." +VMAUTOCATEGORIES_DEBUG_CATEGORYMODIFICATIONS=" Hinzugefügte Kategorien: %s. Entfernte Kategorien: %s." diff --git a/de-DE.plg_system_vmProductAutoParentCategories.sys.ini b/de-DE.plg_system_vmProductAutoParentCategories.sys.ini deleted file mode 100644 index 8d34474..0000000 --- a/de-DE.plg_system_vmProductAutoParentCategories.sys.ini +++ /dev/null @@ -1,38 +0,0 @@ -; @plugin VmProductAutoParentCategories -; @copyright Copyright (C) 2012 Reinhold Kainhofer. -; @Website : http://kainhofer.com -; @license - http://www.gnu.org/licenses/gpl.html GNU/GPL - -VMPRODPARENTCATS_DESCRIPTION="<h2>Virtuemart 2 automatische Artikelkategorien Plugin</h2><p style='font-weight: normal'>Dieses Plugin kann den VirtueMart Artikel automatische alle übergeordneten Kategorien des Artikels zuweisen, oder all übergeordneten Kategorien entfernen. Ebenso können bei Kindartikeln die Kategorien des Elternartikels zugeordnet, oder alle Kategorien entfernt werden.</p>" - -VMPRODPARENTCATS_RUN="Führe Plugin aus:" -VMPRODPARENTCATS_RUN_DESC="Wählen Sie aus, wann das Plugin ausgeführt wird und die Kategorien der VirtueMart Artikeln modifiziert. Beachten Sie, dass aktuell das Plugin nicht manuell ausgeführt werden kann." - -VMPRODPARENTCATS_RUN_NEVER="Nie" -VMPRODPARENTCATS_RUN_BACKEND_LOGIN="Joomla Backend Login" - -VMPRODPARENTCATS_NORMAL_PRODUCTS="Normale Artikel" -VMPRODPARENTCATS_NORMAL_PRODUCTS_DESC="Wählen Sie, wie normale Artikel (d.h. keine Unterartikel) modifiziert werden sollen." -VMPRODPARENTCATS_CHILD_PRODUCTS="Unterprodukte" -VMPRODPARENTCATS_CHILD_PRODUCTS_DESC="Wählen Sie, wie die Kategorien von Unterartikeln modifiziert werden sollen." -VMPRODPARENTCATS_DO_NOTHING="Nicht modifizieren" -VMPRODPARENTCATS_ADD_PARENTS="Artikel zu allen Elternkategorien hinzufügen" -VMPRODPARENTCATS_ADD_ONE_PARENT="Artikel nur zur unmittelbaren Elternkategorie hinzufügen." -VMPRODPARENTCATS_REMOVE_EXCEPT_LEAF="Etferne alle Elternkategorien." -VMPRODPARENTCATS_COPY_CATEGORIES_FROM_PARENT="Dupliziere Kategorien des übergeordneten Artikels." -VMPRODPARENTCATS_REMOVE_ALL="Alle Kategorien entfernen." - -VMPRODPARENTCATS_DEBUG="Debug-Meldungen" -VMPRODPARENTCATS_DEBUG_DESC="Wählen Sie, wie viel Information das Plugin ausgeben soll." -VMPRODPARENTCATS_DBG_NO_OUTPUT="Keine Meldung" -VMPRODPARENTCATS_DBG_REPORT_CHANGES="Änderungen melden" -VMPRODPARENTCATS_DBG_REPORT_ALWAYS="Anzeigen, auch wenn nichts geändert wurde" -VMPRODPARENTCATS_DBG_REPORT_NO_CHANGES="Änderungen melden, aber nicht anwenden" -VMPRODPARENTCATS_DBG_DEBUG="Ausführliche Debugmeldungen" -VMPRODPARENTCATS_DBG_DEBUG_NO_CHANGES="Ausführliche Debugmeldungen, Änderungen nicht anwenden" - -; VMPRODAUTOPARENTCATEGORIES="TEST Message!!!" -VMPRODPARENTCATS_DEBUG_LOADCATS="%s Kategorien geladen." -VMPRODPARENTCATS_DEBUG_LOADPRODUCTS="%s Produkte geladen." -VMPRODPARENTCATS_DEBUG_ARTICLE_MODIFIED="Artikel "_QQ_"%s"_QQ_" (SKU %s) modifiziert: %d Kategorien hinzugefügt, %d Kategorien entfernt." -VMPRODPARENTCATS_DEBUG_ARTICLE_NOT_MODIFIED="Artikel "_QQ_"%s"_QQ_" (SKU %s) modifiziert: %d Kategorien hinzugefügt, %d Kategorien entfernt." diff --git a/en-GB.plg_system_vmAutoParentCategories.sys.ini b/en-GB.plg_system_vmAutoParentCategories.sys.ini new file mode 100644 index 0000000..e3d7cb6 --- /dev/null +++ b/en-GB.plg_system_vmAutoParentCategories.sys.ini @@ -0,0 +1,48 @@ +; @plugin VmAutoParentCategories +; @copyright Copyright (C) 2012 Reinhold Kainhofer. +; @Website : http://kainhofer.com +; @license - http://www.gnu.org/licenses/gpl.html GNU/GPL + +VMAUTOCATEGORIES_DESCRIPTION="<h2>Virtuemart 2 Auto Product Categories plugin</h2><p style='font-weight: normal'>Automatically add VirtueMart products to all parent categories of the categories assigned to it, or remove all parent categories. Additionally, child products can also be adjusted with their parents' categories, or removed from all categories.</p><p style='font-weight: normal'>To call the plugin explicitly, <a href="_QQ_"?vmAutoParentCategories=run"_QQ_">click here</a>.</p>" + +VMAUTOCATEGORIES_RUN="Run plugin on:" +VMAUTOCATEGORIES_RUN_DESC="Select when the plugin shall be run and automatically modify the VirtueMart categories of your products." + +VMAUTOCATEGORIES_RUN_NEVER="Never" +VMAUTOCATEGORIES_RUN_BACKEND_LOGIN="Joomla Backend login" +VMAUTOCATEGORIES_RUN_VM_STARTPAGE="VirtueMart start page (Backend)" +VMAUTOCATEGORIES_RUN_VM_PRODUCTPAGE="VirtueMart product list page (Backend)" + +VMAUTOCATEGORIES_NORMAL_PRODUCTS="Normal products" +VMAUTOCATEGORIES_NORMAL_PRODUCTS_DESC="Choose how the categories of normal products (i.e. not child products) will be modified." +VMAUTOCATEGORIES_CHILD_PRODUCTS="Child products" +VMAUTOCATEGORIES_CHILD_PRODUCTS_DESC="Choose how the categories of child products will be modified." +VMAUTOCATEGORIES_DO_NOTHING="Do not modify" +VMAUTOCATEGORIES_ADD_PARENTS="Add product to all parent categories of assigned categories" +VMAUTOCATEGORIES_ADD_ONE_PARENT="Add product to leaf category and its parent, remove all others" +VMAUTOCATEGORIES_REMOVE_EXCEPT_LEAF="Remove from all but the lowest categories" +VMAUTOCATEGORIES_COPY_CATEGORIES_FROM_PARENT="Copy categories from parent product" +VMAUTOCATEGORIES_REMOVE_ALL="Remove all categories" + +VMAUTOCATEGORIES_DEBUG="Debug Output" +VMAUTOCATEGORIES_DEBUG_DESC="Select how much information about its operation is displayed by the plugin" +VMAUTOCATEGORIES_DBG_NO_OUTPUT="No output" +VMAUTOCATEGORIES_DBG_REPORT_CHANGES="Report changes" +VMAUTOCATEGORIES_DBG_REPORT_ALWAYS="Report, even if nothing was changed" +VMAUTOCATEGORIES_DBG_REPORT_NO_CHANGES="Report changes, don't apply them" +VMAUTOCATEGORIES_DBG_DEBUG="Verbose debug output" +VMAUTOCATEGORIES_DBG_DEBUG_NO_CHANGES="Verbose debug output, don't apply changes" + +VMAUTOCATEGORIES_PROGRESS_ARTICLES_MODIFIED="Modified categories of %d top-level products" +VMAUTOCATEGORIES_PROGRESS_ARTICLES_NO_MODIFICATIONS="No category modifications to top-level products needed." +VMAUTOCATEGORIES_PROGRESS_CHILDREN_MODIFIED="Modified categories of %d child products" +VMAUTOCATEGORIES_PROGRESS_CHILDREN_NO_MODIFICATIONS="No category modifications to child products needed." +VMAUTOCATEGORIES_PROGRESS_NOTAPPLIED="Changes not applied due to configuration settings." + +VMAUTOCATEGORIES_DEBUG_LOADCATS="Loaded %s categories." +VMAUTOCATEGORIES_DEBUG_LOADPRODUCTS="Loaded %s products." +VMAUTOCATEGORIES_DEBUG_ARTICLE_MODIFIED="Product "_QQ_"%s"_QQ_" (SKU %s) modified: added %d categories, removed %d categories." +VMAUTOCATEGORIES_DEBUG_ARTICLE_NOT_MODIFIED="Product "_QQ_"%s"_QQ_" (SKU %s) was not modified." +VMAUTOCATEGORIES_DEBUG_CHILD_MODIFIED="Child product "_QQ_"%s"_QQ_" (SKU %s) modified: added %d categories, removed %d categories." +VMAUTOCATEGORIES_DEBUG_CHILD_NOT_MODIFIED="Child product "_QQ_"%s"_QQ_" (SKU %s) not modified." +VMAUTOCATEGORIES_DEBUG_CATEGORYMODIFICATIONS=" Hinzugefügte Kategorien: %s. Entfernte Kategorien: %s." diff --git a/en-GB.plg_system_vmProductAutoParentCategories.sys.ini b/en-GB.plg_system_vmProductAutoParentCategories.sys.ini deleted file mode 100644 index 34ad13d..0000000 --- a/en-GB.plg_system_vmProductAutoParentCategories.sys.ini +++ /dev/null @@ -1,33 +0,0 @@ -; @plugin VmProductAutoParentCategories -; @copyright Copyright (C) 2012 Reinhold Kainhofer. -; @Website : http://kainhofer.com -; @license - http://www.gnu.org/licenses/gpl.html GNU/GPL - -VMPRODPARENTCATS_DESCRIPTION="<h2>Virtuemart 2 Product Auto Parent Categories plugin</h2><p style='font-weight: normal'>Automatically add VirtueMart products to all parent categories of the categories assigned to it, or remove all parent categories. Similarly, child products can be adjusted with their parents' categories, or removed from all categories.</p>" - -VMPRODPARENTCATS_RUN="Run plugin on:" -VMPRODPARENTCATS_RUN_DESC="Select when the plugin shall be run and automatically modify the VirtueMart categories of your products. Note that there is currently no way to run the plugin manually!" -VMPRODPARENTCATS_RUN_NEVER="Never" -VMPRODPARENTCATS_RUN_BACKEND_LOGIN="Joomla Backend login" - -VMPRODPARENTCATS_NORMAL_PRODUCTS="Normal products" -VMPRODPARENTCATS_NORMAL_PRODUCTS_DESC="Choose how the categories of normal products (i.e. not child products) will be modified." -VMPRODPARENTCATS_CHILD_PRODUCTS="Child products" -VMPRODPARENTCATS_CHILD_PRODUCTS_DESC="Choose how the categories of child products will be modified." -VMPRODPARENTCATS_DO_NOTHING="Do not modify" -VMPRODPARENTCATS_ADD_PARENTS="Add product to all parent categories of assigned categories" -VMPRODPARENTCATS_ADD_ONE_PARENT="Add product to leaf category and its parent, remove all others" -VMPRODPARENTCATS_REMOVE_EXCEPT_LEAF="Remove from all but the lowest categories" -VMPRODPARENTCATS_COPY_CATEGORIES_FROM_PARENT="Copy categories from parent product" -VMPRODPARENTCATS_REMOVE_ALL="Remove all categories" - -VMPRODPARENTCATS_DEBUG="Debug Output" -VMPRODPARENTCATS_DEBUG_DESC="Select how much information about its operation is displayed by the plugin" -VMPRODPARENTCATS_DBG_NO_OUTPUT="No output" -VMPRODPARENTCATS_DBG_REPORT_CHANGES="Report changes" -VMPRODPARENTCATS_DBG_REPORT_ALWAYS="Report, even if nothing was changed" -VMPRODPARENTCATS_DBG_REPORT_NO_CHANGES="Report changes, don't apply them" -VMPRODPARENTCATS_DBG_DEBUG="Verbose debug output" -VMPRODPARENTCATS_DBG_DEBUG_NO_CHANGES="Verbose debug output, don't apply changes" - -VMPRODPARENTCATS_DEBUG_LOADCATS="Loaded %s categories." diff --git a/vmAutoParentCategories.php b/vmAutoParentCategories.php new file mode 100644 index 0000000..7f764ce --- /dev/null +++ b/vmAutoParentCategories.php @@ -0,0 +1,267 @@ +<?php +/** + * @plugin VMAutoParentCategories + * @copyright Copyright (C) 2012 Reinhold Kainhofer - All rights reserved. + * @Website : http://www.kainhofer.com + * @license - http://www.gnu.org/licenses/gpl.html GNU/GPL + **/ + +defined( '_JEXEC' ) or die( 'Restricted access' ); +JFactory::getLanguage()->load('plg_system_vmAutoParentCategories.sys'); + +jimport('joomla.event.plugin'); +class plgSystemVMAutoParentCategories extends JPlugin { + var $_dbg = 'report_changes'; + var $_apply = TRUE; + var $_report = TRUE; + var $_debug = FALSE; + + function initSettings() { + $this->_dbg = $this->params->get('debug','report_changes'); + + $this->_apply = TRUE; // Whether to apply the changes at all + $this->_report = TRUE; // Whether to report changes + $this->_debug = FALSE; // Verbose debug output? + switch ($this->_dbg) { + case 'no_output': + $this->_report = FALSE; + break; + case 'report_changes': + break; + case 'report_always': + break; + case 'report_no_change': + $this->_apply = FALSE; + break; + case 'debug': + $this->_debug = TRUE; + break; + case 'debug_no_changes': + $this->_apply = FALSE; + $this->_debug = TRUE; + break; + } + } + function checkForTrigger() { + $trigger = $this->params->get('run_trigger', 'never'); + // Explicit call will ALWAYS trigger + if (JRequest::getCmd('vmAutoParentCategories') == 'run') { + return TRUE; + } + if ($trigger == 'never') + return FALSE; + + $option = JRequest::getCmd('option'); + $view = JRequest::getCmd('view'); + $task = JRequest::getCmd('task'); + $run = FALSE; + if ($option == 'com_virtuemart') { + if ($trigger == 'virtuemart_startpage') { + $run |= (empty($view) or ($view=='virtuemart')); + } + if ($trigger == 'virtuemart_products') { + $run |= ($view=='product' and empty($task)); + } + } + return $run; + } + function onAfterRoute(){ + /** Alternatively you may use chaining */ + if(!JFactory::getApplication()->isAdmin()) return; + if ($this->checkForTrigger()) { + $this->initSettings(); + $this->updateCategories(); + } + } + + function debugMessage ($msg) { + if ($this->_debug) { + $app = JFactory::getApplication(); + $app->enqueueMessage($msg, 'message'); + } + } + function progressMessage ($msg) { + if ($this->_report) { + $app = JFactory::getApplication(); + $app->enqueueMessage($msg, 'message'); + } + } + + function getCategoriesAllParents($categories, $catparents) { + $newcats=[]; + foreach ($categories as $c) { + $newcats[$c]=1; + $c1=$c; + while ($catparents[$c1]) { + $c1=$catparents[$c1]; + $newcats[$c1]=1; + } + } + return array_keys($newcats); + } + function getCategoriesOnlyLeaf($categories, $catparents) { + $newcats=array_flip($categories); + foreach ($categories as $c) { + $c1=$c; + while ($catparents[$c1]) { + $c1=$catparents[$c1]; + if (isset($newcats[$c1])) { + unset ($newcats[$c1]); + } + } + } + return array_keys($newcats); + } + function getCategoriesOneParent($categories, $catparents) { + $newcats = array_flip($this->getCategoriesOnlyLeaf($categories, $catparents)); + foreach (array_keys($newcats) as $c) { + if (isset($catparents[$c]) and $catparents[$c]) { + $newcats[$catparents[$c]]=1; + } + } + return array_keys($newcats); + } + + function getProductTopParent($product, $products) { + $p = $product; + while ($p->product_parent_id>0) { + $p = $products[$p->product_parent_id]; + } + return $p; + } + function getProductCategories ($virtuemart_product_id = 0) { + $categories = array(); + if ($virtuemart_product_id > 0) { + $q = 'SELECT `virtuemart_category_id` FROM `#__virtuemart_product_categories` WHERE `virtuemart_product_id` = ' . (int)$virtuemart_product_id; + $db = JFactory::getDBO (); + $db->setQuery ($q); + $categories = $db->loadResultArray (); + } + return $categories; + } + function getAllProducts () { + $q = 'SELECT `virtuemart_product_id`, `product_parent_id`, `product_sku`, `l`.`product_name` as `product_name`, `l`.`slug` as `slug` from #__virtuemart_products JOIN `#__virtuemart_products_' . VMLANG . '` as `l` using (`virtuemart_product_id`);'; + $db = JFactory::getDbo (); + $db->setQuery ($q); + $products = $db->loadObjectList ('virtuemart_product_id'); + return $products; + } + function updateProductCategories($virtuemart_product_id, $added, $removed) { + $db = JFactory::getDbo (); + + $sqladd = 'INSERT INTO `#__virtuemart_product_categories` (`virtuemart_product_id`, `virtuemart_category_id`) VALUES ("'.$virtuemart_product_id.'", "%d");'; + foreach ($added as $c) { + $db->setQuery(sprintf($sqladd, $c)); + $db->query (); + $err = $db->getErrorMsg (); + if (!empty($err)) { + vmError ("Database error: Unable to insert product category $c for product $virtuemart_product_id: " . $err); + } + } + + $sqlremove = 'DELETE FROM `#__virtuemart_product_categories` WHERE `virtuemart_product_id`="'.$virtuemart_product_id.'" AND `virtuemart_category_id`="%d";'; + foreach ($removed as $c) { + $db->setQuery(sprintf($sqlremove, $c)); + $db->query (); + $err = $db->getErrorMsg (); + if (!empty($err)) { + vmError ('Database error: Unable to remove product category $c for product $virtuemart_product_id: ' . $err); + } + } + } + + + function updateCategories() { + if (!class_exists( 'VmConfig' )) + require(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php'); + $config = VmConfig::loadConfig(); + $prodaction = $this->params->get('normal_products', 'nothing'); + $childprodaction = $this->params->get('child_products', 'nothing'); + + $app = JFactory::getApplication(); + $catmodel = VmModel::getModel('category'); + $cattree = $catmodel->getCategoryTree(); + + // Store the names and parents for each category id + foreach ($cattree as $cat) { + $catnames[$cat->virtuemart_category_id] = $cat->category_name; + $catparents[$cat->virtuemart_category_id] = $cat->category_parent_id; + } + $this->debugMessage(JText::sprintf('VMAUTOCATEGORIES_DEBUG_LOADCATS', count($cattree))); + + $products=$this->getAllProducts(); + $this->debugMessage(JText::sprintf('VMAUTOCATEGORIES_DEBUG_LOADPRODUCTS', count($products))); + // First, look only at parent products + $modified = 0; + foreach ($products as $p) { + if ($p->product_parent_id) continue; + $cats = $this->getProductCategories($p->virtuemart_product_id); + $newcats = $cats; + switch ($prodaction) { + case 'nothing': continue; + case 'add_parents': $newcats = $this->getCategoriesAllParents($cats, $catparents); break; + case 'add_two_leaves': $newcats = $this->getCategoriesOneParent($cats, $catparents); break; + case 'remove_except_leaf': $newcats = $this->getCategoriesOnlyLeaf($cats, $catparents); break; + } + $added=array_diff($newcats,$cats); + $removed=array_diff($cats,$newcats); + if (!empty($added) or !empty($removed)) { + $modified++; + $p->categories = $newcats; + $this->debugMessage(JText::sprintf('VMAUTOCATEGORIES_DEBUG_ARTICLE_MODIFIED', $p->product_name, $p->product_sku, count($added), count($removed))); + $this->debugMessage(JText::sprintf('VMAUTOCATEGORIES_DEBUG_CATEGORYMODIFICATIONS', implode(", ", $added), implode(", ", $removed))); + if ($this->_apply) { + $this->updateProductCategories($p->virtuemart_product_id, $added, $removed); + } else { + $this->progressMessage(JText::_('VMAUTOCATEGORIES_PROGRESS_NOTAPPLIED')); + } + } else { + $this->debugMessage(JText::sprintf('VMAUTOCATEGORIES_DEBUG_ARTICLE_NOT_MODIFIED', $p->product_name, $p->product_sku)); + } + } + if ($modified>0) { + $this->progressMessage(JText::sprintf('VMAUTOCATEGORIES_PROGRESS_ARTICLES_MODIFIED', $modified)); + } elseif (in_array ($this->_dbg, ['report_always', 'report_no_change', 'debug', 'debug_no_changes'])) { + $this->progressMessage(JText::sprintf('VMAUTOCATEGORIES_PROGRESS_ARTICLES_NO_MODIFICATIONS')); + } + + $childrenmodified=0; + // Now look at the child products and modify them accordingly + foreach ($products as $p) { + if (!$p->product_parent_id) continue; + $topparent = $this->getProductTopParent($p, $products); + $cats = $this->getProductCategories($p->virtuemart_product_id); + $newcats = $cats; + switch ($childprodaction) { + case 'nothing': continue; + case 'add_parents': $newcats = $this->getCategoriesAllParents($cats, $catparents); break; + case 'add_two_leaves': $newcats = $this->getCategoriesOneParent($cats, $catparents); break; + case 'remove_except_leaf': $newcats = $this->getCategoriesOnlyLeaf($cats, $catparents); break; + case 'copy_parent': $newcats = $this->getProductCategories($topparent->virtuemart_product_id); break; + case 'remove_all': $newcats = []; break; + } + $added=array_diff($newcats,$cats); + $removed=array_diff($cats,$newcats); + if (!empty($added) or !empty($removed)) { + $childrenmodified++; + $p->categories = $newcats; + $this->debugMessage(JText::sprintf('VMAUTOCATEGORIES_DEBUG_CHILD_MODIFIED', $p->product_name, $p->product_sku, count($added), count($removed))); + $this->debugMessage(JText::sprintf('VMAUTOCATEGORIES_DEBUG_CATEGORYMODIFICATIONS', implode(", ", $added), implode(", ", $removed))); + if ($this->_apply) { + $this->updateProductCategories($p->virtuemart_product_id, $added, $removed); + } else { + $this->progressMessage(JText::_('VMAUTOCATEGORIES_PROGRESS_NOTAPPLIED')); + } + } else { + $this->debugMessage(JText::sprintf('VMAUTOCATEGORIES_DEBUG_CHILD_NOT_MODIFIED', $p->product_name, $p->product_sku)); + } + } + if ($childrenmodified>0) { + $this->progressMessage(JText::sprintf('VMAUTOCATEGORIES_PROGRESS_CHILDREN_MODIFIED', $childrenmodified)); + } elseif (in_array ($this->_dbg, ['report_always', 'report_no_change', 'debug', 'debug_no_changes'])) { + $this->progressMessage(JText::sprintf('VMAUTOCATEGORIES_PROGRESS_CHILDREN_NO_MODIFICATIONS')); + } + } +} + + diff --git a/vmAutoParentCategories.xml b/vmAutoParentCategories.xml new file mode 100644 index 0000000..cdc97d5 --- /dev/null +++ b/vmAutoParentCategories.xml @@ -0,0 +1,61 @@ +<?xml version="1.0" encoding="utf-8"?> +<install version="2.0" type="plugin" group="system" method="upgrade"> + <name>System - VM Auto Parent Categories</name> + <creationDate>2012-12-18</creationDate> + <author>Reinhold Kainhofer</author> + <copyright>Copyright (C) 2012 Reinhold Kainhofer.</copyright> + <license>http://www.gnu.org/licenses/gpl.html GNU/GPL v3+</license> + <authorEmail>reinhold@kainhofer.com/</authorEmail> + <authorUrl>http://kainhofer.com</authorUrl> + <version>0.0.2</version> + <description>VMAUTOCATEGORIES_DESCRIPTION</description> + + <files> + <filename plugin="vmAutoParentCategories">vmAutoParentCategories.php</filename> + <filename plugin="vmAutoParentCategories">index.html</filename> + </files> + <languages> + <language tag="en-GB">en-GB.plg_system_vmAutoParentCategories.ini</language> + <language tag="de-DE">de-DE.plg_system_vmAutoParentCategories.ini</language> + </languages> + + <config> + <fields name="params"> + + <fieldset name="basic"> + <field name="run_trigger" type="list" default="never" label="VMAUTOCATEGORIES_RUN" description ="VMAUTOCATEGORIES_RUN_DESC" > + <option value="never">VMAUTOCATEGORIES_RUN_NEVER</option> +<!-- <option value="backend-login">VMAUTOCATEGORIES_RUN_BACKEND_LOGIN</option> --> + <option value="virtuemart_startpage">VMAUTOCATEGORIES_RUN_VM_STARTPAGE</option> + <option value="virtuemart_products">VMAUTOCATEGORIES_RUN_VM_PRODUCTPAGE</option> + </field> + <field name="normal_products" type="list" default="nothing" label="VMAUTOCATEGORIES_NORMAL_PRODUCTS" description ="VMAUTOCATEGORIES_NORMAL_PRODUCTS_DESC" > + <option value="nothing">VMAUTOCATEGORIES_DO_NOTHING</option> + <option value="add_parents">VMAUTOCATEGORIES_ADD_PARENTS</option> + <option value="add_two_leaves">VMAUTOCATEGORIES_ADD_ONE_PARENT</option> + <option value="remove_except_leaf">VMAUTOCATEGORIES_REMOVE_EXCEPT_LEAF</option> + </field> + <field name="child_products" type="list" default="nothing" label="VMAUTOCATEGORIES_CHILD_PRODUCTS" description ="VMAUTOCATEGORIES_CHILD_PRODUCTS_DESC" > + <option value="nothing">VMAUTOCATEGORIES_DO_NOTHING</option> + <option value="copy_parent">VMAUTOCATEGORIES_COPY_CATEGORIES_FROM_PARENT</option> + <option value="remove_all">VMAUTOCATEGORIES_REMOVE_ALL</option> + <option value="add_parents">VMAUTOCATEGORIES_ADD_PARENTS</option> + <option value="add_two_leaves">VMAUTOCATEGORIES_ADD_ONE_PARENT</option> + <option value="remove_except_leaf">VMAUTOCATEGORIES_REMOVE_EXCEPT_LEAF</option> + </field> + <field name="debug" type="list" default="report_changes" label="VMAUTOCATEGORIES_DEBUG" description ="VMAUTOCATEGORIES_DEBUG_DESC" > + <option value="no_output">VMAUTOCATEGORIES_DBG_NO_OUTPUT</option> + <option value="report_changes">VMAUTOCATEGORIES_DBG_REPORT_CHANGES</option> + <option value="report_always">VMAUTOCATEGORIES_DBG_REPORT_ALWAYS</option> + <option value="report_no_change">VMAUTOCATEGORIES_DBG_REPORT_NO_CHANGES</option> + <option value="debug">VMAUTOCATEGORIES_DBG_DEBUG</option> + <option value="debug_no_changes">VMAUTOCATEGORIES_DBG_DEBUG_NO_CHANGES</option> + </field> + </fieldset> + </fields> + </config> +</install> + + + + diff --git a/vmProductAutoParentCategories.php b/vmProductAutoParentCategories.php deleted file mode 100644 index 4e009b4..0000000 --- a/vmProductAutoParentCategories.php +++ /dev/null @@ -1,201 +0,0 @@ -<?php -/** - * @plugin VMProductAutoParentCategories - * @copyright Copyright (C) 2012 Reinhold Kainhofer - All rights reserved. - * @Website : http://www.kainhofer.com - * @license - http://www.gnu.org/licenses/gpl.html GNU/GPL - **/ - -defined( '_JEXEC' ) or die( 'Restricted access' ); -JFactory::getLanguage()->load('plg_system_vmProductAutoParentCategories.sys'); - -jimport('joomla.event.plugin'); -class plgSystemVMProductAutoParentCategories extends JPlugin { - var $_dbg = 'report_changes'; - var $_apply = TRUE; - var $_report = TRUE; - var $_debug = FALSE; - - function initSettings() { - $this->_dbg = $this->params->get('debug','report_changes'); - - $this->_apply = TRUE; // Whether to apply the changes at all - $this->_report = TRUE; // Whether to report changes - $this->_debug = FALSE; // Verbose debug output? - switch ($this->_dbg) { - case 'no_output': - $this->_report = FALSE; - break; - case 'report_changes': - break; - case 'report_always': - break; - case 'report_no_change': - $this->_apply = FALSE; - break; - case 'debug': - $this->_debug = TRUE; - break; - case 'debug_no_changes': - $this->_apply = FALSE; - $this->_debug = TRUE; - break; - } -// print("Settings: _dbg=$this->_dbg, _prodaction=$this->_prodaction, _childprodaction=$this->_childprodaction, _apply=$this->_apply, _report=$this->_report, _debug=$this->_debug"); - } - function onAfterRoute(){ - /** Alternatively you may use chaining */ - if(!JFactory::getApplication()->isAdmin()) return; - $option = JRequest::getCmd('option'); - if($option != 'com_virtuemart') return; - $this->initSettings(); - $this->updateCategories(); - } - - function onLoginUser(){ - /** Alternatively you may use chaining */ - if(!JFactory::getApplication()->isAdmin()) return; - JFactory::getApplication()->enqueueMessage("onLoginUser", 'message'); - $this->initSettings(); - } - - - function debugMessage ($msg) { - if ($this->_debug) { - $app = JFactory::getApplication(); - $app->enqueueMessage($msg, 'message'); - } - } - function progressMessage ($msg) { - if ($this->_report) { - $app = JFactory::getApplication(); - $app->enqueueMessage($msg, 'message'); - } - } - - function getCategoriesAllParents($categories, $catparents) { - $newcats=[]; - foreach ($categories as $c) { - $newcats[$c]=1; - $c1=$c; - while ($catparents[$c1]) { - $c1=$catparents[$c1]; - $newcats[$c1]=1; - } - } - return array_keys($newcats); - } - function getCategoriesOnlyLeaf($categories, $catparents) { - $newcats=array_flip($categories); - foreach ($categories as $c) { - $c1=$c; - while ($catparents[$c1]) { - $c1=$catparents[$c1]; - if (isset($newcats[$c1])) { - unset ($newcats[$c1]); - } - } - } - return array_keys($newcats); - } - function getCategoriesOneParent($categories, $catparents) { - $newcats = array_flip($this->getCategoriesOnlyLeaf($categories, $catparents)); - foreach (array_keys($newcats) as $c) { - if (isset($catparents[$c]) and $catparents[$c]) { - $newcats[$catparents[$c]]=1; - } - } - return array_keys($newcats); - } - - function getProductTopParent($product, $products) { - $p = $product; - while ($p->product_parent_id) { - $p = $products[$p->product_parent_id]; - } - return $p; - } - - function updateCategories() { - if (!class_exists( 'VmConfig' )) - require(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php'); - $config = VmConfig::loadConfig(); - $prodaction = $this->params->get('normal_products', 'nothing'); - $childprodaction = $this->params->get('child_products', 'nothing'); - - $app = JFactory::getApplication(); - $catmodel = VmModel::getModel('category'); - $cattree = $catmodel->getCategoryTree(); - - // Store the names and parents for each category id - foreach ($cattree as $cat) { - $catnames[$cat->virtuemart_category_id] = $cat->category_name; - $catparents[$cat->virtuemart_category_id] = $cat->category_parent_id; - } - $this->debugMessage(JText::sprintf('VMPRODPARENTCATS_DEBUG_LOADCATS', count($cattree))); - - $productmodel = VmModel::getModel('product'); - $productmodel->_noLimit = true; - $products = []; - // Create an array of products, indexed by their VM id: - foreach ($productmodel->getProductListing() as $p) { - $products[$p->virtuemart_product_id] = $p; - } - $this->debugMessage(JText::sprintf('VMPRODPARENTCATS_DEBUG_LOADPRODUCTS', count($products))); - // First, look only at parent products - $modified = 0; - foreach ($products as $p) { - if ($p->product_parent_id) continue; - $cats = $p->categories; - $newcats = $cats; - switch ($prodaction) { - case 'nothing': continue; - case 'add_parents': $newcats = $this->getCategoriesAllParents($cats, $catparents); break; - case 'add_two_leaves': $newcats = $this->getCategoriesOneParent($cats, $catparents); break; - case 'remove_except_leaf': $newcats = $this->getCategoriesOnlyLeaf($cats, $catparents); break; - } - $added=array_diff($newcats,$cats); - $removed=array_diff($cats,$newcat); - if (!empty($added) and !empty($removed)) { - - $p->categories = $newcats; - $productmodel->store($p); -VMPRODPARENTCATS_DEBUG_ARTICLE_MODIFIED="Artikel "_QQ_"%s"_QQ_" (SKU %s) modifiziert: %d Kategorien hinzugefügt, %d Kategorien entfernt." - } else { -VMPRODPARENTCATS_DEBUG_ARTICLE_MODIFIED="Artikel "_QQ_"%s"_QQ_" (SKU %s) modifiziert: %d Kategorien hinzugefügt, %d Kategorien entfernt." - -print("Product: $p->product_name"); -print("Added categories: ");print_r($added); -print("Removed categories: ");print_r($removed); - // TODO: Assign the new categories, print out debug statement, store the changes! -print("Old categories: "); print_r($cats); -print("New categories: "); print_r($newcats); - } - - // Now look at the child products and modify them accordingly - foreach ($products as $p) { - if (!$p->product_parent_id) continue; - $topparent = $this->getProductTopParent($p, $products); - $cats = $p->categories; - $newcats = $cats; -// print("Action: $prodaction"); - switch ($childprodaction) { - case 'nothing': continue; - case 'add_parents': $newcats = $this->getCategoriesAllParents($cats, $catparents); break; - case 'add_two_leaves': $newcats = $this->getCategoriesOneParent($cats, $catparents); break; - case 'remove_except_leaf': $newcats = $this->getCategoriesOnlyLeaf($cats, $catparents); break; - case 'copy_parent': $newcats = $topparent->categories; break; - case 'remove_all': $newcats = []; break; - } -print("Old categories: "); print_r($cats); -print("New categories: "); print_r($newcats); - // TODO: Assign the new categories, print out debug statement, store the changes! - } - - $app->enqueueMessage(JText::_('VMPRODAUTOPARENTCATEGORIES'), 'message'); - - } - -} - - diff --git a/vmProductAutoParentCategories.xml b/vmProductAutoParentCategories.xml deleted file mode 100644 index faecfb1..0000000 --- a/vmProductAutoParentCategories.xml +++ /dev/null @@ -1,59 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<install version="2.0" type="plugin" group="system" method="upgrade"> - <name>System - VM Product Auto Parent Categories</name> - <creationDate>2012-12-16</creationDate> - <author>Reinhold Kainhofer</author> - <copyright>Copyright (C) 2012 Reinhold Kainhofer.</copyright> - <license>http://www.gnu.org/licenses/gpl.html GNU/GPL v3+</license> - <authorEmail>reinhold@kainhofer.com/</authorEmail> - <authorUrl>http://kainhofer.com</authorUrl> - <version>0.0.1</version> - <description>VMPRODPARENTCATS_DESCRIPTION</description> - - <files> - <filename plugin="vmProductAutoParentCategories">vmProductAutoParentCategories.php</filename> - <filename plugin="vmProductAutoParentCategories">index.html</filename> - </files> - <languages> - <language tag="en-GB">en-GB.plg_system_vmProductAutoParentCategories.ini</language> - <language tag="de-DE">de-DE.plg_system_vmProductAutoParentCategories.ini</language> - </languages> - - <config> - <fields name="params"> - - <fieldset name="basic"> - <field name="run_trigger" type="list" default="never" label="VMPRODPARENTCATS_RUN" description ="VMPRODPARENTCATS_RUN_DESC" > - <option value="never">VMPRODPARENTCATS_RUN_NEVER</option> - <option value="backend-login">VMPRODPARENTCATS_RUN_BACKEND_LOGIN</option> - </field> - <field name="normal_products" type="list" default="nothing" label="VMPRODPARENTCATS_NORMAL_PRODUCTS" description ="VMPRODPARENTCATS_NORMAL_PRODUCTS_DESC" > - <option value="nothing">VMPRODPARENTCATS_DO_NOTHING</option> - <option value="add_parents">VMPRODPARENTCATS_ADD_PARENTS</option> - <option value="add_two_leaves">VMPRODPARENTCATS_ADD_ONE_PARENT</option> - <option value="remove_except_leaf">VMPRODPARENTCATS_REMOVE_EXCEPT_LEAF</option> - </field> - <field name="child_products" type="list" default="nothing" label="VMPRODPARENTCATS_CHILD_PRODUCTS" description ="VMPRODPARENTCATS_CHILD_PRODUCTS_DESC" > - <option value="nothing">VMPRODPARENTCATS_DO_NOTHING</option> - <option value="copy_parent">VMPRODPARENTCATS_COPY_CATEGORIES_FROM_PARENT</option> - <option value="remove_all">VMPRODPARENTCATS_REMOVE_ALL</option> - <option value="add_parents">VMPRODPARENTCATS_ADD_PARENTS</option> - <option value="add_two_leaves">VMPRODPARENTCATS_ADD_ONE_PARENT</option> - <option value="remove_except_leaf">VMPRODPARENTCATS_REMOVE_EXCEPT_LEAF</option> - </field> - <field name="debug" type="list" default="report_changes" label="VMPRODPARENTCATS_DEBUG" description ="VMPRODPARENTCATS_DEBUG_DESC" > - <option value="no_output">VMPRODPARENTCATS_DBG_NO_OUTPUT</option> - <option value="report_changes">VMPRODPARENTCATS_DBG_REPORT_CHANGES</option> - <option value="report_always">VMPRODPARENTCATS_DBG_REPORT_ALWAYS</option> - <option value="report_no_change">VMPRODPARENTCATS_DBG_REPORT_NO_CHANGES</option> - <option value="debug">VMPRODPARENTCATS_DBG_DEBUG</option> - <option value="debug_no_changes">VMPRODPARENTCATS_DBG_DEBUG_NO_CHANGES</option> - </field> - </fieldset> - </fields> - </config> -</install> - - - - -- GitLab