Register Members List Search Today's Posts Mark Forums Read

Reply
 
Article Options
Creating Custom Datamanagers
Alan @ CIT
Join Date: Nov 2004
Posts: 625

South UK
by Alan @ CIT Alan @ CIT is offline 22 Jun 2006

Note: Originally posted here: http://www.vbulletin.org/forum/showt...43#post1013143

-------------------------------------------------

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:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

This will end the script if the vB_Datamanger class doesn't exist.

Now we need to extend the vB_DataManager class to make our own Datamanager.

On the next line down in your PHP file, put:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

This tells PHP that we want to create a Class called "vB_DataManager_Example", and that we want to base it on the vB_DataManager class. This means that our new DM class will inherit all of the vB_DataManagers variables and methods (functions) so we don't have to make our own save() method for example

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:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

The verificatoin options (the last 2) are optional.

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.


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Now we can add our first field:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Lets go through this line bit by bit.

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:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

This has added a further 3 fields to the $validfields, and closed the $validfields array

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":


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Now we have to give vBulletin a temporary array that it uses to store the data we give it when it's saving to out database table that we specified above. It doesn't really matter what this is called, but for convention, try to stick with the name of your DM class


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

The final step in our very basic Datamanager is to create our Class Initator method. This is a function that is run automaticly when our Example Datamanager is created using datamanager_init().


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

The name of this functoin should be exactly the same as your class name. So in this case, we've called it "vB_DataManager_Example".

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:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

You can now create your new Datamanager as you would any standard Datamanager by using the datamanager_init() function

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:
  • TYPE_NOCLEAN - Any value - it won't be checked/cleaned
  • TYPE_INT - An signed integer
  • TYPE_UINT - An un-signed integer
  • TYPE_NUM - A number
  • TYPE_UNUM - An un-signed number
  • TYPE_UNIXTIME - A unix timestamp (time())
  • TYPE_STR - A string
  • TYPE_NOTRIM - A string that won't have trim() run on it
  • TYPE_NOHTML - A string that will have the HTML made-safe
  • TYPE_FILE - A file upload (ie, $_FILES)
  • TYPE_BOOL - A bool (true or false)
You can also pass Arrays of these items. For example to pass an array of INT's, use TYPE_ARRAY_INT. To pass an array of Strings, use TYPE_ARRAY_STR

Valid Requirement Options:

(The following is taken directly from vBulletin's DataManager documentation)

REQ_YES - the field is required

REQ_NO - the field is not required

REQ_AUTO - the field can be automatically generated. The does not have any effect on code execution at this time. This is appropriate for things like post times that can be reasonably guessed before inserting the new data. (You will still need to write code to generate the appropriate value!)

REQ_INCR - this field is an AUTO_INCREMENT field in the database, and thus will be automatically generated upon insertion.
-------------------------------------------------

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 )
Attached Files
File Type: php class_dm_example.php (678 Bytes, 274 views)

Last edited by Alan @ CIT : 10 Jul 2006 at 11:05.
Views: 7280
Reply With Quote
Comments
  #2  
Old 22 Jun 2006, 19:49
Paul M's Avatar
Paul M Paul M is offline
 
Join Date: Sep 2004
Real name: Paul Marsden
Great Article Alan.
__________________


Lead Developer, vBulletin.Org & vBulletin.Com
Please do not PM me about custom work - I no longer undertake any.

Note: I will not answer support questions via e-mail or PM - please use the relevant thread or forum.
Cable Forum - DigiGuide
Reply With Quote
  #3  
Old 22 Jun 2006, 19:56
-=Sniper=-'s Avatar
-=Sniper=- -=Sniper=- is offline
 
Join Date: May 2002
posting, so easier for me to find when needed, thanks again
Reply With Quote
  #4  
Old 08 Jul 2006, 14:42
AN-net's Avatar
AN-net AN-net is offline
 
Join Date: Dec 2003
only one problem you should not be setting the array or table in the initialization of the variables. they should be set in the constructor.
Reply With Quote
  #5  
Old 08 Jul 2006, 14:49
Alan @ CIT Alan @ CIT is offline
 
