'. t("This module allows users to associate a Prowl API key with their account.") .'

'; break; } return $output; } // function prowl_user_help /** * Implementation of hook_menu() */ function prowl_user_menu() { $items = array(); // autocomplete for users with prowl api key filled in $items['prowl/autocomplete_user'] = array( 'title' => 'Autocomplete Prowl Users', 'page callback' => 'prowl_user_autocomplete_user', 'access callback' => TRUE, 'access arguments' => array('Administer prowl notifications'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implementation of hook_action_info(). */ function prowl_user_action_info() { return array( 'prowl_user_actions_user_notification_action' => array( 'type' => 'system', 'description' => t('Send a tokenized prowl notification to a drupal user.'), 'configurable' => TRUE, 'hooks' => array( 'nodeapi' => array('view', 'insert', 'update', 'delete'), 'comment' => array('view', 'insert', 'update', 'delete'), 'user' => array('view', 'insert', 'update', 'delete', 'login'), 'taxonomy' => array('insert', 'update', 'delete'), ), ), ); } /** * Return a form definition so the Send notification to a drupal user action can be configured. * * @param $context * Default values (if we are editing an existing action instance). * @return * Form definition. */ function prowl_user_actions_user_notification_action_form($context) { if (!isset($context['event'])) { $context['event'] = ''; } if (!isset($context['message'])) { $context['message'] = ''; } if (!isset($context['priority'])) { $context['priority'] = 0; } if (!isset($context['user'])) { $context['user'] = ''; } if (!isset($context['send_test'])) { $context['send_test'] = False; } $form['user'] = array( '#type' => 'textfield', '#title' => t('User Name'), '#autocomplete_path' => 'prowl/autocomplete_user', '#default_value' => $context['user'], '#size' => '20', '#maxlength' => '60', '#description' => t('The Drupal username of the user to which to send the notification (only users with an API key set will autocomplete).'), ); $form['send_test'] = array( '#type' => 'checkbox', '#title' => t('Send a test notification when submitting this form.'), ); $form['event'] = array( '#type' => 'textfield', '#title' => t('Event'), '#default_value' => $context['event'], '#size' => '20', '#maxlength' => '1024', '#description' => t('The message event type.'), ); $form['message'] = array( '#type' => 'textarea', '#title' => t('Message'), '#default_value' => $context['message'], '#cols' => '80', '#rows' => '20', '#description' => t('The message that should be sent.'), ); $form['priority'] = array( '#type' => 'select', '#title' => t('Message priority level'), '#default_value' => $context['priority'], '#options' => array( -2 => t('Very low'), -1 => t('Low'), 0 => t('Moderate'), 1 => t('High'), 2 => t('Emergency'), ), '#description' => t('Set the priority level of this notification'), ); $form['help'] = array( '#type' => 'fieldset', '#collapsible' => TRUE, '#collapsed' => TRUE, '#title' => t('Placeholder tokens'), '#description' => t("The following placeholder tokens can be used in to generate the URL path. Some tokens may not be available, depending on the context in which the action is triggered."), ); $form['help']['tokens'] = array( '#value' => theme('token_help', 'all'), ); return $form; } /** * Submit handler for prowl_user_actions_user_notification_action_form() */ function prowl_user_actions_user_notification_action_submit($form, $form_state) { $form_values = $form_state['values']; // Process the HTML form to store configuration. The keyed array that // we return will be serialized to the database. $params = array( 'event' => $form_values['event'], 'message' => $form_values['message'], 'priority' => $form_values['priority'], 'user' => $form_values['user'], ); if ($form_values['send_test']) { if (prowl_send_user_notification_test($form_values['user'])) { drupal_set_message(t('Sent test notification to user: "%user"', array('%user' => $form_values['user'])), 'status'); } else { drupal_set_message(t('There was an error when attempting to send a notification to user: "%user"', array('%user' => $form_values['user'])), 'warning'); } } return $params; } function prowl_user($op, &$edit, &$account) { switch($op) { case 'register': $form['prowl_info'] = array( '#type' => 'fieldset', '#title' => t('Prowl settings'), '#weight' => -1, '#collapsible' => TRUE, '#collapsed' => FALSE, ); $form['prowl_info']['prowl_api_key'] = array( '#type' => 'textfield', '#title' => t('API key for your prowl account.'), '#size' => 40, '#maxlength' => 40, '#description' => t("Go to your prowl settings page to view/generate your API key."), '#required' => False, // '#prefix' => '

