diff options
Diffstat (limited to 'documentation/examples')
24 files changed, 225 insertions, 127 deletions
diff --git a/documentation/examples/actions/basic.php b/documentation/examples/actions/basic.php new file mode 100644 index 000000000..926e11b79 --- /dev/null +++ b/documentation/examples/actions/basic.php @@ -0,0 +1,22 @@ +<?php +/** + * Demonstrates adding an annotation through an action + * + * This action adds a rating annotation to an entity. If this was coming from + * a five-star rating tool, the rating would be a number between 0 and 5. The + * GUID of the entity being rating is also submitted to the action. + */ + +$rating = get_input('rating'); +$guid = get_input('guid'); + +$entity = get_entity($guid); +if (!$entity) { + register_error(elgg_echo('rating:failure')); + forward(REFERER); +} + +$entity->annotate('rating', $rating); + +system_message(elgg_echo('rating:success')); +forward(REFERER); diff --git a/documentation/examples/actions/manual_tokens.php b/documentation/examples/actions/manual_tokens.php deleted file mode 100644 index 8dcf61fb1..000000000 --- a/documentation/examples/actions/manual_tokens.php +++ /dev/null @@ -1,6 +0,0 @@ -<?php - -$ts = time(); -$token = generate_action_token($ts); - -var_dump($ts, $token); diff --git a/documentation/examples/crontab.example b/documentation/examples/crontab.example new file mode 100644 index 000000000..f25f5cb07 --- /dev/null +++ b/documentation/examples/crontab.example @@ -0,0 +1,28 @@ +# Crontab example. +# +# This file is an example of triggering Elgg cron events. It hits a URL to +# trigger the events. For testing, you can simulate the cronjob by loading the +# URL in a browser. +# +# See http://docs.elgg.org/wiki/Cron for more information +# +# @author Marcus Povey + +# Location of GET (see: http://docs.elgg.org/wiki/What_is_get) +GET='/usr/bin/GET' + +# Location of your site (don't forget the trailing slash!) +ELGG='http://www.example.com/' + +# The crontab +# Don't edit below this line unless you know what you are doing +@reboot $GET ${ELGG}cron/reboot/ +* * * * * $GET ${ELGG}cron/minute/ +*/5 * * * * $GET ${ELGG}cron/fiveminute/ +15,30,45,59 * * * * $GET ${ELGG}cron/fifteenmin/ +30,59 * * * * $GET ${ELGG}cron/halfhour/ +@hourly $GET ${ELGG}cron/hourly/ +@daily $GET ${ELGG}cron/daily/ +@weekly $GET ${ELGG}cron/weekly/ +@monthly $GET ${ELGG}cron/monthly/ +@yearly $GET ${ELGG}cron/yearly/ diff --git a/documentation/examples/events/advanced.php b/documentation/examples/events/advanced.php index c6fc2d0fb..73edea9da 100644 --- a/documentation/examples/events/advanced.php +++ b/documentation/examples/events/advanced.php @@ -1,10 +1,13 @@ <?php +/** + * This snippets demonstrates how returning false changes the normal operation + * of Elgg. + */ -register_elgg_event_handler('create', 'object', 'example_event_handler'); +elgg_register_event_handler('create', 'object', 'example_event_handler'); -function example_event_handler($event, $type, $params) { +function example_event_handler($event, $type, $object) { // Don't allow any non-admin users to create objects // Returning false from this function will halt the creation of the object. - return isadminloggedin(); + return elgg_is_admin_logged_in(); } - diff --git a/documentation/examples/events/all.php b/documentation/examples/events/all.php index 63959a9d2..0ad02c1d4 100644 --- a/documentation/examples/events/all.php +++ b/documentation/examples/events/all.php @@ -1,17 +1,24 @@ <?php +/** + * If you register an 'all' string for the event name, the handler function will + * be called for all events with that name, regardless of event type. The same + * can be done for the event type argument. Registering 'all' for both + * argyuments results in a handler being called for every event. + */ -register_elgg_event_handler('all', 'object', 'example_event_handler'); +elgg_register_event_handler('all', 'object', 'example_event_handler'); // This function will be called for any event of type 'object' -function example_event_handler($event, $type, $params) { +function example_event_handler($event, $type, $object) { // check what sort of object is passed - if ($params instanceof ElggObject) { - $subtype = $params->getSubtype(); + if ($object instanceof ElggObject) { + $subtype = $object->getSubtype(); - switch($subtype) { + switch ($subtype) { case 'blog': case 'thewire': case 'pages': + // prevent these object subtypes from being saved or changed return false; default: return true; @@ -21,4 +28,3 @@ function example_event_handler($event, $type, $params) { return true; } - diff --git a/documentation/examples/events/basic.php b/documentation/examples/events/basic.php index b274137b8..ca2762344 100644 --- a/documentation/examples/events/basic.php +++ b/documentation/examples/events/basic.php @@ -1,13 +1,17 @@ <?php +/** + * This snippet demonstrates how to register for an event. It dumps the + * parameters that the handler receives to the screen. The third argument + * of the handler function is an object that is related to the event. For + * the 'init', 'system' eveny, it is null. + */ -register_elgg_event_handler('init', 'system', 'example_event_handler'); +elgg_register_event_handler('init', 'system', 'example_event_handler'); -function example_event_handler($event, $type, $params) { +function example_event_handler($event, $type, $object) { var_dump($event); - var_dump($object_type); - var_dump($params); + var_dump($type); + var_dump($object); return true; } - - diff --git a/documentation/examples/events/emit.php b/documentation/examples/events/emit.php deleted file mode 100644 index f4cd1b127..000000000 --- a/documentation/examples/events/emit.php +++ /dev/null @@ -1,7 +0,0 @@ -<?php - -$params = new ElggObject(); -trigger_elgg_event('test', 'example', $params); - -// handlers would be registered by saying -register_elgg_event_handler('test', 'example', 'example_event_handler'); diff --git a/documentation/examples/events/trigger.php b/documentation/examples/events/trigger.php new file mode 100644 index 000000000..6ce3a76f0 --- /dev/null +++ b/documentation/examples/events/trigger.php @@ -0,0 +1,11 @@ +<?php +/** + * These two snippets demonstrates triggering an event and how to register for + * that event. + */ + +$object = new ElggObject(); +elgg_trigger_event('test', 'example', $object); + +// elsewhere a handler could be registered by saying +elgg_register_event_handler('test', 'example', 'example_event_handler'); diff --git a/documentation/examples/hooks/advanced.php b/documentation/examples/hooks/advanced.php new file mode 100644 index 000000000..ca036c46a --- /dev/null +++ b/documentation/examples/hooks/advanced.php @@ -0,0 +1,28 @@ +<?php +/** + * This snippet demonstrates how to change the value of a hook. The content + * passed into the hook is 'This is some Sample Content.'. After the two hook + * handlers are done, the new content is 'This is some $@mple Content.'. + */ + +// the output:page hook is triggered by elgg_view_page(). +elgg_register_plugin_hook_handler('output', 'page', 'example_plugin_hook_handler', 600); +elgg_register_plugin_hook_handler('output', 'page', 'example_plugin_hook_handler_2', 601); + +function example_plugin_hook_handler($hook, $type, $value, $params) { + // change a to @ + $value = str_replace('a', '@', $value); + + return $value; +} + +function example_plugin_hook_handler_2($hook, $type, $value, $params) { + // change S to $ + $value = str_replace('S', '$', $value); + + return $value; +} + +$content = 'This is some Sample Content.'; + +echo elgg_view_page('Title', $content); diff --git a/documentation/examples/hooks/all.php b/documentation/examples/hooks/all.php new file mode 100644 index 000000000..76b562335 --- /dev/null +++ b/documentation/examples/hooks/all.php @@ -0,0 +1,12 @@ +<?php +/** + * This snippet demonstrates how to register for multiple hooks with the same + * type. + */ + +elgg_register_plugin_hook_handler('all', 'system', 'example_plugin_hook_handler'); + +// This function will be called for any hook of type 'system' +function example_plugin_hook_handler($hook, $type, $value, $params) { + // logic here. +} diff --git a/documentation/examples/hooks/basic.php b/documentation/examples/hooks/basic.php index fe0b847a2..734d9e884 100644 --- a/documentation/examples/hooks/basic.php +++ b/documentation/examples/hooks/basic.php @@ -1,34 +1,17 @@ <?php +/** + * The handler for a plugin hook receives information about the hook (name and + * type), the current value for the hook, and parameters related to the hook. + */ -register_plugin_hook('get_items', 'example', 'example_plugin_hook'); -register_plugin_hook('get_items', 'example', 'example_plugin_hook_2'); +elgg_register_plugin_hook_handler('forward', '404', 'example_plugin_hook_handler'); -$params = array('username' => 'Joe'); -$items = trigger_plugin_hook('get_items', 'example', $params, $default); +function example_plugin_hook_handler($hook, $type, $value, $params) { + var_dump($hook); + var_dump($type); + var_dump($value); + var_dump($params); -var_dump($items); - -function example_plugin_hook($hook, $type, $value, $params) { - if (is_array($value)) { - $value[] = "Hook Value 1"; - $value[] = "Hook Value 2"; - } - - return $value; -} - -function example_plugin_hook($hook, $type, $value, $params) { - $username = isset($params['username']) ? $params['username'] : NULL; - if (is_array($value)) { - switch($username) { - case 'Joe': - $value[] = "Joe's item"; - break; - case 'John': - $value[] = "Joe's item"; - break; - } - } - - return $value; + // we are not changing $value so return null + return null; } diff --git a/documentation/examples/hooks/register/advanced.php b/documentation/examples/hooks/register/advanced.php deleted file mode 100644 index 48cddd480..000000000 --- a/documentation/examples/hooks/register/advanced.php +++ /dev/null @@ -1,23 +0,0 @@ -<?php - -// the output:page hook is triggered by page_draw(). -register_plugin_hook('output', 'page', 'example_plugin_hook_handler', 600) -register_plugin_hook('output', 'page', 'example_plugin_hook_handler_2', 601); - -function example_plugin_hook_handler($event, $type, $value, $params) { - // change A to @ - $value = str_replace('A', '@', $value); - - return $value -} - -function example_plugin_hook_handler($event, $type, $value, $params) { - // change S to $ - $value = str_replace('S', '$', $value); - - return $value -} - -$content = 'This is some Sample Content.'; - -page_draw('Title', $content); diff --git a/documentation/examples/hooks/register/all.php b/documentation/examples/hooks/register/all.php deleted file mode 100644 index f2e4407a6..000000000 --- a/documentation/examples/hooks/register/all.php +++ /dev/null @@ -1,8 +0,0 @@ -<?php - -register_plugin_hook('all', 'system', 'example_plugin_hook_handler'); - -// This function will be called for any event of type 'object' -function example_plugin_hook_handler($hook, $type, $value, $params) { - // logic here. -} diff --git a/documentation/examples/hooks/register/basic.php b/documentation/examples/hooks/register/basic.php deleted file mode 100644 index 957e82743..000000000 --- a/documentation/examples/hooks/register/basic.php +++ /dev/null @@ -1,14 +0,0 @@ -<?php - -register_plugin_hook('forward', 'system', 'example_plugin_hook_handler'); - -function example_plugin_hook_handler($event, $type, $value, $params) { - var_dump($event); - var_dump($type); - var_dump($value); - var_dump($params): - - return true; -} - - diff --git a/documentation/examples/hooks/register/emit.php b/documentation/examples/hooks/register/emit.php deleted file mode 100644 index bcf991794..000000000 --- a/documentation/examples/hooks/register/emit.php +++ /dev/null @@ -1,6 +0,0 @@ -<?php - -register_elgg_event_handler('test', 'example', 'example_init_system_callback'); - -$params = new ElggObject(); -trigger_elgg_event('test', 'example', $params); diff --git a/documentation/examples/hooks/trigger.php b/documentation/examples/hooks/trigger.php new file mode 100644 index 000000000..4216fd6c0 --- /dev/null +++ b/documentation/examples/hooks/trigger.php @@ -0,0 +1,14 @@ +<?php +/** + * The current value for the hook is passed into the trigger function. Handlers + * can change this value. In this snippet, we check if the value of true was + * changed by the handler functions. + */ + +$result = elgg_trigger_plugin_hook('get_status', 'example', null, true); + +if ($result) { + var_dump('Plugin hook says ok!'); +} else { + var_dump('Plugin hook says no.'); +} diff --git a/documentation/examples/hooks/trigger/advanced.php b/documentation/examples/hooks/trigger/advanced.php deleted file mode 100644 index de33e7a10..000000000 --- a/documentation/examples/hooks/trigger/advanced.php +++ /dev/null @@ -1,9 +0,0 @@ -<?php - -$default = array('Entry 1', 'Entry 2', 'Entry 3'); - -$menu = trigger_plugin_hook('get_menu_items', 'menu', null, $default); - -foreach ($menu as $item) { - var_dump($item); -} diff --git a/documentation/examples/hooks/trigger/basic.php b/documentation/examples/hooks/trigger/basic.php deleted file mode 100644 index a32bf9296..000000000 --- a/documentation/examples/hooks/trigger/basic.php +++ /dev/null @@ -1,9 +0,0 @@ -<?php - -$result = trigger_plugin_hook('get_status', 'example', null, true); - -if ($result) { - var_dump('Plugin hook says ok!'); -} else { - var_dump('Plugin hook says no.'); -} diff --git a/documentation/examples/plugins/README.txt b/documentation/examples/plugins/README.txt new file mode 100644 index 000000000..704f56598 --- /dev/null +++ b/documentation/examples/plugins/README.txt @@ -0,0 +1,5 @@ +Plugin Skeleton +========================= +This directory includes a plugin skeleton to be used as the starting point when +creating a new plugin. Just create a new directory in /mod/ and copy the files +and directories into it. Then update the manifest and start coding.
\ No newline at end of file diff --git a/documentation/examples/events/basic.php.out b/documentation/examples/plugins/actions/.gitignore index e69de29bb..e69de29bb 100644 --- a/documentation/examples/events/basic.php.out +++ b/documentation/examples/plugins/actions/.gitignore diff --git a/documentation/examples/plugins/languages/en.php b/documentation/examples/plugins/languages/en.php new file mode 100644 index 000000000..35f838560 --- /dev/null +++ b/documentation/examples/plugins/languages/en.php @@ -0,0 +1,24 @@ +<?php +/** + * The core language file is in /languages/en.php and each plugin has its + * language files in a languages directory. To change a string, copy the + * mapping into this file. + * + * For example, to change the blog Tools menu item + * from "Blog" to "Rantings", copy this pair: + * 'blog' => "Blog", + * into the $mapping array so that it looks like: + * 'blog' => "Rantings", + * + * Follow this pattern for any other string you want to change. Make sure this + * plugin is lower in the plugin list than any plugin that it is modifying. + * + * If you want to add languages other than English, name the file according to + * the language's ISO 639-1 code: http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes + */ + +$mapping = array( + 'string:here' => 'Display string here', +); + +add_translation('en', $mapping); diff --git a/documentation/examples/plugins/manifest.xml b/documentation/examples/plugins/manifest.xml new file mode 100644 index 000000000..e31624432 --- /dev/null +++ b/documentation/examples/plugins/manifest.xml @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="UTF-8"?> +<plugin_manifest xmlns="http://www.elgg.org/plugin_manifest/1.8"> + <name>My Plugin</name> + <author>My Name</author> + <version>1.0</version> + <description>This is a description of my plugin and its features.</description> + <website>http://www.elgg.org/</website> + <copyright>(C) My Name or Company 2012</copyright> + <license>GNU General Public License version 2</license> + + <requires> + <type>elgg_release</type> + <version>1.8</version> + </requires> + + <category>communication</category> + +</plugin_manifest> diff --git a/documentation/examples/plugins/start.php b/documentation/examples/plugins/start.php new file mode 100644 index 000000000..3af50ce38 --- /dev/null +++ b/documentation/examples/plugins/start.php @@ -0,0 +1,22 @@ +<?php +/** + * Describe plugin here + */ + +elgg_register_event_handler('init', 'system', 'my_plugin_init'); + +function my_plugin_init() { + // Rename this function based on the name of your plugin and update the + // elgg_register_event_handler() call accordingly + + // Register a script to handle (usually) a POST request (an action) + $base_dir = elgg_get_plugins_path() . 'my_plugin/actions/my_plugin'; + elgg_register_action('my_plugin', "$base_dir/my_action.php"); + + // Extend the main CSS file + elgg_extend_view('css/elgg', 'my_plugin/css'); + + // Add a menu item to the main site menu + $item = new ElggMenuItem('my_plugin', elgg_echo('my_plugin:menu'), 'my_url'); + elgg_register_menu_item('site', $item); +} diff --git a/documentation/examples/hooks/register/basic.php.out b/documentation/examples/plugins/views/default/.gitignore index e69de29bb..e69de29bb 100644 --- a/documentation/examples/hooks/register/basic.php.out +++ b/documentation/examples/plugins/views/default/.gitignore |
