PDA

View Full Version : Categories global de-/activation button (Admin)


Tobias
10-04-2006, 08:28 AM
Hi there,

this post was originally posted August 8, 2006.

Solution tested and working with Subdreamer v2.3.5.

Purpose:
Allow the admin to globally switch menu visibility of a specific category, thus avoiding the need for the admin to tediously go through all usergroups on his own.

Solution description:

Added a "button" column to the "View Categories" plugin screen
Upon click: show a form for the current category showing all usergroups in a selection listbox
By default usergroups which have the current category included in the "categorymenuids" are marked/selected (i.e. category is visible in menu)
Upon submitting the form, the categoryid is added (if not exists) to any selected/marked usergroup's "categorymenuids" and removed (if exists) otherwise from any non-selected/unmarked usergroup


Solution example previews:

Usergroup selection form:
http://www.sqr-runner.de/subd/images/sd_cat_visibility1.gif
Update log screen:
http://www.sqr-runner.de/subd/images/sd_cat_visibility2.gif


The solution code is fairly well commented and it also logs activites.

Enjoy,
Tobias

Here you go:


Open file "admin/categories.php"

Change 1: adding new column in table header

Around line 82 find this code:

<td class="tdrow1">Parent Category</td>
<td class="tdrow1" width="60" align="center">Edit</td>
<td class="tdrow1" width="60" align="center">Preview</td>
<td class="tdrow1" width="60" align="center">Delete</td>
</tr>';


Replace it with:

<td class="tdrow1">Parent Category</td>
<td class="tdrow1" width="50" align="center">Edit</td>
<td class="tdrow1" width="50" align="center">Preview</td>
<td class="tdrow1" width="50" align="center">Delete</td>
<td class="tdrow1" width="50" align="center">Menu?</td>
</tr>';


Change 2: fill new column with link contents

Around line 153 find this code:

echo '</tr>';

DisplayCategories($category['categoryid'], $sublevelmarker);
}

// end table
if($parentid == 0)
{
echo '<tr>
<td class="tdrow1" bgcolor="#FCFCFC" colspan="7" align="center">


Replace it with:

if($category['categoryid'] == 1)
{
echo '<td class="tdrow2" align="center"><img src="' . $stylepath . 'images/notrash.gif" /></td>';
}
else
{
echo '<td class="tdrow2" align="center">
<a href="categories.php?action=switchvisibility&categoryid=' . $category['categoryid'] .
'" <img src="' . $stylepath . 'images/configure.gif" title="Switch category menu visibility" /></a>
</td>';
}

echo '</tr>';

DisplayCategories($category['categoryid'], $sublevelmarker);
}

