1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Triggers and actions supported by the revisioning module. |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * Implementation of hook_hook_info(). |
---|
10 | * Defines triggers available in this module. |
---|
11 | */ |
---|
12 | function revisioning_hook_info() { |
---|
13 | return array( |
---|
14 | // First key is name of tab on admin/build/trigger page that triggers appear on |
---|
15 | 'revisioning' => array( |
---|
16 | 'revisioning' => array( // trigger name must equal module name |
---|
17 | // List of trigger operations |
---|
18 | 'publish' => array( |
---|
19 | 'runs when' => t('When publishing a pending revision'), |
---|
20 | ), |
---|
21 | 'revert' => array( |
---|
22 | 'runs when' => t('When reverting to an archived revision'), |
---|
23 | ), |
---|
24 | 'unpublish' => array( |
---|
25 | 'runs when' => t('When unpublishing the current revision'), |
---|
26 | ), |
---|
27 | ), |
---|
28 | ), |
---|
29 | ); |
---|
30 | } |
---|
31 | |
---|
32 | /** |
---|
33 | * Implementation of hook_<trigger_name>(). |
---|
34 | * |
---|
35 | * Note the confusing name -- this due to fact that trigger name needs to equal |
---|
36 | * the module name. |
---|
37 | * @see revisioning_hook_info() |
---|
38 | * |
---|
39 | * @param $op |
---|
40 | * trigger operation name, e.g 'publish', 'unpublish', 'revert' as passed in |
---|
41 | * from revisioning_revisionapi() |
---|
42 | * @param $object |
---|
43 | * typically the node object as passed in from revisioning_revisionapi(); |
---|
44 | * if omitted this function will try to load the node object based on the URL |
---|
45 | */ |
---|
46 | function revisioning_revisioning($op, $object = NULL, $args = NULL) { |
---|
47 | if (!module_exists('trigger')) { |
---|
48 | return; |
---|
49 | } |
---|
50 | $aids = _trigger_get_hook_aids('revisioning', $op); |
---|
51 | if (empty($aids)) { // no actions defined for this trigger |
---|
52 | return; |
---|
53 | } |
---|
54 | watchdog('revisioning', '%op trigger is actioning "@aids"', |
---|
55 | array('%op' => $op, '@aids' => implode(', ', array_keys($aids)))); |
---|
56 | global $user; |
---|
57 | $context = array('hook' => 'revisioning', 'op' => $op, 'user' => $user); |
---|
58 | foreach ($aids as $aid => $action_info) { |
---|
59 | if ($action_info['type'] == 'node') { |
---|
60 | $object = NULL; // @todo: sort out why we need this line i.e. enforce reload |
---|
61 | $nid = arg(1); |
---|
62 | if (!$object && !$node && (arg(0) == 'node') && is_numeric($nid)) { |
---|
63 | // Clear the static node_load() cache to ensure we are passing the |
---|
64 | // updated object to the node actions. |
---|
65 | $node = node_load($nid, NULL, TRUE); |
---|
66 | } |
---|
67 | $obj = $object ? $object : $node; |
---|
68 | } |
---|
69 | else { // assume user object |
---|
70 | $obj = $object ? $object : $user; |
---|
71 | } |
---|
72 | // Include node in context so we can use node-related tokens in the action [#1035354] |
---|
73 | if (is_object($object) && $object->nid) { |
---|
74 | $context['node'] = $object; |
---|
75 | } |
---|
76 | actions_do($aid, $obj, $context, $args); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * Implementation of hook_action_info(). |
---|
82 | * Defines actions available in this module. |
---|
83 | */ |
---|
84 | function revisioning_action_info() { |
---|
85 | return array( |
---|
86 | 'revisioning_delete_archived_action' => array( |
---|
87 | 'type' => 'node', |
---|
88 | 'description' => t('Delete archived revisions of the node'), |
---|
89 | 'configurable' => FALSE, |
---|
90 | 'hooks' => array( |
---|
91 | 'nodeapi' => array('update') |
---|
92 | ) |
---|
93 | ), |
---|
94 | 'revisioning_create_pending_revision_action' => array( |
---|
95 | 'description' => t('Create a new pending revision of a node'), |
---|
96 | 'type' => 'node', |
---|
97 | 'configurable' => FALSE, |
---|
98 | 'hooks' => array( |
---|
99 | 'nodeapi' => array('presave'), |
---|
100 | ) |
---|
101 | ), |
---|
102 | 'revisioning_publish_latest_revision_action' => array( |
---|
103 | 'type' => 'node', |
---|
104 | 'description' => t('Publish the most recent pending revision'), |
---|
105 | 'configurable' => FALSE, |
---|
106 | 'hooks' => array( |
---|
107 | 'nodeapi' => array('presave') |
---|
108 | ) |
---|
109 | ) |
---|
110 | // Don't need 'changes_node_property'; it will generate a superfluous "save |
---|
111 | // post" action; we're alreay making sure db is updated |
---|
112 | // 'behavior' => array('changes_node_property') |
---|
113 | ); |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * Implementation of delete archived action. |
---|
118 | */ |
---|
119 | function revisioning_delete_archived_action(&$node, $context = array()) { |
---|
120 | if (empty($node->revision_moderation)) { |
---|
121 | // return; |
---|
122 | } |
---|
123 | $num_archived = revisioning_get_number_of_archived_revisions($node); |
---|
124 | if ($num_archived > 0) { |
---|
125 | $type = node_get_types('name', $node->type); |
---|
126 | watchdog('revisioning', |
---|
127 | 'Executing deleting archived revisions action for @type %title', array('@type' => $type, '%title' => $node->title), |
---|
128 | WATCHDOG_NOTICE, l(t('view'), "node/$node->nid")); |
---|
129 | if (revisioning_delete_archived_revisions($node)) { |
---|
130 | drupal_set_message(format_plural($num_archived, '@type %title: one archived revision deleted.', '@type %title: @count archived revisions deleted.', |
---|
131 | array('@type' => $type, '%title' => $node->title))); |
---|
132 | } |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * Implementation of create_pending_revision_action. |
---|
138 | */ |
---|
139 | function revisioning_create_pending_revision_action(&$node, $context = array()) { |
---|
140 | if ($node->is_new || empty($node->nid)) { |
---|
141 | return; |
---|
142 | } |
---|
143 | $type = node_get_types('name', $node->type); |
---|
144 | watchdog('revisioning', |
---|
145 | 'Executing create_pending_revision_action for @type %title', array('@type' => $type, '%title' => $node->title), |
---|
146 | WATCHDOG_NOTICE, l(t('view'), "node/$node->nid")); |
---|
147 | // Create a new revision upon saving and subject it to moderation (pending) |
---|
148 | $node->revision = TRUE; |
---|
149 | $node->revision_moderation = TRUE; |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * Implementation of publish_latest_revision action |
---|
154 | */ |
---|
155 | function revisioning_publish_latest_revision_action(&$node, $context = array()) { |
---|
156 | $type = node_get_types('name', $node->type); |
---|
157 | watchdog('revisioning', |
---|
158 | 'Executing publish_latest_revision action for @type %title', array('@type' => $type, '%title' => $node->title), |
---|
159 | WATCHDOG_NOTICE, l(t('view'), "node/$node->nid")); |
---|
160 | if (_revisioning_publish_latest_revision($node)) { |
---|
161 | drupal_set_message(t('Revision has been published.')); |
---|
162 | } |
---|
163 | else { |
---|
164 | drupal_set_message(t('"!title" has no pending revision to be published.', array('!title' => check_plain($node->title))), 'warning'); |
---|
165 | } |
---|
166 | } |
---|