MyPlaza Development - Framework

This is an outline of the framework provided by MyPlaza to module developers. The MyPlaza framework adds a number of features, on top of what MyBB offers to plugin developers. These include:

Most functions are defined in /inc/myplaza/myplaza_general.php and this file is automatically included for you.

Additional general purpose functions

myplaza_send_pm($pm, $touser = null)

This function simply sends a PM to a user. The contents of the PM is sent through the $pm variable, as an array. The optional $touser is an array containing the details of the user to send the PM to (retrievable via the get_user function). It is recommended that you set the $touser variable, however, if it is not set, MyPlaza will try to determine the array from details in the PM.
The $pm array should have, at minimum, elements with the keys 'subject' and 'message'; you may set additional parameters if need be.
Example usage:
myplaza_send_pm(array(
	'subject' => 'My PM',
	'message' => 'This is a PM!'
), get_user(1));
The above code sends a PM to the user with UID of 1.

myplaza_ajax_output_page($data, $type='html')

This function is used to output data for AJAX features. This is somewhat similar to MyBB's output_page() function, however, this is specifically designed for AJAX. The function will automatically convert any character encodings if necessary, and will output all necessary headers, run shutdown functions, and GZips the output if appropriate.
The optional $type variable just specifies what is sent in the Content-type header - by default, it's text/html however, if you want text/plain, set the $type variable to plain.

db_get_columns($table)

This is a simple wrapper for MyBB's $db->show_fields_from function. All this does is set it up so that the keys of the array returned are the field names.
The function returns an array of the fields available in a table, along with some extra information on each field. As with all DB functions supplied with MyPlaza, do not send the TABLE_PREFIX.

db_get_primary_keys($table)

Returns an array of all the primary keys defined in a table. As with all DB functions supplied with MyPlaza, do not send the TABLE_PREFIX.

db_insert_rows($table, $rows, $check_all = false, $escape = false, $replace_into = false, $returnQuery = false)

This inserts multiple rows into the table specified by $table (do not send TABLE_PREFIX). $rows is basically an array of rows to add. This is similar to sending each row to $db->insert_query however, this function tends to run faster since it combines most of these queries into a single one.
Note that, due to the nature of this function, each row in $rows should have the same fields. If they don't, you can supply true to the $check_all parameter (this causes all rows to be checked and filled in appropriately). Supplying true to the optional $escape parameter causes all values in the array to be escaped via $db->escape_string.

db_shutdown_insert_rows($table, $rows)

Inserts multiple rows at shutdown. This is a further optimization to the db_insert_rows function above, by allowing multiple insert calls to be summed up into the least number of queries possible. It is recommended that you should use this function to insert rows into the DB, unless you really need the rows to be inserted immediately (you generally don't).

db_update_rows($table, $rows, $key = '', $updateExpr = '', $returnQuery = false)

Updates multiple rows in a table. Somewhat similar to running $db->update_query on each row in the $rows array, however, this typically runs faster for larger updates. If the update set is large enough, a single INSERT INTO ... ON DUPLICATE KEY UPDATE query is executed, effectively doing all updates in a single query.
For the $rows array, you should send an array of rows to update, with keys of the array being the primary key, or a unique key of the table. Specify which key you have used in the $key variable. Note that this will not work with composite primary, or composite unique keys. If no $key is specified, the function will query for the primary key, which is what will be assumed (if the primary key is found to be composite, the function will die). Note that each row must have exactly the same fields.
The $updateExpr is a powerful switch, allowing you to specify a custom expression, rather than update with the specified value. Use {$values['fieldname']} in the string to represent the specified value. To help illustrate this, we'll take an example - let's say you want to increase the postcount of UID #1's money by 10, UID #2's money by 50, and UID #10's money by 5...etc, and set various values for their reputation - you can use the following statement:
$update_rows = array(
	 1 => array('postnum' => 10, 'reputation' =>  5),
	 2 => array('postnum' => 50, 'reputation' =>  1),
	10 => array('postnum' =>  5, 'reputation' => 10),
	12 => array('postnum' => -2, 'reputation' => 90),
	50 => array('postnum' =>  0, 'reputation' =>  0),
	25 => array('postnum' =>  1, 'reputation' =>  1),
);
db_update_rows('users', $update_rows, 'uid', false,
	'postnum=postnum+{$values[\'postnum\']}, reputation={$values[\'reputation\']}');
The above will be executed as two SQL queries.

db_shutdown_update_rows($table, $rows, $key)

Like db_shutdown_insert_rows this function will execute db_update_rows at shutdown, allowing faster update speed. Note that only one $key may be specified for each table which is to be updated.

db_select($tables, $fields='*', $conditions='', $options=array(), $returnQuery = false)

This is a more complex version than MyBB's $db->simple_select function (also offers more than MyBB's $db->select function). This function is fully backwards compatible with $db->simple_select. Additional functionality include:

