Skip to content

Commit

Permalink
introduced inheritance priority
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg committed Feb 13, 2016
1 parent 3d3a72e commit e79d75b
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 58 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
"contao":{
"sources":{
"system/modules/inherit_article":"system/modules/inherit_article"
}
},
"runonce":[
"system/modules/inherit_article/runonce/runonce.php"
]
}
}
}
2 changes: 1 addition & 1 deletion system/modules/inherit_article/config/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand Down
18 changes: 10 additions & 8 deletions system/modules/inherit_article/dca/tl_article.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand All @@ -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
Expand All @@ -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 ''"
);

Expand All @@ -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'"
);
4 changes: 2 additions & 2 deletions system/modules/inherit_article/languages/de/tl_article.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand All @@ -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).');
4 changes: 2 additions & 2 deletions system/modules/inherit_article/languages/en/tl_article.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand All @@ -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).');
46 changes: 23 additions & 23 deletions system/modules/inherit_article/models/ArticleModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
}
Expand Down
43 changes: 23 additions & 20 deletions system/modules/inherit_article/models/ArticleModel_C35.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
45 changes: 45 additions & 0 deletions system/modules/inherit_article/runonce/runonce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* Contao Open Source CMS
*
* Extension to replace the ArticleModel in order to allow inheritable articles
*
* @copyright inspiredminds 2016
* @package inherit_article
* @link http://www.inspiredminds.at
* @author Fritz Michael Gschwantner <[email protected]>
* @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();

0 comments on commit e79d75b

Please sign in to comment.