'Journal entries', 'description' => 'View journal entries.', 'page callback' => 'journal_view', 'access arguments' => $access, ); return $items; } /** * Add Journal fields to all forms. * * Any form, except a few pre-defined form_ids, will be extended by a fieldset * to enter a journal entry. * * @see journal_skip_form() */ function journal_form_alter(&$form, &$form_state, $form_id) { if (!user_access('access journal')) { return; } // Do not extend this form, if it is in the list of form_ids to skip. if (journal_skip_form($form_id)) { return; } $entry_required = FALSE; if (!isset($form['#submit'])) { $form['#submit'] = array(); } // Shift system_settings_form buttons. if (in_array('system_settings_form_submit', $form['#submit'])) { $weight = isset($form['buttons']['#weight']) ? $form['buttons']['#weight'] : 0; $form['buttons']['#weight'] = $weight + 1; $journal_weight = $weight; $entry_required = TRUE; } else { $journal_weight = 100; } // Prepend our journal submit handler, so we can eliminate the form value of // journal_entry, which would be saved as a variable in system_settings_form() // otherwise. array_unshift($form['#submit'], 'journal_form_submit'); // Store the path on which this form was initially displayed. // We need to store this in a hidden field, since forms with custom '#action's // (like admin/build/modules) will reset our value to $_GET['q'] otherwise. $form['journal']['journal_location'] = array( '#type' => 'hidden', '#value' => (!empty($_REQUEST['journal_location']) ? $_REQUEST['journal_location'] : $_GET['q']), ); // Add journal entry field. $form['journal']['journal_entry'] = array( '#type' => 'textarea', '#title' => t('Journal entry'), '#description' => t('If not empty, contents of this field will be logged to the system journal.'), '#required' => $entry_required, '#weight' => $journal_weight, ); } /** * Save a new journal entry and clean out form values. */ function journal_form_submit($form, &$form_state) { if (!empty($form_state['values']['journal_entry'])) { journal_add_entry($form_state['values']['journal_entry'], $form_state['values']['journal_location']); } unset($form_state['values']['journal_entry'], $form_state['values']['journal_location']); } /** * Indicate if a form must not extended. * * @param string $form_id * A form_id to check against. * * @return bool * True if form should be skipped, false if form can be extended. * * @todo Fetch custom form_ids from a variable. * @todo Allow to define custom form_ids in a settings page? * @todo Introduce a new FAPI attribute #journal = TRUE to require a journal * entry if Journal module is enabled - OR - introduce a new hook_journal? */ function journal_skip_form($form_id) { $skip_ids = array( 'devel_switch_user_form', 'search_block_form', 'search_theme_form', 'user_filter_form', 'user_login_block', ); if (in_array($form_id, $skip_ids)) { return TRUE; } return FALSE; } /** * Implementation of hook_user(). */ function journal_user($op, &$edit, &$user) { if ($op == 'delete') { db_query('UPDATE {journal} SET uid = 0 WHERE uid = %d', $user->uid); } } /** * Implementation of hook_block(). */ function journal_block($op = 'list', $delta = 0, $edit = array()) { if ($op == 'list') { $blocks['backlog'] = array( 'info' => t('Journal entries'), 'weight' => -10, 'enabled' => 1, 'region' => 'right', ); return $blocks; } else if ($op == 'view' && user_access('access journal')) { switch($delta) { case 'backlog': $result = db_query("SELECT j.*, u.name FROM {journal} j INNER JOIN {users} u ON j.uid = u.uid WHERE j.location = '%s' ORDER BY j.timestamp DESC", $_GET['q']); if ($output = journal_output($result, 'list')) { drupal_add_css(drupal_get_path('module', 'journal') .'/journal.css', 'module', 'all', FALSE); $block = array( 'subject' => t('Journal entries'), 'content' => $output, ); } break; } return $block; } } /** * Output a sortable table containing all journal entries. */ function journal_view() { $sql = "SELECT j.*, u.name FROM {journal} j INNER JOIN {users} u ON j.uid = u.uid"; $header = array( array('data' => t('Date'), 'field' => 'j.timestamp', 'sort' => 'desc'), array('data' => t('User'), 'field' => 'u.name'), t('Message'), t('Location'), ); $tablesort = tablesort_sql($header); $result = pager_query($sql . $tablesort, 50); return journal_output($result, 'table', $header); } /** * Render journal entries. * * Use this function to render and return * - a journal provided as a database query result resource or * - a custom journal provided as an array containing journal entry objects. * * This function may look insane to some, but it ensures that implementation of * journal module into other modules is as easy as possible. * * @param array $journal * A database query result resource or an array containing journal entry * objects to output. * @param string $format * Whether to output all log entries as 'table', 'list' or plain 'text'. * @param array $header * An array containing header data for 'table' output. * * @todo Add XML output format. */ function journal_output($journal, $format = 'table', $header = array()) { $type = gettype($journal); switch ($format) { case 'text': // Output delimiter in first line, since this may chance. $output = '\t' . "\n"; while ($entry = ($type == 'resource' ? db_fetch_object($journal) : array_shift($journal))) { $row = array( $entry->timestamp, $entry->uid, $entry->message, $entry->location, ); $output .= implode("\t", $row) ."\n"; } break; case 'list': $output = ''; while ($entry = ($type == 'resource' ? db_fetch_object($journal) : array_shift($journal))) { $output .= '