From e79d75b1eeb2b793c1f27b7ce00784a12b3cb821 Mon Sep 17 00:00:00 2001 From: fritzmg Date: Sat, 13 Feb 2016 16:08:39 +0100 Subject: [PATCH] introduced inheritance priority --- README.md | 2 +- composer.json | 5 +- .../inherit_article/config/autoload.php | 2 +- .../inherit_article/dca/tl_article.php | 18 ++++---- .../languages/de/tl_article.php | 4 +- .../languages/en/tl_article.php | 4 +- .../inherit_article/models/ArticleModel.php | 46 +++++++++---------- .../models/ArticleModel_C35.php | 43 +++++++++-------- .../inherit_article/runonce/runonce.php | 45 ++++++++++++++++++ 9 files changed, 111 insertions(+), 58 deletions(-) create mode 100644 system/modules/inherit_article/runonce/runonce.php diff --git a/README.md b/README.md index 956d07a..7f5b1f6 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Simple extension to replace the `\ArticleModel` in order to allow inheritable ar Since version `1.1.0` you can also optionally set a maximum inheritance level. e.g. a maximum inheritance of `2` means that the article will only be inherited down two levels of the page hierarchy. -Since version `1.2.0` you can also let the inherited article to be added after the other articles. +Since version `1.2.0` you can also let the inherited article to be added after the other articles. This has changed to a priority setting in version `1.3.0`. A negative priority means, that the inherited article is put after regular ones. ![Article settings](https://raw.githubusercontent.com/fritzmg/contao-inherit-article/master/inherit_article.png) diff --git a/composer.json b/composer.json index e38d09b..8407226 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,10 @@ "contao":{ "sources":{ "system/modules/inherit_article":"system/modules/inherit_article" - } + }, + "runonce":[ + "system/modules/inherit_article/runonce/runonce.php" + ] } } } \ No newline at end of file diff --git a/system/modules/inherit_article/config/autoload.php b/system/modules/inherit_article/config/autoload.php index 15569f8..a9810b8 100644 --- a/system/modules/inherit_article/config/autoload.php +++ b/system/modules/inherit_article/config/autoload.php @@ -5,7 +5,7 @@ * * Extension to replace the ArticleModel in order to allow inheritable articles * - * @copyright inspiredminds 2015 + * @copyright inspiredminds 2016 * @package inherit_article * @link http://www.inspiredminds.at * @author Fritz Michael Gschwantner diff --git a/system/modules/inherit_article/dca/tl_article.php b/system/modules/inherit_article/dca/tl_article.php index 25cdaa1..10cf498 100644 --- a/system/modules/inherit_article/dca/tl_article.php +++ b/system/modules/inherit_article/dca/tl_article.php @@ -5,7 +5,7 @@ * * Extension to replace the ArticleModel in order to allow inheritable articles * - * @copyright inspiredminds 2015 + * @copyright inspiredminds 2016 * @package inherit_article * @link http://www.inspiredminds.at * @author Fritz Michael Gschwantner @@ -16,7 +16,9 @@ /** * Add palettes to tl_article */ -$GLOBALS['TL_DCA']['tl_article']['palettes']['default'] = str_replace( '{publish_legend', '{inherit_legend:hide},inherit,inheritLevel,inheritAfter;{publish_legend', $GLOBALS['TL_DCA']['tl_article']['palettes']['default']); +$GLOBALS['TL_DCA']['tl_article']['palettes']['default'] = str_replace( '{publish_legend', '{inherit_legend:hide},inherit;{publish_legend', $GLOBALS['TL_DCA']['tl_article']['palettes']['default']); +$GLOBALS['TL_DCA']['tl_article']['palettes']['__selector__'][] = 'inherit'; +$GLOBALS['TL_DCA']['tl_article']['subpalettes']['inherit'] = 'inheritLevel,inheritPriority'; /** * Add fields to tl_article @@ -26,7 +28,7 @@ 'exclude' => true, 'label' => &$GLOBALS['TL_LANG']['tl_article']['inherit'], 'inputType' => 'checkbox', - 'eval' => array('tl_class'=>'w50 m12'), + 'eval' => array('submitOnChange'=>true), 'sql' => "char(1) NOT NULL default ''" ); @@ -39,11 +41,11 @@ 'sql' => "smallint(5) unsigned NOT NULL default '0'" ); -$GLOBALS['TL_DCA']['tl_article']['fields']['inheritAfter'] = array +$GLOBALS['TL_DCA']['tl_article']['fields']['inheritPriority'] = array ( - 'label' => &$GLOBALS['TL_LANG']['tl_article']['inheritAfter'], + 'label' => &$GLOBALS['TL_LANG']['tl_article']['inheritPriority'], 'exclude' => true, - 'inputType' => 'checkbox', - 'eval' => array('tl_class'=>'w50'), - 'sql' => "char(1) NOT NULL default ''" + 'inputType' => 'text', + 'eval' => array('maxlength'=>5, 'rgxp'=>'digit', 'tl_class'=>'w50'), + 'sql' => "smallint(5) NOT NULL default '0'" ); diff --git a/system/modules/inherit_article/languages/de/tl_article.php b/system/modules/inherit_article/languages/de/tl_article.php index b6c2572..cb2cd7f 100644 --- a/system/modules/inherit_article/languages/de/tl_article.php +++ b/system/modules/inherit_article/languages/de/tl_article.php @@ -5,7 +5,7 @@ * * Extension to replace the ArticleModel in order to allow inheritable articles * - * @copyright inspiredminds 2015 + * @copyright inspiredminds 2016 * @package inherit_article * @link http://www.inspiredminds.at * @author Fritz Michael Gschwantner @@ -16,4 +16,4 @@ $GLOBALS['TL_LANG']['tl_article']['inherit_legend'] = 'Vererbung'; $GLOBALS['TL_LANG']['tl_article']['inherit'] = array('Vererben','Artikel in der Seitenstruktur nach unten vererben.'); $GLOBALS['TL_LANG']['tl_article']['inheritLevel'] = array('Maximale Vererbung','Anzahl an Seitenebenen, die der Artikel maximal nach unten vererbt wird (0 = kein Limit).'); -$GLOBALS['TL_LANG']['tl_article']['inheritAfter'] = array('Am Ende hinzufügen','Fügt diesen Artikel hinter den anderen an, wenn er vererbt wird.'); +$GLOBALS['TL_LANG']['tl_article']['inheritPriority'] = array('Priorität','Legt die Reihenfolge der kombinierten Artikel fest (negative Werte benutzen, um vererbte Artikel hinten an zu stellen).'); diff --git a/system/modules/inherit_article/languages/en/tl_article.php b/system/modules/inherit_article/languages/en/tl_article.php index 033df0d..db1e81e 100644 --- a/system/modules/inherit_article/languages/en/tl_article.php +++ b/system/modules/inherit_article/languages/en/tl_article.php @@ -5,7 +5,7 @@ * * Extension to replace the ArticleModel in order to allow inheritable articles * - * @copyright inspiredminds 2015 + * @copyright inspiredminds 2016 * @package inherit_article * @link http://www.inspiredminds.at * @author Fritz Michael Gschwantner @@ -16,4 +16,4 @@ $GLOBALS['TL_LANG']['tl_article']['inherit_legend'] = 'Inheritance'; $GLOBALS['TL_LANG']['tl_article']['inherit'] = array('Inherit','Inherit the article downwards in the page hierarchy.'); $GLOBALS['TL_LANG']['tl_article']['inheritLevel'] = array('Maximum inheritance','Number of pages the article is inherited downwards in the hierarchy (0 = all).'); -$GLOBALS['TL_LANG']['tl_article']['inheritAfter'] = array('Add to end','Adds this inherited article to the end of the others.'); +$GLOBALS['TL_LANG']['tl_article']['inheritPriority'] = array('Priority','Determines the order of the combined articles (use negative values to put inherited articles to the bottom).'); diff --git a/system/modules/inherit_article/models/ArticleModel.php b/system/modules/inherit_article/models/ArticleModel.php index 09096ea..dde4acc 100644 --- a/system/modules/inherit_article/models/ArticleModel.php +++ b/system/modules/inherit_article/models/ArticleModel.php @@ -126,9 +126,6 @@ public static function findPublishedByPidAndColumn($intPid, $strColumn, array $a // restore current parent id $intCurrentPid = $arrParent[0]; - // aggregated collection - $objCollectionTotal = null; - // all models $arrCombinedModels = array(); @@ -156,34 +153,37 @@ public static function findPublishedByPidAndColumn($intPid, $strColumn, array $a // get the models $arrModels = $objCollection->getModels(); - // don't do anything on level 0 - if( $level == 0 ) - { - $arrCombinedModels = $arrModels; - } - else + // process each model + foreach( $arrModels as $objModel ) { - $arrInheritAfter = array(); - $arrInheritBefore = array(); - - // go through each model - foreach( $arrModels as $objModel ) - { - if( $objModel->inheritAfter ) - $arrInheritAfter[] = $objModel; - else - $arrInheritBefore[] = $objModel; - } - - $arrCombinedModels = array_merge( $arrCombinedModels, $arrInheritAfter ); - $arrCombinedModels = array_merge( $arrInheritBefore, $arrCombinedModels ); + // save the level (needed for sorting) + $objModel->level = $level; + + // level 0 is also always priority 0 + if( $level == 0 ) + $objModel->inheritPriority = 0; } + + // combine models + $arrCombinedModels = array_merge( $arrCombinedModels, $arrModels ); } // increase level ++$level; } + // sort + usort( $arrCombinedModels, function( $a, $b ) + { + if( $a->inherit == $b->inherit && $a->inheritPriority == $b->inheritPriority && $a->level == $b->level ) + return $a->sorting - $b->sorting; + if( $a->inherit == $b->inherit && $a->inheritPriority == $b->inheritPriority ) + return $b->level - $a->level; + if( $a->inheritPriority == $b->inheritPriority ) + return $b->inherit - $a->inherit; + return $b->inheritPriority - $a->inheritPriority; + }); + // return the combined collection return $arrCombinedModels ? new \Model\Collection( $arrCombinedModels, $t ) : null; } diff --git a/system/modules/inherit_article/models/ArticleModel_C35.php b/system/modules/inherit_article/models/ArticleModel_C35.php index aff0f88..c957290 100644 --- a/system/modules/inherit_article/models/ArticleModel_C35.php +++ b/system/modules/inherit_article/models/ArticleModel_C35.php @@ -244,34 +244,37 @@ public static function findPublishedByPidAndColumn($intPid, $strColumn, array $a // get the models $arrModels = $objCollection->getModels(); - // don't do anything on level 0 - if( $level == 0 ) + // process each model + foreach( $arrModels as $objModel ) { - $arrCombinedModels = $arrModels; - } - else - { - $arrInheritAfter = array(); - $arrInheritBefore = array(); - - // go through each model - foreach( $arrModels as $objModel ) - { - if( $objModel->inheritAfter ) - $arrInheritAfter[] = $objModel; - else - $arrInheritBefore[] = $objModel; - } - - $arrCombinedModels = array_merge( $arrCombinedModels, $arrInheritAfter ); - $arrCombinedModels = array_merge( $arrInheritBefore, $arrCombinedModels ); + // save the level (needed for sorting) + $objModel->level = $level; + + // level 0 is also always priority 0 + if( $level == 0 ) + $objModel->inheritPriority = 0; } + + // combine models + $arrCombinedModels = array_merge( $arrCombinedModels, $arrModels ); } // increase level ++$level; } + // sort + usort( $arrCombinedModels, function( $a, $b ) + { + if( $a->inherit == $b->inherit && $a->inheritPriority == $b->inheritPriority && $a->level == $b->level ) + return $a->sorting - $b->sorting; + if( $a->inherit == $b->inherit && $a->inheritPriority == $b->inheritPriority ) + return $b->level - $a->level; + if( $a->inheritPriority == $b->inheritPriority ) + return $b->inherit - $a->inherit; + return $b->inheritPriority - $a->inheritPriority; + }); + // return the combined collection return $arrCombinedModels ? new \Model\Collection( $arrCombinedModels, $t ) : null; } diff --git a/system/modules/inherit_article/runonce/runonce.php b/system/modules/inherit_article/runonce/runonce.php new file mode 100644 index 0000000..bd6f83b --- /dev/null +++ b/system/modules/inherit_article/runonce/runonce.php @@ -0,0 +1,45 @@ + + * @license GPL-2.0 + */ + + +/** + * Runonce for autoupdate + */ +class InheritArticleRunOnce +{ + + public function run() + { + // get the database + $objDb = \Database::getInstance(); + + if( $objDb->tableExists('tl_article') ) + { + if( $objDb->fieldExists('inheritAfter', 'tl_article') && !$objDb->fieldExists('inheritPriority', 'tl_article') ) + { + // create field + $objDb->execute("ALTER TABLE `tl_article` ADD `inheritPriority` smallint(5) NOT NULL default '0'"); + + if( $objDb->fieldExists('inheritPriority', 'tl_article', true) ) + { + $objDb->execute("UPDATE tl_article SET inheritPriority = '-1' WHERE inheritAfter = '1' AND inherit = '1'"); + } + } + } + } + +} + +$objInheritArticleRunOnce = new InheritArticleRunOnce(); +$objInheritArticleRunOnce->run();