Only allow News sub category to select from on front end

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
Duketown

Only allow News sub category to select from on front end

Post by Duketown »

Hi,

I was struggling with the front end submission of an article. I have set up multiple categories in the News (version 2.11 with Core 1.9.2). Some of the categories have sub categories. One of the parent categories is called Review and has subcategories. The 'problem' I was facing was that I wanted to use front end entry of articles, but only allow the visitor/user to select from the sub categories of parent category Review.
As a programmer I thought of changing the code of action.fesubmit.php in the news module. However that is not upgrade friendly. I used in stead a combination of the smarty and a UDT.

In the front end template of news, you will find the following for the category selection:

Code: Select all

	<div class="pageoverflow">
		<p class="pagetext">{$categorytext}:</p>
		<p class="pageinput">{$inputcategory}</p>
	</div>
I needed to intercept the $inputcategory variable and amend that. For that I used the following UDT:

Code: Select all

// The list of categories build in this UDT will be used in the front end submit of reviews, to only allow 
// selection of module names. This leaves room to have news about the site or news coming from
// external sources to be booked in a different category
// UDT RebuildInputCategory by Duketown 2011
$inputcategory = $params['inputcategory']; 
$parentno = $params['parentno'];

// Intercept the inputcategory given by the News module.
// It also contains the return id and security id. By substringing it, we don't have to worry about that
// You might have to alter the total length of 57
$inputctg = substr($inputcategory, 0, 57);

// Build the category list
$db = cmsms()->GetDb();
$query = "SELECT * FROM ".cms_db_prefix()."module_news_categories 
   WHERE parent_id = ?";
$dbresult = $db->Execute($query, array($parentno));

// Read all the categories and form options from each of them
while ($dbresult && $row = $dbresult->FetchRow()) {
    $inputctg .= '<option value="' . $row['news_category_id'] . '">';
    $inputctg .= $row['news_category_name'];
    $inputctg .= '</option>';
}
$inputctg .= '</select>';
return $inputctg;
As you see two parameters are to be passed to this UDT RebuildInputCategory. The first being the original content of $inputcategory (that is prepared by the News module. The second is the id of the parent. You can find the value that you need by editing the parent category. Look in the address bar (the URL) of your browser. The last part will contain '&m1_catid=2' (the 2 might of course be different from your parent id).
Notice that the last line of the UDT contains the variable with the option set and that it is given back to the calling part.

Now that the UDT is prepared, go back to the form template. We have to change the {$inputcategory} into the following:

Code: Select all

{RebuildInputCategory inputcategory=$inputcategory parentno="2"}
See, I pass the two parameters to the UDT:
The first being the variable passed from the News module and the second being the parent number as found during editing the parent category.
The parentno parameter will sure have something else in your situation.

Happy UDT-ing,
Duketown
;D Donations welcome whenever we meet
Post Reply

Return to “Tips and Tricks”