[Tutorial] Add option to disable download category

Hello folks! I thought about to edit categories in the default download module and changed it to my needs. After you deactivate a download category, it wont show up in your list.

 

 

 

 

 

 

 

Important information: BEFORE do this tutorial, backup the database tables and files you’ll edit.

  • lib/skins/pandaair_theme/downloads_list.php
  • admin/modules/Downloads/Downloads.php
  • admin/templates/downloads_categoryform.php
  • core/common/DownloadData.class.php
  • database table  phpvms_downloads
  1. Open your database and run the following command. It will add a column to your downloads table. Be sure to add the right prefix for the table:

    ALTER TABLE phpvms_downloads ADD active INT NOT NULL DEFAULT ‘1’ AFTER hits;

 

2. Replace the content of the file “admin/templates/downloads_categoryform.php” with following:

\<?php if(!defined('IN\_PHPVMS') && IN\_PHPVMS !== true) { die(); } ?\> \<h3\>\<?php echo $title?\>\</h3\> \<form id="form" method="post" action="\<?php echo adminaction('/downloads/overview'); ?\>"\> \<dl\> \<dt\>Category Name\</dt\> \<dd\>\<input name="name" type="text" value="\<?php echo $category-\>name; ?\>" /\>\</dd\> \<dt\>Category Active? \<input name="active" type="checkbox" \<?= ($category-\>active) ? "checked":"" ?\> \<?= (empty($category-\>name)) ? "checked":"" ?\> style="margin-bottom: 20px;" /\>\</dt\> \<dt\>\</dt\> \<dd\>\<input type="hidden" name="id" value="\<?php echo $category-\>id;?\>" /\> \<input type="hidden" name="action" value="\<?php echo $action;?\>" /\> \<input type="submit" name="submit" value="\<?php echo $title;?\>" /\> \</dd\> \</dl\> \</form\>

We added the checkbox to enable/disable the category.

 

  1. Edit the file “core/common/DownloadData.class.php”

The following two functions have to be replaced: “AddCategory” and “EditAsset”:

public static function AddCategory($name, $link = '', $image = '', $active = 1) { if ($name == '') return false; $sql = "INSERT INTO " . TABLE\_PREFIX . "downloads (pid, name, link, image) VALUES (0, '$name', '$link', '$image', '$active')"; $res = DB::query($sql); if (DB::errno() != 0) return false; return true; }

public static function EditAsset($data) { /\*$data = array( 'id' =\> '' 'parent\_id' =\> '', 'name' =\> '', 'description' =\> '', 'link' =\> '', 'image' =\> '', ); \*/ if ($data['id'] == '' || $data['name'] == '') return false; $data['id'] = intval($data['id']); $data['name'] = DB::escape($data['name']); $data['parent\_id'] = intval($data['parent\_id']); $sql = "UPDATE " . TABLE\_PREFIX . "downloads SET `pid`={$data['parent\_id']}, `name`='{$data['name']}', `active`='{$data['active']}', `description`='{$data['description']}', link='{$data['link']}', image='{$data['image']}' WHERE id={$data['id']}"; $res = DB::query($sql); if (DB::errno() != 0) return false; return true; }

Well, we included there the “active” parameter from the checkbox, so it will be changed in the database.

 

  1. The file " admin/modules/Downloads/Downloads.php" contains two functions “AddCategoryPost” and “EditCategoryPost”. We have to replace them with the following:

    protected function AddCategoryPost() { $this->checkPermission(EDIT_DOWNLOADS); if ($this->post->name == ‘’) { $this->set(‘message’, ‘No category name entered!’); $this->render(‘core_error.php’); return; } if (DownloadData::FindCategory($this->post->name)) { $this->set(‘message’, ‘Category already exists’); $this->render(‘core_error.php’); return; } if(!empty($this->post->active)){ $active = 1; } else { $active = 0; } DownloadData::AddCategory($this->post->name, ‘’, ‘’, $active); $this->set(‘message’, ‘Category added!’); $this->render(‘core_success.php’); }

    protected function EditCategoryPost() { $this->checkPermission(EDIT_DOWNLOADS); if ($this->post->name == ‘’) { $this->set(‘message’, ‘No category name entered!’); $this->render(‘core_error.php’); return; } if(!empty($this->post->active)){ $active = 1; } else { $active = 0; } $data = array(‘id’ => $this->post->id, ‘name’ => $this->post->name, ‘parent_id’ => ‘’, ‘description’ => ‘’, ‘link’ => ‘’, ‘image’ => ‘’, ‘active’ => $active); DownloadData::EditAsset($data); $this->set(‘message’, ‘Category edited!’); $this->render(‘core_success.php’); }

That’s it for the “tricky” part. Now let’s hide the deactivated categories in the  core/templates/downloads_list.php.

Just search for the “foreach” loop of the categories and check if the category has “1” in the active parameter. If it doesn’t, don’t show the category:

foreach($allcategories as $category) { if(!$category-\>active) { continue; } // more stuff.....

 

Have fun and howdy!