Firefox PHP

Sample Code for Working with Custom Profile Fields in Module Development

Posted by Joe Curia 
Sample Code for Working with Custom Profile Fields in Module Development
October 17, 2008 06:16PM
In a number of modules I have used custom profile fields to allow users to disable/enable features provided by the module. I have been reusing the same base code to incorporate the custom profile fields and thought it would be nice to make that base code available to other module developers. Feel free to use or abuse any of this code. :-)

This code allows mod devs to add custom profile fields without requiring template or profile work on the part of the forum admin. For the purpose of providing code with a working example, this code is set to create a custom profile field to allow the users to disable YOUR_FOO in YOUR_MOD

First, in your settings.php page you would use the following code:
Language: PHP
//Add these lines before your form, to check if the necessary custom profile field have been created   foreach ($PHORUM["PROFILE_FIELDS"] as $key => $cstm_field) { if ($cstm_field["name"] == "phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO") { if (isset($cstm_field["deleted"]) && $cstm_field["deleted"] == TRUE) { $DISABLE_YOUR_FOO_status = 2; } else { $DISABLE_YOUR_FOO_status = 1; } } } if (!isset($DISABLE_YOUR_FOO_status)) { include_once("./include/api/base.php"); include_once("./include/api/custom_profile_fields.php"); phorum_api_custom_profile_field_configure(array ( ';id'; => NULL, ';name'; => ';phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO';, ';length'; => 1, ';html_disabled'; => TRUE, ';show_in_admin'; => TRUE, )); $DISABLE_YOUR_FOO_status = 1; }   //add these lines to your form:   if ($DISABLE_YOUR_FOO_status == 2) { $frm->addmessage("Please add the deleted custom profile field named \"phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO\" if you would like to allow users to disable YOUR_FOO."); } else { $frm->addrow("Allow users to disable YOUR_FOO: ", $frm->checkbox("allow_DISABLE_YOUR_FOO", "1", "", $PHORUM["phorum_mod_YOUR_MOD"]["allow_DISABLE_YOUR_FOO"])); }

Next, in your /lang folder you would create a language file or add these lines for each language that you want to include in your mod (sample english.php shown):
Language: PHP
<?php $PHORUM["DATA"]["LANG"]["phorum_mod_YOUR_MOD"]["DisableYOUR_MOD"] = "Disable YOUR_FOO:"; $PHORUM["DATA"]["LANG"]["phorum_mod_YOUR_MOD"]["DisableYOUR_MODYes"] = "Yes"; $PHORUM["DATA"]["LANG"]["phorum_mod_YOUR_MOD"]["DisableYOUR_MODNo"] = "No"; ?>

Next, you would add this function to your mod to show the custom profile field in the Control Center. In this sample, the custom profile field will be shown on the Forum Settings page:
Language: PHP
function phorum_mod_YOUR_MOD_tpl_cc_usersettings($profile) { global $PHORUM;   //show the option to allow users to disable YOUR_FOO on the forum settings page if (!empty($profile["PANEL"]) && $profile["PANEL"] == "forum") {   //only if the option to allow users to disable YOUR_FOO has been enabled if (!empty($PHORUM["phorum_mod_YOUR_MOD"]["allow_DISABLE_YOUR_FOO"])) { foreach ($PHORUM["PROFILE_FIELDS"] as $key => $cstm_field) { if ($cstm_field["name"] == "phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO") { if (!empty($cstm_field["deleted"]) && $cstm_field["deleted"] == TRUE) { $DISABLE_YOUR_FOO_status = 2; } else { $DISABLE_YOUR_FOO_status = 1; } } } //only if the custom profile field has been installed and not deleted if ($DISABLE_YOUR_FOO_status == 1) { $currval = isset($profile["phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO"]); print "<dt>".$PHORUM["DATA"]["LANG"]["phorum_mod_YOUR_MOD"]["DisableYourFoo"]; ?></dt> <dd><select name="phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO"> <option value="1"<?php if ($PHORUM["DATA"]["PROFILE"]["phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO"] == 1) { ?> selected="selected"<?php } print ">".$PHORUM["DATA"]["LANG"]["phorum_mod_YOUR_MOD"]["DisableYourFooYes"]; ?></option> <option value="0"<?php if ($PHORUM["DATA"]["PROFILE"]["phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO"] == 0) { ?> selected="selected"<?php } print ">".$PHORUM["DATA"]["LANG"]["phorum_mod_YOUR_MOD"]["DisableYourFooNo"]; ?></option></select></dd> <?php } } }   return $profile;   }

To make use of this code, edit your info.txt file to include these lines as needed:
// add this line to show the custom profile field in the Control Center
hook: tpl_cc_usersettings|phorum_mod_YOUR_MOD_tpl_cc_usersettings

// add this line if not already in use
hook: lang|

Finally, here is one example of a way to make use of the custom profile field in your other mod functions:
Language: PHP
function phorum_mod_YOUR_MOD_YOUR_HOOK() {   //do not YOUR_FOO if the user has disabled YOUR_FOO with admin permission if (!empty($PHORUM["phorum_mod_YOUR_MOD"]["allow_DISABLE_YOUR_FOO"]) && !empty($PHORUM["user"]["phorum_mod_YOUR_MOD_DISABLE_YOUR_FOO"])) return;

Obviously a number of things could be changed here. In the sample provided, I am using a simple Yes/No field which stores 1 for Yes and 0 for No. This code can be modified to store text or other data as well.

I hope this does help fellow mod devs working with custom profile fields and if anyone has questions on this code, please feel free to ask.


Joe Curia (aka Azumandias)
Modules: l0Admin Mass Email00000000l000000Automatic Time Zones000ll.l00000Enhanced Custom Profiles0.00Google Calendar0000l.l000000Post Previews
000000000Admin Security Suite000000000000Check Modules for Upgrades0000External Authentication000000Group Auto-Email00000.00000Private Message Alerts
000000000Attachment Download Counter0000Custom Attachment Icons000ll.ll00Favorite Forums000000.00000Highlighted Search Terms0000Self-Delete Posts Option
000000000Attachment Watermarks0l00000000Custom Language Database00l.l.0Forum Lockdown00000.00000Ignore Forums0000000000000Threaded Tree View
000000000Automatic Message Pruning00.llll.00Easy Color Scheme Manager0l.l00Forum Subscriptions0000lll000Moderated User Group
Templates:lGeneric Integration000000000 0000Simple Rounded000000 00000000Tabbed Emerald
Sorry, only registered users may post in this forum.

Click here to login