Commit ac3b0eeb authored by m.stenta's avatar m.stenta
Browse files

Initial commit: basic log entity type.

parents
<?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';
}
<?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;
}
}
name = "Log"
description = "A general purpose record keeping system."
core = 7.x
package = "Log"
dependencies[] = entity
files[] = log.class.inc
<?php
/**
* @file
* Log install
*/
/**
* Implements hook_schema().
*/
function log_schema() {
$schema['log'] = array(
'description' => 'Logs',
'fields' => array(
'lid' => array(
'description' => 'Log ID',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'timestamp' => array(
'description' => 'Timestamp of the log',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'name' => array(
'description' => 'Log name',
'type' => 'varchar',
'length' => '255',
'not null' => FALSE,
),
'type' => array(
'description' => 'Log type',
'type' => 'varchar',
'length' => '255',
'not null' => FALSE,
),
'uid' => array(
'description' => 'The log owner',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('lid'),
'indexes' => array(
'timestamp' => array('timestamp'),
'name' => array('name'),
'type' => array('type'),
'uid' => array('uid'),
),
);
$schema['log_type'] = array(
'description' => 'Stores information about all defined log types.',
'fields' => array(
'id' => array(
'description' => 'Primary Key: Unique log type ID.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'type' => array(
'description' => 'The machine-readable name of this log type.',
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
),
'label' => array(
'description' => 'The human-readable name of this log type.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
) + entity_exportable_schema_fields(),
'primary key' => array('id'),
'unique keys' => array(
'type' => array('type'),
),
);
return $schema;
}
<?php
/**
* @file
* Log
* A general purpose record keeping system.
*/
/****************************************************************
* Drupal hooks
* **************************************************************/
/**
* Implements hook_permission().
*/
function log_permission() {
return array(
'administer log module' => array(
'title' => t('Administer log module'),
'description' => t('Gives full access to everything in the log module.'),
'restrict access' => TRUE,
),
'administer log types' => array(
'title' => t('Administer log '),
'restrict access' => TRUE,
),
'create log entities' => array(
'title' => t('Create log entities'),
),
'view own log entities' => array(
'title' => t('View own log entities'),
),
'edit own log entities' => array(
'title' => t('Edit own log entities'),
),
'delete own log entities' => array(
'title' => t('Delete own log entities'),
),
'view any log entities' => array(
'title' => t('View any log entities'),
'restrict access' => TRUE,
),
'edit any log entities' => array(
'title' => t('Edit any log entities'),
'restrict access' => TRUE,
),
'delete any log entities' => array(
'title' => t('Delete any log entities'),
'restrict access' => TRUE,
),
);
}
/**
* Implements hook_menu().
*/
function log_menu() {
$items = array();
$items['log/add'] = array(
'title' => 'Add log',
'page callback' => 'log_add_types_page',
'access callback' => 'log_access',
'access arguments' => array('create'),
'file' => 'log.admin.inc',
);
$log_uri = 'log/%log';
$log_uri_argument_position = 1;
$items[$log_uri] = array(
'title callback' => 'entity_label',
'title arguments' => array('log', $log_uri_argument_position),
'page callback' => 'log_view',
'page arguments' => array($log_uri_argument_position),
'access callback' => 'log_access',
'access arguments' => array('view', $log_uri_argument_position),
'file' => 'log.pages.inc',
);
$items[$log_uri . '/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[$log_uri . '/delete'] = array(
'title' => 'Delete log',
'title callback' => 'log_label',
'title arguments' => array($log_uri_argument_position),
'page callback' => 'drupal_get_form',
'page arguments' => array('log_delete_form', $log_uri_argument_position),
'access callback' => 'log_access',
'access arguments' => array('edit', $log_uri_argument_position),
'file' => 'log.admin.inc',
);
$items[$log_uri . '/edit'] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array('log_form', $log_uri_argument_position),
'access callback' => 'log_access',
'access arguments' => array('edit', $log_uri_argument_position),
'file' => 'log.admin.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
foreach (log_types() as $type => $info) {
$items['log/add/' . $type] = array(
'title' => 'Add log',
'page callback' => 'log_add',
'page arguments' => array(2),
'access callback' => 'log_access',
'access arguments' => array('create'),
'file' => 'log.admin.inc',
);
}
$items['admin/structure/log-types/%log_type/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array('log_type_form_delete_confirm', 4),
'access arguments' => array('administer log types'),
'weight' => 1,
'type' => MENU_NORMAL_ITEM,
'file' => 'log.admin.inc',
);
return $items;
}
/**
* Implements hook_entity_info().
*/
function log_entity_info() {
$return = array(
'log' => array(
'label' => t('Log'),
'entity class' => 'Log',
'controller class' => 'LogController',
'base table' => 'log',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'lid',
'bundle' => 'type',
),
'bundle keys' => array(
'bundle' => 'type',
),
'bundles' => array(),
'load hook' => 'log_load',
'view modes' => array(
'full' => array(
'label' => t('Default'),
'custom settings' => FALSE,
),
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'module' => 'log',
'access callback' => 'log_access',
),
);
$return['log_type'] = array(
'label' => t('Log type'),
'entity class' => 'LogType',
'controller class' => 'LogTypeController',
'base table' => 'log_type',
'fieldable' => FALSE,
'bundle of' => 'log',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'type',
'label' => 'label',
),
'module' => 'log',
// Enable the entity API's admin UI.
'admin ui' => array(
'path' => 'admin/structure/log-types',
'file' => 'log.admin.inc',
'controller class' => 'LogTypeUIController',
),
'access callback' => 'log_type_access',
);
return $return;
}
/**
* Implements hook_entity_info_alter().
*/
function log_entity_info_alter(&$entity_info) {
foreach (log_types() as $type => $info) {
$entity_info['log']['bundles'][$type] = array(
'label' => $info->label,
'admin' => array(
'path' => 'admin/structure/log-types/manage/%log_type',
'real path' => 'admin/structure/log-types/manage/' . $type,
'bundle argument' => 4