Wiki source for AddLinkAction

Show raw source

=====Addlink Action=====
>>==See also:==
- Documentation: AddLinkActionInfo.
==works with:==
- Wikka (v0.1)
>>//NOT Included in any Wikka version//{{lastedit show="3"}}
This is the development page for the addlink action.

- Save the code below as ##action/addpage.php##
- Give it the same file permissions as the other php files in that directory.

===Code Walktrough===
- If you have write and read access:
- Read the current links and build arrays $al_data and $al_category.
- if $_POST["url"] is set:
add link to array, rebuild the layout, save the page and refresh the current page.
- if $_POST["url"] is NOT set:
print the html form.
- If you have DON'T write and read access:
do nothing / print error.

=== Code ===
* This action will allow you to fast add links in a structured way.
* @package Actions
* @subpackage Links
* @name AddLink
* @author {@link OnegWR} (v0.1)
* @version 0.1
* @copyright Copyright © 2005
* @license {@link GNU Lesser General Public License}
* @since Wikka
* @input NONE ( $_POST["category"], $_POST["url"], $_POST["title"] = string )
* @output html form
* @todo {@link AddLinkAction} (list)
$al_key_default = $al_key = "First"; // Default key
$al_category["First"] = "First"; // LookUpTable for 'safe'-key-names
$al_data = array(); // to store the links in
$al_select = ""; // $al_category in html-select format
$al_output = $al_last = ""; // rebuild the page for $this->SavePage()
if( $this->HasAccess("write") && $this->HasAccess("read") ){ // Admin only???
$al_page = $this->LoadPage($this->tag);
list($al_header,$al_links,$al_footer) = explode("----", $al_page["body"],3);
preg_match_all("/(==.*?==)|(\[\[.*?\]\])/", $al_links, $matches);
//print "\n<pre>\n"; print_r($matches); print "\n<pre>\n".count($matches[0])."\n";
foreach( $matches[0] as $al_n => $al_item ){
if( preg_match("/==(.*?)==/", $al_item, $match) ){
$al_key_tmp = preg_replace("/[^A-Za-z0-9]/","",$match[1]);
if($al_key_tmp <> ""){ // This would also ignore 'strange' titles
//print "\n<pre>\n"; print_r($al_data); print_r($al_category); print "\n<pre>\n";

if( isset($_POST["url"]) && $_POST["url"]<>"" ){ // add the link + save the page
if( $this->htmlspecialchars_ent($_POST["title"]) == "" ) {
$al_newlink = "[[".$this->cleanUrl($_POST["url"])."]]";
$al_newlink = "[[".$this->cleanUrl($_POST["url"])." ".$this->htmlspecialchars_ent($_POST["title"])."]]";
if( $this->htmlspecialchars_ent($_POST["category"]) == "" ){
$al_key = $al_key_default;
$al_key = $this->htmlspecialchars_ent($_POST["category"]);
$al_data[$al_key][] = $al_newlink; // Goal of this script: Add the link to the array

if(isset($al_data["First"])){ // Here the layout is re-build
$al_output .= "==First==\n~".implode(" ",$al_data["First"])."\n";
if(isset($al_data["Last"])) {
$al_last .= "==Last==\n~".implode(" ",$al_data["Last"])."\n";
foreach($al_data as $al_k => $al_v){
$al_output .= "==".$al_category[$al_k]."==\n~".implode(" ",$al_data[$al_k])."\n";
$al_output .= $al_last;
$this->SavePage($this->tag, $al_header."----\n".$al_output."----".$al_footer, "Link added in category $al_key");
//print "ok";
//print "\n<pre>\n"; print_r($al_data); print "\n<pre>\n";
}else{ // print the form
print $this->FormOpen('',$this->tag);
foreach($al_category as $al_cat_safe => $al_cat_full){
$al_select.="<option value=\"".$al_cat_safe."\">".$al_cat_full."</option>";
<table border="0" cellpadding="3" cellspacing="0">
<td rowspan="2" valign="top"><select name="category" size="3"><?php print $al_select; ?></select></td>
<td>Url: </td>
<td valign="top"><input type="text" size="50" maxlength="255" name="url" value="<?php print $_GET["url"]; ?>"></td>
<td rowspan="2" valign="bottom"><input type=submit name=submit value="Add"></td>
<td valign="bottom"><input type="text" size="50" maxlength="255" name="title" value="<?php print $_GET["title"]; ?>"></td>
print $this->FormClose();
//print "\n<!-- ERROR: You don't have the needed rights for accessing the action addlink -->\n";
=== To Do ===
- Test: how is this script behaving with an German, Chinese,... char set?
- Layout: How to structure the links?
- Code: is there an easy way to add 2 or 3 levels in the titles (like when you save your bookmark as an html file).
- CSS: convert the 'table'-form into pure css layout.
- HTML: What is the max length of an "option" in a "select"?
- Add a switch: "Last added", FIFO with the last ?10? added links...
- Add more checks: is the link or refer in the blacklist?,...

=== Beta ===
- I'm experimenting with javascript code to auto add the url and tilte into the form via $_GET variables. So adding a link would only take two mouse-clicks...
- javascript:void(window.location.href=''+escape(window.location.href)+'&title='+escape(document.title))
- I don't think it's possible to make this link browser independent, so we'll end up with a few links like this...

Valid XHTML :: Valid CSS: :: Powered by WikkaWiki