// end table
if($parentid == 0)
{
echo '<tr>
<td class="tdrow1" bgcolor="#FCFCFC" colspan="8" align="center">


Change 3: main functionality code

At the END of the file, BEFORE these lines:

// ############################### PRINT FOOTER ################################

PrintFooter();


PASTE (insert) this block:


// ############################ SWITCH VISIBILITY #############################
// CONDEV 20060807 added form to switch menu visibility for specific category
if($action == 'switchvisibility')
{
global $DB;

PrintSection('Category Menu Visibility Settings');

// this page always has to run with the categoryid variable set, so lets get it now, default 1 (home)
$categoryid = is_numeric($_GET['categoryid']) ? intval($_GET['categoryid']) : (is_numeric($_POST['categoryid']) ? intval($_POST['categoryid']) : '');
$getcategory = $DB->query_first("SELECT categoryid, parentid, name FROM " . TABLE_PREFIX . "categories WHERE categoryid = $categoryid");

if(($getcategory[0]) && ($getcategory[0]>1))
{
echo '<form method="post" action="categories.php">
<input type="hidden" name="action" value="updatecategorymenuids" />
<input type="hidden" name="categoryid" value="'.$categoryid.'" />
<table width="100%" border="0" cellpadding="5" cellspacing="0">
<tr>
<td class="tdrow2" width="150">Category:</td>
<td class="tdrow3"><b>' . $getcategory['name'] . ' (' . $categoryid . ')</b></td>
</tr>
<tr>
<td class="tdrow2" valign="top">Usergroups:</td>
<td class="tdrow3">';

$categorymenuids = array();
$getusergroups = $DB->query("SELECT usergroupid, name, categorymenuids
FROM " . TABLE_PREFIX . "usergroups
ORDER BY usergroupid");

// Build selection listbox
echo '<select name="usergroupids[]" size="6" multiple>';
echo '<option value="0">[None]</option>';
while($usergroup = $DB->fetch_array($getusergroups))
{
// convert the string to an array
$categorymenuids = explode(",",$usergroup['categorymenuids']);
//echo $categorymenuids;
echo "<option value=\"$usergroup[usergroupid]\" ".
iif(in_array($categoryid,$categorymenuids),"selected","").">" . $usergroup['name'] .
"</option>";
}
echo '</select>
<br />
<p><b>Please select all usergroups for which this category should be visible in the menu.</b></p>
<p><b>For hiding the category from ALL users, just select "<i>[None]</i>"</b>.</p>
(Use CTRL to select multiple sections)</td>
</tr>
<tr>
<td class="tdrow1" colspan="2" align="center">
<input type="submit" value="Update Category" />
</td>
</tr>
</table>
</form>';
}

EndSection();

} // 'switchvisibility'


// ############################ UPDATE USERGROUPS #############################
// CONDEV 20060807 usergroup processing to add/remove category from menus
if($action == 'updatecategorymenuids')
{
global $DB;

PrintSection('Processing Menu Visibilities for Category ' . $categoryid);

echo '<form method="post" action="categories.php">
<input type="hidden" name="action" value="displaycategories" />
<table width="100%" border="0" cellpadding="5" cellspacing="0">';

// This page always has to run with the categoryid variable set - do nothing
// if none or invalid category was specified.
$categoryid = is_numeric($_POST['categoryid']) ? intval($_POST['categoryid']) : NULL;
if(isset($categoryid) && ($categoryid>0))
{

$categorymenuids = isset($_POST['usergroupids']) ? $_POST['usergroupids'] : NULL;
if(isset($categorymenuids))
{
echo '<tr>
<td colspan="2">
<table width="100%">
<tr><td class="tdrow2" width="250"><b>Category visible for usergroups:</b></td>
<td class="tdrow2">' . implode(",", $usergroupids) . '</b></td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="tdrow1" width="20%"><b>Usergroups</b></td>
<td class="tdrow1"><b>Actions</b></td>
</tr>';

$getusergroups = $DB->query("SELECT usergroupid, name, categorymenuids " .
"FROM " . TABLE_PREFIX . "usergroups ".
"ORDER BY usergroupid");

// Iterate through all existing usergroups
while($usergroup = $DB->fetch_array($getusergroups))
{
// Trick: adding a comma here allows a matching search for the
// categoryid even if it's the last in the string:
$categorymenuids = $usergroup['categorymenuids'] . ',';
$DoUpdate = false; // actually perform an update?
$IsAdded = false; // was category added?
$msg = '';

// Shall the current usergroup show the category?
$CatVisible = (in_array($usergroup[0],$usergroupids));

// Is category already visible (included)?
if(strpos($categorymenuids, $categoryid.',')>0)
{
if($CatVisible)
{
$msg = ' Unchanged (already <b>visible</b>)';
}
else
{
// Remove category by empty-string replacement.
// Note that the search includes a trailing comma:
$categorymenuids = str_replace($categoryid.",", "", $categorymenuids);
$DoUpdate = true;
$msg = '<b>-- Visibility OFF</b>';
}
}
else
{
if(!$CatVisible)
{
$msg = ' Unchanged (already hidden)';
}
else
{
// Just append category *with* comma!
$categorymenuids .= $categoryid . ",";
$IsAdded = true;
$DoUpdate = true;
$msg = '<b>++ Visibility ON</b>';
}
}

// Log the action
echo '<tr>
<td class="tdrow2" width="20%"><p><b>' . $usergroup[0] . ':' . $usergroup[1] . '</b></p></td>
<td class="tdrow2">' . $msg . '<br />'; // Do not close TD here...

// Is update of usergroup required?
if($DoUpdate)
{
// Remove trailing comma we added before (also when category was added)!
$categorymenuids = substr($categorymenuids,0,-1);

// If category was added: convert to array, sort it and write it back
if($IsAdded)
{
$newcategorymenuids = array();
$newcategorymenuids = explode(",",$categorymenuids); // convert to array
sort($newcategorymenuids); // sort array
$categorymenuids = implode(",",$newcategorymenuids); // back as string
}

// Serious part: update now...
$DB->query("UPDATE " . TABLE_PREFIX . "usergroups SET categorymenuids = '" .
$categorymenuids . "'
WHERE usergroupid = " . $usergroup['usergroupid'] . ';');
}
// ...in case of an SQL error: it's message will land in the current table cell
echo '</td></tr>';
} //while
echo '</table>';
}
else
{
echo "No Usergroups found for processing.";
}

}
else
{
echo 'Invalid or no Category specified.';
}

echo '</td></tr>
<tr>
<td class="tdrow1" colspan="2" align="center">
<input type="submit" value="Back to Categories" />
</td>
</tr>
</table>
</form>';
EndSection();

} // 'updatecategorymenuids'



Last step: save and/or upload the file. :-)



Mass Media Pro Plugin - Live Demo : Mass Media Pro (http://www.sddepot.com/mass_media/)
My SDDepot Blog (http://www.sddepot.com/community/blog/tobias/) - SDDepot - Skins and Plugins (http://www.sddepot.com/)

recon
03-05-2007, 05:00 PM
Excellent work like a charm tested with old and new sd. Thank you!