db_update($table, $update, $where='', $limit='', $expr = true, $returnQuery = false, $immediate = false)

Identical to $db->update_query, however, doesn't escape values, so you can perform update queries involving expressions.

db_next_auto_increment($table)

Retrieves the next auto_increment value of the specified table.

Activation functions

These functions are only available during activation/deactivation. You can use them at other times (if you really need to) by including /inc/myplaza/myplaza_install.php

add_helpdoc($helpdoc = array())

Adds a help document. The optional $helpdoc parameter can be used to specify any additional information. If the parameter is not supplied, MyPlaza will use MyBB's translation $lang variables.
Generally, you should make sure that the following are set before calling this function: $lang->help_myplaza_modulename_name, $lang->help_myplaza_modulename_desc, and $lang->help_myplaza_modulename_document, where modulename is the filename of the module.

remove_helpdoc($id = '')

Removes a helpdoc added by add_helpdoc. If you specified a custom ID when adding the helpdoc, make sure you specify it here too.

add_templates(&$new_templates, $immediate = true)

Inserts custom templates. $new_templates is an array of the templates to insert, where the keys of the array are the template names. By default, the templates are inserted at shutdown. If, for some odd reason, you need to insert them immediately, set $immediate to true.

module_activate($module)

This will activate a module. Do not include the '.php' extension.

module_deactivate($module)

This will deactivate a module. Do not include the '.php' extension.

find_replace_myplaza_template($title, $find, $replacement = '', $method = 'preg_replace', $immediate = false)

This is used if you need to modify a MyPlaza template. This function is similar to MyBB's find_replace_templatesets function. You can use the str_replace function, by specifying that in the $method variable, rather than the default preg_replace method.

add_settings($new_settings, $gid = 0, $immediate = false)

This adds a bunch of settings. Supply the function with an array of settings, specifying, at least, the name, type and default value. Note that if you don't specify the title or description, this function will look for it in $lang->setting_settingname and $lang->setting_settingname_desc respectively (where settingname is the name of the setting to be added).

plugins_add_passive_hook($hook, $function, $priority = 10, $adminOnly = false, $file='')

Adds a passive hook. Passive hooks allow MyPlaza/MyBB to control the adding of hooks, meaning that your module file doesn't have to be included on each page load (which can reduce page generation times).

plugins_remove_passive_hooks($file = '')

Removes all passive hooks added by this module.

db_create_table($name, $fields, $keys = array(), $type = '', $returnQuery = false)

This is a wrapper function for a CREATE TABLE query.

db_drop_tables($tables)

This is a wrapper function for a DROP TABLE query. $tables can be an array of tables to drop, or just a single table. Again, do not send TABLE_PREFIX.

db_add_columns($table, $columns, $returnQuery = false)

Adds fields to an existing table.

db_remove_columns($table, $columns, $returnQuery = false)

Removes fields from an existing table. $columns is just an array of field names to be dropped.

db_alter_column($table, $fieldname, $info, $returnQuery = false)

Alters a field $fieldname from a table, based on information supplied in the $info array. Supply the field information in the $info array.

