/[drupal]/contributions/modules/edit_authoring_info/edit_authoring_info.module
ViewVC logotype

Contents of /contributions/modules/edit_authoring_info/edit_authoring_info.module

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


Revision 1.2 - (show annotations) (download) (as text)
Sun Sep 9 02:09:32 2007 UTC (2 years, 2 months ago) by davidlesieur
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Changes since 1.1: +3 -13 lines
File MIME type: text/x-php
#129894 by webchick: Port to 5.x.
1 <?php
2 // $Id: edit_authoring_info.module,v 1.1 2007/02/23 03:42:27 davidlesieur Exp $
3
4 /**
5 * Implementation of hook_perm().
6 */
7 function edit_authoring_info_perm() {
8 return array('edit date authored', 'edit author');
9 }
10
11 /**
12 * Implementation of hook_form_alter().
13 */
14 function edit_authoring_info_form_alter($form_id, &$form) {
15 if (isset($form['type']) && $form_id == $form['type']['#value'] .'_node_form') {
16 // Check 'administer nodes' to avoid conflict with the core code
17 if (!user_access('administer nodes') && (user_access('edit date authored') || user_access('edit author'))) {
18 $node = $form['#node'];
19
20 $form['author'] = array(
21 '#type' => 'fieldset',
22 '#title' => t('Authoring information'),
23 '#collapsible' => TRUE,
24 '#collapsed' => FALSE,
25 '#weight' => 20,
26 );
27
28 if (user_access('edit date authored')) {
29 if (!isset($node->created)) {
30 $node->created = time();
31 }
32 if (!isset($node->date)) {
33 $node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O');
34 }
35
36 // Authored on
37 $form['author']['date'] = array(
38 '#type' => 'textfield',
39 '#title' => t('Authored on'),
40 '#maxlength' => 25,
41 '#description' => t('Format: %time. Leave blank to use the time of form submission.', array('%time' => $node->date)),
42 );
43 if (isset($node->nid)) {
44 $form['author']['date']['#default_value'] = $node->date;
45 }
46 }
47
48 if (user_access('edit author')) {
49 $form['author']['name'] = array(
50 '#type' => 'textfield',
51 '#title' => t('Authored by'),
52 '#maxlength' => 60,
53 '#autocomplete_path' => 'user/autocomplete',
54 '#default_value' => $node->name ? $node->name : '',
55 '#weight' => -1,
56 '#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', 'Anonymous'))),
57 );
58 }
59
60 $form['#validate']['edit_authoring_info_form_validate'] = current($form['#validate']);
61 }
62 }
63 }
64
65 /**
66 * Callback for form validation.
67 */
68 function edit_authoring_info_form_validate($form_id, $form_values) {
69 if (user_access('edit date authored')) {
70 // Validate the "authored on" field. As of PHP 5.1.0, strtotime returns FALSE instead of -1 upon failure.
71 if (!empty($form_values['date']) && strtotime($form_values['date']) <= 0) {
72 form_set_error('date', t('You have to specify a valid date.'));
73 }
74 }
75 if (user_access('edit author')) {
76 // Validate the "authored by" field.
77 if (!empty($form_values['name']) && !($account = user_load(array('name' => $form_values['name'])))) {
78 // The use of empty() is mandatory in the context of usernames
79 // as the empty string denotes the anonymous user. In case we
80 // are dealing with an anonymous user we set the user ID to 0.
81 form_set_error('name', t('The username %name does not exist.', array ('%name' => theme('placeholder', $form_values['name']))));
82 }
83 }
84 }
85
86 /**
87 * Implementation of hook_nodeapi().
88 */
89 function edit_authoring_info_nodeapi(&$node, $op) {
90 // Check 'administer nodes' to avoid conflict with the core code
91 if ($op == 'submit' && !user_access('administer nodes')) {
92 if (user_access('edit date authored')) {
93 // Populate the "authored on" field
94 $node->created = $node->date ? strtotime($node->date) : NULL;
95 }
96 if (user_access('edit author')) {
97 // Populate the "authored by" field.
98 if ($account = user_load(array('name' => $node->name))) {
99 $node->uid = $account->uid;
100 }
101 else {
102 $node->uid = 0;
103 }
104 }
105 }
106 }

  ViewVC Help
Powered by ViewVC 1.1.2