StewardManscat
03-17-2005, 12:07 AM
This hack gives you finer control over the placement of modules on every page.
Wrote this hack to help me see and manage modules on a page. I also wanted to define pages where the same module appears in different columns or in a different order than the default.
Seeing only the modules that are on one page is helpful if you have a lot of pages and modules.
There is one database change and two files to edit. It's not too harsh: four cut and paste jobs.
Once installed, go to admincp > CMPS > Edit pages.
In the drop-down box, select "Layout" (screen print layout01.jpg attached. An empty layout).
Click "Build layout using modules currently active for this page" to populate the layout for this page using your current data.
Screen print layout02.jpg attached: For my Home/Default page layout.
Using the drop down list, pick modules to add to this page. The additional drop-lists specify where to put them (top of column, bottom of column, and left, right or centre column).
Once a modules is in the layout, use the links below each modules to shuffle them: up, down, left or right. Use the X to delete a module from the page.
When you add a module to a page layout, it is the same as checking the module under 'Edit Page'. When you remove a module from a page layout, it is the same as unchecking the module under 'Edit Page'. In other words, you are enabling/disabling the module for the page.
Apart from that, it functions independently of CMPS. If there is no layout for a given page, the normal CMPS bhaviour takes effect. That is, defining a layout for a page is an override.
Select 'Clear this layout' to revert to normal behaviour for the page.
I am not sure how ready this is for release. It seems to be working fine for me. But I only wrote it an hour ago! I did not test it with empty tables or other extreme conditions. I'm putting it out here to see if anyone else has the interest or can find some bugs. Thanks.
Ready? Here goes...
Import/Maintanance > Execute SQL query:
alter table YOUR_TABLE_PREFIX_adv_pages add layout mediumtext NOT NULL
(Or use phpMyAdmin or similar. Substitute your own table prefix).
In file forum/admin/vba_cmps_admin.php:
Find:
case 'remove':
page = 'vba_cmps_admin.php?do=removepage&pageid=';
break;
Add below:
case 'layout':
page = 'vba_cmps_admin.php?do=layout&pageid=';
break;
Find:
$diroptions = array(
'edit' => $vbphrase['edit'],
'remove' => $vbphrase['delete']
Replace with:
$diroptions = array(
'edit' => $vbphrase['edit'],
'remove' => $vbphrase['delete'],
'layout' => 'Layout'
Find:
print_cp_footer();
Add above:
// ######################## Edit Page Layout ############################
function print_layout_row($col, $coltitle)
{
global $page, $_REQUEST, $vbphrase, $modules, $layout;
echo '<td width="33%" valign="top">';
print_table_start(false, '100%');
print_description_row($coltitle, '', 2, 'thead');
$colarray = $layout[$col];
if (!empty($colarray))
{
$row=0;
foreach ($colarray AS $column)
{
$title=$modules[$column];
print_label_row('
<span class="smallfont">
<strong>
<a href="vba_cmps_admin.php?do=editmodule&modid=' . $column . '">' . $modules[$column] . '</a>
</strong><BR>
<a href="vba_cmps_admin.php?do=shiftlayout&shift=L&row='.$row.'&col='.$col.'&pageid='.$pa ge['pageid'].'" title="Shift left">‹</a>
<a href="vba_cmps_admin.php?do=shiftlayout&shift=R&row='.$row.'&col='.$col.'&pageid='.$pa ge['pageid'].'" title="Shift right">›</a>
<a href="vba_cmps_admin.php?do=shiftlayout&shift=U&row='.$row.'&col='.$col.'&pageid='.$pa ge['pageid'].'" title="Shift up">^</a>
<a href="vba_cmps_admin.php?do=shiftlayout&shift=D&row='.$row.'&col='.$col.'&pageid='.$pa ge['pageid'].'" title="Shift down">v</a>
<a href="vba_cmps_admin.php?do=shiftlayout&shift=X&row='.$row.'&col='.$col.'&pageid='.$pa ge['pageid'].'" title="Remove from page">x</a>
</span>
');
$row++;
}
unset($column);
}
else
{
print_label_row($vbphrase['no_modules']);
}
print_table_footer(true, '', '', false);
echo '</td>';
}
// ####################### Edit Page Layout #########################
if ($_REQUEST['do'] == 'layout')
{
// There oughtta be a phrase...
$optPos=array('Top', 'Bottom');
$optCol=array('Left', 'Centre', 'Right');
require_once('./includes/adminfunctions_template.php');
print_form_header('vba_cmps_admin', 'dolayout');
globalize($_REQUEST, array('pageid' => INT));
// Get our current layout. Initialize to array if never used before
$page = $DB_site->query_first("SELECT pageid, title,layout FROM " . TABLE_PREFIX . "adv_pages WHERE pageid = '$pageid'");
$layout = unserialize($page['layout']);
if (! is_array($layout))
{
$layout = array( array(), array(), array() );
}
// Pick list of all available modules
$getmodules = $DB_site->query("SELECT title,modid FROM " . TABLE_PREFIX . "adv_modules WHERE active='1' ORDER BY title");
while ($module = $DB_site->fetch_array($getmodules))
{
$modules[$module['modid']] = $module['title'];
}
// First part: Load/Clear from existing CMPS schema
print_label_row("<div align=\"center\"><A HREF=\"vba_cmps_admin.php?$session[sessionurl]&do=layoutnew&pageid=$pageid\">Build layout using modules currently active for this page</A></div>");
print_label_row("<div align=\"center\"><A HREF=\"vba_cmps_admin.php?$session[sessionurl]&do=layoutclear&pageid=$pageid\">Clear this layout</A></div>");
print_table_break();
// Second part: Pick module to add to layout
print_table_start(false, '100%');
print_table_header('Add modules to page ' . $page['title']);
print_select_row(' ', 'addmodid',$modules);
print_select_row(' ', 'addmodpos',$optPos);
print_select_row(' ', 'addmodcol',$optCol);
construct_hidden_code('do', 'addtolayout');
construct_hidden_code('pageid', $page['pageid']);
print_submit_row();
print_table_footer();
// Third part: Show columns
print_table_start(false, '100%');
print_table_header('Modules', 3);
echo '<tr class="alt2">';
print_layout_row(0, $vbphrase['left_column']);
print_layout_row(1, $vbphrase['center_column']);
print_layout_row(2, $vbphrase['right_column']);
print_table_footer();
}
// ######################### Add module to layout ###########################
if ($_REQUEST['do'] == 'addtolayout')
{
globalize($_REQUEST, array('pageid' => INT, 'addmodid' => INT, 'addmodpos' => INT, 'addmodcol' => INT));
$page = $DB_site->query_first("SELECT layout,modules FROM " . TABLE_PREFIX . "adv_pages WHERE pageid = '$pageid'");
$layout = unserialize($page['layout']);
if ( ! is_array($layout) )
{
$layout = array( array(), array(), array() );
}
if ($addmodpos==1)
{
array_push($layout[$addmodcol], $addmodid);
}
else
{
array_unshift($layout[$addmodcol], $addmodid);
}
// Add module to page
$modules = explode(',',$page['modules']);
if ( is_array($modules) )
{
if ( ! in_array($addmodid,$modules) )
$modules[] = $addmodid;
$smod = implode(',',$modules);
}
$DB_site->query("UPDATE " . TABLE_PREFIX . "adv_pages SET modules='$smod', layout = '" . serialize($layout) . "' WHERE pageid = '$pageid'");
print_cp_redirect("vba_cmps_admin.php?$session[sessionurl]&do=layout&pageid=$pageid", 0);
}
// ######################### Shift Layout (UP,DOWN,LEFT,RIGHT,DELETE) ###########################
if ($_REQUEST['do'] == 'shiftlayout')
{
globalize($_REQUEST, array('pageid' => INT, 'row' => INT, 'col' => INT, 'shift' => STR));
$page = $DB_site->query_first("SELECT layout,modules FROM " . TABLE_PREFIX . "adv_pages WHERE pageid = '$pageid'");
$smod = $page['modules'];
$layout = unserialize($page['layout']);
if ( ! is_array($layout) )
{
$layout = array( array(), array(), array() );
}
$column = $layout[$col];
switch($shift)
{
case 'X': // Delete
$modid = $column[$row];
// Remove from layout
array_splice($column,$row, count($column)-$row, array_slice($column,$row+1));
// Remove from from page[modules] list
$modules = explode(',',$page['modules']);
if ( is_array($modules) )
{
$i = array_search($modid,$modules);
if ( $i === FALSE )
;
else
array_splice($modules,$i, count($modules)-$i, array_slice($modules,$i+1));
$smod = implode(',',$modules);
}
break;
case 'U': // Up
$modid = $column[0];
array_shift($column);
$column[]=$modid;
break;
case 'D': // Down
$modid = array_pop($column);
array_unshift($column,$modid);
break;
case 'L': // Left
case 'R': // Right
// Delete from column
$modid = $column[$row];
array_splice($column,$row, count($column)-$row, array_slice($column,$row+1));
$layout[$col]=$column;
switch($shift)
{
case 'L': // Left
if (--$col < 0)
{
$col=2;
}
break;
case 'R': // Right
if (++$col > 2)
{
$col=0;
}
break;
}
$column = $layout[$col];
// Insert
if ( count($column) < $row )
{
$column[] = $modid;
}
else
{
array_splice($column, $row, 0, $modid);
}
break;
}
$layout[$col] = $column;
$DB_site->query("UPDATE " . TABLE_PREFIX . "adv_pages SET modules='$smod', layout = '" . serialize($layout) . "' WHERE pageid = '$pageid'");
print_cp_redirect("vba_cmps_admin.php?$session[sessionurl]&do=layout&pageid=$pageid", 0);
}
// ######################### Create layout from existing stuff ###########################
if ($_REQUEST['do'] == 'layoutnew')
{
globalize($_REQUEST, array('pageid' => INT) );
$page = $DB_site->query_first("SELECT pageid, layout, modules FROM " . TABLE_PREFIX . "adv_pages WHERE pageid = '$pageid'");
$layout = array( array(), array(), array() );
if ( $page['modules'] != '' )
{
$q='SELECT modid,modcol from ' . TABLE_PREFIX . 'adv_modules WHERE modid IN (' . $page['modules'] . ') ORDER BY displayorder';
$mods = $DB_site->query($q);
while ($mod = $DB_site->fetch_array($mods))
{
$layout[$mod['modcol']][] = $mod['modid'];
}
}
$DB_site->query("UPDATE " . TABLE_PREFIX . "adv_pages SET layout = '" . serialize($layout) . "' WHERE pageid = '$pageid'");
print_cp_redirect("vba_cmps_admin.php?$session[sessionurl]&do=layout&pageid=$pageid", 0);
}
// ######################### Clear layout ###########################
if ($_REQUEST['do'] == 'layoutclear')
{
globalize($_REQUEST, array('pageid' => INT) );
$DB_site->query_first("UPDATE " . TABLE_PREFIX . "adv_pages SET layout='' WHERE pageid = '$pageid'");
print_cp_redirect("vba_cmps_admin.php?$session[sessionurl]&do=layout&pageid=$pageid", 0);
}
In file forum/admin/vba_cmps_include_bottom.php:
Find:
// Process Active Modules
if (is_array($modules))
{
foreach ($modules AS $mods)
{
if (in_array($mods['modid'], explode(',', $pages['modules'])))
{
Replace with:
function page_layout( &$mods, $pages )
{
$layout = unserialize($pages['layout']);
if ( ! is_array($layout) )
{
return;
}
for ($col = 0 ; $col < 3; $col++)
{
$row = 0;
foreach($layout[$col] AS $aModuleId)
{
if ($aModuleId == $mods['modid'])
{
$mods['displayorder'] = $row;
$mods['modcol'] = $col;
return;
}
$row++;
}
}
}
// Process Active Modules
if (is_array($modules))
{
foreach ($modules AS $mods)
{
if (in_array($mods['modid'], explode(',', $pages['modules'])))
{
page_layout($mods,$pages);
Wrote this hack to help me see and manage modules on a page. I also wanted to define pages where the same module appears in different columns or in a different order than the default.
Seeing only the modules that are on one page is helpful if you have a lot of pages and modules.
There is one database change and two files to edit. It's not too harsh: four cut and paste jobs.
Once installed, go to admincp > CMPS > Edit pages.
In the drop-down box, select "Layout" (screen print layout01.jpg attached. An empty layout).
Click "Build layout using modules currently active for this page" to populate the layout for this page using your current data.
Screen print layout02.jpg attached: For my Home/Default page layout.
Using the drop down list, pick modules to add to this page. The additional drop-lists specify where to put them (top of column, bottom of column, and left, right or centre column).
Once a modules is in the layout, use the links below each modules to shuffle them: up, down, left or right. Use the X to delete a module from the page.
When you add a module to a page layout, it is the same as checking the module under 'Edit Page'. When you remove a module from a page layout, it is the same as unchecking the module under 'Edit Page'. In other words, you are enabling/disabling the module for the page.
Apart from that, it functions independently of CMPS. If there is no layout for a given page, the normal CMPS bhaviour takes effect. That is, defining a layout for a page is an override.
Select 'Clear this layout' to revert to normal behaviour for the page.
I am not sure how ready this is for release. It seems to be working fine for me. But I only wrote it an hour ago! I did not test it with empty tables or other extreme conditions. I'm putting it out here to see if anyone else has the interest or can find some bugs. Thanks.
Ready? Here goes...
Import/Maintanance > Execute SQL query:
alter table YOUR_TABLE_PREFIX_adv_pages add layout mediumtext NOT NULL
(Or use phpMyAdmin or similar. Substitute your own table prefix).
In file forum/admin/vba_cmps_admin.php:
Find:
case 'remove':
page = 'vba_cmps_admin.php?do=removepage&pageid=';
break;
Add below:
case 'layout':
page = 'vba_cmps_admin.php?do=layout&pageid=';
break;
Find:
$diroptions = array(
'edit' => $vbphrase['edit'],
'remove' => $vbphrase['delete']
Replace with:
$diroptions = array(
'edit' => $vbphrase['edit'],
'remove' => $vbphrase['delete'],
'layout' => 'Layout'
Find:
print_cp_footer();
Add above:
// ######################## Edit Page Layout ############################
function print_layout_row($col, $coltitle)
{
global $page, $_REQUEST, $vbphrase, $modules, $layout;
echo '<td width="33%" valign="top">';
print_table_start(false, '100%');
print_description_row($coltitle, '', 2, 'thead');
$colarray = $layout[$col];
if (!empty($colarray))
{
$row=0;
foreach ($colarray AS $column)
{
$title=$modules[$column];
print_label_row('
<span class="smallfont">
<strong>
<a href="vba_cmps_admin.php?do=editmodule&modid=' . $column . '">' . $modules[$column] . '</a>
</strong><BR>
<a href="vba_cmps_admin.php?do=shiftlayout&shift=L&row='.$row.'&col='.$col.'&pageid='.$pa ge['pageid'].'" title="Shift left">‹</a>
<a href="vba_cmps_admin.php?do=shiftlayout&shift=R&row='.$row.'&col='.$col.'&pageid='.$pa ge['pageid'].'" title="Shift right">›</a>
<a href="vba_cmps_admin.php?do=shiftlayout&shift=U&row='.$row.'&col='.$col.'&pageid='.$pa ge['pageid'].'" title="Shift up">^</a>
<a href="vba_cmps_admin.php?do=shiftlayout&shift=D&row='.$row.'&col='.$col.'&pageid='.$pa ge['pageid'].'" title="Shift down">v</a>
<a href="vba_cmps_admin.php?do=shiftlayout&shift=X&row='.$row.'&col='.$col.'&pageid='.$pa ge['pageid'].'" title="Remove from page">x</a>
</span>
');
$row++;
}
unset($column);
}
else
{
print_label_row($vbphrase['no_modules']);
}
print_table_footer(true, '', '', false);
echo '</td>';
}
// ####################### Edit Page Layout #########################
if ($_REQUEST['do'] == 'layout')
{
// There oughtta be a phrase...
$optPos=array('Top', 'Bottom');
$optCol=array('Left', 'Centre', 'Right');
require_once('./includes/adminfunctions_template.php');
print_form_header('vba_cmps_admin', 'dolayout');
globalize($_REQUEST, array('pageid' => INT));
// Get our current layout. Initialize to array if never used before
$page = $DB_site->query_first("SELECT pageid, title,layout FROM " . TABLE_PREFIX . "adv_pages WHERE pageid = '$pageid'");
$layout = unserialize($page['layout']);
if (! is_array($layout))
{
$layout = array( array(), array(), array() );
}
// Pick list of all available modules
$getmodules = $DB_site->query("SELECT title,modid FROM " . TABLE_PREFIX . "adv_modules WHERE active='1' ORDER BY title");
while ($module = $DB_site->fetch_array($getmodules))
{
$modules[$module['modid']] = $module['title'];
}
// First part: Load/Clear from existing CMPS schema
print_label_row("<div align=\"center\"><A HREF=\"vba_cmps_admin.php?$session[sessionurl]&do=layoutnew&pageid=$pageid\">Build layout using modules currently active for this page</A></div>");
print_label_row("<div align=\"center\"><A HREF=\"vba_cmps_admin.php?$session[sessionurl]&do=layoutclear&pageid=$pageid\">Clear this layout</A></div>");
print_table_break();
// Second part: Pick module to add to layout
print_table_start(false, '100%');
print_table_header('Add modules to page ' . $page['title']);
print_select_row(' ', 'addmodid',$modules);
print_select_row(' ', 'addmodpos',$optPos);
print_select_row(' ', 'addmodcol',$optCol);
construct_hidden_code('do', 'addtolayout');
construct_hidden_code('pageid', $page['pageid']);
print_submit_row();
print_table_footer();
// Third part: Show columns
print_table_start(false, '100%');
print_table_header('Modules', 3);
echo '<tr class="alt2">';
print_layout_row(0, $vbphrase['left_column']);
print_layout_row(1, $vbphrase['center_column']);
print_layout_row(2, $vbphrase['right_column']);
print_table_footer();
}
// ######################### Add module to layout ###########################
if ($_REQUEST['do'] == 'addtolayout')
{
globalize($_REQUEST, array('pageid' => INT, 'addmodid' => INT, 'addmodpos' => INT, 'addmodcol' => INT));
$page = $DB_site->query_first("SELECT layout,modules FROM " . TABLE_PREFIX . "adv_pages WHERE pageid = '$pageid'");
$layout = unserialize($page['layout']);
if ( ! is_array($layout) )
{
$layout = array( array(), array(), array() );
}
if ($addmodpos==1)
{
array_push($layout[$addmodcol], $addmodid);
}
else
{
array_unshift($layout[$addmodcol], $addmodid);
}
// Add module to page
$modules = explode(',',$page['modules']);
if ( is_array($modules) )
{
if ( ! in_array($addmodid,$modules) )
$modules[] = $addmodid;
$smod = implode(',',$modules);
}
$DB_site->query("UPDATE " . TABLE_PREFIX . "adv_pages SET modules='$smod', layout = '" . serialize($layout) . "' WHERE pageid = '$pageid'");
print_cp_redirect("vba_cmps_admin.php?$session[sessionurl]&do=layout&pageid=$pageid", 0);
}
// ######################### Shift Layout (UP,DOWN,LEFT,RIGHT,DELETE) ###########################
if ($_REQUEST['do'] == 'shiftlayout')
{
globalize($_REQUEST, array('pageid' => INT, 'row' => INT, 'col' => INT, 'shift' => STR));
$page = $DB_site->query_first("SELECT layout,modules FROM " . TABLE_PREFIX . "adv_pages WHERE pageid = '$pageid'");
$smod = $page['modules'];
$layout = unserialize($page['layout']);
if ( ! is_array($layout) )
{
$layout = array( array(), array(), array() );
}
$column = $layout[$col];
switch($shift)
{
case 'X': // Delete
$modid = $column[$row];
// Remove from layout
array_splice($column,$row, count($column)-$row, array_slice($column,$row+1));
// Remove from from page[modules] list
$modules = explode(',',$page['modules']);
if ( is_array($modules) )
{
$i = array_search($modid,$modules);
if ( $i === FALSE )
;
else
array_splice($modules,$i, count($modules)-$i, array_slice($modules,$i+1));
$smod = implode(',',$modules);
}
break;
case 'U': // Up
$modid = $column[0];
array_shift($column);
$column[]=$modid;
break;
case 'D': // Down
$modid = array_pop($column);
array_unshift($column,$modid);
break;
case 'L': // Left
case 'R': // Right
// Delete from column
$modid = $column[$row];
array_splice($column,$row, count($column)-$row, array_slice($column,$row+1));
$layout[$col]=$column;
switch($shift)
{
case 'L': // Left
if (--$col < 0)
{
$col=2;
}
break;
case 'R': // Right
if (++$col > 2)
{
$col=0;
}
break;
}
$column = $layout[$col];
// Insert
if ( count($column) < $row )
{
$column[] = $modid;
}
else
{
array_splice($column, $row, 0, $modid);
}
break;
}
$layout[$col] = $column;
$DB_site->query("UPDATE " . TABLE_PREFIX . "adv_pages SET modules='$smod', layout = '" . serialize($layout) . "' WHERE pageid = '$pageid'");
print_cp_redirect("vba_cmps_admin.php?$session[sessionurl]&do=layout&pageid=$pageid", 0);
}
// ######################### Create layout from existing stuff ###########################
if ($_REQUEST['do'] == 'layoutnew')
{
globalize($_REQUEST, array('pageid' => INT) );
$page = $DB_site->query_first("SELECT pageid, layout, modules FROM " . TABLE_PREFIX . "adv_pages WHERE pageid = '$pageid'");
$layout = array( array(), array(), array() );
if ( $page['modules'] != '' )
{
$q='SELECT modid,modcol from ' . TABLE_PREFIX . 'adv_modules WHERE modid IN (' . $page['modules'] . ') ORDER BY displayorder';
$mods = $DB_site->query($q);
while ($mod = $DB_site->fetch_array($mods))
{
$layout[$mod['modcol']][] = $mod['modid'];
}
}
$DB_site->query("UPDATE " . TABLE_PREFIX . "adv_pages SET layout = '" . serialize($layout) . "' WHERE pageid = '$pageid'");
print_cp_redirect("vba_cmps_admin.php?$session[sessionurl]&do=layout&pageid=$pageid", 0);
}
// ######################### Clear layout ###########################
if ($_REQUEST['do'] == 'layoutclear')
{
globalize($_REQUEST, array('pageid' => INT) );
$DB_site->query_first("UPDATE " . TABLE_PREFIX . "adv_pages SET layout='' WHERE pageid = '$pageid'");
print_cp_redirect("vba_cmps_admin.php?$session[sessionurl]&do=layout&pageid=$pageid", 0);
}
In file forum/admin/vba_cmps_include_bottom.php:
Find:
// Process Active Modules
if (is_array($modules))
{
foreach ($modules AS $mods)
{
if (in_array($mods['modid'], explode(',', $pages['modules'])))
{
Replace with:
function page_layout( &$mods, $pages )
{
$layout = unserialize($pages['layout']);
if ( ! is_array($layout) )
{
return;
}
for ($col = 0 ; $col < 3; $col++)
{
$row = 0;
foreach($layout[$col] AS $aModuleId)
{
if ($aModuleId == $mods['modid'])
{
$mods['displayorder'] = $row;
$mods['modcol'] = $col;
return;
}
$row++;
}
}
}
// Process Active Modules
if (is_array($modules))
{
foreach ($modules AS $mods)
{
if (in_array($mods['modid'], explode(',', $pages['modules'])))
{
page_layout($mods,$pages);