Please login or register.

Login with username, password and session length
Advanced search  

News:

You need/want an older version of sNews ? Download an older/unsupported version here.

Author Topic: [WIP] Assigning Content as "Uncategorized" after Deleting a Category  (Read 1866 times)

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6020
  • Semantically Challenged
    • snews.ca

Applies to MEMU and MESU Packages
This modification applies to MEMU and MESU Packages that do not have the SubCategories mods added, tho I am developing it to run with an MEMU+++ project that assigns both articles and pages to Categories & SubCategories.

I started out looking for a better solution to the Bug wherein... after deleting a Category without re-locating its associated content beforhand... the content (articles or pages assigned to the deleted category) would no longer be accessible by the Admin yet they would still be stored in the articles table in the database.

My first solution (a quick fix, currently applied in the official 1.6 download package) was to add in a message that appears when the Admin clicks the Category "Delete" button, warning the Admin to go back and re-locate the content or delete it. It also disables the "Delete" button until the Category is empty.

However... my MEMU+++ project also uses a modified version of Philmoz's // ARTICLES - ADMIN LIST Admin Panel mod, which displays content View/Edit links by expandable category titles. It only displays content assigned to Categories (and sucategories). I wanted to be able to delete a category without dealing with warning messages or worrying about lost content. This mod became the solution (for me) and it is a work in progress.

Thanks to a suggestion from Joost... this began with a simple query and string to show only content with published status of "3" added to the top of Philmoz's modified function. Joost suggested the idea of assigning published status of "3" to content when the category is being deleted. I then added a few more strings to do a few more things.
1 - When a category is deleted, any of its associated content gets updated with published status of "3" (uncategorized).
2 - The Uncategorized Panel does not show (is invisible) until there is at least one uncategorized article
     (or page as this project assigns pages to cats too) that has published status of "3".
3 - When there are uncategorized content items, the "Edit" link also shows whether it is an article or page.
4 - Once all the content titles are re-located to other categories (by editing), the Panel is no longer visible.



This section generates the Panel:
Code: [Select]
<?php

#======= "Uncategorized" (orphaned) links mod July 22.08 ==============
$query "SELECT * FROM ".db('prefix')."articles WHERE published = '3'";
$result mysql_query($query);
if (!$result || !mysql_num_rows($result)) {echo ''; } else {
echo '<div class="otherpanels"><div class="admintitle">'.l('uncategorized1').'</div>'.l('uncategorized2').'';
while($r mysql_fetch_array($result)){
   if ($r['published'] == 3) {
      echo '<p><strong>'.$r['title'].'</strong>&nbsp;.&nbsp;<a href="'.db('website').'index.php?action=admin_article&amp;id='.$r['id'].'">Edit ';
      if ($r['position'] == 3) { echo 'page'; } else if ($r['position'] == 1) { echo 'article'; }
      echo '</a>&nbsp;.&nbsp; '.date(s('date_format'), strtotime($r['date']));
      echo  l('divider').' ['.l('status').' '.l('orphan').']';
      echo '</p>';
   }
}
echo '</div>';
}

?>

This new function, modified from 1.7RC for 1.6, is used to handle Category Deletion:
Code: [Select]
<?php

// DELETE CATEGORY BY ID ========== JULY 17.08 - added & modded from 1.7RC for 1.6 with subcats mods
function delete_cat($id){
$catdata mysql_fetch_array(mysql_query("SELECT catorder,subcat FROM ".db('prefix')."categories WHERE id = $id"));
$cat_order $catdata['catorder'];
$cat_subcat $catdata['subcat'];
mysql_query("DELETE FROM ".db('prefix')."categories WHERE id = $id LIMIT 1;");
# "Uncategorized" - 1 string: sets published status of deleted Cat's articles to 3 (Joost, July 22.08)
mysql_query("UPDATE ".db('prefix')."articles SET published=3 WHERE category= $id");
$query mysql_query("SELECT id,catorder FROM ".db('prefix')."categories WHERE catorder > $cat_order AND subcat = $cat_subcat");
while ($r mysql_fetch_array($query)) {
mysql_query("UPDATE ".db('prefix')."categories SET catorder = catorder - 1 WHERE id = '$r[id]'");
}
}

?>

The query string within the "case" responsible for deleting categories in function processing() is replaced with a call to the above function:

Code: [Select]
<?php

case(isset($_POST['delete_category'])):
# JULY 22.08 - query string replaced by function delete_cat($id);
//mysql_query("DELETE FROM ".db('prefix')."categories WHERE id = $id LIMIT 1;");
delete_cat($id);
break;

<?

And the following language variable/values are added to the language variable array:

Code: [Select]
<?php

# "Uncategorized" (orphaned) links mod July 22.08
$l['orphan'] = '<span style="color: #9c0000;">Uncategorized</span>';
$l['uncategorized1'] = 'Un-categorized Content:';
$l['uncategorized2'] = 'The content listed below once belonged to a category that you recently deleted.<br />Click "Edit" for any content title to re-locate it now.<br />';

<?


« Last Edit: July 23, 2008, 02:38:14 pm by Keyrocks »
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

funlw65

  • Hero Member
  • *****
  • Karma: 96
  • Posts: 771
    • Country Lab
Re: [WIP] Assigning Content as "Uncategorized" after Deleting a Category
« Reply #1 on: July 22, 2008, 10:26:58 pm »

seems a good solution for 1.6 version. I like it.
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6020
  • Semantically Challenged
    • snews.ca
Re: [WIP] Assigning Content as "Uncategorized" after Deleting a Category
« Reply #2 on: July 23, 2008, 07:36:43 pm »

NOTE: A simplified version of this Mod has been added to the current Official News16 Download (updated July 23.08) so that it works with the existing default Admin interface and default functions. It now replaces (supercedes) the previous Admin Warning message Patch.
« Last Edit: July 23, 2008, 07:38:31 pm by Keyrocks »
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU