Latest Register Log In

+ Advanced Search

Modifications Guide

Modifications Guide
By
Oct 16, 2003 (Edited Mar 10, 2014)

First, consider if your task can be accomplished by adding fields (which automatically become available as template variables), making toplists and editing/creating templates (note that PHP can be used in the templates). Most common extension tasks can be handled via those features.

If you want to go beyond the limitations of fields and templates, and you know PHP, WSN provides easy ways to extend the source code to create new features. Using these built-in options instead of hacking up the source files will allow you to keep upgrades easy so you can continue taking advantage of WSN's bug fixes, security updates and new features without any hard work.

If you want to add new sorts of data, you can drop in new database tables. Functionality to insert and edit with your new table is built in, just create the templates and class.

Any files placed in the /modifications/ directory will be included in every page. If possible, put your modifications here and just conditionalize when the code executes appropriately (the currenturlcontains() function can help you filter down to a specific page). More detail on the modifications directory over here.

Certain important functions can be overridden with plugins. Plugins can handle any manipulations you wish to do on an object before it gets added, for example. Plugins can be used to add new functionality or change existing functionality. If there's an additional function you wish were pluggable, contact WSN support and your wish might be granted.

Suppose you want to add the ability to show the number of comments per day a member has posted. Once you know that all methods of a class are available as template variables, your first thought will be to open /classes/member.php to add a new method. In order to keep your changes upgradeable, though, you should use a pseudomethod instead -- this will be effectively the same, but stored in a different file so that it's unaffected by upgrades.

In the /classes/ directory you can find the files that control how listings, categories, comments and members work. As of 5.0, most of the classes are children of classes/generic.php's class -- any methods of the generic class can be called from the child classes, so I've placed methods that are applicable to both attachments and categories (for example) in generic.php to avoid duplication of code. Again though, addition of new methods should done with pseudomethods so as not to require forking off your own version of the script. If you want to add new classes (extending generic), place the files in modifications/classes/ named as the class name to get them autoloaded with the __autoload magic fuction.

If you ever need to get information from a particular listing, category, comment or member in the code you're writing, you can get this easily by declaring an object, passing it 'id' and the id of the record you want. Assuming $id contains the id you want:

$alink = new onelink('id', $id);
$acategory = new category('id', $id);
$acomment = new comment('id', $id);
$amember = new member('id', $id);


Using $acategory->name thereafter in your code, for example, would give you the name of the category. All the database fields become attributes of the object.

If want to grab a large number of items at once using a mysql query, you can use the $db object (if in a function, be sure to use global $db; to get access to it). See classes/database.php for the available $db methods. This code will grab the links with more than 5 votes, in order of most votes first, limited to only the first 5, and will create objects for them in the loop which you could process in some way if you wished:

$one = '{LINKTITLE} - {LINKRATING}<br >'; // example content
$query = $db->select($settings->linkfields, 'linkstable', 'votes>5', 'ORDER BY votes DESC', 'LIMIT 0,5');
$num = $db->numrows($query);
for ($x=0; $x<$num; $x++)
{
$alink = new onelink('row', $db->row($query));
$all .= $alink->replacements($one);
}


At the end of the for loop, you'll have one line for each link that has more than 5 votes displayling the title and rating, contained in $all.

If you don't want to waste time learning the special methods of $db, you can just pass SQL directly to the $db->query($SQL) method (though when doing that you'll still need to use the appropriate variable for the table name, such as $linkstable for the links table).

Some generalizations about classes: the "add" function will control how the object is added to the database (see the beforeaddition and afteraddition pluggable functions to make upgradeable changes around additions of objects), and the "update" function will control how fields are updated. "deletethis" does deletions. All class values and functions are public, since it was originally designed for PHP 4. To create a new object you use $obj = new type('new', 'new'); and it will automatically inherit any field values which are among the $_POST values. If you want all attributes blank, not inheriting $_POST, use $obj = new type('blank', 'blank');. As the example above notes, $obj = new type('row', $db->row($query)); allows you to create an object based on a row of data, which can be quicker than processing by id number.

To register a new member, you can simply do this:
$mem = new member('new', 'new');
$mem->assigndefaults();
$mem->name = 'Some Username';
$mem->password = 'Some Password';
$mem->email = 'some@email.com';
$mem->add();


Experiment, be creative, and ask for help on the forum or via email when needed.

To change how redirection works after doing stuff, see adding logic to redirects.




Description For advanced users: how to expand WSN.
Rating
Views 5,597 views. Averaging 1 view per day.
Similar Listings