handling custom display of modul on pages created after mods

Your code modifications and patches you want to share with others.
Locked
disadb
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Wed May 12, 2010 10:18 am

handling custom display of modul on pages created after mods

Post by disadb » Wed May 12, 2010 2:29 pm

hack tested on Joomla! 1.5.15 Stable [ Wojmamni Ama Mamni ] 05-November-2009 04:00 GMT
with PHP 5.2.10


hello, I came to make this little hack because I often needed to assign a module to a selection of pages...
The thing is that, when we add menus, so itemids, we have to re-assign every modules that had a custom display.

For ex:
I want to have a pathway on every pages of my site except home page, I should assign the pathway module to selected pages in list (so i choose every but home).
But then if i create a new menu entry for a new article, the pathway won't be display until I add the page to the custom selection of pages in the pathway module.

my hack is designed to handle automatically the display of modules on new pages created after the module.
It consists on: [list=1]1- adding a radio button in the admin module edition to select all pages except selected below (step 1).[/list]
[list=2]2- Handling changes, write in DB, autoselect radiobutton if prevously set and returning on module edition (step 2).[/list]
[list=3]3- Handling display of module on frontend according to written exclusions in DB (step 3).[/list]
[list=4]4- Language translation (step 4).[/list]



step1: adding radio button in backend and adapting JS
this radio button is right next to assign radiobuttons such as:
'all', 'none', 'Select Menu Item(s) from the List', 'All but selected in the list'.

for that, edit script 'administrator/components/com_modules/admin.modules.html.php':
from line 374,

Code: Select all

<script type="text/javascript">
to line 425

Code: Select all

<?php endif; ?>
:
replace with following:

Code: Select all

				<script type="text/javascript">
					function allselections() {
						var e = document.getElementById('selections');
							e.disabled = true;
						var i = 0;
						var n = e.options.length;
						for (i = 0; i < n; i++) {
							e.options[i].disabled = true;
							e.options[i].selected = true;
						}
					}
					function allbutselections() {
						var e = document.getElementById('selections');
							e.disabled = false;
						var i = 0;
						var n = e.options.length;
						for (i = 0; i < n; i++) {
							e.options[i].disabled = false;
						}
					}
					function disableselections() {
						var e = document.getElementById('selections');
							e.disabled = true;
						var i = 0;
						var n = e.options.length;
						for (i = 0; i < n; i++) {
							e.options[i].disabled = true;
							e.options[i].selected = false;
						}
					}
					function enableselections() {
						var e = document.getElementById('selections');
							e.disabled = false;
						var i = 0;
						var n = e.options.length;
						for (i = 0; i < n; i++) {
							e.options[i].disabled = false;
						}
					}
				</script>
				<table class="admintable" cellspacing="1">
					<tr>
						<td valign="top" class="key">
							<?php echo JText::_( 'Menus' ); ?>:
						</td>
						<td>
						<?php if ($row->client_id != 1) : ?>
							<?php if ($row->pages == 'all') { ?>
                                <label for="menus-all">
                                    <input id="menus-all" type="radio" name="menus" value="all" onclick="allselections();" checked="checked" />
                                    <?php echo JText::_( 'All' ); ?>
                                </label>
                                <label for="menus-none">
                                    <input id="menus-none" type="radio" name="menus" value="none" onclick="disableselections();" />
                                    <?php echo JText::_( 'None' ); ?>
                                </label>
                                <label for="menus-select">
                                    <input id="menus-select" type="radio" name="menus" value="select" onclick="enableselections();" />
                                    <?php echo JText::_( 'Select From List' ); ?>
                                </label>
                                <label for="menus-all-but">
                                    <input id="menus-all-but" type="radio" name="menus" value="allbut" onclick="allbutselections();" />
                                    <?php echo JText::_( 'All But Selected In List' ); ?>
                                </label>
							<?php } elseif ($row->pages == 'none') { ?>
                                <label for="menus-all">
                                <input id="menus-all" type="radio" name="menus" value="all" onclick="allselections();" />
                                <?php echo JText::_( 'All' ); ?>
                                </label>
                                <label for="menus-none">
                                    <input id="menus-none" type="radio" name="menus" value="none" onclick="disableselections();" checked="checked" />
                                    <?php echo JText::_( 'None' ); ?>
                                </label>
                                <label for="menus-select">
                                    <input id="menus-select" type="radio" name="menus" value="select" onclick="enableselections();" />
                                    <?php echo JText::_( 'Select From List' ); ?>
                                </label>
                                <label for="menus-all-but">
                                    <input id="menus-all-but" type="radio" name="menus" value="allbut" onclick="allbutselections();" />
                                    <?php echo JText::_( 'All But Selected In List' ); ?>
                                </label>
							<?php } elseif ($row->pages == 'allbut') { ?>
                                <label for="menus-all">
                                <input id="menus-all" type="radio" name="menus" value="all" onclick="allselections();" />
                                <?php echo JText::_( 'All' ); ?>
                                </label>
                                <label for="menus-none">
                                    <input id="menus-none" type="radio" name="menus" value="none" onclick="disableselections();" />
                                    <?php echo JText::_( 'None' ); ?>
                                </label>
                                <label for="menus-select">
                                    <input id="menus-select" type="radio" name="menus" value="select" onclick="enableselections();" />
                                    <?php echo JText::_( 'Select From List' ); ?>
                                </label>
                                <label for="menus-all-but">
                                    <input id="menus-all-but" type="radio" name="menus" value="allbut" onclick="allbutselections();" checked="checked" />
                                    <?php echo JText::_( 'All But Selected In List' ); ?>
                                </label>
							<?php } else { ?>
                                <label for="menus-all">
                                    <input id="menus-all" type="radio" name="menus" value="all" onclick="allselections();" />
                                    <?php echo JText::_( 'All' ); ?>
                                </label>
                                <label for="menus-none">
                                    <input id="menus-none" type="radio" name="menus" value="none" onclick="disableselections();" />
                                    <?php echo JText::_( 'None' ); ?>
                                </label>
                                <label for="menus-select">
                                    <input id="menus-select" type="radio" name="menus" value="select" onclick="enableselections();" checked="checked" />
                                    <?php echo JText::_( 'Select From List' ); ?>
                                </label>
                                <label for="menus-all-but">
                                    <input id="menus-all-but" type="radio" name="menus" value="allbut" onclick="allbutselections();" />
                                    <?php echo JText::_( 'All But Selected In List' ); ?>
                                </label>
							<?php } ?>
						<?php endif; ?>