Employee Search Preference

', ); return $form; break; case 'form': //load up the user in question $usr = user_load(array('uid' => $account->uid)); // see if picture is listed $data = unserialize($usr->data); $attr = (!empty($data['prowl_api_key'])) ? $data['prowl_api_key'] : ''; $form['prowl_info'] = array( '#type' => 'fieldset', '#title' => t('Prowl settings'), '#weight' => -1, '#collapsible' => TRUE, '#collapsed' => FALSE, ); $form['prowl_info']['prowl_api_key'] = array( '#type' => 'textfield', '#title' => t('API key for your prowl account.'), '#default_value' => $attr, '#size' => 40, '#maxlength' => 40, '#description' => t("Go to your prowl settings page to view/generate your API key."), '#required' => False, // '#attributes' => $attr, ); return $form; break; } } /// Internal/API Functions /** * Implementation of a configurable Drupal action. * Sends a configurable prowl notification to the admin user's phone * @return boolean success */ function prowl_actions_user_notification_action(&$object, $context = array()) { $context['global'] = NULL; if (is_numeric($context['comment']->nid) && empty($context['node'])) { $context['node'] = node_load(array('nid' => $context['comment']->nid)); } if (!empty($context['node']) && empty($context['user'])) { $context['user'] = user_load(array('uid' => $context['node']->uid)); } $context['message'] = token_replace_multiple($context['message'], $context); return prowl_send_user_notification($name, $context['message'], $context['event'], $context['priority']); } /** * Send prowl notification to a site user * @return boolean success */ function prowl_send_user_notification($name, $message, $event, $priority = PROWL_MODERATE, $application = PROWL_APPLICATION) { $success = True; // get apikey from DB - doing a query ourselves to avoid the performance hit of a call to user_load() $apikey = ''; $result = db_query("SELECT data FROM {users} WHERE name LIKE '%s'", $name); if ($user_data = db_fetch_object($result)) { $data = unserialize($user_data->data); if (ctype_alnum($data['prowl_api_key']) && (strlen($data['prowl_api_key']) == 40)) { $apikey = $data['prowl_api_key']; } } if (isset($apikey) && strlen($apikey) == 40) { $prowl = new Prowl($apikey); $prowl->push(array( 'application' => $application, 'event' => $event, 'description' => $message, 'priority' => $priority, )); if (PROWL_DEBUG) { watchdog('prowl', 'api key: %apikey | priority: %priority | message: %message | event: %event | application: %application', array('%apikey' => $apikey, '%message' => $message, '%priority' => $priority, '%event' => $event, '%application' => $application), WATCHDOG_ERROR); } // log errors (if any) if ($prowl_errors = $prowl->getError()) { if (strpos($prowl_errors, 'Request Successful') === False) { watchdog('prowl', 'Push Notification Error: %error', array('%error' => $prowl_errors), WATCHDOG_ERROR); $success = False; } } } else { $success = False; } return $success; } // prowl_send_user_notification() /** * Send test prowl notification to given API key * @return boolean success */ function prowl_send_user_notification_test($name) { return prowl_send_user_notification($name, 'mic check 1 2', 'notification test'); } // prowl_send_user_notification_test() /** * Autocomplete function for users with Prowl API keys defined * @param $string * Search String * @return string */ function prowl_user_autocomplete_user($string = '') { if ($string != '') { $matches = array(); $result = db_query("SELECT name, data FROM {users} WHERE LOWER(name) LIKE LOWER('%s%%')", $string); while ($name = db_fetch_object($result)) { $data = unserialize($name->data); if (ctype_alnum($data['prowl_api_key']) && (strlen($data['prowl_api_key']) == 40)) { $matches[$name->name] = $name->name; } } print drupal_to_js($matches); } } // prowl_user_autocomplete_user()