123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782 |
- <?php
- /**
- * @file
- * The functions for the Tripal bulk loader.
- *
- * @defgroup tripal_bulk_loader Tripal Bulk Loader Module
- * @ingroup tripal_modules
- */
- include('includes/tripal_bulk_loader.loader.inc');
- include('includes/tripal_bulk_loader.constants.inc');
- // Administration
- include('includes/tripal_bulk_loader.admin.inc');
- include('includes/tripal_bulk_loader.admin.templates.inc');
- // API
- include('api/tripal_bulk_loader.api.templates.inc');
- /**
- * Implements hook_init
- * Used to add stylesheets and javascript files to the header
- *
- * @ingroup tripal_bulk_loader
- */
- 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');
- }
- /**
- * Implements hook_menu
- *
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_menu() {
- $items = array();
- // Bulk Loading Job Node
- $items['node/%node/constants/%/edit'] = array(
- 'title' => 'Edit Constant Set',
- 'description' => 'Edit a group of constants associated with the current bulk loader',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_bulk_loader_edit_constant_set_form', 1, 3),
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'type' => MENU_CALLBACK,
- );
- $items['node/%node/constants/%/delete'] = array(
- 'title' => 'Delete Constant Set',
- 'description' => 'Delete a group of constants associated with the current bulk loader',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_bulk_loader_delete_constant_set_form', 1, 3),
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'type' => MENU_CALLBACK,
- );
- // Admin pages -----------------
- $items['admin/tripal/loaders/bulk'] = array(
- 'title' => 'Bulk Loader',
- 'description' => 'Templates for loading tab-delimited data',
- 'page callback' => 'tripal_bulk_loader_admin_jobs_listing',
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'type' => MENU_NORMAL_ITEM,
- );
- $items['admin/tripal/loaders/bulk/configure'] = array(
- 'title' => 'Configure',
- 'description' => 'Configuration of global options related to bulk loading jobs',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_bulk_loader_configuration_form'),
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'weight' => 8,
- 'type' => MENU_LOCAL_TASK,
- );
- $items['admin/tripal/loaders/bulk/help'] = array(
- 'title' => 'Help',
- 'description' => "A description of the Tripal Bulk Loader module including a short description of it's usage.",
- 'page callback' => 'tripal_bulk_loader_admin_manage_templates',
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'weight' => 10,
- 'type' => MENU_LOCAL_TASK,
- );
- // Enable View Callbacks
- $items['admin/tripal/loaders/bulk/views/jobs/enable'] = array(
- 'title' => 'Enable Jobs Administrative View',
- 'description' => 'Enable Jobs Administrative View',
- 'page callback' => 'tripal_views_admin_enable_view',
- 'page arguments' => array('tripal_bulk_loading_jobs','admin/tripal/loaders/bulk'),
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'type' => MENU_CALLBACK,
- );
- $items['admin/tripal/loaders/bulk/views/templates/enable'] = array(
- 'title' => 'Enable Templates Administrative View',
- 'description' => 'Enable Templates Administrative View',
- 'page callback' => 'tripal_views_admin_enable_view',
- 'page arguments' => array('tripal_bulk_loader_templates', 'admin/tripal/loaders/bulk'),
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'type' => MENU_CALLBACK,
- );
- // Create/Edit Template --------
- $items['admin/tripal/loaders/bulk/template/create'] = array(
- 'title' => 'Create 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 tripal_bulk_loader'),
- 'weight' => -8,
- 'type' => MENU_CALLBACK,
- );
- $items['admin/tripal/loaders/bulk/template/%tblid/edit'] = array(
- 'title' => 'Edit 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',5),
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'type' => MENU_CALLBACK,
- );
- /**
- $items['admin/tripal/loaders/bulk/template/edit'] = array(
- 'title' => 'Edit 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',FALSE),
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'weight' => -6,
- 'type' => MENU_CALLBACK,
- );
- */
- // Add/Edit Record -----
- $items['admin/tripal/loaders/bulk/template/%tblid/add_record'] = 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_template_field_form','create_record',5,FALSE),
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'type' => MENU_CALLBACK,
- );
- $items['admin/tripal/loaders/bulk/template/%tblid/edit_record/%tblid'] = 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',5,7),
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'type' => MENU_CALLBACK,
- );
- $items['admin/tripal/loaders/bulk/template/%tblid/delete_record/%tblid'] = array(
- 'title' => 'Delete Template Record',
- 'description' => 'Delete a record in an existing tripal bulk loader template.',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_bulk_loader_delete_template_record_form',5,7),
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'type' => MENU_CALLBACK,
- );
- $items['admin/tripal/loaders/bulk/template/%tblid/duplicate_record/%tblid'] = array(
- 'title' => 'Duplicate Template Record',
- 'description' => 'Duplicate a record in an existing tripal bulk loader template.',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_bulk_loader_duplicate_template_record_form',5,7),
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'type' => MENU_CALLBACK,
- );
- // Add/Edit Field ------
- $items['admin/tripal/loaders/bulk/template/%tblid/add_field/%tblid'] = 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_template_field_form','create',5,7),
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'type' => MENU_CALLBACK,
- );
- $items['admin/tripal/loaders/bulk/template/%tblid/edit_field/%tblid/%tblid'] = 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_template_field_form','edit',5,7,8),
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'type' => MENU_CALLBACK,
- );
- $items['admin/tripal/loaders/bulk/template/%tblid/delete_field/%tblid/%tblid'] = array(
- 'title' => 'Delete Template Field',
- 'description' => 'Delete an existing field from a tripal bulk loader template.',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_bulk_loader_delete_template_field_form',5,7,8),
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'type' => MENU_CALLBACK,
- );
- // Delete Template -----
- $items['admin/tripal/loaders/bulk/template/%tblid/delete'] = array(
- 'title' => 'Delete Template',
- 'description' => 'Delete bulk loader template',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_bulk_loader_delete_template_base_form',5),
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'weight' => -4,
- 'type' => MENU_CALLBACK,
- );
- // Import/Export ---------
- $items['admin/tripal/loaders/bulk/templates/import'] = array(
- 'title' => 'Import Template',
- 'description' => 'Import Loaders',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_bulk_loader_import_template_form'),
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'weight' => 2,
- 'type' => MENU_CALLBACK,
- );
- $items['admin/tripal/loaders/bulk/template/%tblid/export'] = array(
- 'title' => 'Export Template',
- 'description' => 'Export Loaders',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_bulk_loader_export_template_form', 5),
- 'access arguments' => array('administer tripal_bulk_loader'),
- 'weight' => 4,
- 'type' => MENU_CALLBACK,
- );
- return $items;
- }
- function tblid_to_arg($arg, $map, $index) {
- if (preg_match('/^\d+$/', $arg)) {
- return $arg;
- }
- }
- /**
- * Implements hook_views_api()
- *
- * Purpose: Essentially this hook tells drupal that there is views support for
- * for this module which then includes tripal_views.views.inc where all the
- * views integration code is
- *
- * @ingroup tripal_views
- */
- function tripal_bulk_loader_views_api() {
- return array(
- 'api' => 3.0,
- );
- }
- /**
- * Implements hook_theme
- *
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_theme() {
- return array(
- 'node__tripal_bulk_loader' => array(
- 'template' => 'node--tripal-bulk-loader',
- 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
- 'render element' => 'node',
- 'base hook' => 'node',
- ),
- 'tripal_bulk_loader_template' => array(
- 'variables' => array('template_id' => NULL),
- 'template' => 'tripal_bulk_loader_template',
- 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
- ),
- 'tripal_bulk_loader_modify_template_base_form' => array(
- 'template' => 'tripal_bulk_loader_modify_template_base_form',
- 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
- 'render element' => 'form'
- ),
- 'tripal_bulk_loader_field_regex_fieldset' => array(
- 'file' => 'theme/tripal_bulk_loader.theme.inc',
- 'function' => 'tripal_bulk_loader_field_regex_fieldset',
- 'render element' => 'element'
- ),
- 'tripal_bulk_loader_admin' => array(
- 'template' => 'tripal_bulk_loader_admin',
- 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
- ),
- );
- }
- /**
- * Implement hook_access().
- *
- * This hook allows node modules to limit access to the node types they define.
- *
- * @param $op
- * The operation to be performed
- *
- * @param $node
- * The node on which the operation is to be performed, or, if it does not yet exist, the
- * type of node to be created
- *
- * @param $account
- * A user object representing the user for whom the operation is to be performed
- *
- * @return
- * If the permission for the specified operation is not set then return FALSE. If the
- * permission is set then return NULL as this allows other modules to disable
- * access. The only exception is when the $op == 'create'. We will always
- * return TRUE if the permission is set.
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_node_access($node, $op, $account) {
- if ($op == 'create') {
- if (!user_access('create tripal_bulk_loader', $account)) {
- return NODE_ACCESS_DENY;
- }
- return NODE_ACCESS_ALLOW;
- }
- if ($op == 'update') {
- if (!user_access('edit tripal_bulk_loader', $account)) {
- return NODE_ACCESS_DENY;
- }
- return NODE_ACCESS_ALLOW;
- }
- if ($op == 'delete') {
- if (!user_access('delete tripal_bulk_loader', $account)) {
- return NODE_ACCESS_DENY;
- }
- return NODE_ACCESS_ALLOW;
- }
- if ($op == 'view') {
- if (!user_access('access tripal_bulk_loader', $account)) {
- return NODE_ACCESS_DENY;
- }
- return NODE_ACCESS_ALLOW;
- }
- return NODE_ACCESS_IGNORE;
- }
- /**
- * Implements hook_perm
- *
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_permission() {
- return array(
- 'access tripal_bulk_loader' => array(
- 'title' => t('View Tripal Bulk Loading Jobs'),
- 'description' => t('Permission to view Tripal Bulk Loader Nodes')
- ),
- 'create tripal_bulk_loader' => array(
- 'title' => t('Create Tripal Bulk Loading Jobs'),
- 'description' => t('Permission to create Tripal Bulk Loader Nodes')
- ),
- 'edit tripal_bulk_loader' => array(
- 'title' => t('Edit Tripal Bulk Loading Jobs'),
- 'description' => t('Permission to edit Tripal Bulk Loader Nodes')
- ),
- 'delete tripal_bulk_loader' => array(
- 'title' => t('Delete Tripal Bulk Loading Jobs'),
- 'description' => t('Permission to delete Tripal Bulk Loader Nodes')
- ),
- 'administer tripal_bulk_loader' => array(
- 'title' => t('Administrate Tripal Bulk Loader'),
- 'description' => t('Permission to administrate the Tripal Bulk Loader including template management.')
- ),
- );
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- // Node Functions
- //////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * Implements hook_node_info
- *
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_node_info() {
- $nodes = array();
- $nodes['tripal_bulk_loader'] = array(
- 'name' => t('Bulk Loading Job'),
- 'base' => '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;
- }
- /**
- * Implements node_form
- * Used to gather the extra details stored with a Bulk Loading Job Node
- *
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_form($node, $form_state) {
- $form = array();
- if (isset($form_state['values'])) {
- $node = $form_state['values'] + (array)$node;
- $node = (object) $node;
- }
- $results = db_select('tripal_bulk_loader_template', 't')
- ->fields('t', array('template_id','name'))
- ->execute();
- $templates = array();
- foreach ($results as $template) {
- $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'] = array(
- '#type' => 'fieldset',
- '#title' => t('Basic Details'),
- );
- $form['loader']['loader_name'] = array(
- '#type' => 'textfield',
- '#title' => t('Loading Job Name'),
- '#weight' => -10,
- '#required' => TRUE,
- '#default_value' => (isset($node->loader_name)) ? $node->loader_name : ''
- );
- $form['loader']['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' => (isset($node->template_id)) ? $node->template_id : current($templates),
- );
- $form['loader']['file']= array(
- '#type' => 'textfield',
- '#title' => t('Data File'),
- '#description' => t('Please specify the data file to be loaded. This must be a tab-delimited text file with UNIX line endings.'),
- '#weight' => -8,
- '#default_value' => (isset($node->file)) ? $node->file : '',
- '#maxlength' => 1024,
- );
- $form['loader']['has_header'] = array(
- '#type' => 'radios',
- '#title' => t('File has a Header'),
- '#options' => array( 1 => 'Yes', 2 => 'No'),
- '#weight' => -7,
- '#default_value' => (isset($node->file_has_header)) ? $node->file_has_header : 1,
- );
- $form['loader']['keep_track_inserted'] = array(
- '#type' => 'radios',
- '#title' => t('Keep track of inserted record IDs'),
- '#description' => t('This enables the ability to revert an entire loading job even if '
- .'it completed successfully. Furthermore, it displays the number of records '
- .'successfully inserted into each table.'),
- '#options' => array( 1 => 'Yes', 0 => 'No'),
- '#weight' => -7,
- '#default_value' => (isset($node->keep_track_inserted)) ? $node->keep_track_inserted : variable_get('tripal_bulk_loader_keep_track_inserted', 0),
- );
- return $form;
- }
- /**
- * Implements node_load
- *
- * D7 Changes: now loads all $nodes at once so need to add loops
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_load($nodes) {
- // Loading Job Details
- // Add fields from the tripal_bulk_loader
- $result = db_select('tripal_bulk_loader', 'tbl')
- ->fields('tbl')
- ->condition('nid',array_keys($nodes),'IN')
- ->execute();
- foreach ($result as $record) {
- $nodes[$record->nid]->loader_name = $record->loader_name;
- $nodes[$record->nid]->template_id = $record->template_id;
- $nodes[$record->nid]->file = $record->file;
- $nodes[$record->nid]->job_id = $record->job_id;
- $nodes[$record->nid]->job_status = $record->job_status;
- $nodes[$record->nid]->file_has_header = $record->file_has_header;
- $nodes[$record->nid]->keep_track_inserted = $record->keep_track_inserted;
- $nodes[$record->nid]->exposed_fields = array();
- $nodes[$record->nid]->constants = array();
- }
- // Job Details
- // Add fields from tripal_jobs
- $result = db_query('SELECT tbl.nid, tj.* FROM {tripal_jobs} tj '
- . 'LEFT JOIN {tripal_bulk_loader} tbl ON tbl.job_id=tj.job_id '
- . 'WHERE tbl.nid IN (:nids)',
- array(':nids' => array_keys($nodes))
- );
- foreach ($result as $record) {
- $nodes[$record->nid]->job = $record;
- }
- // Add the Loader Template
- // Add fields from tripal_bulk_loader_template
- $result = db_query('SELECT tbl.nid, tblt.* FROM {tripal_bulk_loader_template} tblt '
- . 'LEFT JOIN {tripal_bulk_loader} tbl ON tbl.template_id=tblt.template_id '
- . 'WHERE tbl.nid IN (:nids)',
- array(':nids' => array_keys($nodes))
- );
- foreach ($result as $dbrecord) {
- $nodes[$dbrecord->nid]->template = $dbrecord;
- $nodes[$dbrecord->nid]->template->template_array = unserialize($dbrecord->template_array);
- // Add exposed field list
- $template = $nodes[$dbrecord->nid]->template->template_array;
- $nodes[$dbrecord->nid]->exposed_fields = array();
- if ($template) {
- foreach ($template as $record_id => $record) {
- foreach ($record['fields'] as $field_id => $field) {
- if (isset($field['exposed'])) {
- if ($field['exposed']) {
- $nodes[$dbrecord->nid]->exposed_fields[] = array(
- 'record_id' => $record_id,
- 'field_id' => $field_id,
- 'title' => $field['title'],
- );
- }
- }
- }
- }
- }
- }
- // Add inserted records
- // Add fields from tripal_bulk_loader_inserted
- $result = db_query('SELECT tbli.* FROM {tripal_bulk_loader_inserted} tbli '
- . 'WHERE tbli.nid IN (:nids)',
- array(':nids' => array_keys($nodes))
- );
- foreach ($result as $record) {
- $record->num_inserted = sizeof(preg_split('/,/', $record->ids_inserted));
- $nodes[$record->nid]->inserted_records->{$record->table_inserted_into} = $record;
- }
- // Add constants
- // Add fields from tripal_bulk_loader_constants
- $result = db_query('SELECT tblc.* FROM {tripal_bulk_loader_constants} tblc '
- . 'WHERE tblc.nid IN (:nids) '
- . 'ORDER BY group_id, record_id, field_id',
- array(':nids' => array_keys($nodes))
- );
- foreach ($result as $record) {
- $nodes[$record->nid]->constants[$record->group_id][$record->record_id][$record->field_id] = array(
- 'constant_id' => $record->constant_id,
- 'group_id' => $record->group_id,
- 'chado_table' => $record->chado_table,
- 'chado_field' => $record->chado_field,
- 'record_id' => $record->record_id,
- 'field_id' => $record->field_id,
- 'value' => $record->value
- );
- }
- }
- /**
- * Implements node_insert
- * Insert the data from the node form on Create content
- *
- * D7 Changes: seems to need db_insert; not recommended to change $node
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_insert($node) {
- $node->title = $node->loader_name;
- db_insert('tripal_bulk_loader')->fields(array(
- 'nid' => $node->nid,
- 'loader_name' => $node->loader_name,
- 'template_id' => $node->template_id,
- 'file' => $node->file,
- 'file_has_header' => $node->has_header,
- 'job_status' => 'Initialized',
- 'keep_track_inserted' => $node->keep_track_inserted
- ))->execute();
- drupal_set_message(t('After reviewing the details, please Submit this Job (by clicking the "Submit Job" button below). No data will be loaded until the submitted job is reached in the queue.'));
- }
- /**
- * Implements node_delete
- * Deletes the data when the delete button on the node form is clicked
- *
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_delete($node) {
- $tables = array();
- $tables[] = 'tripal_bulk_loader';
- $tables[] = 'tripal_bulk_loader_constants';
- $tables[] = 'tripal_bulk_loader_inserted';
- foreach($tables as $table) {
- db_delete($table)
- ->condition('nid',$node->nid)
- ->execute();
- }
- }
- /**
- * Implements node_update
- * Updates the data submitted by the node form on edit
- *
- * D7 Changes: db_update is much easier
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_update($node) {
- // Update tripal_bulk_loader
- db_update('tripal_bulk_loader')->fields(array(
- 'nid' => $node->nid,
- 'loader_name' => $node->loader_name,
- 'template_id' => $node->template_id,
- 'file' => $node->file,
- 'file_has_header' => $node->has_header,
- 'keep_track_inserted' => $node->keep_track_inserted
- ))->condition('nid',$node->nid)->execute();
- // Add a job if the user want to load the data
- /**
- No job checkbox in the form
- 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(t("Can not open %file. Job not scheduled.", array('%file' => $node->file)));
- }
- }
- */
- }
- ///////////////////////////////////////////////////////////
- /**
- * Preprocessor function for the tripal_bulk_loader template
- *
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_preprocess_tripal_bulk_loader_template(&$variables) {
- $resource = db_select('tripal_bulk_loader_template','t')
- ->fields('t')
- ->condition('template_id', $variables['template_id'])
- ->execute();
- $template = $resource->fetchObject();
- $template->template_array = unserialize($template->template_array);
- $variables['template'] = $template;
- }
- /**
- * Get the progress of the current constant set from the progress file
- *
- * When transactions are used, database updates to drupal cannot be made. Thus a separate
- * method to keep track of progress was implemented: save a period to the file for each
- * record successfully inserted; each line in the file represents a processed line.
- *
- * @param $job_id
- * The id of the tripal job to check the progress of
- * @param $node
- * The tripal_bulk_loader node associated with the job
- *
- * @return
- * An array with the following keys:
- * num_lines = the number of lines in the file processed so far
- * total_lines = the total number of lines in the input file
- * percent_file = the percent the input file has been loaded
- * num_records = the number of records successfully inserted
- *
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_progess_file_get_progress($job_id, $update_progress = TRUE) {
- $filename = '/tmp/tripal_bulk_loader_progress-' . $job_id . '.out';
- if (!file_exists($filename)) {
- return (object) array();
- }
- $num_lines = trim(`wc --lines < $filename`);
- $num_records = trim(`grep -o "." $filename | wc --lines`);
- $job = db_query("SELECT j.*, b.file, b.file_has_header, c.num as num_constant_sets
- FROM {tripal_jobs} j
- LEFT JOIN {tripal_bulk_loader} b ON b.job_id=j.job_id
- LEFT JOIN (
- SELECT nid, count(distinct(group_id)) as num
- FROM {tripal_bulk_loader_constants}
- GROUP BY nid
- ) c ON c.nid=b.nid
- WHERE j.job_id=:job", array(':job' =>$job_id))->execute();
- if ($job->num_constant_sets) {
- $num_constant_sets_loaded = round($job->progress / (100 / $job->num_constant_sets), 4);
- // If the next constant set has started loading
- if ($job->num_constant_sets != $num_constant_sets_loaded) {
- // total lines in input file
- $total_lines = trim(`wc --lines < $job->file`);
- if ($job->file_has_header) {
- $total_lines--;
- }
- // percent of the current constant set loaded
- $percent = round($num_lines/$total_lines * 100, 2);
- // percent of the total job = (<# fully loaded constant sets> * 100 )
- // + <percent of current constant set>
- // / <total number of constant sets>
- $total_percent = (($num_constant_sets_loaded * 100) + $percent) / $job->num_constant_sets;
- // update the progress of the job
- if ($update_progress AND ($percent != 0 OR $percent != 100)) {
- tripal_job_set_progress($job_id, round($total_percent, 0));
- }
- }
- }
- return (object) array(
- 'num_lines' => $num_lines,
- 'total_lines' => $total_lines,
- 'percent_file' => $percent,
- 'num_constant_sets_loaded' => $num_constant_sets_loaded,
- 'total_percent' => $total_percent,
- 'num_records' => $num_records
- );
- }
- /**
- * 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
- *
- * @ingroup tripal_bulk_loader
- */
- 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;
- }
- }
- /**
- * Implements hook_coder_ignore().
- * Defines the path to the file (tripal_bulk_loader.coder_ignores.txt) where ignore rules for coder are stored
- *
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_coder_ignore() {
- return array(
- 'path' => drupal_get_path('module', 'tripal_bulk_loader'),
- 'line prefix' => drupal_get_path('module', 'tripal_bulk_loader'),
- );
- }
|