MyPlaza specific functions

buy_item($iid, $cost = null, $visibleOverride = false)

This is the central buy routine for the current user buying an item. The item is identified via $iid (note, if you have a global variable $item, this function will use that instead of querying for the item details). The optional $cost variable is only used for verification purposes, ie, if a displayed cost differs to the one stored in the DB, an error will be generated. Set $visibleOverride to true if you want the item to be purchased even if the user cannot see the item in the plaza.
This function returns true if the buying is successful, or false otherwise.

calcCost($cost, $extraRate = 1)

Calculates and returns the cost of an item, taking into consideration the global item rate, and usergroup premium rates. You can specify an extra factor to multiply by in the $extraRate parameter.

calcStock($stockOffset, $restockAmt, $restockTime)

Calculates and returns how much stock there is, based on the supplied arguments (which should be values retrieved from the DB).

calcStockOffset($stock, $restockAmt, $restockTime)

This is basically the reverse of the calcStock function - it calculates the stock offset to store in the DB.

format_item($itemname, $itemformat)

Formats an item/category name, $itemname, with the supplied format string (which must contain {name} as the placeholder), $itemformat.

my_format_money($money)

Formats money to be displayed. Basically adds the necessary prefixes/suffixes.

myplaza_add_item($item, $module = '', $immediate = false)

Adds an item, with details in the $item array argument. You may need to supply a $module argument, specifying the name of the module, depending on where you call this function.

myplaza_langload($langfile = '', $load_other = false)

Loads the _lang() and/or _lang_admin() functions of a module, into the $lang object. Basically the equivalent of $lang->load() but for modules.

get_myplaza_category($cid)

Gets information about a category, identified by $cid and returns it as an array.

get_myplaza_item($iid)

Gets information about an item, identified by $iid and returns it as an array.

myplaza_remove_items($iids)

Removes/deletes one or more items, definied by an array of $iids.

myplaza_remove_module_items($module = '')

Removes/deletes all items added under the current module (you may need to specify $module if you call this function at certain places).

myplaza_standard_buy($module, $item_where, $runfunc = '')

This is a standard (non-AJAX) buy routine for buying items outside the plaza, on a custom page.

myplaza_xmlhttp_buy($xmlhttp_action, $item_where, $runfunc = '')

This is a standard (AJAX) buy routine for buying items outisde the plaza.

shutdown_exit($force_run = false)

Generally, you should use this instead of the exit command, as, due to the number of functions which run at shutdown, this may be required to get things to work.

user_change_money(&$user, $amount, $rate = 1, $immediate = false)

This adds/removes money from a user. Specify the user array in $user and the amount to add in $amount. You can also include an extra $rate to multiply by. If you want the update to be performed immediately, rather than at shutdown, set $immediate to true.

visible_to_ugroup($ugroup, $visiblegroups)

Returns true if $ugroup is a part of $visiblegroups; used for determining if something is visible to a certain usergroup.

Replacement functions

my_find_replace_templatesets($title, $find, $replace, $autocreate = true, $method = 'preg_replace', $immediate = false)

This function is almost completely backward compatible with MyBB's find_replace_templatesets(). This function is included as convenience, as you don't have to include /inc/adminfunctions_templates.php each time you want to use it. Also, this provides an extra $method, so you can use str_replace instead of preg_replace. Also allows the user to specify different template replacement strategies.

Client side AJAX functions

These are Javascript functions, which you can find in /jscripts/myplaza_global.js.

MyPlazaPopupMenu (class)

This class is similar to MyBB's popumenu (actually, it's copied and a modified copy). The menu differs in that it will stay open to allow users to enter data or click buttons.
The menu is created in the same way as the MyBB popupmenu.

fadeInElement(elemId, alpha, rate, targetAlpha, completeFunc)

Fades in an element (from high transparency to low transparency). Specify the ID of the element in elemId, the initial alpha, the rate at which to perform the fade (alpha is increased at this rate per 0.05 seconds), the final alpha value to achieve (targetAlpha) and a function to execute when the animation is complete (completeFunc).

