log.pages.inc 7.31 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

m.stenta's avatar
m.stenta committed
104
  // Store the log entity in the form for use later.
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  $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
123
  // Timestamp.
124
125
126
127
128
129
130
131
132
133
134
  $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,
  );

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

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

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

201
202
  // 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
203
  if (empty($form_state['values']['log']->id) && empty($form_state['log_done_warning'])) {
204
205
206
207
208
209
    $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;
    }
  }
210
211
212
213
}

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

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

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

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

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

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

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

/**
 * Delete confirmation form.
m.stenta's avatar
m.stenta committed
244
245
246
247
248
249
250
251
252
253
 *
 * @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.
254
 */
m.stenta's avatar
m.stenta committed
255
function log_delete_form(array $form, array &$form_state, Log $log) {
256
257
258
259
260
  $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
261
262
263
  $form['log_type_id'] = array(
    '#type' => 'value',
    '#value' => entity_id('log', $log));
264
265
  $log_uri = entity_uri('log', $log);
  return confirm_form($form,
266
    t('Are you sure you want to delete %title?', array('%title' => entity_label('log', $log))),
267
268
269
270
271
272
273
274
275
    $log_uri['path'],
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

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

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

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