log.pages.inc 7.25 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
m.stenta's avatar
m.stenta committed
5
 * Log pages.
6
7
8
9
 */

/**
 * Log view callback.
m.stenta's avatar
m.stenta committed
10
11
12
13
14
15
 *
 * @param Log $log
 *   The log entity.
 *
 * @return array
 *   Returns the entity render array.
16
 */
m.stenta's avatar
m.stenta committed
17
function log_view(Log $log) {
18
19

  // Set the title of the page to the log's label.
20
  drupal_set_title(entity_label('log', $log));
21
22
23
24

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

25
26
27
28
29
30
  // Add the log's "done" status.
  if ($log->done) {
    $status = 'DONE';
  }
  else {
    $status = 'NOT DONE';
m.stenta's avatar
m.stenta committed
31
    drupal_set_message(t('This log item is not done.'), 'warning');
32
33
34
35
36
37
38
  }
  $output['log'][$log->id]['done'] = array(
    '#markup' => '<div style="float: right;">Status: <strong>' . $status . '</strong></div>',
    '#weight' => -100,
  );

  // Add the log's timestamp.
39
40
41
  if (!empty($log->timestamp)) {
    $date = date('M j Y', $log->timestamp);
    $output['log'][$log->id]['timestamp'] = array(
42
43
      '#markup' => '<p><strong>Date:</strong> ' . $date . '</p>',
      '#weight' => -99,
44
45
    );
  }
46

47
  return $output;
48
}
49
50
51
52
53
54
55

/**
 * 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) {
56
57
58
    if (log_access('create', $log_type)) {
      $items[] = l(entity_label('log_type', $log_type), 'log/add/' . $log_type_key);
    }
59
60
61
62
63
64
65
66
67
68
69
70
  }
  return array(
    'list' => array(
      '#theme' => 'item_list',
      '#items' => $items,
      '#title' => t('Select a type of log to create.'),
    ),
  );
}

/**
 * Add new log page callback.
m.stenta's avatar
m.stenta committed
71
72
73
74
75
76
 *
 * @param string $type
 *   The log type.
 *
 * @return array
 *   Returns the log form array.
77
78
79
80
81
82
83
84
85
86
87
88
89
90
 */
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.
m.stenta's avatar
m.stenta committed
91
92
93
94
95
96
97
98
99
100
 *
 * @param array $form
 *   The form array.
 * @param array $form_state
 *   The form state array.
 * @param Log $log
 *   The log entity.
 *
 * @return array
 *   Returns the log form array.
101
 */
m.stenta's avatar
m.stenta committed
102
function log_form(array $form, array &$form_state, Log $log) {
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

  $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,
    );
  }

m.stenta's avatar
m.stenta committed
122
  // Timestamp.
123
124
125
126
127
128
129
130
131
132
133
  $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,
  );

134
  // If the log is new, and the log type marks new logs as "done" by default,
135
136
  // mark the log as done and add some Javascript to auto-check/uncheck based
  // on the timestamp.
137
138
  if (empty($log->id) && !empty($log_type->done)) {
    $log->done = TRUE;
139
    drupal_add_js(drupal_get_path('module', 'log') . '/log.done.js');
140
  }
141
142
143
144
145
146
  $form['done'] = array(
    '#type' => 'checkbox',
    '#title' => t('Done'),
    '#description' => t('Mark this log as done.'),
    '#default_value' => $log->done,
  );
147
148

  // Log user id.
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  $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.
m.stenta's avatar
m.stenta committed
172
  $log_id = entity_id('log', $log);
173
174
  if (!empty($log_id) && log_access('delete', $log)) {

m.stenta's avatar
m.stenta committed
175
176
    // 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).
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    $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.
m.stenta's avatar
m.stenta committed
192
193
194
195
196
 *
 * @param array $form
 *   The form array.
 * @param array $form_state
 *   The form state array.
197
 */
m.stenta's avatar
m.stenta committed
198
function log_form_validate(array $form, array &$form_state) {
199

200
201
  // If a new log is marked "done", but the timestamp is in the future, display
  // a warning, but allow the user to continue if they submit the form again.
m.stenta's avatar
m.stenta committed
202
  if (empty($form_state['values']['log']->id) && empty($form_state['log_done_warning'])) {
203
204
205
206
207
208
    $timestamp = strtotime($form_state['values']['timestamp']);
    if (!empty($form_state['values']['done']) && $timestamp > REQUEST_TIME) {
      form_set_error('done', t('You marked this log item as "done", but it has a date in the future. If you are OK with this fact, click save again.'));
      $form_state['log_done_warning'] = TRUE;
    }
  }
209
210
211
212
}

/**
 * Log submit handler.
m.stenta's avatar
m.stenta committed
213
214
215
216
217
 *
 * @param array $form
 *   The form array.
 * @param array $form_state
 *   The form state array.
218
 */
m.stenta's avatar
m.stenta committed
219
function log_form_submit(array $form, array &$form_state) {
220
221

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

224
225
226
227
  // Convert the date to a timestamp.
  $form_state['values']['timestamp'] = strtotime($form_state['values']['timestamp']);

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

230
  // Save the log.
231
232
  log_save($log);

233
  // Redirect to the log.
234
235
236
  $log_uri = entity_uri('log', $log);
  $form_state['redirect'] = $log_uri['path'];

237
  // Print a message.
238
  drupal_set_message(t('%title saved.', array('%title' => entity_label('log', $log))));
239
240
241
242
}

/**
 * Delete confirmation form.
m.stenta's avatar
m.stenta committed
243
244
245
246
247
248
249
250
251
252
 *
 * @param array $form
 *   The form array.
 * @param array $form_state
 *   The form state array.
 * @param Log $log
 *   The log entity to delete.
 *
 * @return array
 *   Returns a form array.
253
 */
m.stenta's avatar
m.stenta committed
254
function log_delete_form(array $form, array &$form_state, Log $log) {
255
256
257
258
259
  $form['log'] = array(
    '#type' => 'value',
    '#value' => $log,
  );
  // Always provide entity id in the same form key as in the entity edit form.
m.stenta's avatar
m.stenta committed
260
261
262
  $form['log_type_id'] = array(
    '#type' => 'value',
    '#value' => entity_id('log', $log));
263
264
  $log_uri = entity_uri('log', $log);
  return confirm_form($form,
265
    t('Are you sure you want to delete %title?', array('%title' => entity_label('log', $log))),
266
267
268
269
270
271
272
273
274
    $log_uri['path'],
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

/**
 * Delete form submit handler.
m.stenta's avatar
m.stenta committed
275
276
277
278
279
 *
 * @param array $form
 *   The form array.
 * @param array $form_state
 *   The form state array.
280
 */
m.stenta's avatar
m.stenta committed
281
function log_delete_form_submit(array $form, array &$form_state) {
282
283
284
  $log = $form_state['values']['log'];
  log_delete($log);

285
  drupal_set_message(t('%title was deleted.', array('%title' => entity_label('log', $log))));
286
287

  $form_state['redirect'] = '<front>';
m.stenta's avatar
m.stenta committed
288
}