fadeOutElement(elemId, alpha, rate, targetAlpha, completeFunc)

Same as fadeInElement but increases transparency instead of decreasing it.

setOpacity(element, value)

Set the opacity of an element. value is the opacity level, from 0 (transparent) to 1 (opaque).

doAjaxBuy(formObj, xmlhttpAction, submitElem, newVal, messageId, completeFunc)

Performs a purchase through AJAX. You will need to point to the form via formObj so that MyPlaza can build the AJAX request by reading the form values.
When you run this function, a request to xmlhttp.php?action=xmlhttpAction is sent, via AJAX, with the contents of all the elements of the form sent as the postBody. submitElem and newVal are really provided for your convenience (and may be removed later) since most of the time, you'll make use of them. submitElem should point to the submit button in your form, and newVal is the text that submitElem should be changed to (MyPlaza will also disable the submitElem button during AJAX processing). messageId should be the ID of the DIV/SPAN (or whatever) HTML element where the returned message is displayed. completeFunc is simply the function that is executed once buying is complete and successful.

Extra general purpose hooks

I added these hooks primarily because I believe MyBB sorely lacks these (and they're needed by MyPlaza :P):

admin_users_add_code

This hook is run in the AdminCP -> Users & Groups -> Create New User page, just above when the submit button is echo'd. This allows you to add any extra fields to the form.

admin_users_edit_userlinks

This hook allows you to change the links at the top of the page, when the admin is editing the user.

admin_users_edit_code

Like the admin_users_add_code hook, but for editing a user.

admin_usergroups_add_code

Like the admin_users_add_code hook, but for adding a usergroup.

admin_usergroups_edit_code

Like the admin_users_edit_code hook, but for editing a usergroup.

myplaza_wol_process

When the Who's Online list is being processed - note this is executed for each user in the WOL.

myplaza_wol($replacement)

Hook allows you to place your own text for the Who's Online list. Modify the $replacement variable to do this.

MyPlaza specific hooks

myplaza_admin_start

Executed at the beginning of /admin/myplaza.php

myplaza_admin_categories_hopto

Beginning of the Categories & Items processing.

myplaza_admin_categories_items

Executed after items & categories displayed.

myplaza_admin_categories_code_foot

End of the Categories & Items section.

myplaza_admin_inline_deleteitems

myplaza_admin_add_category_code

Allows you to change the code used in Add Categories page.

myplaza_admin_add_category_code_foot

myplaza_admin_add_category

myplaza_admin_delete_category

myplaza_admin_edit_category_code

myplaza_admin_edit_category_code_foot

myplaza_admin_edit_category

myplaza_admin_edit_item_start

myplaza_admin_edit_item_code_foot

myplaza_admin_edit_item

myplaza_admin_add_item

myplaza_admin_do_add_item

myplaza_admin_do_add_item_end

myplaza_admin_delete_item

myplaza_admin_modules_code

myplaza_admin_do_modules

myplaza_admin_modules_upload

myplaza_admin_am_start

myplaza_admin_am_code_foot

myplaza_admin_am_viewbackupfile_start

myplaza_admin_am_viewbackupfile_end

myplaza_admin_am_viewbackup_start

myplaza_admin_am_viewbackup_end

myplaza_admin_am_delete_original

myplaza_admin_am_delete_backups

myplaza_admin_am_restore_original

myplaza_admin_am_restore_backups

myplaza_admin_log_start

myplaza_admin_log_query

myplaza_admin_log_code

myplaza_admin_prunelog

myplaza_admin_maintanence_code

myplaza_admin_resetmoney_process

myplaza_admin_backup_process

myplaza_admin_restore_process

plaza_start

plaza_buyitem_end

plaza_stats_start

plaza_stats

plaza_history

plaza_end

plaza_buyitem_start

plaza_buyitem

plaza_buyitem_success