Skip to content
Permalink
Browse files

Initial commit: basic log entity type.

  • Loading branch information...
mstenta committed Feb 4, 2014
0 parents commit ac3b0eebcd65515fe97833055d215c89d0450c0a
Showing with 816 additions and 0 deletions.
  1. +259 −0 log.admin.inc
  2. +83 −0 log.class.inc
  3. +6 −0 log.info
  4. +84 −0 log.install
  5. +370 −0 log.module
  6. +14 −0 log.pages.inc
@@ -0,0 +1,259 @@
<?php
/**
* @file
* Log admin pages
*/
/**
* 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) {
$items[] = l(entity_label('log_type', $log_type), 'log/add/' . $log_type_key);
}
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('Create @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,
);
$form['name'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#title' => t('Name'),
'#default_value' => $log->name,
'#weight' => -100,
);
$form['timestamp'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#title' => t('Timestamp'),
'#default_value' => $log->timestamp,
'#weight' => -99,
);
$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)) {
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#submit' => array('log_form_submit_delete'),
);
}
return $form;
}
/**
* Log validate handler.
*/
function log_form_validate($form, &$form_state) {
}
/**
* Log submit handler.
*/
function log_form_submit($form, &$form_state) {
$log = $form_state['values']['log'];
entity_form_submit_build_entity('log', $log, $form, $form_state);
log_save($log);
$log_uri = entity_uri('log', $log);
$form_state['redirect'] = $log_uri['path'];
drupal_set_message(t('Log %title saved.', array('%title' => entity_label('log', $log))));
}
function log_form_submit_delete($form, &$form_state) {
$log = $form_state['values']['log'];
$log_uri = entity_uri('log', $log);
$form_state['redirect'] = $log_uri['path'] . '/delete';
}
/**
* 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,
t('Are you sure you want to delete log %title?', array('%title' => entity_label('log', $log))),
$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);
drupal_set_message(t('Log %title deleted.', array('%title' => entity_label('log', $log))));
$form_state['redirect'] = '<front>';
}
/**
* Generates the log type editing form.
*/
function log_type_form($form, &$form_state, $log_type, $op = 'edit') {
if ($op == 'clone') {
$log_type->label .= ' (cloned)';
$log_type->type = '';
}
$form['label'] = array(
'#title' => t('Label'),
'#type' => 'textfield',
'#default_value' => $log_type->label,
'#description' => t('The human-readable name of this log type.'),
'#required' => TRUE,
'#size' => 30,
);
// Machine-readable type name.
$form['type'] = array(
'#type' => 'machine_name',
'#default_value' => isset($log_type->type) ? $log_type->type : '',
'#maxlength' => 32,
'#disabled' => $log_type->isLocked() && $op != 'clone',
'#machine_name' => array(
'exists' => 'log_types',
'source' => array('label'),
),
'#description' => t('A unique machine-readable name for this log type. It must only contain lowercase letters, numbers, and underscores.'),
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save log type'),
'#weight' => 40,
);
if (!$log_type->isLocked() && $op != 'add' && $op != 'clone') {
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete log type'),
'#weight' => 45,
'#limit_validation_errors' => array(),
'#submit' => array('log_type_form_submit_delete')
);
}
return $form;
}
/**
* Submit handler for creating/editing log_type.
*/
function log_type_form_submit(&$form, &$form_state) {
$log_type = entity_ui_form_submit_build_entity($form, $form_state);
// Save and go back.
log_type_save($log_type);
// Redirect user back to list of log types.
$form_state['redirect'] = 'admin/structure/log-types';
}
function log_type_form_submit_delete(&$form, &$form_state) {
$form_state['redirect'] = 'admin/structure/log-types/' . $form_state['log_type']->type . '/delete';
}
/**
* Log type delete form.
*/
function log_type_form_delete_confirm($form, &$form_state, $log_type) {
$form['log_type'] = array(
'#type' => 'value',
'#value' => $log_type,
);
// 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_type' ,$log_type));
return confirm_form($form,
t('Are you sure you want to delete log type %title?', array('%title' => entity_label('log_type', $log_type))),
'log/' . entity_id('log_type' ,$log_type),
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
}
/**
* Log type delete form submit handler.
*/
function log_type_form_delete_confirm_submit($form, &$form_state) {
$log_type = $form_state['values']['log_type'];
log_type_delete($log_type);
watchdog('log_type', '@type: deleted %title.', array('@type' => $log_type->type, '%title' => $log_type->label));
drupal_set_message(t('@type %title has been deleted.', array('@type' => $log_type->type, '%title' => $log_type->label)));
$form_state['redirect'] = 'admin/structure/log-types';
}
@@ -0,0 +1,83 @@
<?php
/**
* @file
* Log classes.
*/
/**
* Log class.
*/
class Log extends Entity {
protected function defaultLabel() {
$log_type = log_type_load($this->type);
return $log_type->label . ': ' . $this->name;
}
protected function defaultUri() {
return array('path' => 'log/' . $this->identifier());
}
}
/**
* Log controller class.
*/
class LogController extends EntityAPIController {
public function create(array $values = array()) {
global $user;
$values += array(
'lid' => NULL,
'timestamp' => REQUEST_TIME,
'name' => '',
'type' => '',
'uid' => $user->uid,
);
return parent::create($values);
}
}
/**
* Log Type class.
*/
class LogType extends Entity {
public $type;
public $label;
public $weight = 0;
public function __construct($values = array()) {
parent::__construct($values, 'log_type');
}
function isLocked() {
return isset($this->status) && empty($this->is_new) && (($this->status & ENTITY_IN_CODE) || ($this->status & ENTITY_FIXED));
}
}
/**
* Log type controller class.
*/
class LogTypeController extends EntityAPIControllerExportable {
public function create(array $values = array()) {
$values += array(
'id' => NULL,
'type' => '',
'label' => '',
);
return parent::create($values);
}
}
/**
* UI controller for Log Type.
*/
class LogTypeUIController extends EntityDefaultUIController {
/**
* Overrides hook_menu() defaults.
*/
public function hook_menu() {
$items = parent::hook_menu();
$items[$this->path]['description'] = 'Manage log types.';
return $items;
}
}
@@ -0,0 +1,6 @@
name = "Log"
description = "A general purpose record keeping system."
core = 7.x
package = "Log"
dependencies[] = entity
files[] = log.class.inc
Oops, something went wrong.

0 comments on commit ac3b0ee

Please sign in to comment.
You can’t perform that action at this time.