123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- <?php
- include('tripal_bulk_loader.loader.inc');
- include('tripal_bulk_loader.info_hooks.inc');
- /**
- * tripal_bulk_loader_init
- */
- function tripal_bulk_loader_init(){
- // Add javascript and style sheet
- drupal_add_css(drupal_get_path('theme', 'tripal').'/css/tripal_bulk_loader.css');
- drupal_add_js(drupal_get_path('theme', 'tripal').'/js/tripal_bulk_loader.js');
- }
- /**
- * tripal_bulk_loader_menu
- */
- function tripal_bulk_loader_menu() {
- $items = array();
- // Show all loaders
- $items['/tripal_bulk_loaders'] = array(
- 'title' => 'Tripal Bulk Loaders',
- 'description' => 'Tripal bulk loaders for loading tab-delimited file into chado database',
- 'page callback' => 'tripal_bulk_loader_list',
- 'access arguments' => array('access tripal_bulk_loader'),
- 'type' => MENU_NORMAL_ITEM,
- );
- // Admin page to create the template
- $items['admin/tripal/tripal_bulk_loader_template'] = array(
- 'title' => 'Bulk Loader Template',
- 'description' => 'Templates for loading tab-delimited data',
- 'page callback' => 'tripal_bulk_loader_admin_template',
- 'access arguments' => array('administer site configuration'),
- 'type' => MENU_NORMAL_ITEM,
- 'file' => 'tripal_bulk_loader.admin.inc',
- );
- // Create/Edit Template -------
- $items['admin/tripal/tripal_bulk_loader_template/create'] = array(
- 'title' => 'Create Bulk Loader Template',
- 'description' => 'Create loader template for loading tab-delimited data',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_bulk_loader_modify_template_base_form', 'create'),
- 'access arguments' => array('administer site configuration'),
- 'type' => MENU_NORMAL_ITEM,
- 'file' => 'tripal_bulk_loader.admin.inc',
- );
- $items['admin/tripal/tripal_bulk_loader_template/edit'] = array(
- 'title' => 'Edit Bulk Loader Template',
- 'description' => 'Edit loader template for loading tab-delimited data',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_bulk_loader_modify_template_base_form', 'edit'),
- 'access arguments' => array('administer site configuration'),
- 'type' => MENU_NORMAL_ITEM,
- 'file' => 'tripal_bulk_loader.admin.inc',
- );
- $items['admin/tripal/tripal_bulk_loader_template/edit_record'] = array(
- 'title' => 'Edit Template Record',
- 'description' => 'Edit a record in an existing tripal bulk loader template.',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_bulk_loader_edit_template_record_form'),
- 'access arguments' => array('administer site configuration'),
- 'type' => MENU_CALLBACK,
- 'file' => 'tripal_bulk_loader.admin.inc',
- );
- $items['admin/tripal/tripal_bulk_loader_template/add_field'] = array(
- 'title' => 'Add Template Field',
- 'description' => 'Add a template field to an existing tripal bulk loader template.',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_bulk_loader_add_template_field_form'),
- 'access arguments' => array('administer site configuration'),
- 'type' => MENU_CALLBACK,
- 'file' => 'tripal_bulk_loader.admin.inc',
- );
- $items['admin/tripal/tripal_bulk_loader_template/edit_field'] = array(
- 'title' => 'Edit Template Field',
- 'description' => 'Edit an existing field from a tripal bulk loader template.',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_bulk_loader_edit_template_field_form'),
- 'access arguments' => array('administer site configuration'),
- 'type' => MENU_CALLBACK,
- 'file' => 'tripal_bulk_loader.admin.inc',
- );
- // Delete Template -----
- $items['admin/tripal/tripal_bulk_loader_template/delete'] = array(
- 'title' => 'Delete Bulk Loader Template',
- 'description' => 'Delete bulk loader template',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_bulk_loader_delete_template_base_form'),
- 'access arguments' => array('administer site configuration'),
- 'type' => MENU_NORMAL_ITEM,
- 'file' => 'tripal_bulk_loader.admin.inc',
- );
- // Import/Export ---------
- $items['admin/tripal/tripal_bulk_loader_template/import'] = array(
- 'title' => 'Import Bulk Loader Template',
- 'description' => 'Import Loaders',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_bulk_loader_import_export_template_form', 'import'),
- 'access arguments' => array('administer site configuration'),
- 'type' => MENU_NORMAL_ITEM,
- 'file' => 'tripal_bulk_loader.admin.inc',
- );
- $items['admin/tripal/tripal_bulk_loader_template/export'] = array(
- 'title' => 'Export Bulk Loader Template',
- 'description' => 'Export Loaders',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_bulk_loader_import_export_template_form', 'export'),
- 'access arguments' => array('administer site configuration'),
- 'type' => MENU_NORMAL_ITEM,
- 'file' => 'tripal_bulk_loader.admin.inc',
- );
- // AHAH ---------
- $items['admin/tripal/tripal_bulk_loader_template/add_field_ahah'] = array(
- 'page callback' => 'tripal_bulk_loader_add_field_ahah',
- 'access arguments' => array('administer site configuration'),
- 'type' => MENU_CALLBACK,
- 'file' => 'tripal_bulk_loader.admin.inc',
- );
- $items['admin/tripal/tripal_bulk_loader_template/edit_field_ahah'] = array(
- 'page callback' => 'tripal_bulk_loader_edit_field_ahah',
- 'access arguments' => array('administer site configuration'),
- 'type' => MENU_CALLBACK,
- 'file' => 'tripal_bulk_loader.admin.inc',
- );
-
- return $items;
- }
- /**
- * tripal_bulk_loader_list
- */
- function tripal_bulk_loader_list () {
- return "Loaders";
- }
- /**
- * tripal_bulk_loader_access
- */
- function tripal_bulk_loader_access($op, $node, $account){
- if ($op == 'create') {
- return user_access('create tripal_bulk_loader', $account);
- }
- if ($op == 'update') {
- if (user_access('edit tripal_bulk_loader', $account)) {
- return TRUE;
- }
- }
- if ($op == 'delete') {
- if (user_access('delete tripal_bulk_loader', $account)) {
- return TRUE;
- }
- }
- if ($op == 'view') {
- if (user_access('access tripal_bulk_loader', $account)) {
- return TRUE;
- }
- }
- return FALSE;
- }
- /**
- * tripal_bulk_loader_perm
- */
- function tripal_bulk_loader_perm(){
- return array(
- 'access tripal_bulk_loader',
- 'create tripal_bulk_loader',
- 'delete tripal_bulk_loader',
- 'edit tripal_bulk_loader',
- );
- }
- /**
- * tripal_bulk_loader_node_info
- */
- function tripal_bulk_loader_node_info() {
- $nodes = array();
- $nodes['tripal_bulk_loader'] = array(
- 'name' => t('Bulk Loading Job'),
- 'module' => 'tripal_bulk_loader',
- 'description' => t('A bulk loader for inserting tab-delimited data into chado database'),
- 'has_title' => TRUE,
- 'has_body' => FALSE,
- 'locked' => TRUE
- );
- return $nodes;
- }
- /**
- * tripal_bulk_loader_form
- */
- function tripal_bulk_loader_form ($node){
- $form = array();
- $sql = "SELECT * FROM {tripal_bulk_loader_template}";
- $results = db_query($sql);
- $templates = array ();
- while ($template = db_fetch_object ($results)) {
- $templates [$template->template_id] = $template->name;
- }
-
- if (!$templates) {
- $form['label'] = array(
- '#type' => 'item',
- '#description' => t("Loader template needs to be created before any bulk loader can be added. Go to 'Tripal Management > Bulk Loader Template' to create the template."),
- '#weight' => -10,
- );
-
- return $form;
- }
-
- $form['loader_name'] = array(
- '#type' => 'textfield',
- '#title' => t('Loading Job Name'),
- '#weight' => -10,
- '#required' => TRUE,
- '#default_value' => $node->loader_name
- );
-
- $form['template_id'] = array(
- '#type' => 'select',
- '#title' => t('Template'),
- '#description' => t('Please specify a template for this loader'),
- '#options' => $templates,
- '#weight' => -9,
- '#required' => TRUE,
- '#default_value' => $node->template_id
- );
-
- $form['file']= array(
- '#type' => 'textfield',
- '#title' => t('Data File'),
- '#description' => t('Please specify the data file to be loaded.'),
- '#weight' => -8,
- '#default_value' => $node->file
- );
-
- $form['has_header'] = array(
- '#type' => 'radios',
- '#title' => 'File has a Header',
- '#options' => array( 't' => 'Yes', 'f' => 'No'),
- '#weight' => -7,
- '#default_value' => $node->file_has_header,
- );
- return $form;
- }
- /**
- * tripal_bulk_loader_theme
- */
- function tripal_bulk_loader_theme() {
- return array(
- 'tripal_bulk_loader_node_form' => array(
- 'arguments' => array('form' => NULL),
- ),
- 'tripal_bulk_loader_template' => array(
- 'arguments'=> array('template_id' => NULL),
- 'template' => 'tripal_bulk_loader_template'
- ),
- 'tripal_bulk_loader_modify_template_base_form' => array(
- 'arguments' => array('form' => NULL),
- 'template' => 'tripal_bulk_loader_modify_template_base_form',
- ),
- );
- }
- /**
- * theme_tripal_bulk_loader_node_form
- */
- function theme_tripal_bulk_loader_node_form($form) {
- // Do not show [Save] and [Preview] buttons if loader template is not available
- if($form['label']['#type'] == "item") {
- unset($form['buttons']);
- }
- return drupal_render($form);
- }
- /**
- * tripal_bulk_loader_load
- */
- function tripal_bulk_loader_load($node){
- $sql = "SELECT * FROM {tripal_bulk_loader} WHERE nid = %d";
- $node = db_fetch_object(db_query($sql, $node->nid));
- $node->title = 'Bulk Loading Job: '.$node->loader_name;
-
- // Add the loader template
- $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
- $results = db_fetch_object(db_query($sql, $node->template_id));
- $template = unserialize($results->template_array);
- $node->template = $results;
- $node->template->template_array = $template;
- return $node;
- }
- /**
- * tripal_bulk_loader_insert
- */
- function tripal_bulk_loader_insert ($node) {
-
- $sql = "INSERT INTO {tripal_bulk_loader} (nid, loader_name, template_id, file, file_has_header) VALUES (%d, '%s', %d, '%s', %d)";
- db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->has_header);
- $node->title =$node->loader_name;
- drupal_write_record('node',$node,'nid');
- drupal_write_record('node_revision',$node,'nid');
- }
- /**
- * tripal_bulk_loader_delete
- */
- function tripal_bulk_loader_delete ($node) {
- $sql = "DELETE FROM {tripal_bulk_loader} WHERE nid = %d";
- db_query($sql, $node->nid);
- }
- /**
- * tripal_bulk_loader_update
- */
- function tripal_bulk_loader_update ($node) {
- $sql = "UPDATE {tripal_bulk_loader} SET nid = %d, loader_name = '%s', template_id = %d, file = '%s', file_has_header = '%s' WHERE nid = %d";
- db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->has_header, $node->nid);
- // Add a job if the user want to load the data
- global $user;
- if($node->job) {
- $job_args[0] =$node->loader_name;
- $job_args[1] = $node->template_id;
- $job_args[2] = $node->file;
- if (is_readable($node->file)) {
- $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
- tripal_add_job("Bulk Load: $fname",'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
- } else {
- drupal_set_message("Can not open $node->file. Job not scheduled.");
- }
- }
- }
- /**
- * Preprocessor function for the tripal_bulk_loader template
- */
- function tripal_bulk_loader_preprocess_tripal_bulk_loader_template (&$variables) {
- $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
- $template = db_fetch_object(db_query($sql, $variables['template_id']));
- $template->template_array = unserialize($template->template_array);
- $variables['template'] = $template;
-
- }
- /**
- * Implements hook_job_describe_args()
- * Specifically to make viewing past tripal jobs more readable for jobs registered by this module
- *
- * @params $callback
- * The callback passed into tripal_add_job()
- * @param $args
- * The arguements passed into tripal_add_job()
- * @return
- * An array where keys are the human readable headers describing each arguement
- * and the value is the aguement passed in after formatting
- */
- function tripal_bulk_loader_job_describe_args($callback,$args){
- $new_args = array();
- if($callback == 'tripal_bulk_loader_load_data'){
- //1st arg is the nid for a bulk loader node
- $node = node_load($args[0]);
- $new_args['Bulk Loading Job'] = l($node->title, 'node/'.$args[0]);
- return $new_args;
- }
-
- }
|