log.pages.inc 5.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
<?php

/**
 * @file
 * Log pages
 */

/**
 * Log view callback.
 */
function log_view($log) {
12
13

  // Set the title of the page to the log's label.
14
  drupal_set_title(entity_label('log', $log));
15
16
17
18

  // Build the log entity render array.
  $output = entity_view('log', array(entity_id('log', $log) => $log), 'full');

19
20
21
22
23
24
25
26
27
28
29
30
31
32
  // Add the log's "done" status.
  if ($log->done) {
    $status = 'DONE';
  }
  else {
    $status = 'NOT DONE';
    drupal_set_message('This log item is not done.', 'warning');
  }
  $output['log'][$log->id]['done'] = array(
    '#markup' => '<div style="float: right;">Status: <strong>' . $status . '</strong></div>',
    '#weight' => -100,
  );

  // Add the log's timestamp.
33
34
35
  if (!empty($log->timestamp)) {
    $date = date('M j Y', $log->timestamp);
    $output['log'][$log->id]['timestamp'] = array(
36
37
      '#markup' => '<p><strong>Date:</strong> ' . $date . '</p>',
      '#weight' => -99,
38
39
    );
  }
40

41
  return $output;
42
}
43
44
45
46
47
48
49

/**
 * Page to select log Type to add new log.
 */
function log_add_types_page() {
  $items = array();
  foreach (log_types() as $log_type_key => $log_type) {
50
51
52
    if (log_access('create', $log_type)) {
      $items[] = l(entity_label('log_type', $log_type), 'log/add/' . $log_type_key);
    }
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  }
  return array(
    'list' => array(
      '#theme' => 'item_list',
      '#items' => $items,
      '#title' => t('Select a type of log to create.'),
    ),
  );
}

/**
 * Add new log page callback.
 */
function log_add($type) {
  $log_type = log_types($type);

  $log = entity_create('log', array('type' => $type));
  drupal_set_title(t('Log @name', array('@name' => entity_label('log_type', $log_type))));

  $output = drupal_get_form('log_form', $log);

  return $output;
}

/**
 * Log Form.
 */
function log_form($form, &$form_state, $log) {

  $form['log'] = array(
    '#type' => 'value',
    '#value' => $log,
  );

  // Load the log type.
  $log_type = log_type_load($log->type);

  // Only display the name field if it's editable.
  if (!empty($log_type->name_edit)) {
    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Name'),
      '#description' => t('Leave this blank to automatically generate a name.'),
      '#default_value' => $log->name,
    );
  }

100
101
102
103
104
105
106
107
108
109
110
111
  // Timestamp
  $timestamp_default = !empty($log->timestamp) ? date('Y-m-d H:i:s', $log->timestamp) : '';
  $form['timestamp'] = array(
    '#type' => 'date_select',
    '#title' => t('Date'),
    '#date_format' => 'M j Y',
    '#date_type' => DATE_FORMAT_UNIX,
    '#date_year_range' => '-10:+3',
    '#default_value' => $timestamp_default,
    '#required' => TRUE,
  );

112
113
114
115
  // If the log is new, and the log type marks new logs as "done" by default,
  // mark the log as done.
  if (empty($log->id) && !empty($log_type->done)) {
    $log->done = TRUE;
116
  }
117
118
119
120
121
122
  $form['done'] = array(
    '#type' => 'checkbox',
    '#title' => t('Done'),
    '#description' => t('Mark this log as done.'),
    '#default_value' => $log->done,
  );
123
124

  // Log user id.
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
  $form['uid'] = array(
    '#type' => 'value',
    '#value' => $log->uid,
  );

  field_attach_form('log', $log, $form, $form_state);

  $submit = array();
  if (!empty($form['#submit'])) {
    $submit += $form['#submit'];
  }

  $form['actions'] = array(
    '#weight' => 100,
  );

  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save log'),
    '#submit' => $submit + array('log_form_submit'),
  );

  // Show Delete button if allowed.
  $log_id = entity_id('log' ,$log);
  if (!empty($log_id) && log_access('delete', $log)) {

    // Get the destination query parameter. If it is the current path, change to <front>
    // (because the current path won't exist once the log is deleted).
    $destination = drupal_get_destination();
    if ($destination['destination'] == current_path()) {
      $destination['destination'] = '<front>';
    }
    $form['actions']['delete'] = array(
      '#type' => 'markup',
      '#markup' => l(t('Delete'), 'log/' . $log_id . '/delete', array('query' => $destination)),
    );
  }

  return $form;
}

/**
 * Log validate handler.
 */
function log_form_validate($form, &$form_state) {

}

/**
 * Log submit handler.
 */
function log_form_submit($form, &$form_state) {
177
178

  // Load the log entity from the form values.
179
180
  $log = $form_state['values']['log'];

181
182
183
184
  // Convert the date to a timestamp.
  $form_state['values']['timestamp'] = strtotime($form_state['values']['timestamp']);

  // Build the entity from the form values.
185
186
  entity_form_submit_build_entity('log', $log, $form, $form_state);

187
  // Save the log.
188
189
  log_save($log);

190
  // Redirect to the log.
191
192
193
  $log_uri = entity_uri('log', $log);
  $form_state['redirect'] = $log_uri['path'];

194
  // Print a message.
195
  drupal_set_message(t('%title saved.', array('%title' => entity_label('log', $log))));
196
197
198
199
200
201
202
203
204
205
206
207
208
209
}

/**
 * Delete confirmation form.
 */
function log_delete_form($form, &$form_state, $log) {
  $form['log'] = array(
    '#type' => 'value',
    '#value' => $log,
  );
  // Always provide entity id in the same form key as in the entity edit form.
  $form['log_type_id'] = array('#type' => 'value', '#value' => entity_id('log' ,$log));
  $log_uri = entity_uri('log', $log);
  return confirm_form($form,
210
    t('Are you sure you want to delete %title?', array('%title' => entity_label('log', $log))),
211
212
213
214
215
216
217
218
219
220
221
222
223
224
    $log_uri['path'],
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

/**
 * Delete form submit handler.
 */
function log_delete_form_submit($form, &$form_state) {
  $log = $form_state['values']['log'];
  log_delete($log);

225
  drupal_set_message(t('%title was deleted.', array('%title' => entity_label('log', $log))));
226
227
228

  $form_state['redirect'] = '<front>';
}