Linklist Plugin

Creating and modifying plugins.
Post Reply
lauterunfug
Regular
Posts: 54
Joined: Wed Mar 16, 2005 9:26 am

Linklist Plugin

Post by lauterunfug »

I am using the linklist plugin, included in the sidebar with EventWrapper.

It's working all right, except that there is a horizontal line too much in it.

Image

Does anyone knows the reason for that?
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Linklist Plugin

Post by garvinhicking »

Yes. Do not configure the title of that plugin using the title of the event wrapper plugin, but specify the title within the linklist plugin.

Look at the HTML code to see that both plugins try to emit their title, and thus the doubling.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
lauterunfug
Regular
Posts: 54
Joined: Wed Mar 16, 2005 9:26 am

Post by lauterunfug »

so, I have two possibilities to write a titel:
a) in the linklistplugin itself
b) in the event wrapper plugin

thus the two lines.

When I put a title only in b) it looks like:
Image

When I put a title only in a) it looks like:
Image

so there are always these two lines.. doesn't matter where I put the title. Or am I wrong?
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

As a matter of fact, yes. You were right. I just committed a fix to the linklist plugin so that if the title is empty, it will not print the title.

As soon as you use that version (0.41) you can define the title in the event wrapper again, and then you shouldn't get another duplicate title.

HTH,
Garvin