Join Date: Nov 2004
Location: South UK
Real name: Alan
Originally Posted by AN-net
only one problem you should not be setting the array or table in the initialization of the variables. they should be set in the constructor.
That's not true at all, either way is acceptable in OOP, it's all down to how you where taught

Thanks,
Alan.
__________________
IMPORTANT: I'm afraid that I no longer develop software / modifications for vBulletin. However, thank you to everyone who installed / supported my modifications!
Reply With Quote
  #6  
Old 08 Jul 2006, 17:16
Princeton's Avatar
Princeton Princeton is offline
 
Join Date: Nov 2001
Real name: Joe Velez
Just what our members need ... a great article!!
__________________
Latest Articles:
Liquid Layout = Less Ad Revenue?
How to Monetize Your Site
Improve Web Page Performance
How To Write For The Web


If it needs instructions, there's room for improvement.
Give users what they actually want, not what they say they want. And whatever you do, don't give them new features just because your competitors have them!
Reply With Quote
  #7  
Old 08 Jul 2006, 23:28
Code Monkey's Avatar
Code Monkey Code Monkey is offline
 
Join Date: May 2004
Interesting. Do you have to create your own presave, save, and postsave functions?
__________________
Gaming Forums
Reply With Quote
  #8  
Old 09 Jul 2006, 10:14
Alan @ CIT Alan @ CIT is offline
 
Join Date: Nov 2004
Location: South UK
Real name: Alan
Nope, not unless you wish to change the way they function.

Thanks,
Alan.
__________________
IMPORTANT: I'm afraid that I no longer develop software / modifications for vBulletin. However, thank you to everyone who installed / supported my modifications!
Reply With Quote
  #9  
Old 09 Jul 2006, 12:02
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Real name: Dean Clatworthy
May I suggest combining all the code into one example at the end of the article
__________________
Dean Clatworthy - Web Developer/Designer
Reply With Quote
  #10  
Old 09 Jul 2006, 13:51
Alan @ CIT Alan @ CIT is offline
 
Join Date: Nov 2004
Location: South UK
Real name: Alan
^^ you mean like the attached PHP file that's always been there?

Thanks,
Alan.
__________________
IMPORTANT: I'm afraid that I no longer develop software / modifications for vBulletin. However, thank you to everyone who installed / supported my modifications!
Reply With Quote
  #11  
Old 09 Jul 2006, 23:31
Code Monkey's Avatar
Code Monkey Code Monkey is offline
 
Join Date: May 2004
Well, I tried it with a simple table and nothing got inserted. So there must be something more to this.
__________________
Gaming Forums
Reply With Quote
  #12  
Old 10 Jul 2006, 06:58
Alan @ CIT Alan @ CIT is offline
 
Join Date: Nov 2004
Location: South UK
Real name: Alan
Check the $errors array to see if there where any problems.

Alternativly, PM me your datamanager PHP file, and the PHP your using to test it, and I'll take a look.

The code above will (well, works here) work without changes though

Thanks,
Alan.
__________________
IMPORTANT: I'm afraid that I no longer develop software / modifications for vBulletin. However, thank you to everyone who installed / supported my modifications!
Reply With Quote
  #13  
Old 10 Jul 2006, 08:14
Danny.VBT's Avatar
Danny.VBT Danny.VBT is offline
 
Join Date: Oct 2004
Alan,

Shouldn't


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

be:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

As verify_username does not exist in the base class. (It does in class_dm_user.php)
__________________
while($coffeeMachine->supply($Danny)) { $Danny->run(); }

Twitter Blog
Reply With Quote
  #14  
Old 10 Jul 2006, 11:05
Alan @ CIT Alan @ CIT is offline
 
Join Date: Nov 2004
Location: South UK
Real name: Alan
That'll teach me to base my example of the User DM

Edited first post
__________________
IMPORTANT: I'm afraid that I no longer develop software / modifications for vBulletin. However, thank you to everyone who installed / supported my modifications!
Reply With Quote
  #15  
Old 17 Jul 2006, 19:24
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
So with the help of this tuitorial, I could create a data manager for creating User Notes about members, correct?
__________________
Please feel free to PM me about custom work, installations, and upgrades!

Eclipse Records - Streetcult Street Team
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Article Options

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Jump


New To Site? Need Help?

All times are GMT. The time now is 07:36.

Layout Options | Width: Wide Color: