| 1 |
<?php
|
| 2 |
// $Id: trigger_example.module,v 1.1 2008/08/04 22:25:46 drewish Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_help().
|
| 6 |
*/
|
| 7 |
function trigger_example_help($path, $arg) {
|
| 8 |
switch ($path) {
|
| 9 |
case 'trigger_example':
|
| 10 |
return '<p>'. t('Use this page to call hook_trigger_example() with the ping and pong operations. You can create actions to display a message or email the user on the <a href="@actions-url">Actions settings page</a> and assign these actions with the ping and pong events on the <a href="@triggers-url">Triggers settings page</a>.', array('@actions-url' => url('admin/settings/actions'), '@triggers-url' => url('admin/build/trigger/trigger_example'))) .'</p>';
|
| 11 |
case 'admin/build/trigger/trigger_example':
|
| 12 |
$explanation = '<p>'. t('Triggers are system events, such as when new content is added or when a user logs in. Trigger module combines these triggers with actions (functional tasks), such as unpublishing content or e-mailing an administrator. The <a href="@url">Actions settings page</a> contains a list of existing actions and provides the ability to create and configure additional actions.', array('@url' => url('admin/settings/actions'))) .'</p>';
|
| 13 |
return $explanation .'<p>'. t('Below you can assign actions to run when certain comment-related triggers happen. For example, you could promote a post to the front page when a comment is added.') .'</p>';
|
| 14 |
}
|
| 15 |
}
|
| 16 |
|
| 17 |
/**
|
| 18 |
* Implementation of hook_menu().
|
| 19 |
*
|
| 20 |
* Provide a form that can be used to fire the module's triggers.
|
| 21 |
*/
|
| 22 |
function trigger_example_menu() {
|
| 23 |
$items['trigger_example'] = array(
|
| 24 |
'title' => t('Trigger Example'),
|
| 25 |
'description' => t('Provides a form to test the triggers.'),
|
| 26 |
'page callback' => 'drupal_get_form',
|
| 27 |
'page arguments' => array('trigger_example_form'),
|
| 28 |
'access callback' => TRUE,
|
| 29 |
);
|
| 30 |
return $items;
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Implementation of hook_hook_info().
|
| 35 |
*
|
| 36 |
* This hook is used to tell Drupal which hooks we provide that can be used
|
| 37 |
* as triggers for actions. We provide a text description of when each of the
|
| 38 |
* operations occurs.
|
| 39 |
*/
|
| 40 |
function trigger_example_hook_info() {
|
| 41 |
return array(
|
| 42 |
'trigger_example' => array(
|
| 43 |
// One important thing to note here. If you want to use the trigger
|
| 44 |
// module's default menu and forms handlers your hook needs to have the
|
| 45 |
// same name as your module. If you've got the "foo" module with a hook
|
| 46 |
// "hook_fooapi" then you're going to have to implement hook_forms and
|
| 47 |
// map the form ids back to the trigger_assign_form callback.
|
| 48 |
'trigger_example' => array(
|
| 49 |
'ping' => array(
|
| 50 |
'runs when' => t('A ping happens'),
|
| 51 |
),
|
| 52 |
'pong' => array(
|
| 53 |
'runs when' => t('A pong happens'),
|
| 54 |
),
|
| 55 |
),
|
| 56 |
),
|
| 57 |
);
|
| 58 |
}
|
| 59 |
|
| 60 |
/**
|
| 61 |
* Function to generate ping events and invoke hook_trigger_example().
|
| 62 |
*/
|
| 63 |
function trigger_example_ping() {
|
| 64 |
$count = variable_get('trigger_example_pings', 0) + 1;
|
| 65 |
|
| 66 |
// Invoke hook_trigger_example() with the ping operation.
|
| 67 |
module_invoke_all('trigger_example', 'ping', $count);
|
| 68 |
|
| 69 |
variable_set('trigger_example_pings', $count);
|
| 70 |
}
|
| 71 |
|
| 72 |
/**
|
| 73 |
* Function to generate pong events and invoke hook_trigger_example().
|
| 74 |
*/
|
| 75 |
function trigger_example_pong() {
|
| 76 |
$count = variable_get('trigger_example_pings', 0);
|
| 77 |
|
| 78 |
// Invoke hook_trigger_example() with the pong operation.
|
| 79 |
module_invoke_all('trigger_example', 'pong', $count);
|
| 80 |
|
| 81 |
variable_set('trigger_example_pings', max(0, $count - 1));
|
| 82 |
}
|
| 83 |
|
| 84 |
/**
|
| 85 |
* Implementation of hook_trigger_example().
|
| 86 |
*/
|
| 87 |
function trigger_example_trigger_example($op, $count) {
|
| 88 |
// Our module is dependent on the trigger module but other modules that this
|
| 89 |
// code get's copied into might not so it's a good idea to check first.
|
| 90 |
if (!module_exists('trigger')) {
|
| 91 |
break;
|
| 92 |
}
|
| 93 |
|
| 94 |
// Find any the ids of any actions associated with this hook/operation pair.
|
| 95 |
if ($aids = _trigger_get_hook_aids('trigger_example', $op)) {
|
| 96 |
// Setup the context for our trigger.
|
| 97 |
$context = array(
|
| 98 |
'hook' => 'trigger_example',
|
| 99 |
'op' => $op,
|
| 100 |
'count' => $count
|
| 101 |
);
|
| 102 |
|
| 103 |
// Since we're not operating on an object we need to create a dummy.
|
| 104 |
$dummy = new stdClass();
|
| 105 |
foreach ($aids as $aid => $action_info) {
|
| 106 |
actions_do($aid, $dummy, $context);
|
| 107 |
}
|
| 108 |
}
|
| 109 |
}
|
| 110 |
|
| 111 |
/**
|
| 112 |
* Implementation of hook_action_info_alter().
|
| 113 |
*
|
| 114 |
* None of the built-in actions will be enabled for our hook by default. We
|
| 115 |
* need to implement hook_action_info_alter() so that we can enable a couple.
|
| 116 |
*/
|
| 117 |
function trigger_example_action_info_alter(&$info) {
|
| 118 |
if (isset($info['system_message_action']['hooks'])) {
|
| 119 |
$info['system_message_action']['hooks']['trigger_example'] = array('ping', 'pong');
|
| 120 |
}
|
| 121 |
if (isset($info['system_send_email_action']['hooks'])) {
|
| 122 |
$info['system_send_email_action']['hooks']['trigger_example'] = array('ping', 'pong');
|
| 123 |
}
|
| 124 |
}
|
| 125 |
|
| 126 |
/**
|
| 127 |
* A form to help fire our triggers.
|
| 128 |
*/
|
| 129 |
function trigger_example_form(&$form_state) {
|
| 130 |
$form['help'] = array(
|
| 131 |
'#type' => 'item',
|
| 132 |
'#value' => format_plural(variable_get('trigger_example_pings', 0), 'There is only @count ping out there.', 'There are @count pings out there. Come on pong them back.'),
|
| 133 |
);
|
| 134 |
$form['ping'] = array(
|
| 135 |
'#type' => 'submit',
|
| 136 |
'#value' => t('Ping'),
|
| 137 |
);
|
| 138 |
if (variable_get('trigger_example_pings', 0)) {
|
| 139 |
$form['pong'] = array(
|
| 140 |
'#type' => 'submit',
|
| 141 |
'#value' => t('Pong'),
|
| 142 |
);
|
| 143 |
}
|
| 144 |
return $form;
|
| 145 |
}
|
| 146 |
|
| 147 |
function trigger_example_form_submit($form, &$form_state) {
|
| 148 |
if ($form_state['values']['op'] == t('Ping')) {
|
| 149 |
trigger_example_ping();
|
| 150 |
}
|
| 151 |
else {
|
| 152 |
trigger_example_pong();
|
| 153 |
}
|
| 154 |
}
|