/[drupal]/contributions/modules/library/library.actions.inc
ViewVC logotype

Contents of /contributions/modules/library/library.actions.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download) (as text)
Tue Sep 30 17:37:46 2008 UTC (13 months, 3 weeks ago) by jastraat
Branch: MAIN
Changes since 1.5: +0 -17 lines
File MIME type: text/x-php
#314859 - moved unrelated function out of actions file so that triggers is not required.
1 <?php
2 // $Id$
3
4 /**
5 * Implementation of hook_hook_info()
6 */
7 function library_hook_info() {
8 $op = array();
9 foreach (library_actions() as $aid => $action) {
10 $op['after_'. library_clean_action_name($action['name'])] = array('runs when' => t('After a '. $action['name']));
11 }
12
13 return array(
14 'library' => array(
15 'library' => $op
16 )
17 );
18 }
19
20 /**
21 * Since the library hooks are generated dynamically based on the list of library actions,
22 * it's necessary to call the list of actions directly.
23 * This generates a list of actions for the given trigger that are then called by actions_do()
24 */
25 function _library_get_hook_aids($hook, $op = '') {
26 $aids = array();
27 $result = db_query("SELECT aa.aid, a.type FROM {trigger_assignments} aa LEFT JOIN {actions} a ON aa.aid = a.aid WHERE aa.hook = '%s' AND aa.op = '%s' ORDER BY weight", $hook, $op);
28 while ($action = db_fetch_object($result)) {
29 $aids[] = $action->aid;
30 }
31 return $aids;
32 }
33
34 /**
35 * Implementation of hook_action_info().
36 */
37 function library_action_info() {
38 $hooks = array();
39 foreach (library_actions() as $aid => $action) {
40 $hooks[] = 'after_'. library_clean_action_name($action['name']);
41 }
42
43 return array(
44 'library_send_email_action' => array(
45 'description' => t('Send Library e-mail'),
46 'type' => 'library',
47 'configurable' => TRUE,
48 'hooks' => array(
49 'library' => $hooks,
50 )
51 ),
52 'library_renew_action' => array(
53 'description' => t('Extend Library Item Due Date'),
54 'type' => 'library',
55 'configurable' => FALSE,
56 'hooks' => array(
57 'library' => $hooks,
58 )
59 ),
60 );
61 }
62
63 /**
64 * Implementation of a Drupal action.
65 * Renews a library item if it is checked out and due dates are enabled.
66 */
67
68 function library_renew_action(&$object, $context = array()) {
69 $patron = $context['patron'];
70 $item = $context['item'];
71 $item_obj = library_load($item['id']);
72 if ($item_obj && !empty($item_obj->last_transaction_id) && !empty($item_obj->last_transaction_name) && !empty($item_obj->last_patron_id) && !empty($item_obj->last_due_date) &&
73 $item_obj->last_patron_id == $patron['nid'] && $item_obj->in_circulation == LIBRARY_CIRCULATION && $item_obj->library_status == LIBRARY_ITEM_UNAVAILABLE) {
74
75 $type = $item_obj->type;
76 $clean = library_clean_action_name($item_obj->last_transaction_name);
77 $period = variable_get('library_period_for_'. $type .'_'. $clean, 0);
78 if ($period > 0) {
79 $duedate = library_get_due_date(time(), $clean, $type);
80 if ($duedate) {
81 db_query("UPDATE {library_transactions} SET duedate = %d WHERE tid = %d", $duedate, $item_obj->last_transaction_id);
82 watchdog('library', '%name renewed %item [ID: %item_id ].', array('%name' => check_plain($patron['name']), '%item' => check_plain($item['title']), '%item_id' => $item['id']));
83 }
84 }
85 }
86 }
87
88 /**
89 * Implementation of hook_mail()
90 */
91 function library_mail($key, &$message, $params) {
92 $language = $message['language'];
93 switch ($key) {
94 case 'notify_overdue':
95 $variables = library_mail_tokens($params, $language);
96 $message['subject'] .= _library_mail_text($key .'_subject', $language, $variables);
97 $message['body'][] = _library_mail_text($key .'_body', $language, $variables);
98 break;
99 case 'action_send_email':
100 $context = $params['context'];
101 $variables = array(
102 '%site_name' => variable_get('site_name', 'Drupal'),
103 '%patron_name' => $context['patron']['name'],
104 '%patron_email' => $context['patron']['email'],
105 '%patron_id' => $context['patron']['nid'],
106 '%node_url' => url('node/'. $context['item']['nid'], array('absolute' => TRUE)),
107 '%node_type' => $context['item']['node_type'],
108 '%title' => $context['item']['title'],
109 '%item_id' => $context['item']['id'],
110 '%barcode' => $context['item']['barcode'],
111 '%transaction_name' => $context['transaction']['action_name'],
112 '%notes' => $context['transaction']['notes'],
113 );
114 $subject = strtr($context['subject'], $variables);
115 $body = strtr($context['message'], $variables);
116 $message['subject'] .= str_replace(array("\r", "\n"), '', $subject);
117 $message['body'][] = drupal_html_to_text($body);
118 break;
119 }
120 }
121
122
123 /**
124 * Return a form definition so the Send email action can be configured.
125 *
126 * @see library_send_email_action_validate()
127 * @see library_send_email_action_submit()
128 * @param $context
129 * Default values (if we are editing an existing action instance).
130 * @return
131 * Form definition.
132 */
133 function library_send_email_action_form($context) {
134 // Set default values for form.
135 if (!isset($context['recipient'])) {
136 $context['recipient'] = '';
137 }
138 if (!isset($context['subject'])) {
139 $context['subject'] = '';
140 }
141 if (!isset($context['message'])) {
142 $context['message'] = '';
143 }
144
145 $form['recipient'] = array(
146 '#type' => 'textfield',
147 '#title' => t('Recipient'),
148 '#default_value' => $context['recipient'],
149 '#maxlength' => '254',
150 '#description' => t('The email address to which the message should be sent.'),
151 );
152 $form['subject'] = array(
153 '#type' => 'textfield',
154 '#title' => t('Subject'),
155 '#default_value' => $context['subject'],
156 '#maxlength' => '254',
157 '#description' => t('The subject of the message.'),
158 );
159 $form['message'] = array(
160 '#type' => 'textarea',
161 '#title' => t('Message'),
162 '#default_value' => $context['message'],
163 '#cols' => '80',
164 '#rows' => '20',
165 '#description' => t('The message that should be sent. You may include the following variables: %site_name, %patron_name, %patron_email, %node_url, %item_id, %node_type, %title, %barcode, %transaction_name, %notes. Not all variables will be available in all contexts.'),
166 );
167 return $form;
168 }
169
170 /**
171 * Validate library_send_email_action form submissions.
172 */
173 function library_send_email_action_validate($form, $form_state) {
174 $form_values = $form_state['values'];
175 // Validate the configuration form.
176 if (!valid_email_address($form_values['recipient']) && $form_values['recipient'] != '%patron_email') {
177 // We want the literal %patron_email placeholder to be emphasized in the error message.
178 form_set_error('recipient', t('Please enter a valid email address or %patron_email.', array('%patron_email' => '%patron_email')));
179 }
180 }
181
182 /**
183 * Process library_send_email_action form submissions.
184 */
185 function library_send_email_action_submit($form, $form_state) {
186 $form_values = $form_state['values'];
187 // Process the HTML form to store configuration. The keyed array that
188 // we return will be serialized to the database.
189 $params = array(
190 'recipient' => $form_values['recipient'],
191 'subject' => $form_values['subject'],
192 'message' => $form_values['message'],
193 );
194 return $params;
195 }
196
197 /**
198 * Implementation of a configurable Drupal action. Sends an email.
199 */
200 function library_send_email_action(&$object, $context) {
201
202 $recipient = $context['recipient'];
203 $patron = $context['patron'];
204 $language = language_default();
205 if (!empty($patron->uid)) {
206 $account = user_load($patron->uid);
207 $language = user_preferred_language($account);
208 }
209
210 if ($recipient == '%patron_email') {
211 $recipient = $context['patron']['email'];
212 }
213
214 $params = array('context' => $context);
215
216 if (drupal_mail('library', 'action_send_email', $recipient, $language, $params)) {
217 watchdog('library', 'Sent email to %recipient', array('%recipient' => $recipient));
218 }
219 else {
220 watchdog('error', 'Unable to send email to %recipient', array('%recipient' => $recipient));
221 }
222 }
223
224 function library_mail_tokens($params, $language) {
225 $items = '';
226 foreach ($params['items'] as $id => $values) {
227 $items .= $values['item_name'] .' (Due '. format_date($values['due_date'], 'short') .') ';
228 }
229 $tokens = array(
230 '!patronname' => $params['patron']['patron_name'],
231 '!site' => variable_get('site_name', 'Drupal'),
232 '!items' => $items,
233 );
234 return $tokens;
235 }
236
237 /**
238 */
239 function library_email_form_submit($form, &$form_state) {
240 if ($form_state['values']['op'] == t('Send E-mail')) {
241
242 drupal_set_message(t('The email has been sent.'));
243 }
244 $form_state['redirect'] = 'library-items';
245 return;
246 }

  ViewVC Help
Powered by ViewVC 1.1.2