Modifications Guide
By Paul
By Paul
If simply adding fields and custom templates doesn't do the job for you, and you know a little PHP, perhaps you want to customize the code to create a new feature.
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... by using the modifications directory you will make upgrades easy on yourself. See this article for more detail.
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.
The /classes/ directory is your friend. In the /classes/ directory you can find the files that control how images, categories, comments and members work. As of 5.0, most of the classes are children of classes/generic.php's class.
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.
If you ever need to get information from a particular image, 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:
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:
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:
Experiment, be creative, and ask for help on the forum when needed.
If you must make alterations to the native source code, please see creating modification files for a way to maintain those changes effectively.
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... by using the modifications directory you will make upgrades easy on yourself. See this article for more detail.
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.
The /classes/ directory is your friend. In the /classes/ directory you can find the files that control how images, categories, comments and members work. As of 5.0, most of the classes are children of classes/generic.php's class.
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.
If you ever need to get information from a particular image, 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:
$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));
}
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->register();
Experiment, be creative, and ask for help on the forum when needed.
If you must make alterations to the native source code, please see creating modification files for a way to maintain those changes effectively.
Rating:




3.63/5 based on 8 votes. The median rating is 5.
Rate this article:
Submitted: 10/18/03 (Edited 06/12/08)
Description: For advanced users: how to expand WSN Gallery.
Views:
3839 views. Averaging 2 per day.
In the most recent 30 day period, there've been 10 views.

Print
E-Mail