tripal_bulk_loader.module 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. <?php
  2. include('tripal_bulk_loader.loader.inc');
  3. /**
  4. * Implements hook_init
  5. * Used to add stylesheets and javascript files to the header
  6. */
  7. function tripal_bulk_loader_init(){
  8. // Add javascript and style sheet
  9. drupal_add_css(drupal_get_path('theme', 'tripal').'/css/tripal_bulk_loader.css');
  10. drupal_add_js(drupal_get_path('theme', 'tripal').'/js/tripal_bulk_loader.js');
  11. }
  12. /**
  13. * Implements hook_menu
  14. */
  15. function tripal_bulk_loader_menu() {
  16. $items = array();
  17. // Show all loaders
  18. $items['tripal_bulk_loaders'] = array(
  19. 'title' => 'Tripal Bulk Loaders',
  20. 'description' => 'Tripal bulk loaders for loading tab-delimited file into chado database',
  21. 'page callback' => 'tripal_bulk_loader_list',
  22. 'access arguments' => array('access tripal_bulk_loader'),
  23. 'type' => MENU_NORMAL_ITEM,
  24. );
  25. // Admin page to create the template
  26. $items['admin/tripal/tripal_bulk_loader_template'] = array(
  27. 'title' => 'Bulk Loader Template',
  28. 'description' => 'Templates for loading tab-delimited data',
  29. 'page callback' => 'tripal_bulk_loader_admin_template',
  30. 'access arguments' => array('administer site configuration'),
  31. 'type' => MENU_NORMAL_ITEM,
  32. 'file' => 'tripal_bulk_loader.admin.inc',
  33. );
  34. // Create/Edit Template -------
  35. $items['admin/tripal/tripal_bulk_loader_template/create'] = array(
  36. 'title' => 'Create Bulk Loader Template',
  37. 'description' => 'Create loader template for loading tab-delimited data',
  38. 'page callback' => 'drupal_get_form',
  39. 'page arguments' => array('tripal_bulk_loader_modify_template_base_form', 'create'),
  40. 'access arguments' => array('administer site configuration'),
  41. 'type' => MENU_NORMAL_ITEM,
  42. 'file' => 'tripal_bulk_loader.admin.inc',
  43. );
  44. $items['admin/tripal/tripal_bulk_loader_template/edit'] = array(
  45. 'title' => 'Edit Bulk Loader Template',
  46. 'description' => 'Edit loader template for loading tab-delimited data',
  47. 'page callback' => 'drupal_get_form',
  48. 'page arguments' => array('tripal_bulk_loader_modify_template_base_form', 'edit'),
  49. 'access arguments' => array('administer site configuration'),
  50. 'type' => MENU_NORMAL_ITEM,
  51. 'file' => 'tripal_bulk_loader.admin.inc',
  52. );
  53. $items['admin/tripal/tripal_bulk_loader_template/edit_record'] = array(
  54. 'title' => 'Edit Template Record',
  55. 'description' => 'Edit a record in an existing tripal bulk loader template.',
  56. 'page callback' => 'drupal_get_form',
  57. 'page arguments' => array('tripal_bulk_loader_edit_template_record_form'),
  58. 'access arguments' => array('administer site configuration'),
  59. 'type' => MENU_CALLBACK,
  60. 'file' => 'tripal_bulk_loader.admin.inc',
  61. );
  62. $items['admin/tripal/tripal_bulk_loader_template/add_field'] = array(
  63. 'title' => 'Add Template Field',
  64. 'description' => 'Add a template field to an existing tripal bulk loader template.',
  65. 'page callback' => 'drupal_get_form',
  66. 'page arguments' => array('tripal_bulk_loader_add_template_field_form'),
  67. 'access arguments' => array('administer site configuration'),
  68. 'type' => MENU_CALLBACK,
  69. 'file' => 'tripal_bulk_loader.admin.inc',
  70. );
  71. $items['admin/tripal/tripal_bulk_loader_template/edit_field'] = array(
  72. 'title' => 'Edit Template Field',
  73. 'description' => 'Edit an existing field from a tripal bulk loader template.',
  74. 'page callback' => 'drupal_get_form',
  75. 'page arguments' => array('tripal_bulk_loader_edit_template_field_form'),
  76. 'access arguments' => array('administer site configuration'),
  77. 'type' => MENU_CALLBACK,
  78. 'file' => 'tripal_bulk_loader.admin.inc',
  79. );
  80. // Delete Template -----
  81. $items['admin/tripal/tripal_bulk_loader_template/delete'] = array(
  82. 'title' => 'Delete Bulk Loader Template',
  83. 'description' => 'Delete bulk loader template',
  84. 'page callback' => 'drupal_get_form',
  85. 'page arguments' => array('tripal_bulk_loader_delete_template_base_form'),
  86. 'access arguments' => array('administer site configuration'),
  87. 'type' => MENU_NORMAL_ITEM,
  88. 'file' => 'tripal_bulk_loader.admin.inc',
  89. );
  90. // Import/Export ---------
  91. $items['admin/tripal/tripal_bulk_loader_template/import'] = array(
  92. 'title' => 'Import Bulk Loader Template',
  93. 'description' => 'Import Loaders',
  94. 'page callback' => 'drupal_get_form',
  95. 'page arguments' => array('tripal_bulk_loader_import_export_template_form', 'import'),
  96. 'access arguments' => array('administer site configuration'),
  97. 'type' => MENU_NORMAL_ITEM,
  98. 'file' => 'tripal_bulk_loader.admin.inc',
  99. );
  100. $items['admin/tripal/tripal_bulk_loader_template/export'] = array(
  101. 'title' => 'Export Bulk Loader Template',
  102. 'description' => 'Export Loaders',
  103. 'page callback' => 'drupal_get_form',
  104. 'page arguments' => array('tripal_bulk_loader_import_export_template_form', 'export'),
  105. 'access arguments' => array('administer site configuration'),
  106. 'type' => MENU_NORMAL_ITEM,
  107. 'file' => 'tripal_bulk_loader.admin.inc',
  108. );
  109. // AHAH ---------
  110. $items['node/add/tripal-bulk-loader/ahah'] = array(
  111. 'page callback' => 'tripal_bulk_loader_create_node_ahah',
  112. 'access arguments' => array('create tripal_bulk_loader'),
  113. 'type' => MENU_CALLBACK,
  114. );
  115. $items['admin/tripal/tripal_bulk_loader_template/add_field_ahah'] = array(
  116. 'page callback' => 'tripal_bulk_loader_add_field_ahah',
  117. 'access arguments' => array('administer site configuration'),
  118. 'type' => MENU_CALLBACK,
  119. 'file' => 'tripal_bulk_loader.admin.inc',
  120. );
  121. $items['admin/tripal/tripal_bulk_loader_template/edit_field_ahah'] = array(
  122. 'page callback' => 'tripal_bulk_loader_edit_field_ahah',
  123. 'access arguments' => array('administer site configuration'),
  124. 'type' => MENU_CALLBACK,
  125. 'file' => 'tripal_bulk_loader.admin.inc',
  126. );
  127. return $items;
  128. }
  129. /**
  130. * Implements hook_theme
  131. */
  132. function tripal_bulk_loader_theme() {
  133. return array(
  134. 'tripal_bulk_loader_template' => array(
  135. 'arguments'=> array('template_id' => NULL),
  136. 'template' => 'tripal_bulk_loader_template'
  137. ),
  138. 'tripal_bulk_loader_modify_template_base_form' => array(
  139. 'arguments' => array('form' => NULL),
  140. 'template' => 'tripal_bulk_loader_modify_template_base_form',
  141. ),
  142. 'tripal_bulk_loader_edit_template_field_form' => array(
  143. 'arguments' => array('form' => NULL),
  144. 'template' => 'tripal_bulk_loader_edit_template_field_form',
  145. ),
  146. 'tripal_bulk_loader_add_template_field_form' => array(
  147. 'arguments' => array('form' => NULL),
  148. 'template' => 'tripal_bulk_loader_add_template_field_form',
  149. ),
  150. );
  151. }
  152. /**
  153. * Implements hook_access
  154. */
  155. function tripal_bulk_loader_access($op, $node, $account){
  156. if ($op == 'create') {
  157. if(!user_access('create tripal_bulk_loader', $account)){
  158. return FALSE;
  159. }
  160. }
  161. if ($op == 'update') {
  162. if (!user_access('edit tripal_bulk_loader', $account)) {
  163. return FALSE;
  164. }
  165. }
  166. if ($op == 'delete') {
  167. if (!user_access('delete tripal_bulk_loader', $account)) {
  168. return FALSE;
  169. }
  170. }
  171. if ($op == 'view') {
  172. if (!user_access('access tripal_bulk_loader', $account)) {
  173. return FALSE;
  174. }
  175. }
  176. return NULL;
  177. }
  178. /**
  179. * Implements hook_perm
  180. */
  181. function tripal_bulk_loader_perm(){
  182. return array(
  183. 'access tripal_bulk_loader',
  184. 'create tripal_bulk_loader',
  185. 'delete tripal_bulk_loader',
  186. 'edit tripal_bulk_loader',
  187. );
  188. }
  189. /**
  190. * Creates a listing page for all bulk loading jobs
  191. */
  192. function tripal_bulk_loader_list () {
  193. $num_results_per_page = 50;
  194. $output = '';
  195. $header = array('','Status','Loader','File');
  196. $rows = array();
  197. $query = 'SELECT * FROM {tripal_bulk_loader} l '
  198. .'LEFT JOIN {node} n ON n.nid = l.nid '
  199. .'LEFT JOIN {tripal_bulk_loader_template} t ON t.template_id = cast(l.template_id as integer)';
  200. $resource = pager_query($query, $num_results_per_page, 0, NULL);
  201. while ($r = db_fetch_object($resource)) {
  202. $row = array(
  203. l($r->title, 'node/'.$r->nid),
  204. $r->job_status,
  205. $r->name,
  206. $r->file
  207. );
  208. $rows[] = $row;
  209. }
  210. $output .= theme('table', $header, $rows);
  211. return $output;
  212. }
  213. //////////////////////////////////////////////////////////////////////////////////////////////
  214. // Node Functions
  215. //////////////////////////////////////////////////////////////////////////////////////////////
  216. /**
  217. * Implements hook_node_info
  218. */
  219. function tripal_bulk_loader_node_info() {
  220. $nodes = array();
  221. $nodes['tripal_bulk_loader'] = array(
  222. 'name' => t('Bulk Loading Job'),
  223. 'module' => 'tripal_bulk_loader',
  224. 'description' => t('A bulk loader for inserting tab-delimited data into chado database'),
  225. 'has_title' => TRUE,
  226. 'has_body' => FALSE,
  227. 'locked' => TRUE
  228. );
  229. return $nodes;
  230. }
  231. /**
  232. * Implements node_form
  233. * Used to gather the extra details stored with a Bulk Loading Job Node
  234. */
  235. function tripal_bulk_loader_form ($node, $form_state){
  236. $form = array();
  237. if (isset($form_state['values'])) {
  238. $node = $form_state['values'] + (array)$node;
  239. $node = (object) $node;
  240. }
  241. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  242. $results = db_query($sql);
  243. $templates = array ();
  244. while ($template = db_fetch_object ($results)) {
  245. $templates [$template->template_id] = $template->name;
  246. }
  247. if (!$templates) {
  248. $form['label'] = array(
  249. '#type' => 'item',
  250. '#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."),
  251. '#weight' => -10,
  252. );
  253. return $form;
  254. }
  255. $form['loader'] = array(
  256. '#type' => 'fieldset',
  257. '#title' => t('Basic Details'),
  258. );
  259. $form['loader']['loader_name'] = array(
  260. '#type' => 'textfield',
  261. '#title' => t('Loading Job Name'),
  262. '#weight' => -10,
  263. '#required' => TRUE,
  264. '#default_value' => $node->loader_name
  265. );
  266. $form['loader']['template_id'] = array(
  267. '#type' => 'select',
  268. '#title' => t('Template'),
  269. '#description' => t('Please specify a template for this loader'),
  270. '#options' => $templates,
  271. '#weight' => -9,
  272. '#required' => TRUE,
  273. '#default_value' => $node->template_id,
  274. '#ahah' => array(
  275. 'path' => 'node/add/tripal-bulk-loader/ahah',
  276. 'wrapper' => 'set-constants',
  277. 'event' => 'change',
  278. 'method' => 'replace',
  279. 'effect' => 'fade',
  280. ),
  281. );
  282. $form['loader']['file']= array(
  283. '#type' => 'textfield',
  284. '#title' => t('Data File'),
  285. '#description' => t('Please specify the data file to be loaded.'),
  286. '#weight' => -8,
  287. '#default_value' => $node->file
  288. );
  289. $form['loader']['has_header'] = array(
  290. '#type' => 'radios',
  291. '#title' => t('File has a Header'),
  292. '#options' => array( 1 => 'Yes', 2 => 'No'),
  293. '#weight' => -7,
  294. '#default_value' => $node->file_has_header,
  295. );
  296. return $form;
  297. }
  298. /**
  299. * AHAH Function: Replace $form['constants'] in tripal_bulk_loader_form
  300. *
  301. * @return
  302. * JSON Data printed to the screen
  303. */
  304. function tripal_bulk_loader_create_node_ahah () {
  305. // The form is generated in an include file which we need to include manually.
  306. include_once 'modules/node/node.pages.inc';
  307. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  308. $form_build_id = $_POST['form_build_id'];
  309. $form = form_get_cache($form_build_id, $form_state);
  310. $args = $form['#parameters'];
  311. $form_id = array_shift($args);
  312. $form_state['post'] = $form['#post'] = $_POST;
  313. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  314. $form_state['ahah_submission'] = TRUE;
  315. $form['#programmed'] = $form['#redirect'] = FALSE;
  316. drupal_process_form($form_id, $form, $form_state);
  317. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  318. $form_element = $form['exposed_fields'];
  319. // Remove the wrapper so we don't double it up.
  320. unset($form_element['#prefix'], $form_element['#suffix']);
  321. $output = theme('status_messages');
  322. $output .= drupal_render($form_element);
  323. // Final rendering callback.
  324. print drupal_json(array('status' => TRUE, 'data' => $output));
  325. exit();
  326. }
  327. /**
  328. * Implements node_load
  329. */
  330. function tripal_bulk_loader_load($node){
  331. $sql = "SELECT * FROM {tripal_bulk_loader} WHERE nid = %d";
  332. $node = db_fetch_object(db_query($sql, $node->nid));
  333. $node->title = 'Bulk Loading Job: '.$node->loader_name;
  334. // Add the loader template
  335. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  336. $results = db_fetch_object(db_query($sql, $node->template_id));
  337. $template = unserialize($results->template_array);
  338. $node->template = $results;
  339. $node->template->template_array = $template;
  340. // Add inserted records
  341. $sql = 'SELECT * FROM {tripal_bulk_loader_inserted} WHERE nid=%d';
  342. $resource = db_query($sql,$node->nid);
  343. while ($r = db_fetch_object($resource)) {
  344. $r->num_inserted = sizeof(preg_split('/,/',$r->ids_inserted));
  345. $node->inserted_records->{$r->table_inserted_into} = $r;
  346. }
  347. // Add constants
  348. $sql = 'SELECT * FROM {tripal_bulk_loader_constants} WHERE nid=%d ORDER BY record_id, field_id';
  349. $resource = db_query($sql,$node->nid);
  350. while ($r = db_fetch_object($resource)) {
  351. $node->constants[$r->record_id][$r->field_id] = array(
  352. 'chado_table'=>$r->chado_table,
  353. 'chado_field'=>$r->chado_field,
  354. 'record_id'=>$r->record_id,
  355. 'field_id'=>$r->field_id,
  356. 'value'=>$r->value
  357. );
  358. }
  359. return $node;
  360. }
  361. /**
  362. * Implements node_insert
  363. * Insert the data from the node form on Create content
  364. */
  365. function tripal_bulk_loader_insert ($node) {
  366. // Insert into tripal_bulk_loader
  367. $sql = "INSERT INTO {tripal_bulk_loader} (nid, loader_name, template_id, file, file_has_header, job_status) VALUES (%d, '%s', %d, '%s', %d, '%s')";
  368. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->has_header, 'Initialized');
  369. // Update title
  370. $node->title =$node->loader_name;
  371. drupal_write_record('node',$node,'nid');
  372. drupal_write_record('node_revision',$node,'nid');
  373. drupal_set_message('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.');
  374. }
  375. /**
  376. * Implements node_delete
  377. * Deletes the data when the delete button on the node form is clicked
  378. */
  379. function tripal_bulk_loader_delete ($node) {
  380. $sql = "DELETE FROM {tripal_bulk_loader} WHERE nid = %d";
  381. db_query($sql, $node->nid);
  382. }
  383. /**
  384. * Implements node_update
  385. * Updates the data submitted by the node form on edit
  386. */
  387. function tripal_bulk_loader_update ($node) {
  388. // Update tripal_bulk_loader
  389. $sql = "UPDATE {tripal_bulk_loader} SET nid = %d, loader_name = '%s', template_id = %d, file = '%s', file_has_header = '%s' WHERE nid = %d";
  390. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->has_header, $node->nid);
  391. // Add a job if the user want to load the data
  392. global $user;
  393. if($node->job) {
  394. $job_args[0] =$node->loader_name;
  395. $job_args[1] = $node->template_id;
  396. $job_args[2] = $node->file;
  397. if (is_readable($node->file)) {
  398. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  399. tripal_add_job("Bulk Load: $fname",'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  400. } else {
  401. drupal_set_message("Can not open $node->file. Job not scheduled.");
  402. }
  403. }
  404. }
  405. ///////////////////////////////////////////////////////////
  406. // Set Constants Form
  407. ///////////////////////////////////////////////////////////
  408. /**
  409. * Set constants (exposed fields in template)
  410. *
  411. * @param $form_state
  412. * The current state of the form
  413. * @param $node
  414. * The node to set constants for
  415. *
  416. * @return
  417. * A form array to be rendered by drupal_get_form()
  418. */
  419. function tripal_bulk_loader_set_constants_form ($form_state, $node) {
  420. $form = array();
  421. $form['nid'] = array(
  422. '#type' => 'hidden',
  423. '#value' => $node->nid
  424. );
  425. $form['exposed_fields'] = array(
  426. '#type' => 'fieldset',
  427. '#title' => t('Constant Values'),
  428. '#collapsible' => TRUE,
  429. '#collapsed' => ($node->template_id) ? FALSE : TRUE,
  430. '#prefix' => '<div id="set-constants">',
  431. '#suffix' => '</div>',
  432. );
  433. $form['exposed_fields']['explanation'] = array(
  434. '#type' => 'item',
  435. '#value' => t('The following fields are constants in the selected template that you need to set values for.')
  436. );
  437. // Add textifelds for exposed fields of the current template
  438. $exposed_fields = FALSE;
  439. $indexes = array();
  440. if ($node->template_id) {
  441. if (isset($node->template)) {
  442. foreach ($node->template->template_array as $record_id => $record) {
  443. foreach ($record['fields'] as $field_id => $field) {
  444. if ($field['exposed']) {
  445. $exposed_fields = TRUE;
  446. $indexes[$record_id][] = $field_id;
  447. switch($field['type']) {
  448. case 'table field':
  449. $form['exposed_fields'][$record_id.'-'.$field_id] = array(
  450. '#type' => 'textfield',
  451. '#title' => t($field['title']),
  452. '#description' => t($field['exposed_description']),
  453. '#default_value' => (isset($node->constants[$record_id][$field_id]['value'])) ? $node->constants[$record_id][$field_id]['value'] : $field['constant value'],
  454. );
  455. break;
  456. case 'constant':
  457. $form['exposed_fields'][$record_id.'-'.$field_id] = array(
  458. '#type' => 'textfield',
  459. '#title' => t($field['title']),
  460. '#description' => t('Enter the case-sensitive value of this constant for your spreadsheet'),
  461. '#default_value' => (isset($node->constants[$record_id][$field_id]['value'])) ? $node->constants[$record_id][$field_id]['value'] : $field['constant value'],
  462. );
  463. break;
  464. }
  465. $form['exposed_fields'][$record_id.'-'.$field_id.'-table'] = array(
  466. '#type' => 'hidden',
  467. '#value' => $record['table'],
  468. );
  469. $form['exposed_fields'][$record_id.'-'.$field_id.'-field'] = array(
  470. '#type' => 'hidden',
  471. '#value' => $field['field'],
  472. );
  473. $form['exposed_fields'][$record_id.'-'.$field_id.'-type'] = array(
  474. '#type' => 'hidden',
  475. '#value' => $field['type'],
  476. );
  477. }
  478. }
  479. }
  480. }
  481. }
  482. $form['template'] = array(
  483. '#type' => 'hidden',
  484. '#value' => serialize($node->template->template_array)
  485. );
  486. $form['exposed_fields']['indexes'] = array(
  487. '#type' => 'hidden',
  488. '#value' => serialize($indexes),
  489. );
  490. if (!$exposed_fields) {
  491. $form['exposed_fields']['explanation'] = array(
  492. '#type' => 'item',
  493. '#value' => t('There are no exposed fields for this template.')
  494. );
  495. }
  496. $form['exposed_fields']['submit'] = array(
  497. '#type' => 'submit',
  498. '#value' => t('Save Changes')
  499. );
  500. return $form;
  501. }
  502. /**
  503. * Validate that the values entered exist in the database
  504. * if indicated in hte template array
  505. */
  506. function tripal_bulk_loader_set_constants_form_validate ($form_state, $form) {
  507. $template = unserialize($form_state['template']['#value']);
  508. $indexes = unserialize($form_state['exposed_fields']['indexes']['#value']);
  509. foreach ($indexes as $record_id => $array) {
  510. foreach($array as $field_id) {
  511. if ($template[$record_id]['fields'][$field_id]['exposed_validate']) {
  512. $result = db_fetch_object(db_query(
  513. "SELECT 1 as valid FROM %s WHERE %s='%s'",
  514. $template[$record_id]['table'],
  515. $template[$record_id]['fields'][$field_id]['field'],
  516. $form_state['exposed_fields'][$record_id.'-'.$field_id]['#value']
  517. ));
  518. if (!$result->valid) {
  519. $msg = 'A '.$form_state['exposed_fields'][$record_id.'-'.$field_id]['#title'].' of "'.$form_state['exposed_fields'][$record_id.'-'.$field_id]['#value'].'" must already exist!';
  520. form_set_error($record_id.'-'.$field_id, $msg);
  521. } else {
  522. drupal_set_message('Confirmed a '.$form_state['exposed_fields'][$record_id.'-'.$field_id]['#title'].' of "'.$form_state['exposed_fields'][$record_id.'-'.$field_id]['#value'].'" already exists.');
  523. }
  524. }
  525. }
  526. }
  527. }
  528. /**
  529. * Insert/update the constants associated with this node
  530. */
  531. function tripal_bulk_loader_set_constants_form_submit ($form, $form_state) {
  532. // Insert/Update constants
  533. $template = unserialize($form_state['values']['template']);
  534. $indexes = unserialize($form_state['values']['indexes']);
  535. foreach ($indexes as $record_id => $array) {
  536. foreach($array as $field_id) {
  537. tripal_bulk_loader_update_constant(
  538. $form_state['values'][nid],
  539. $form_state['values'][$record_id.'-'.$field_id.'-table'],
  540. $form_state['values'][$record_id.'-'.$field_id.'-field'],
  541. $record_id,
  542. $field_id,
  543. $form_state['values'][$record_id.'-'.$field_id]
  544. );
  545. }
  546. }
  547. }
  548. /**
  549. * Inserts/Updates a tripal bulk loading job constant
  550. *
  551. * @param $nid
  552. * The node ID of the the tripal bulk loading job the constant is associated with
  553. * @param $table
  554. * The chado table the constant is associated with
  555. * @param $field
  556. * The chado field the constant is associated with
  557. * @param $record_id
  558. * The index in the template array for this record
  559. * @param $field_id
  560. * The index in the template array for this field
  561. *
  562. * NOTE: $template_array[$record_id]['table'] = $table and $template_array[$record_id]['fields'][$field_id]['field'] = $field
  563. * both are included as a means of double-checking the constant still is still in thesame place in the template array.
  564. * For example, that the template was not edited and the records moved around after the job was submitted but before it was run.
  565. *
  566. * @return
  567. * On success it returns the object (with primary key if inserted);
  568. * on failure it returns FALSE
  569. */
  570. function tripal_bulk_loader_update_constant ($nid, $table, $field, $record_id, $field_id, $value) {
  571. $record = array(
  572. 'nid'=>$nid,
  573. 'chado_table'=>$table,
  574. 'chado_field'=>$field,
  575. 'record_id'=>$record_id,
  576. 'field_id'=>$field_id,
  577. 'value'=>$value
  578. );
  579. // Check to see if already exists
  580. $exists = db_fetch_object(db_query(
  581. "SELECT constant_id FROM {tripal_bulk_loader_constants} WHERE nid=%d AND record_id=%d AND field_id=%d",
  582. $record['nid'],
  583. $record['record_id'],
  584. $record['field_id']
  585. ));
  586. if ($exists->constant_id) {
  587. $record['constant_id'] = $exists->constant_id;
  588. $status = drupal_write_record('tripal_bulk_loader_constants',$record,'constant_id');
  589. if ($status) {
  590. return $record;
  591. } else {
  592. return FALSE;
  593. }
  594. } else {
  595. $status = drupal_write_record('tripal_bulk_loader_constants',$record);
  596. if ($status) {
  597. return $record;
  598. } else {
  599. return FALSE;
  600. }
  601. }
  602. }
  603. ///////////////////////////////////////////////////////////
  604. /**
  605. * Preprocessor function for the tripal_bulk_loader template
  606. */
  607. function tripal_bulk_loader_preprocess_tripal_bulk_loader_template (&$variables) {
  608. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  609. $template = db_fetch_object(db_query($sql, $variables['template_id']));
  610. $template->template_array = unserialize($template->template_array);
  611. $variables['template'] = $template;
  612. }
  613. /**
  614. * Implements hook_job_describe_args()
  615. * Specifically to make viewing past tripal jobs more readable for jobs registered by this module
  616. *
  617. * @params $callback
  618. * The callback passed into tripal_add_job()
  619. * @param $args
  620. * The arguements passed into tripal_add_job()
  621. * @return
  622. * An array where keys are the human readable headers describing each arguement
  623. * and the value is the aguement passed in after formatting
  624. */
  625. function tripal_bulk_loader_job_describe_args($callback,$args){
  626. $new_args = array();
  627. if($callback == 'tripal_bulk_loader_load_data'){
  628. //1st arg is the nid for a bulk loader node
  629. $node = node_load($args[0]);
  630. $new_args['Bulk Loading Job'] = l($node->title, 'node/'.$args[0]);
  631. return $new_args;
  632. }
  633. }