Page 1 of 1

Form methods within serendipity and drop down menu plugins

Posted: Tue Feb 06, 2007 2:23 pm
by spiritquest
Hi,

I am creating a series of plugins either from scratch or based on existing internal plugins like the archives plugin.

One thing I have changed in this is to create a drop down menu from the archive list instead of a physical list.

I have achieved this through the use of the serendipity_generateCategoryList function:

I am using this above function as I don't want to provide a complete list of categories. Only the ones that have posts. That part of the code works fine.

eg.

Code: Select all

		echo '<form name="drop_down_categories"><select><option value="">browse categories</option>';
		echo serendipity_generateCategoryList($catArray, array(0), 2,17,1);
		echo '</select></form>';
So here is my challenge/question:

To access these categories the URL:

http://<base install>/index.php?/categories/<cat_num>-<catn_name>

My question is how do I use the form method to load the relevant category pages when they are clicked within the drop down ...

I am also a little unsure as to how I use the second array parameter of the function, I know this is important for generating the functionality.



Secondly, and slightly related.


I am using the static page module, and want to provide drop down menu support for certain pages, I think knowing the asnwer to the above would help me with that solution.

Many thanks.

Re: Form methods within serendipity and drop down menu plugi

Posted: Tue Feb 06, 2007 2:39 pm
by garvinhicking
Hi!

For the first thing, try a code like this:

Code: Select all

		echo '<form id="dropdown" name="drop_down_categories" action="index.php"><select onchange="document.getElementById(\'dropdown']).submit()" name="serendipity[category]"><option value="">browse categories</option>';
		echo serendipity_generateCategoryList($catArray, array(0), 2,17,1);
		echo '</select></form>';
So you must:

1. specify the form target
2. Name the element with the $serendipity['GET']['category'] variable
3. Add a javascript onchange handler to submit a form when the select dropdown is changed
I am using the static page module, and want to provide drop down menu support for certain pages, I think knowing the asnwer to the above would help me with that solution.
That is harder. You would need a javascript function that will set the target URL (self.location.href) to the permalink URL pattern of the staticpage.

Best regards,
Garvin

Posted: Tue Feb 06, 2007 2:54 pm
by spiritquest
Thanks for the quick reply Garvin,

I will implement your notes into the plugin, I think you answered my questions on the categories, a great help.
That is harder. You would need a javascript function that will set the target URL (self.location.href) to the permalink URL pattern of the staticpage.
Is there a recomended file to incoroprate custom javascript functions that the plugin will call, or do i need to include the function within the template file ?

I'll play around though .. I've got a fair bit to be getting on with :)

Cheers.

Posted: Tue Feb 06, 2007 3:05 pm
by garvinhicking
Hi!
Is there a recomended file to incoroprate custom javascript functions that the plugin will call, or do i need to include the function within the template file ?
I propose to edit the serendipity_plugin_staticpage file, and instead of the place where it uses '<a href=....' you will put your <option value='... with the desired location. Then something like this should be used:

Code: Select all

<script type="text/javascript">
function goToFormURL() {
  self.location.href = document.getElementById('staticpage').options[document.getElementById('staticpage').selectedIndex].value;
}
</script>

<form...>
<select id="staticpage" name="staticpage" onchange="goToFormURL()">
<option value="/pages/bio.html">My bio</option> 
<option value="/pages/faq.html">FAQ</option>
</select>
</form>
Untested, of course. *g*

Best regards,
Garvin

Posted: Wed Feb 07, 2007 11:40 am
by spiritquest
Hi Garvin,

Having some interesting developments here:


Here is what I have had to do to get the URL argument decoded, however to get a location.href to run smoothly and also using the example idea you gav me above to work so that the category shows up in teh header. I've had to do this.


The URL has to conform to the permaLink Category format.

So I've done this:

Code: Select all

	if (isset($_GET['cats'])){ // Checks if the input was fom the form

	
			$catURLdecode = rawurldecode($serendipity['plugindata']['smartyvars']['uriargs']);
			$catURL = ereg_replace ('index\.php\?cats=', 'index.php?', $catURLdecode);
						$replaceURL = $serendipity['baseURL'].$catURL;
			
	echo '	<script type="text/javascript" language="javascript">
<!--
location.href="'.$replaceURL.'";
//-->
</script>'; 
			//exit;
		}
It's a long way round I know, but i don't know of any other way, its not that great on a user level either as a blank page appears momentarily before directing the user to the page, but it does show the correct category header.

I've used ereg_replace to reformat the URI correctly.

This is pretty much how I would have to do things for the archives, due to the fact that the forward slashes come out encoded when set as values in a form.

Posted: Wed Feb 07, 2007 12:07 pm
by garvinhicking
Hi!

If you use the category plugin as a base, you should see the permalink pattern there as well that you can use, if you want to omit the category ID. The category plugin does output the right link, so you can use that to adapt to the right location.

Using that redirect method also looks a bit ugly to me :)

Best regards,
Garvin

Posted: Wed Feb 07, 2007 12:16 pm
by spiritquest
Hi Garvin,

The problem with the category plugin is that its hard coding the link as an anchor on the page.

Its the fact that the value is being generated as a form, which is not the default method used by the category plugin.

I'm actually not using the category plugin as my base, but I will take a look through it and see if I can get some info from it, it seems that the multiCat option is something I can use for it.

Posted: Wed Feb 07, 2007 12:51 pm
by garvinhicking
Hi!

Yes, but the category plugin can be templated using the plugin_categories.tpl file so that you could use <option> instead of <a>...?

HTH,
Garvin

Posted: Wed Feb 07, 2007 1:06 pm
by spiritquest
Hey Garvin,

I see .. I will have to look into the templating.

The major difference between my needs for the system I'm setting up and the default categories plugin is that I must only show categories that have posts in them, any blank categories should be omitted from the list.

There doesn't seem to be a configuration option for this, I could alter the plugin, but I started by doing this, the following code is everything up to present time:

Code: Select all

	
$activeCats = serendipity_db_query("SELECT DISTINCT (categoryid) from serendipity_entrycat");
		
	echo '<form id="drop_down_categories" name="drop_down_categories" action="' . $serendipity['baseURL'] . $serendipity['indexFile'] . '?frontpage" method="post">
		<select onchange="document.drop_down_categories.submit()" name="serendipity[multiCat][]">				
			
		for($i=0; $i < count($activeCats); $i++){
			$catinfo = serendipity_fetchCategoryInfo($activeCats[$i]['categoryid']);
				
			echo '<option  value="'.$catinfo['categoryid'].'">'.$catinfo['category_name'].'</option>';

		}

		echo '</select>
<input type="hidden" name="serendipity[isMultiCat]" value="'. GO .'">
</form>';
However, I am still exploring whats possible with serendipity and am getting my head arouns the whole structure, so .tpl editing could be very useful, I know I'll be needing to make some alterations to the overall layout soon. I am going to make the changes to the .tpl you proposed here, and see how it differs from the plugin I created using the above code. :)

Cheers !

Posted: Wed Feb 07, 2007 1:55 pm
by garvinhicking
Hi!

You should definitely inspect that categories plugin (found in include/plugins_internal.inc.php). It has an option to count entries within each category! You can add a check for > 0 within the Smarty .tpl file :)

HTH and have fun,
Garvin