category help

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
imaboob

category help

Post by imaboob »

What I want to do is remove the "all authors" checkbox for 0 level users so that only theyand admin can post to their categories. Also- would it be possible to limit 0 level users to one category? Any ideas re: these issues will be appreciated.
imaboob

actually...

Post by imaboob »

the documentation claims that categories can be assigned to certain users. I cannot find anywhere that allows the admin to do this. It appears that each user can start their own categories and even choose whether all authors can post to it.

I need to have it so that only the admin can create and assign categories to users. Any ideas? Or at least so that users at level 0 can only have 1 category to post in and it needs to be named the same as their username.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: actually...

Post by garvinhicking »

You are right that currently there is no method to limit the amount of categories per user, so you would need to patch the code to do this.

If you know some PHP, you should easily be able to do this. If so, I can give you means on how to achieve what you want via creating a plugin...

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/
ima

thanks

Post by ima »

I found a work-around kinda thing and it is working for me. I would like info re: how to make a plug-in for the future, though.

Another question- are there any known browser issues? I asked a friend to look at the site and he said it was "off balance" until he refreshed and then it was ok. I'm not sure what he meant but maybe there is a css problem with ie 5 or something is all I could think of...
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: thanks

Post by garvinhicking »

Okay, for making a plugin with the restriction, you would do the following. I just assume you're using Version 0.8 of Serendipity; if not, you should be able to apply that to 0.7 code easily.

First you'd open the file include/admin/category.inc.php

There you'd spot the code:

Code: Select all

        $query = "INSERT INTO {$serendipity['dbPrefix']}category (category_name, category_description, authorid, category_icon, parentid, category_left, category_right) values ('$name', '$desc', $authorid, '$icon', $parentid, 0, 0)";
        serendipity_db_query($query);
        echo '<div class="serendipityAdminMsgSuccess">'. CATEGORY_SAVED .'</div>';
(This is line 26-28 for my latest version).

You would modify this code to read:

Code: Select all

        $query = "INSERT INTO {$serendipity['dbPrefix']}category (category_name, category_description, authorid, category_icon, parentid, category_left, category_right) values ('$name', '$desc', $authorid, '$icon', $parentid, 0, 0)";
        serendipity_plugin_api::hook_event('backend_create_category', $query);
        serendipity_db_query($query);
        echo '<div class="serendipityAdminMsgSuccess">'. CATEGORY_SAVED .'</div>';
This way, you inserted a plugin hook which plugins can now use. You would create a simple event plugin called plugins/serendipity_event_categorycheck/serendipity_event_categorycheck.php:

Code: Select all

<?php

class serendipity_event_categorycheck extends serendipity_event {
    function introspect(&$propbag) {
        global $serendipity;

        $propbag->add('name', 'Check Category Creation');
        $propbag->add('description', 'Check Category Creation');
        $propbag->add('event_hooks',  array('backend_create_category' => true));
        $propbag->add('configuration', array('maxcategory'));
        $propbag->add('author', 'Garvin Hicking');
        $propbag->add('version', '1.0');
        $propbag->add('stackable', false);
    }

    function introspect_config_item($name, &$propbag) {
        switch($name) {
            case 'maxcategory':
                $propbag->add('type',        'string');
                $propbag->add('name',        'How many categories per author?');
                $propbag->add('description', 'Enter a number');
                $propbag->add('default',     1);
                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_create_category':
                    $check = "SELECT count(categoryid) as ccount FROM {$serendipity['dbPrefix']} WHERE authorid = '" .  $serendipity['authorid'] . "' GROUP BY authorid";
                    $rows = serendipity_db_query($check, true);
                    if ($rows['ccount'] > $this->get_config('maxcategory')) {
                        echo 'You cannot create more than ' . $this->get_config('maxcategory') . ' categories!';
                        $eventData = ""; // Create an empty SQL dummy query for execution in event hook
                    }
                    
                    return true;
                    break;

                default:
                    return false;
                    break;
            }
        } else {
            return false;
        }
    }
}
/* vim: set sts=4 ts=4 expandtab : */
(I hardcoded language strings to make the code smaller - but for a final version you would use language Constants like all the other plugins)

You can see that the plugin executes on the event hook, performs an SQL query to check how many authors exist, and depending on the outcome either leaves the SQL query for creating a category intact, or sets it to an empty string.

You can easily expand the system to check if a superuser is creating a category, or if the category to be created is a subcategory, or whatever. You may need to use $serendipity['GET'] and $serendipity['POST'] for some variable access.

About the browser issues: Yes, IE5 sometimes has problems. Also mod_gzip on a server creates some troubles. But this is more a IE5/CSS problem, and not one of Serendipity per se...

HTH,
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/
Post Reply