step2: handling custom excluded pages, using DB
edit file 'administrator/components/com_modules/controller.php':
and replace line 325-326,

Code: Select all

					. ' SET moduleid = '.(int) $row->id .', menuid = '.(int) $menuid
					;
with

Code: Select all

					. ' SET moduleid = '.(int) $row->id .', menuid = '.($menus == 'allbut'?'-':'').(int) $menuid
					;//keeping the same principle, just adding a minus if the selected pages are excluded from display...
replace line 452

Code: Select all

				$row->pages = null;
with

Code: Select all

				$row->pages = $lookup[0]->value>0?null:'allbut';//keeping same principle, if negative menuid is found, set 'allbut'
at line 468 add

Code: Select all

				foreach ($lookup as $k=>$v) $lookup[$k]->value= abs($v->value);
				//to load selected pages in list need to remove minus otherwise selected option in list won't be found
before

Code: Select all

$lists['access'] 		= JHTML::_('list.accesslevel',  $row );

step3: Handling display of module on frontend according to written exclusions in DB
edit file 'libraries/joomla/application/module/helper.php':
and replace line 272,

Code: Select all

		$wheremenu = isset( $Itemid ) ? ' AND ( mm.menuid = '. (int) $Itemid .' OR mm.menuid = 0 )' : '';
with

Code: Select all

		$wheremenu = isset( $Itemid ) ? ' AND ( mm.menuid = '. (int) $Itemid .' OR mm.menuid = 0 OR (mm.menuid<0 AND mm.menuid!=-'. (int) $Itemid .'))' : '';
		//extending query selection to negative occurences of menuid
at the following line (275) refine $query with 'distinct' so that the multiple negatives occurences of one module will be loaded only once; such as :

Code: Select all

		$query = 'SELECT DISTINCT id, title, module, position, content, showtitle, control, params'
at line 290, replace

Code: Select all

		$total = count($modules);
with

Code: Select all

		foreach($modules as $k=>$module)
		//this modification is needed to remove the module from loading selection by unsetting the matching
		//entry in the array of modules.
		//$modulesTmp is a temporary array used to set array keys back to a continue order,
		//which means after unsetting an entry we have an array entries indexed with keys like (0,1,2,4,5,...)
		//so the temp array sets back to (0,1,2,3,4,...)
		//after that, the total has changed so we count array entries only after setting $modules= $modulesTmp

		{
			$db->setQuery("SELECT `moduleid` FROM #__modules_menu WHERE menuid='-$Itemid' AND moduleid='$module->id'");
			if (!$db->loadResult()) $modulesTmp[]= $module;
			else unset($modules[$k]);
		}
		$total = count($modules=$modulesTmp);

step4: Set a translation for new radiobutton text in backend
edit file 'administrator/language/en-GB/en-GB.com_modules.ini' to add:

Code: Select all

ALL BUT SELECTED IN LIST=All but selected in the list


That's it, it is very little but can be very helpful and time-saving.
Perhaps this can be added to the future versions of Joomla!
Thank you for reading,
regards,
Last edited by ooffick on Wed May 12, 2010 2:49 pm, edited 1 time in total.
Reason: Mod Note: Removed manual Signature. Please read the Forum rules for details.
***** Manual signatures are NOT allowed ********** Manual signatures are NOT allowed ********** Manual signatures are NOT allowed ********** Manual signatures are NOT allowed *****_______
disadb, web developer.
Visit my cocktails en/fr website at http://sweetcocktails.free.fr

Locked

Return to “Core Hacks and Patches”