(BTW, if you want to apply it manually, here's the diff for serendipity_event_linklist.php:

Code: Select all

     function generate_output() {
         global $serendipity;
-        $output .= '<h3 class="serendipitySideBarTitle serendipity_event_linklist">'.$this->get_config('title').'</h3>';
+        $t = $this->get_config('title');
+        if (!empty($t)) {
+            $output .= '<h3 class="serendipitySideBarTitle serendipity_event_linklist">'.$this->get_config('title').'</h3>';
+        }
(The lines with '+' got added, the ones with '-' removed/replaced.)

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
lauterunfug
Regular
Posts: 54
Joined: Wed Mar 16, 2005 9:26 am

Post by lauterunfug »

Perfect! You're the man! Thanx to you!
lauterunfug
Regular
Posts: 54
Joined: Wed Mar 16, 2005 9:26 am

Post by lauterunfug »

and for those, who are looking for the whole code for the linklist plugin with the fixed code from garvinhicking:

Code: Select all

<?php # $Id#

// Probe for a language include with constants. Still include defines later on, if some constants were missing
$probelang = dirname(__FILE__) . '/lang_' . $serendipity['lang'] . '.inc.php';
if (file_exists($probelang)) {
    include $probelang;
}

switch ($serendipity['lang']) {
    case 'en':
    default:
        @define('PLUGIN_EVENT_LINKLIST_TITLE', 'Link List');
        @define('PLUGIN_EVENT_LINKLIST_DESC', 'Maintain a Link List for display.');
        @define('PLUGIN_EVENT_LINKLIST_LINK', 'Link');
        @define('PLUGIN_EVENT_LINKLIST_LINK_NAME', 'Name');
        @define('PLUGIN_EVENT_LINKLIST_ADMINLINK', 'Manage Links');
        @define('PLUGIN_EVENT_LINKLIST_ORDER', 'Order links by:');
        @define('PLUGIN_EVENT_LINKLIST_ORDER_DESC', 'Choose how to order the links for display.');
        @define('PLUGIN_EVENT_LINKLIST_ORDER_NUM_ORDER', 'Custom');
        @define('PLUGIN_EVENT_LINKLIST_ORDER_DATE_ACS', 'Date (Oldest to Newest)');
        @define('PLUGIN_EVENT_LINKLIST_ORDER_DATE_DESC', 'Date (Newest to Oldest)');
        @define('PLUGIN_EVENT_LINKLIST_ORDER_CATEGORY', 'Categorically');
        @define('PLUGIN_EVENT_LINKLIST_ORDER_JSCATEGORY', 'Categorically, with Javascript');
        @define('PLUGIN_EVENT_LINKLIST_ORDER_ALPHA', 'Alphabetically');
        @define('PLUGIN_EVENT_LINKLIST_LINKS', 'Manage Links');
        @define('PLUGIN_EVENT_LINKLIST_NOLINKS', 'No Links in List');
        @define('PLUGIN_EVENT_LINKLIST_TITLEDESC','The title of the plugin. The value is passed to sidebar wrapper.');
        @define('PLUGIN_EVENT_LINKLIST_CATEGORY', 'Use categories');
        @define('PLUGIN_EVENT_LINKLIST_CATEGORYDESC','Use categories to organize links.');
        @define('PLUGIN_EVENT_LINKLIST_ADDLINK','Add a Link');
        @define('PLUGIN_EVENT_LINKLIST_EDITLINK','Edit a Link');
        @define('PLUGIN_EVENT_LINKLIST_LINKDESC','Description of Link');
        @define('PLUGIN_EVENT_LINKLIST_DEFAULT_NOTE','Please note, this is an event plugin and must either use the Event Output Wrapper, or a custom Sidebar to show sidebar list.');
        @define('PLUGIN_EVENT_LINKLIST_CATEGORY_NAME','Category system to use:');
        @define('PLUGIN_EVENT_LINKLIST_CATEGORY_NAME_DESC','You can choose to use the blog category system, or the custom categories provided with this plugin.');
        @define('PLUGIN_EVENT_LINKLIST_CATEGORY_NAME_CUSTOM','Custom');
        @define('PLUGIN_EVENT_LINKLIST_CATEGORY_NAME_DEFAULT','Default');
        @define('PLUGIN_EVENT_LINKLIST_CATDB_WARNING','You are configured to use custom categories, but the category database does not exist. Please click here to create the database.');
        @define('PLUGIN_EVENT_LINKLIST_ADD_CAT','Manage categories');
        @define('PLUGIN_EVENT_LINKLIST_CAT_NAME','Category Name');
        @define('PLUGIN_EVENT_LINKLIST_PARENT_CATEGORY','Parent Category');
        @define('PLUGIN_EVENT_LINKLIST_ADMINCAT','Administer Categories');
        @define('PLUGIN_EVENT_LINKLIST_CACHE_NAME','Cache sidebar');
        @define('PLUGIN_EVENT_LINKLIST_CACHE_DESC','Caching the sidebar results increases the speed of your page.');
        break;
}

class serendipity_event_linklist extends serendipity_event {

    var $title = PLUGIN_EVENT_LINKLIST_TITLE;

    function introspect(&$propbag) {
        global $serendipity;

        $propbag->add('name', PLUGIN_EVENT_LINKLIST_TITLE);
        $propbag->add('description', PLUGIN_EVENT_LINKLIST_DESC);
        $propbag->add('event_hooks',  array('backend_sidebar_entries_event_display_linklist'  => true,
                                            'backend_sidebar_entries'                         => true
                                            ));
        $propbag->add('author', 'Matthew Groeninger');
        $propbag->add('version', '0.4');
        $propbag->add('requirements',  array(
            'serendipity' => '0.8',
            'smarty'      => '2.6.7',
            'php'         => '4.1.0'
        ));
        $propbag->add('configuration', array('note','title','display','category','cache'));
        $propbag->add('stackable',     false);

    }

    function introspect_config_item($name, &$propbag)
    {
        switch ($name) {
            case 'note':
                $propbag->add('type',    'content');
                $propbag->add('default', PLUGIN_EVENT_LINKLIST_DEFAULT_NOTE);
                break;
            case 'display':
                $select = array();
                $select["alpha"] = PLUGIN_EVENT_LINKLIST_ORDER_ALPHA;
                $select["category"] = PLUGIN_EVENT_LINKLIST_ORDER_CATEGORY;
                $select["js_category"] = PLUGIN_EVENT_LINKLIST_ORDER_JSCATEGORY;
                $select["order_num"] = PLUGIN_EVENT_LINKLIST_ORDER_NUM_ORDER;
                $select["datedesc"] = PLUGIN_EVENT_LINKLIST_ORDER_DATE_DESC;
                $select["dateacs"] = PLUGIN_EVENT_LINKLIST_ORDER_DATE_ACS;
                $propbag->add('type', 'select');
                $propbag->add('name', PLUGIN_EVENT_LINKLIST_ORDER);
                $propbag->add('description', PLUGIN_EVENT_LINKLIST_ORDER_DESC);
                $propbag->add('select_values', $select);
                break;
            case 'title':
                $propbag->add('type',        'string');
                $propbag->add('name',        TITLE);
                $propbag->add('description', PLUGIN_EVENT_LINKLIST_TITLEDESC);
                $propbag->add('default', '');
                break;
            case 'category':
                 $propbag->add('type',          'radio');
                 $propbag->add('name', PLUGIN_EVENT_LINKLIST_CATEGORY_NAME);
                 $propbag->add('description', PLUGIN_EVENT_LINKLIST_CATEGORY_NAME_DESC);
                 $propbag->add('radio',
                     array( 'value' => array('custom','default'),
                     'desc'  => array(PLUGIN_EVENT_LINKLIST_CATEGORY_NAME_CUSTOM,PLUGIN_EVENT_LINKLIST_CATEGORY_NAME_DEFAULT)
                     ));
                 $propbag->add('radio_per_row', '2');
                 $propbag->add('default', 'custom');
                break;
            case 'cache':
                 $propbag->add('type',          'radio');
                 $propbag->add('name', PLUGIN_EVENT_LINKLIST_CACHE_NAME);
                 $propbag->add('description', PLUGIN_EVENT_LINKLIST_CACHE_DESC);
                 $propbag->add('radio',
                     array( 'value' => array('yes','no'),
                     'desc'  => array(YES,NO)
                     ));
                 $propbag->add('radio_per_row', '2');
                 $propbag->add('default', 'no');
                break;


            default:
               return false;
        } 
        return true;
    }

    function event_hook($event, &$bag, &$eventData, $addData = null) {
        global $serendipity;

        $hooks = &$bag->get('event_hooks');

        if (isset($hooks[$event])) {
            switch($event) {
                case 'backend_sidebar_entries_event_display_linklist':

                    if ($this->get_config('category') == 'custom' && $this->get_config('catbd')!= 'done') {
                        echo '<a href="?serendipity[adminModule]=event_display&serendipity[adminAction]=linklist&submit=create_custom">'.PLUGIN_EVENT_LINKLIST_CATDB_WARNING.'</a>';
                    }
                    if (isset($_POST['REMOVE'])) {
                        if (isset($_POST['serendipity']['link_to_remove'])) {
                            foreach ($_POST['serendipity']['link_to_remove'] as $key) {
                                $this->del_link($key);
                            }
                        } else {
                            if (isset($_POST['serendipity']['category_to_remove'])) {
                                foreach ($_POST['serendipity']['category_to_remove'] as $key) {
                                    $this->del_category($key);
                                }
                            }
                        }
                    }

                    if (isset($_POST['SAVE'])) {
                        foreach ($_POST['serendipity']['link_to_recat'] AS $key => $row) {
                            $this->update_cat($key,$row);
                        }
                    }

                    if (isset($_POST['ADD'])) {
                       if (isset($_POST['serendipity']['add_link']['title']) && isset($_POST['serendipity']['add_link']['link'])) {
                            $this->add_link($_POST['serendipity']['add_link']['link'],$_POST['serendipity']['add_link']['title'],$_POST['serendipity']['add_link']['desc'],$_POST['serendipity']['link_to_recat']['cat']);
                       } else {
                           if (isset($_POST['serendipity']['add_category']['title'])) {
                               $this->add_cat($_POST['serendipity']['add_category']['title'],$_POST['serendipity']['link_to_recat']['cat']);
                           }
                       }                       
                    }

                    if (isset($_POST['EDIT'])) {
                       if (isset($_POST['serendipity']['add_link']['title']) && isset($_POST['serendipity']['add_link']['link'])&& isset($_POST['serendipity']['add_link']['id'])) {
                            $this->update_link($_POST['serendipity']['add_link']['id'],$_POST['serendipity']['add_link']['link'],$_POST['serendipity']['add_link']['title'],$_POST['serendipity']['add_link']['desc'],$_POST['serendipity']['link_to_recat']['cat']);
                       }
                    }
                    switch ($_GET['submit']){
                        case 'move up':
                            $this->move_up($_GET['serendipity']['link_to_move']);
                        break;

                        case 'move down':
                            $this->move_down($_GET['serendipity']['link_to_move']);
                        break;

                        case 'create_custom':
                            $this->create_cattable();
                        break;
                    }
                    if ($this->get_config('cache') == 'yes') {
                        $output = $this->generate_output();
                        $this->set_config('cached_output',$output);
                    }
                    if (isset($_GET['serendipity']['edit_link'])) {
                        $this->output_add_edit_linkadmin(TRUE,$_GET['serendipity']['edit_link']);
                    } else {
                        if (isset($_GET['serendipity']['manage_category'])) {
                            $this->output_categoryadmin(TRUE,$_GET['serendipity']['edit_link']);
                        } else {
                            $this->output_add_edit_linkadmin(FALSE);
                            $this->output_linkadmin();
                        }
                    }
                    return true;
                    break;

                case 'backend_sidebar_entries':
                        echo '<li><a href="?serendipity[adminModule]=event_display&serendipity[adminAction]=linklist">'.PLUGIN_EVENT_LINKLIST_ADMINLINK.'</a></li>';
                    return true;
                    break;

                default:
                    return false;
                    break;
            }
        } else {
            return false;
        }
    }

    function generate_content(&$title) {
        if ($this->get_config('cache') == 'yes') {
            $output = $this->get_config('cached_output');
            if (!isset($output)) {
                $output = $this->generate_output();
                $this->set_config('cached_output',$output);
            }
               echo $output;
        } else {
            echo $this->generate_output();
        }
    }

    function generate_output() {
        global $serendipity;
	      $t = $this->get_config('title');
    	    if (!empty($t)) {
        	    $output .= '<h3 class="serendipitySideBarTitle serendipity_event_linklist">'.$this->get_config('title').'</h3>';
        	}       

//$output .= '<h3 class="serendipitySideBarTitle serendipity_event_linklist">'.$this->get_config('title').'</h3>';
        $display = $this->get_config('display');

        if ($display == 'category' || $display == 'js_category') {
            if ($this->get_config('category') == 'custom') {
                $table = $serendipity['dbPrefix'].'link_category';
            } else {
               $table = $serendipity['dbPrefix'].'category';
            }
            switch ($display) {
                case 'js_category':
                         $output .= '<script type="text/javascript">
                             function show(el) {
                                if (document.getElementById(el).style.display == \'none\') {
                                    document.getElementById(el).style.display = \'block\';
                                } else {
                                    document.getElementById(el).style.display = \'none\';
                                }
                             }
                             </script>';
                         break;
            }
            $output .= $this->category_output($table,0,0,$display);
        } else {
            $q = $this->set_query($display);
            $sql = serendipity_db_query($q);

            if ($sql && is_array($sql)) {
                foreach($sql AS $key => $row) {
                    $name = $row['name'];
                    $link = $row['link'];
                    $id = $row['id'];
                    $output .=  '<a href=http://'.$link.'>'.$name.'</a><br />';
                }
            }
        }
        return $output;
    }

    function category_output($table,$catid,$level,$tags)
    {
        global $serendipity;
        $output = '';
        $indent_int = $level*5+20;

        switch ($tags) {
            case 'js_category':
                    $category = '<div><a href="#" onclick="show(\'_catname_\'); return false" style="text-decoration:none">_catname_</a></div>'."\n";
                    $open_category = '<div style="margin-left:'.$indent_int.'px" id="_catname_" style="display: none">'."\n";
                    $close_category ='</div>'."\n";
                    $link_style = '<a href=http://_link_>_name_</a><br \>'."\n";
                break;
            case 'category':
                    $category = '<div>_catname_</div>'."\n";
                    $open_category = '<div style="margin-left:'.$indent_int.'px" id="_catname_">'."\n";
                    $close_category ='</div>'."\n";
                    $link_style = '<a href=http://_link_>_name_</a><br \>'."\n";
               break;
            case 'xml':
                    $category = ''."\n";
                    $open_category = '<dir name="_catname_">';
                    $close_category ='</dir>'."\n";
                    $link_style = '<link name="_name_" link="http://_link_" />'."\n";
                break;
        }

        if ($level == 0) {
            $catid = $level;
        } else {
            $q = 'SELECT s.* FROM '.$table.' AS s WHERE categoryid='.$catid.' ORDER BY s.category_name ASC';
            $sql = serendipity_db_query($q);
            if ($sql && is_array($sql)) {
                $replace_name = "/_catname_/";
                $cat_name = $sql[0]['category_name'];

                $category = preg_replace($replace_name,$cat_name,$category);
                $open_category = preg_replace($replace_name,$cat_name,$open_category);

                $output .= $category;
                $output .= $open_category;
            }
        }

        $q = 'SELECT s.* FROM '.$table.' AS s WHERE parentid='.$catid.' ORDER BY s.category_name ASC';
        $sql = serendipity_db_query($q);
        if ($sql && is_array($sql)) {
            foreach($sql AS $key => $row) {
                 $output .= $this->category_output($table,$row['categoryid'],$level+1,$tags);
            }
        }
        $q = 'SELECT     s.link              AS link,
                         s.title             AS name,
                         s.category          AS cat_id,
                         s.id                AS id
                         FROM    '.$serendipity['dbPrefix'].'links AS s
                         WHERE    s.category='.$catid.' ORDER BY s.title ASC';
        $sql = serendipity_db_query($q);
        if ($sql && is_array($sql)) {
            foreach($sql AS $key => $row) {
                $link_out = $link_style;
                $name = $row['name'];
                $link = $row['link'];
                $id = $row['id'];

                $replace_linkname = "/_name_/";
                $replace_link = "/_link_/";
                
                $link_out = preg_replace($replace_linkname,$name,$link_out);
                $link_out = preg_replace($replace_link,$link,$link_out);
                $output .=  $link_out;
            }
        }
        $output .= $close_category;
        return $output;
    }



    function cleanup() {
        if ($this->get_config('cache') == 'yes') {
            $output = $this->generate_output();
            $this->set_config('cached_output',$output);
        }
        return true;
    }

    function install() {
        global $serendipity;
        // Create table
        $q   = "CREATE TABLE ".$serendipity['dbPrefix']."links (
                    id {AUTOINCREMENT} {PRIMARY},
                    date_added int(10) {UNSIGNED} NULL,
                    link varchar(250) default NULL,
                    title varchar(250) default NULL,
                    descrip text,
                    order_num int(4),
                    category int(11),
                    last_result int(4),
                    last_result_time int(10) {UNSIGNED} NULL,
                    num_bad_results int(11)      
                )";

        $sql = serendipity_db_schema_import($q);

        $q   = "CREATE INDEX dateind ON {$serendipity['dbPrefix']}links (date_added);";
        $sql = serendipity_db_schema_import($q);

        $q   = "CREATE INDEX titleind ON {$serendipity['dbPrefix']}links (title);";
        $sql = serendipity_db_schema_import($q);

        $q   = "CREATE INDEX catind ON {$serendipity['dbPrefix']}links (category);";
        $sql = serendipity_db_schema_import($q);

        $this->create_cattable();
    }

    function create_cattable() {
 
        global $serendipity;
        $q   = "CREATE TABLE ".$serendipity['dbPrefix']."link_category (
                    categoryid {AUTOINCREMENT} {PRIMARY},
                    category_name varchar(255) default NULL,
                    parentid int(11) default 0
                )";
        $sql = serendipity_db_schema_import($q);
        $this->set_config('catbd','done');
    }


    function uninstall() {
        global $serendipity;
        // Drop table
        $q   = "DROP TABLE ".$serendipity['dbPrefix']."links";
        $sql = serendipity_db_schema_import($q);
        $q   = "DROP TABLE ".$serendipity['dbPrefix']."link_category";
        $sql = serendipity_db_schema_import($q);
    }

    function add_link($link,$name,$desc,$catid = 0) {
        global $serendipity;

        $parts_arr = parse_url($link);
        if (strcmp($parts_arr['pass'], '') != 0) {
            $ret_url .= $parts_arr['user'];
        }
        if (strcmp($parts_arr['pass'], '') != 0) {
         $ret_url .= ':' . $parts_arr['pass'];
        }
        if ((strcmp($parts_arr['user'], '') != 0) || (strcmp($parts_arr['pass'], '') != 0)) {
         $ret_url .= '@';
        }
        $ret_url .= $parts_arr['host'];
        if (strcmp($parts_arr['port'], '') != 0) {
           $ret_url .= ':' . $parts_arr['port'];
        }
        $ret_url .= $parts_arr['path'];
        if (strcmp($parts_arr['query'], '') != 0) {
            $ret_url .= '?' . $parts_arr['query'];
        }
        if (strcmp($parts_arr['fragment'], '') != 0) {
            $ret_url .= '#' . $parts_arr['fragment'];
        }

        $q   = 'SELECT count(id) FROM '.$serendipity['dbPrefix'].'links';
        $sql = serendipity_db_query($q);

        $values['date_added'] = time();
        $values['link'] = $ret_url;
        $values['title'] = $name;
        $values['descrip'] = $desc;
        $values['order_num'] = count($sql);
        $values['category'] = $catid;
        $values['order_num'] = $sql[0][0];
        serendipity_db_insert('links', $values);
    }


    function add_cat($name,$parent) {
        global $serendipity;

        $values['category_name'] = $name;
        $values['parentid'] = $parent;
        serendipity_db_insert('link_category', $values);
    }

    function update_link($id, $link, $title, $desc, $catid) {
        global $serendipity;

        $parts_arr = parse_url($link);
        if (strcmp($parts_arr['pass'], '') != 0) {
            $ret_url .= $parts_arr['user'];
        }
        if (strcmp($parts_arr['pass'], '') != 0) {
         $ret_url .= ':' . $parts_arr['pass'];
        }
        if ((strcmp($parts_arr['user'], '') != 0) || (strcmp($parts_arr['pass'], '') != 0)) {
         $ret_url .= '@';
        }
        $ret_url .= $parts_arr['host'];
        if (strcmp($parts_arr['port'], '') != 0) {
           $ret_url .= ':' . $parts_arr['port'];
        }
        $ret_url .= $parts_arr['path'];
        if (strcmp($parts_arr['query'], '') != 0) {
            $ret_url .= '?' . $parts_arr['query'];
        }
        if (strcmp($parts_arr['fragment'], '') != 0) {
            $ret_url .= '#' . $parts_arr['fragment'];
        }
       

        $values['link'] = $ret_url;
        $values['title'] = $title;
        $values['descrip'] = $desc;
        $values['category'] = $catid;
        $key['id'] = $id;
        serendipity_db_update('links', $key, $values);
    }


    function del_link($id) {
        global $serendipity;

        $q   = 'SELECT order_num FROM '.$serendipity['dbPrefix'].'links where id='.$id;
        $sql = serendipity_db_query($q);

        if ($sql && is_array($sql)) {
            $res = $sql[0];
            $order_num = $res['order_num'];
            $q   = 'DELETE FROM '.$serendipity['dbPrefix'].'links where id='.$id;
            $sql = serendipity_db_query($q);

            $q   = 'UPDATE '.$serendipity['dbPrefix'].'links SET order_num=order_num-1 where order_num > '.$order_num;
            $sql = serendipity_db_query($q);
        }
    }


    function del_category($id) {
        global $serendipity;
        $q   = 'DELETE FROM '.$serendipity['dbPrefix'].'link_category where categoryid='.$id;
        $sql = serendipity_db_query($q);

        $values['category'] = 0;
        $key['category'] = $id;
        serendipity_db_update('links', $key, $values);
    }

    function update_cat($id,$cat) {
        global $serendipity;

        $q   = 'UPDATE '.$serendipity['dbPrefix'].'links SET category = '.$cat.' where id = '.$id;
        $sql = serendipity_db_query($q);
    }

    function move_up($id) {
        global $serendipity;
        $q   = 'SELECT order_num FROM '.$serendipity['dbPrefix'].'links where id='.$id;
        $sql = serendipity_db_query($q);

        if ($sql && is_array($sql)) {
            $res = $sql[0];
            $order_num = $res['order_num']-1;
            if ($order_num >= 0)
            {
                $q   = 'UPDATE '.$serendipity['dbPrefix'].'links SET order_num=order_num-1 where id = '.$id;
                $sql = serendipity_db_query($q);

                $q   = 'UPDATE '.$serendipity['dbPrefix'].'links SET order_num=order_num+1 where order_num = '.$order_num.' AND id !='.$id;
                $sql = serendipity_db_query($q);
            }
        }
    }

    function move_down($id) {
        global $serendipity;

        $q   = 'SELECT count(id) AS countit FROM '.$serendipity['dbPrefix'].'links';
        $sql = serendipity_db_query($q);
        if ($sql && is_array($sql)) {
            $res = $sql[0];
            $count = $res['countit'];
        } else {
            $count = 0;
        }

        global $serendipity;
        $q   = 'SELECT order_num FROM '.$serendipity['dbPrefix'].'links where id='.$id;
        $sql = serendipity_db_query($q);

        if ($sql && is_array($sql)) {
            $res = $sql[0];
            $order_num = $res['order_num']+1;
            if ($order_num <= $count)
            {
                $q   = 'UPDATE '.$serendipity['dbPrefix'].'links SET order_num=order_num+1 where id = '.$id;
                $sql = serendipity_db_query($q);

                $q   = 'UPDATE '.$serendipity['dbPrefix'].'links SET order_num=order_num-1 where order_num = '.$order_num.' AND id !='.$id;
                $sql = serendipity_db_query($q);
            }
        }
    }


    function output_linkadmin() {
        global $serendipity;
        $display = $this->get_config('display');
        $q = $this->set_query($display);
        $categories = $this->build_categories();

        echo '<h3>'.PLUGIN_EVENT_LINKLIST_ADMINLINK.'</h3>';
?>
        <form action="?serendipity[adminModule]=event_display&serendipity[adminAction]=linklist" method="post">
        <table border="0" cellpadding="5" cellspacing="0" width="100%">
            <tr>
                <td> </td>
                <td><strong><?php echo PLUGIN_EVENT_LINKLIST_LINK_NAME; ?></strong></td>
                <td><strong><?php echo PLUGIN_EVENT_LINKLIST_LINK; ?></strong></td>
                <td><strong><?php echo CATEGORY; ?></strong></td>
                <?php echo $tdoutput; ?>
            </tr>
<?php


        $sql = serendipity_db_query($q);
        if ($sql && is_array($sql)) {
            $sort_idx = 0;
            foreach($sql AS $key => $row) {
                $name = $row['name'];
                $link = $row['link'];
                $current_category = $row['cat_id'];
                $id = $row['id'];
                if ($display == 'order_num') {
                    if ($sort_idx == 0) {
                        $moveup   = '<td style="border-bottom: 1px solid #000000"> </td>';
                    } else {
                        $moveup   = '<td style="border-bottom: 1px solid #000000"><a href="?serendipity[adminModule]=event_display&serendipity[adminAction]=linklist&submit=move+up&serendipity[link_to_move]=' . $id . '" style="border: 0"><img src="' . serendipity_getTemplateFile('admin/img/uparrow.png') .'" border="0" alt="' . UP . '" /></a></td>';
                    }
                    if ($sort_idx == (count($sql)-1)) {
                        $movedown = '<td style="border-bottom: 1px solid #000000"> </td>';
                    } else {
                        $movedown = '<td style="border-bottom: 1px solid #000000">'.($moveup != '' ? ' ' : '') . '<a href="?serendipity[adminModule]=event_display&serendipity[adminAction]=linklist&submit=move+down&serendipity[link_to_move]=' . $id . '" style="border: 0"><img src="' . serendipity_getTemplateFile('admin/img/downarrow.png') . '" alt="'. DOWN .'" border="0" /></a></td>';
                    }
                }
?>
                <tr>
                    <td style="border-bottom: 1px solid #000000" align="right">
                        <div>
                           <input type="checkbox" name="serendipity[link_to_remove][]" value="<?php echo $id; ?>" />
                         </div>
                    </td>
                    <td style="border-bottom: 1px solid #000000"><strong><a href="?serendipity[adminModule]=event_display&serendipity[adminAction]=linklist&serendipity[edit_link]=<?php echo $id; ?>"><?php echo $name; ?></a></strong></td>
                    <td style="border-bottom: 1px solid #000000" nowrap="nowrap">
                        <div><?php echo $link?></div>
                    </td>
                    <td style="border-bottom: 1px solid #000000">
                    <?php echo $this->category_box($id,$categories,$current_category); ?>

                    </td>
                    <?php echo $moveup ?>
                    <?php echo $movedown ?>
                </tr>
<?php
                $sort_idx++;
            }
            echo '<br />';
            echo '</table>';
            echo '<div>';
            echo '<input type="submit" name="REMOVE" title="'.REMOVE.'"  value="'.DELETE.'" class="serendipityPrettyButton" />';
            echo ' ';
            echo '<input type="submit" name="SAVE" title="'.SAVE.'"  value="'.SAVE.'" class="serendipityPrettyButton" />';
            echo '</div>';
            echo '</form>';
        }
    }

    function category_box($id,$categories,$current_category = 0)
    {
        $x = "\n<select name=\"serendipity[link_to_recat][".$id."]\">\n";
        foreach ($categories as $k => $v) {
            $x .= "    <option value=\"$k\"" . ($k == $current_category ? ' selected="selected"' : '') . ">$v</option>\n";
        }
        return $x . "</select>\n";
    }

    function output_add_edit_linkadmin($edit = FALSE,$id = -1) {
        global $serendipity;
        $display = $this->get_config('display');
        $categories = $this->build_categories();
        if ($edit) {
            $maintitle = PLUGIN_EVENT_LINKLIST_EDITLINK;
            $q = 'SELECT link,title,category,descrip FROM '.$serendipity['dbPrefix'].'links WHERE id = '.$id; 
            $sql = serendipity_db_query($q);
            if ($sql && is_array($sql)) {
                $res = $sql[0];
                $link = $res['link'];
                $title = $res['title'];
                $cat = $res['category'];
                $desc = $res['descrip'];
            }
            $button = '<input type="submit" name="EDIT" title="'.EDIT.'"  value="'.EDIT.'" class="serendipityPrettyButton" />';
        } else {
            $maintitle = PLUGIN_EVENT_LINKLIST_ADDLINK;
            $button = '<input type="submit" name="ADD" title="'.ADD.'"  value="'.ADD.'" class="serendipityPrettyButton" />';
        }

        if ($this->get_config('category') == 'custom') {
            $catlink = '(<a href="?serendipity[adminModule]=event_display&serendipity[adminAction]=linklist&serendipity[manage_category]=1">'.PLUGIN_EVENT_LINKLIST_ADD_CAT.'</a>)';
        }
        echo '<h3>'.$maintitle.'</h3>';
?>
        <form action="?serendipity[adminModule]=event_display&serendipity[adminAction]=linklist" method="post">
        <input type="hidden" name="serendipity[add_link][id]" value="<?php echo $id; ?>">
        <table border="0" cellpadding="5" cellspacing="0" width="100%">
                <tr><td><?php echo PLUGIN_EVENT_LINKLIST_LINK; ?></td><td><input type="text" name="serendipity[add_link][link]" value="<?php echo $link; ?>" size="30" /></td></tr>
                <tr><td><?php echo PLUGIN_EVENT_LINKLIST_LINK_NAME; ?></td><td><input type="text" name="serendipity[add_link][title]" value="<?php echo $title; ?>" size="30" /></td></tr>
                <tr><td><?php echo CATEGORY; ?> <?php echo $catlink;?></font></td><td><?php echo $this->category_box('cat',$categories,$cat); ?></td></tr>
                <tr><td><?php echo PLUGIN_EVENT_LINKLIST_LINKDESC; ?></td><td><textarea style="width: 100%" name="serendipity[add_link][desc]" id="serendipity[add_link][desc]" cols="80" rows="3"><?php echo $desc; ?></textarea></td></tr>
  
<?php       echo '</table>';
            echo '<div>';
            echo $button;
            echo '</div>';
            echo '</form>';
    }

    function output_categoryadmin()  {
        global $serendipity;
        $display = $this->get_config('display');
        $categories = $this->build_categories();
        $maintitle = PLUGIN_EVENT_LINKLIST_ADD_CAT;
        $button = '<input type="submit" name="ADD" title="'.ADD.'"  value="'.ADD.'" class="serendipityPrettyButton" />';

        echo '<h3>'.$maintitle.'</h3>';
?>
        <form action="?serendipity[adminModule]=event_display&serendipity[adminAction]=linklist&serendipity[manage_category]=1" method="post">
        <input type="hidden" name="serendipity[add_link][id]" value="<?php echo $id; ?>">
        <table border="0" cellpadding="5" cellspacing="0" width="100%">
                <tr><td><?php echo PLUGIN_EVENT_LINKLIST_CAT_NAME; ?></td><td><input type="text" name="serendipity[add_category][title]" size="30" /></td></tr>
                <tr><td><?php echo PLUGIN_EVENT_LINKLIST_PARENT_CATEGORY; ?></font></td><td><?php echo $this->category_box('cat',$categories,$cat); ?></td></tr>
  
<?php       echo '</table>';
            echo '<div>';
            echo $button;
            echo '</div>';
            echo '</form>';

        echo '<h3>'.PLUGIN_EVENT_LINKLIST_ADMINCAT.'</h3>';
?>
        <form action="?serendipity[adminModule]=event_display&serendipity[adminAction]=linklist&serendipity[manage_category]=1" method="post">
        <table border="0" cellpadding="1" cellspacing="0" width="100%">
            <tr>
                <td></td>
                <td><strong><?php echo CATEGORY; ?></strong></td>
            </tr>
<?php
        $q = 'SELECT    s.* FROM '.$serendipity['dbPrefix'].'link_category AS s 
                        ORDER BY s.category_name DESC';

        $categories = serendipity_db_query($q);
        $categories = @serendipity_walkRecursive($categories, 'categoryid', 'parentid', VIEWMODE_THREADED);

        foreach ( $categories as $category ) {
?>
            <tr>
                <td width="16">
                <input type="checkbox" name="serendipity[category_to_remove][]" value="<?php echo $category['categoryid']; ?>" />
                </td>
                <td width="300" style="padding-left: <?php echo ($category['depth']*15)+20 ?>px"><img src="<?php echo serendipity_getTemplateFile('admin/img/folder.png') ?>" style="vertical-align: bottom;"> <?php echo htmlspecialchars($category['category_name']) ?></td>
            </tr>
<?php           
        }
        echo '<br />';
        echo '</table>';
        echo '<div>';
        echo '<input type="submit" name="REMOVE" title="'.REMOVE.'"  value="'.DELETE.'" class="serendipityPrettyButton" />';
        echo '</div>';
        echo '</form>';

    }


    function set_query($display) {
        global $serendipity;
        switch($display) {
            case 'js_category':
            case 'category':
                $q = 'SELECT    s.link              AS link,
                                s.title             AS name,
                                s.category          AS cat_id,
                                s.id                AS id
                                FROM    '.$serendipity['dbPrefix'].'links AS s
                                ORDER BY    s.category';
                break;
            case 'order_num':
                $q = 'SELECT    s.link              AS link,
                                s.title             AS name,
                                s.category          AS cat_id,
                                s.id                AS id,
                                s.order_num         AS order_num
                                FROM    '.$serendipity['dbPrefix'].'links AS s
                                ORDER BY    s.order_num ASC';
                $tdoutput = '<td colspan="2"> </td>';
                break;

            case 'dateacs':
                $q = 'SELECT    s.link              AS link,
                                s.title             AS name,
                                s.category          AS cat_id,
                                s.id                AS id
                                FROM    '.$serendipity['dbPrefix'].'links AS s
                                ORDER BY date_added ASC';
                break;

            case 'datedesc':
                $q = 'SELECT    s.link              AS link,
                                s.title             AS name,
                                s.category          AS cat_id,
                                s.id                AS id
                                FROM    '.$serendipity['dbPrefix'].'links AS s
                                ORDER BY date_added DESC';
                break;
            default:
                $q = 'SELECT    s.link              AS link,
                                s.title             AS name,
                                s.category          AS cat_id,
                                s.id                AS id
                                FROM    '.$serendipity['dbPrefix'].'links AS s
                                ORDER BY    s.title ASC';
                break; 
        }
        return $q;
    }

    function build_categories() {
         global $serendipity;
         if ($this->get_config('category') == 'custom') {
             $table = $serendipity['dbPrefix'].'link_category';
         } else {
             $table = $serendipity['dbPrefix'].'category';
         }
         $q = 'SELECT    s.categoryid AS id, 
                        s.category_name AS name
                        FROM '.$table.' AS s 
                        ORDER BY s.category_name DESC';
         $sql = serendipity_db_query($q);
         $categories['0'] = '';
         if ($sql && is_array($sql)) {
             foreach($sql AS $key => $row) {
                 $categories[$row['id']] = $row['name'];
             }
         }
        return $categories;
    }

}

/* vim: set sts=4 ts=4 expandtab : */

?>
Post Reply