Creating Custom Datamanagers
by
22 Jun 2006
Note: Originally posted here: Spoiler (click to open)
Creating custom datamangers is fairly easy once you've done one
To start with, create your new PHP file to hold your DM code and save it as includes/class_dm_example.php Now, the first thing we need to do is check that the vB_Datamanger class exists - without that, we can't do much of anything. At the top of your new PHP file, put: PHP Code:
if (!class_exists('vB_DataManager'))
Now we need to extend the vB_DataManager class to make our own Datamanager. On the next line down in your PHP file, put: PHP Code:
class vB_DataManager_Example extends vB_DataManager
Now we need to give our new Datamanager some fields to update. For example, the User DM would have fields such as "userid", "username", "email", etc. These fields will usually be the same as the columns in your database table. These fields all go in an array called $validfields. The $validfields array is in the following format: PHP Code:
'fieldname' => array(type, required?, verify data?, function name to verify data)
With that in mind, lets start adding to our own $validfields array. Our example datamanager will have 4 fields: 'exampleid' - this is an auto increment field from the database which is incremented automaticly. 'userid' - a vBulletin user id 'username' - a vBulletin username 'exampletext' - some random text First things first, lets create our $validfields array variable. PHP Code:
var $validfields = array(
PHP Code:
'exampleid' => array(TYPE_UINT, REQ_INCR, VF_METHOD, 'verify_nonzero'),
First we have the field name - in this case 'exampleid'. Next we have the type - in this case we're saying it will be an Unsigned Int (ie, a number). A list of valid types can be found at the bottom of this post. Next we specify that it is required, and that it is an auto-increment value with REQ_INCR. Valid options for this field can be found at the bottom of this post. Next we tell it that we want to verify the data, with VF_METHOD And finally, we give it the name of a function to verifiy the data with. In this case, we are using the verify_nonzero() function which is a standard function in the vB_DataManager class. Now we can add our other 3 fields to the $validfields array: PHP Code:
'userid' => array(TYPE_UINT, REQ_YES, VF_METHOD, 'verify_nonzero'),
Now, the next step is to tell vBulletin what table to save our data in. Your table should match the $validfields array in terms of layout and column names. In this case, we'll use a table called "example": PHP Code:
var $table = 'example';
PHP Code:
var $example = array();
PHP Code:
function vB_DataManager_Example(&$registry, $errtype = ERRTYPE_STANDARD)
You don't need to worry about the rest of the code, and if you understand OOP, you'll know what it means Now, all we've got left to do is add our closing bracket for our class, and we're done: PHP Code:
}
That's the gist of it anyway, take a look at some of the existing Datamanagers for more advanced options such as Bitfields, custom verify methods, etc. Valid field types:
Valid Requirement Options: (The following is taken directly from vBulletin's DataManager documentation)
(class_dm_example.php attached) Thanks, Alan. Close
------------------------------------------------- Creating Custom Datamanagers This article will show you the basics of creating your own fully working Datamanager. To start with, create your new PHP file to hold your DM code and save it as includes/class_dm_example.php Now, the first thing we need to do is check that the vB_Datamanger class exists - without that, we can't do much of anything. At the top of your new PHP file, put: PHP Code:
if (!class_exists('vB_DataManager'))
Now we need to extend the vB_DataManager class to make our own Datamanager. On the next line down in your PHP file, put: PHP Code:
class vB_DataManager_Example extends vB_DataManager
Now we need to give our new Datamanager some fields to update. For example, the User DM would have fields such as "userid", "username", "email", etc. These fields will usually be the same as the columns in your database table. These fields all go in an array called $validfields. The $validfields array is in the following format: PHP Code:
'fieldname' => array(type, required?, verify data?, function name to verify data)
With that in mind, lets start adding to our own $validfields array. Our example datamanager will have 4 fields: 'exampleid' - this is an auto increment field from the database which is incremented automaticly. 'userid' - a vBulletin user id 'username' - a vBulletin username 'exampletext' - some random text First things first, lets create our $validfields array variable. PHP Code:
var $validfields = array(
PHP Code:
'exampleid' => array(TYPE_UINT, REQ_INCR, VF_METHOD, 'verify_nonzero'),
First we have the field name - in this case 'exampleid'. Next we have the type - in this case we're saying it will be an Unsigned Int (ie, a number). A list of valid types can be found at the bottom of this post. Next we specify that it is required, and that it is an auto-increment value with REQ_INCR. Valid options for this field can be found at the bottom of this post. Next we tell it that we want to verify the data, with VF_METHOD And finally, we give it the name of a function to verifiy the data with. In this case, we are using the verify_nonzero() function which is a standard function in the vB_DataManager class. Now we can add our other 3 fields to the $validfields array: PHP Code:
'userid' => array(TYPE_UINT, REQ_YES, VF_METHOD, 'verify_nonzero'),
Now, the next step is to tell vBulletin what table to save our data in. Your table should match the $validfields array in terms of layout and column names. In this case, we'll use a table called "example": PHP Code:
var $table = 'example';
PHP Code:
var $example = array();
PHP Code:
function vB_DataManager_Example(&$registry, $errtype = ERRTYPE_STANDARD)
You don't need to worry about the rest of the code, and if you understand OOP, you'll know what it means Now, all we've got left to do is add our closing bracket for our class, and we're done: PHP Code:
}
That's the gist of it anyway, take a look at some of the existing Datamanagers for more advanced options such as Bitfields, custom verify methods, etc. Valid field types:
Valid Requirement Options: (The following is taken directly from vBulletin's DataManager documentation)
Good luck using your new found knowledge of the vBulletin Input Cleaner class, and remember: If you get stuck, just ask! Knowledge sharing is what vBulletin.org is all about! (Note: If you want to reproduce this article anywhere, I have no objections, but I do request that you give me credit for writing it, and a PM letting me know would be appreciated ) |