PDA


View Full Version : [MOD] Define module layout on a per-page basis


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&amp;modid=' . $column . '">' . $modules[$column] . '</a>
</strong><BR>
<a href="vba_cmps_admin.php?do=shiftlayout&amp;shift=L&amp;row='.$row.'&amp;col='.$col.'&amp;pageid='.$pa ge['pageid'].'" title="Shift left">&lsaquo;</a> &nbsp;
<a href="vba_cmps_admin.php?do=shiftlayout&amp;shift=R&amp;row='.$row.'&amp;col='.$col.'&amp;pageid='.$pa ge['pageid'].'" title="Shift right">&rsaquo;</a>&nbsp;
<a href="vba_cmps_admin.php?do=shiftlayout&amp;shift=U&amp;row='.$row.'&amp;col='.$col.'&amp;pageid='.$pa ge['pageid'].'" title="Shift up">^</a>&nbsp;
<a href="vba_cmps_admin.php?do=shiftlayout&amp;shift=D&amp;row='.$row.'&amp;col='.$col.'&amp;pageid='.$pa ge['pageid'].'" title="Shift down">v</a>&nbsp;
<a href="vba_cmps_admin.php?do=shiftlayout&amp;shift=X&amp;row='.$row.'&amp;col='.$col.'&amp;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&amp;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&amp;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&amp;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&amp;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&amp;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&amp;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);

Cyricx
03-29-2005, 03:36 PM
Would love this mod, I myself run several pages as well :)

Tried to add it into my site, followed the instructions to a T, however when I go to do anything through the admincp I get a blank page.

Any ideas?

StewardManscat
03-29-2005, 04:05 PM
Awww.

You mean your whole admincp is not functioning?!

First thing that comes to mind: check for empty lines at the end of a file, after the final closing ?>

Sometimes that causes blank pages. Generally speaking you don't want anything, not even one empty space, after that closing tag.

Otherwise, no, sorry, I dunno. If it were me, I'd put in the changes one at a time, checking the admincp after each to make sure it's still there! If you can narrow it down to one of the changes above maybe I can help further.

I also notice that the vbulletin gremlins are attacking the php code in this post. EG


&amp;row='.$r ow.'&amp;col='.$col


was actually pasted in as


&amp;row='.$row.'&amp;col='.$col


... without the space in $row

Not sure if that's what's killing you either. But it sure can't be helping.

Have a text file with these changes, I'll post that here tonight if you like (when I get home).

Good luck. Lemme know.

Cyricx
03-29-2005, 09:53 PM
Nope, the rest of the admin cp works, it just won't do any of the actions inside the vba_cmps_admin.php

You can try to access them outside of the admincp and they come up blank.

Like for example if you try to load this up

http://www.mmogcommunities.com/forums/admincp/vba_cmps_admin.php?do=modifysettings

It comes up a blank page.

I applied the fixes you mentioned - hadn't had a chance to fine tooth comb the code yet hehe was hacking on lunch break ;).

Any other ideas?

StewardManscat
03-30-2005, 12:25 PM
That link takes me to a login page of course.

Attached is my copy of vba_cmps_admin. Maybe you can do a file compare. Perhaps I left out some crucial change.

If you are okay to send user/passwords for admincp and ftp, I will have a look.

(Or: if you send your vba_cmps_admin, I'll do the file compare. I may have one other unrelated hack already in this file).

Cyricx
04-02-2005, 03:28 PM
Sorry it took me so long to get back to ya, had an inventory at work that was slaughtering me.

I uploaded your admin and it's working fine.

Only reason I put the link above is so you'd see which function is was burping on ;)

What other hacks have you added into vba? Didn't see some commenting on some of the changes so not sure what they were for.

StewardManscat
04-02-2005, 03:56 PM
Glad you got it working. Great.

The other hack was this one (http://www.vbadvanced.com/forum/showthread.php?t=4552). Turns out I did not find it useful. But seeing how it worked sped me along the way to developing this one.