rendered paste body<?php/** * Implements hook_permission() */function mikrotimeui_permission() { return array( 'access mikrotime ui' => array( 'title' => t('Access Mikrotime UI'), 'description' => t('Access Mikrotime UI'), ), );} /** * Implements hook_menu() */function mikrotimeui_menu() { $items = array(); $items['ui'] = array( 'page callback' => 'mikrotimeui_lists', 'access arguments' => array('access mikrotime ui'), 'type' => MENU_NORMAL_ITEM, 'title' => 'Mikrotime Data UI', ); $items['ui/%'] = array( 'page callback' => 'mikrotimeui_list', 'page arguments' => array(1), 'access arguments' => array('access mikrotime ui'), 'type' => MENU_CALLBACK, ); $items['ui/%/add'] = array( 'page callback' => 'drupal_get_form', 'page arguments' => array('mikrotimeui_add', 1), 'access arguments' => array('access mikrotime ui'), 'type' => MENU_CALLBACK, ); $items['ui/%/delete/%'] = array( 'page callback' => 'drupal_get_form', 'page arguments' => array('mikrotimeui_delete', 1, 3), 'access arguments' => array('access mikrotime ui'), 'type' => MENU_CALLBACK, ); return $items;}/** * Implements hook_init() */function mikrotimeui_init() { if (module_load_include('module', 'mikrotimeapi') === FALSE) { drupal_set_message('Could not access the Mikrotime API.', 'error'); }}/** * Returns the schema defined by the Drupal Schema API * Avoids losing the 'description' attributes */function _mikrotimeui_get_schema() { $schema = &drupal_static(__FUNCTION__); if (!$schema) { module_load_include('install', 'mikrotimeapi'); $schema = module_invoke('mikrotimeapi', 'schema'); } return $schema;}/** * Lists all types of entity available */function mikrotimeui_lists() { // Get metadata $schema = _mikrotimeui_get_schema(); // Define possible operations $operations = array( 'List All' => array( 'url' => '', 'permissions' => 'read mikrotime api', ), 'Add New' => array( 'url' => 'add', 'permissions' => 'write mikrotime api', ), 'Delete All' => array( 'url' => 'delete', 'permissions' => 'write mikrotime api', ), ); // Remove operations that the current user cannot perform foreach ($operations as $name => $op) { if (!user_access($op['permissions'])) { unset($operations[$name]); } } // Define a table $table = array( 'header' => array( 'Entity Type', 'Entry Count', array( 'data' => 'Operations', 'colspan' => count($operations), ), ), 'rows' => array(), ); // Build table data foreach ($schema as $eid => $entity) { $row = array( $entity['description'], db_query('SELECT COUNT(*) FROM {'.$eid.'}')->fetchField(), ); foreach ($operations as $text => $op) { $row[] = l($text, arg(0) . '/' . substr($eid, 3) . '/' . $op['url']); } $table['rows'][] = $row; } return theme('table', $table);}/** * Lists all of a specific entity type * @param $type The type of entity to list */function mikrotimeui_list($type) { // Get data and metadata $data = mikrotimeapi_db_get($type); $schema = _mikrotimeui_get_schema(); if (isset($schema["mt_$type"])) { $schema = $schema["mt_$type"]; } else { $schema = array( 'description' => ucwords($type), 'fields' => array(), ); } // Define table $table = array( 'header' => array(), 'rows' => array(), 'empty' => t('Could not find any entries of type %type', array('%type' => $schema['description'])), 'sticky' => TRUE, ); // Get headers from schema foreach ($schema['fields'] as $fid => $field) { $table['header'][] = $field['description']; } $operations = array( 'Delete' => array( 'url' => 'delete', 'permission' => 'write mikrotime data', ), ); foreach ($operations as $text => $op) { if (!user_access($op['permission'])) { unset($operations[$text]); } } if (count($operations)) { $table['header'][] = array( 'data' => t('Operations'), 'colspan' => count($operations), ); } // Get data from API foreach ($data as $id => $entry) { $table['rows'][] = _mikrotimeui_list_entry($entry, $schema, $operations); } return theme('table', $table);}/** * Helper function for mirkotimeui_list * Formats rows of data based on schema definition * @param $entry A database object to format * @param $metadata The schema definition of the entity */function _mikrotimeui_list_entry($entry, $metadata, $operations = array()) { $row = array(); foreach ($metadata['fields'] as $fid => $field) { // Get appropriately formatted data $celldata = _mikrotimeui_list_entry_field($entry->$fid, $field); // Generate links to other entities if (isset($metadata['foreign keys'][$fid])) { $fk = $metadata['foreign keys'][$fid]; $text = db_select($fk['table'], 'fk') ->fields('fk') ->condition($fk['column'], $entry->{$fk['column']}) ->execute() ->fetchObject(); $url = arg(0) . '/' . substr($fk['table'], 3) . '/list'; $celldata = l(_mikrotimeui_fk_name($text, $fk['table']), $url); } $row[] = array( 'data' => $celldata, 'class' => "mikrotime-$fid", ); } // Generate links for operations $pk = $metadata['primary key'][0]; foreach ($operations as $text => $op) { $row[] = l($text, arg(0) . '/' . arg(1) . '/' . $op['url'] . '/' . $entry->$pk); } return $row;}/** * Helper function for _mikrotimeui_list_entry * Formats a single field of a database entity * @param $data The field data * @param $metadata The schema definition for the field */function _mikrotimeui_list_entry_field($data, $metadata) { if (is_null($data)) { return '<em class="null-entry">NULL</em>'; } switch ($metadata['type']) { case 'int': // Handle dates if (isset($metadata['size']) && $metadata['size'] == 'normal') { return format_date($data); } default: return $data; }}/** * Validation function for Drupal forms * Throws a form error if the user does not have write permission on the mtAPI */function mikrotimeui_write_permission($form, &$form_state) { if (!user_access('write mikrotime data')) { form_set_error(t('You do not have permission to write to the Mikrotime API.')); }}/** * Form for adding a new entity * @param $form * @param &$form_state * @param &$type The type of entity we are creating */function mikrotimeui_add($form, &$form_state, $type) { // Get metadata $schema = _mikrotimeui_get_schema(); $schema = $schema["mt_$type"]; // Set page title drupal_set_title(t('Add New !type', array('!type' => $schema['description']))); // Require write permissions $form['#validate'] = array('mikrotimeui_write_permission'); // Wrapper fieldset $form[$type] = array( '#type' => 'fieldset', '#title' => "{$schema['description']} Details", '#tree' => TRUE, ); // Create a form field for each field defined in the schema foreach ($schema['fields'] as $fid => $field) { // Don't create a field for primary keys - these will be automatically generated if (isset($schema['primary key']) && in_array($fid, $schema['primary key'])) { continue; } // Set up basic parameters $form[$type][$fid] = array( '#title' => $field['description'], '#required' => $field['type'] == 'serial' || (isset($field['not null']) && $field['not null']) ); // Create a dropdown for foreign key fields if (isset($schema['foreign keys'][$fid])) { $fk = $schema['foreign keys'][$fid]; $foreign_data = db_select($fk['table'], 'ft') ->fields('ft', array()) ->execute() ->fetchAllAssoc($fk['column']); $form[$type][$fid]['#type'] = 'select'; $form[$type][$fid]['#required'] = TRUE; $form[$type][$fid]['#options'] = array(); foreach ($foreign_data as $fkk => $fkv) { $form[$type][$fid]['#options'][$fkk] = _mikrotimeui_fk_name($fkv, $fk['table']); } continue; } // Select what type of field to use switch ($field['type']) { case 'text': if (isset($field['size']) && $field['size'] == 'big') { $form[$type][$fid]['#type'] = 'textarea'; break; } else { $form[$type][$fid]['#type'] = 'textfield'; break; } case 'int': if (isset($field['size']) && $field['size'] == 'normal') { $form[$type][$fid]['#type'] = 'date'; break; } default: $form[$type][$fid]['#type'] = 'textfield'; } } // Submit button $form['submit'] = array( '#type' => 'submit', '#value' => t('Add New !type', array('!type' => $schema['description'])), ); return $form;}/** * Submit handler for mikrotimeui_add * @param $form * @param &$form_state */function mikrotimeui_add_submit($form, &$form_state) { // Pre-process data foreach ($form_state['values'][arg(1)] as $key => &$value) { if ($form[arg(1)][$key]['#type'] == 'date') { $value = strtotime("{$value['day']}-{$value['month']}-{$value['year']}"); } } // Build a database insertion try { $query = db_insert('mt_' . arg(1)) ->fields($form_state['values'][arg(1)]) ->execute(); } catch(Exception $e) { drupal_set_message('Something went wrong! <pre>' . var_export($e, true) . '</pre>', 'error'); return; } // Display a message to the user drupal_set_message('Successfully added new entry to database.', 'success'); drupal_goto(arg(0) . '/' . arg(1));}function mikrotimeui_delete($form, &$form_state, $type, $id) { $form[$type] = array( '#type' => 'fieldset', '#title' => 'Confirm Deletion', '#validate' => array('mikrotimeui_write_permission'), 'text' => array( '#type' => 'markup', '#markup' => '<p>' . t('Are you sure you wish to delete this?') . '</p>', ), 'submit' => array( '#type' => 'submit', '#value' => 'Confirm Delete', '#suffix' => l('Cancel', arg(0) . '/' . arg(1)), ), ); return $form;}function mikrotimeui_delete_submit($form, &$form_state) { // Find out primary key $schema = _mikrotimeui_get_schema(); $pk = $schema['mt_'.arg(1)]['primary key'][0]; // Build delete query try { $query = db_delete('mt_' . arg(1)) ->condition($pk, arg(3)) ->execute(); } catch(Exception $e) { drupal_set_message('Something went wrong!', 'error'); } drupal_set_message('Successfully deleted entry.', 'success'); drupal_goto(arg(0) . '/' . arg(1));}/** * Renders a friendly name for an entity * @param $entity The entity to display * @param $type The type of entity */function _mikrotimeui_fk_name($entity, $type) { switch ($type) { case 'mt_run': return $entity->ruid; case 'mt_rider': return "{$entity->firstname} {$entity->lastname} ({$entity->sponsor})"; default: return $entity->name; }}