tripal_bulk_loader.module 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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['admin/tripal/tripal_bulk_loader_template/add_field_ahah'] = array(
  111. 'page callback' => 'tripal_bulk_loader_add_field_ahah',
  112. 'access arguments' => array('administer site configuration'),
  113. 'type' => MENU_CALLBACK,
  114. 'file' => 'tripal_bulk_loader.admin.inc',
  115. );
  116. $items['admin/tripal/tripal_bulk_loader_template/edit_field_ahah'] = array(
  117. 'page callback' => 'tripal_bulk_loader_edit_field_ahah',
  118. 'access arguments' => array('administer site configuration'),
  119. 'type' => MENU_CALLBACK,
  120. 'file' => 'tripal_bulk_loader.admin.inc',
  121. );
  122. return $items;
  123. }
  124. /**
  125. * Implements hook_theme
  126. */
  127. function tripal_bulk_loader_theme() {
  128. return array(
  129. 'tripal_bulk_loader_template' => array(
  130. 'arguments'=> array('template_id' => NULL),
  131. 'template' => 'tripal_bulk_loader_template'
  132. ),
  133. 'tripal_bulk_loader_modify_template_base_form' => array(
  134. 'arguments' => array('form' => NULL),
  135. 'template' => 'tripal_bulk_loader_modify_template_base_form',
  136. ),
  137. 'tripal_bulk_loader_edit_template_field_form' => array(
  138. 'arguments' => array('form' => NULL),
  139. 'template' => 'tripal_bulk_loader_edit_template_field_form',
  140. ),
  141. 'tripal_bulk_loader_add_template_field_form' => array(
  142. 'arguments' => array('form' => NULL),
  143. 'template' => 'tripal_bulk_loader_add_template_field_form',
  144. ),
  145. );
  146. }
  147. /**
  148. * Implements hook_access
  149. */
  150. function tripal_bulk_loader_access($op, $node, $account){
  151. if ($op == 'create') {
  152. if(!user_access('create tripal_bulk_loader', $account)){
  153. return FALSE;
  154. }
  155. }
  156. if ($op == 'update') {
  157. if (!user_access('edit tripal_bulk_loader', $account)) {
  158. return FALSE;
  159. }
  160. }
  161. if ($op == 'delete') {
  162. if (!user_access('delete tripal_bulk_loader', $account)) {
  163. return FALSE;
  164. }
  165. }
  166. if ($op == 'view') {
  167. if (!user_access('access tripal_bulk_loader', $account)) {
  168. return FALSE;
  169. }
  170. }
  171. return NULL;
  172. }
  173. /**
  174. * Implements hook_perm
  175. */
  176. function tripal_bulk_loader_perm(){
  177. return array(
  178. 'access tripal_bulk_loader',
  179. 'create tripal_bulk_loader',
  180. 'delete tripal_bulk_loader',
  181. 'edit tripal_bulk_loader',
  182. );
  183. }
  184. /**
  185. * Creates a listing page for all bulk loading jobs
  186. */
  187. function tripal_bulk_loader_list () {
  188. $num_results_per_page = 50;
  189. $output = '';
  190. $header = array('','Status','Loader','File');
  191. $rows = array();
  192. $query = 'SELECT * FROM {tripal_bulk_loader} l '
  193. .'LEFT JOIN {node} n ON n.nid = l.nid '
  194. .'LEFT JOIN {tripal_bulk_loader_template} t ON t.template_id = cast(l.template_id as integer)';
  195. $resource = pager_query($query, $num_results_per_page, 0, NULL);
  196. while ($r = db_fetch_object($resource)) {
  197. $row = array(
  198. l($r->title, 'node/'.$r->nid),
  199. $r->job_status,
  200. $r->name,
  201. $r->file
  202. );
  203. $rows[] = $row;
  204. }
  205. $output .= theme('table', $header, $rows);
  206. return $output;
  207. }
  208. //////////////////////////////////////////////////////////////////////////////////////////////
  209. // Node Functions
  210. //////////////////////////////////////////////////////////////////////////////////////////////
  211. /**
  212. * Implements hook_node_info
  213. */
  214. function tripal_bulk_loader_node_info() {
  215. $nodes = array();
  216. $nodes['tripal_bulk_loader'] = array(
  217. 'name' => t('Bulk Loading Job'),
  218. 'module' => 'tripal_bulk_loader',
  219. 'description' => t('A bulk loader for inserting tab-delimited data into chado database'),
  220. 'has_title' => TRUE,
  221. 'has_body' => FALSE,
  222. 'locked' => TRUE
  223. );
  224. return $nodes;
  225. }
  226. /**
  227. * Implements node_form
  228. * Used to gather the extra details stored with a Bulk Loading Job Node
  229. */
  230. function tripal_bulk_loader_form ($node, $form_state){
  231. $form = array();
  232. if (isset($form_state['values'])) {
  233. $node = $form_state['values'] + (array)$node;
  234. $node = (object) $node;
  235. }
  236. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  237. $results = db_query($sql);
  238. $templates = array ();
  239. while ($template = db_fetch_object ($results)) {
  240. $templates [$template->template_id] = $template->name;
  241. }
  242. if (!$templates) {
  243. $form['label'] = array(
  244. '#type' => 'item',
  245. '#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."),
  246. '#weight' => -10,
  247. );
  248. return $form;
  249. }
  250. $form['loader'] = array(
  251. '#type' => 'fieldset',
  252. '#title' => t('Basic Details'),
  253. );
  254. $form['loader']['loader_name'] = array(
  255. '#type' => 'textfield',
  256. '#title' => t('Loading Job Name'),
  257. '#weight' => -10,
  258. '#required' => TRUE,
  259. '#default_value' => $node->loader_name
  260. );
  261. $form['loader']['template_id'] = array(
  262. '#type' => 'select',
  263. '#title' => t('Template'),
  264. '#description' => t('Please specify a template for this loader'),
  265. '#options' => $templates,
  266. '#weight' => -9,
  267. '#required' => TRUE,
  268. '#default_value' => $node->template_id,
  269. );
  270. $form['loader']['file']= array(
  271. '#type' => 'textfield',
  272. '#title' => t('Data File'),
  273. '#description' => t('Please specify the data file to be loaded.'),
  274. '#weight' => -8,
  275. '#default_value' => $node->file
  276. );
  277. $form['loader']['has_header'] = array(
  278. '#type' => 'radios',
  279. '#title' => t('File has a Header'),
  280. '#options' => array( 1 => 'Yes', 2 => 'No'),
  281. '#weight' => -7,
  282. '#default_value' => $node->file_has_header,
  283. );
  284. return $form;
  285. }
  286. /**
  287. * Implements node_load
  288. */
  289. function tripal_bulk_loader_load($node){
  290. $sql = "SELECT * FROM {tripal_bulk_loader} WHERE nid = %d";
  291. $node = db_fetch_object(db_query($sql, $node->nid));
  292. $node->title = 'Bulk Loading Job: '.$node->loader_name;
  293. // Add the loader template
  294. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  295. $results = db_fetch_object(db_query($sql, $node->template_id));
  296. $template = unserialize($results->template_array);
  297. $node->template = $results;
  298. $node->template->template_array = $template;
  299. // Add inserted records
  300. $sql = 'SELECT * FROM {tripal_bulk_loader_inserted} WHERE nid=%d';
  301. $resource = db_query($sql,$node->nid);
  302. while ($r = db_fetch_object($resource)) {
  303. $r->num_inserted = sizeof(preg_split('/,/',$r->ids_inserted));
  304. $node->inserted_records->{$r->table_inserted_into} = $r;
  305. }
  306. // Add constants
  307. $sql = 'SELECT * FROM {tripal_bulk_loader_constants} WHERE nid=%d ORDER BY record_id, field_id';
  308. $resource = db_query($sql,$node->nid);
  309. while ($r = db_fetch_object($resource)) {
  310. $node->constants[$r->record_id][$r->field_id] = array(
  311. 'chado_table'=>$r->chado_table,
  312. 'chado_field'=>$r->chado_field,
  313. 'record_id'=>$r->record_id,
  314. 'field_id'=>$r->field_id,
  315. 'value'=>$r->value
  316. );
  317. }
  318. return $node;
  319. }
  320. /**
  321. * Implements node_insert
  322. * Insert the data from the node form on Create content
  323. */
  324. function tripal_bulk_loader_insert ($node) {
  325. // Insert into tripal_bulk_loader
  326. $sql = "INSERT INTO {tripal_bulk_loader} (nid, loader_name, template_id, file, file_has_header, job_status) VALUES (%d, '%s', %d, '%s', %d, '%s')";
  327. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->has_header, 'Initialized');
  328. // Update title
  329. $node->title =$node->loader_name;
  330. drupal_write_record('node',$node,'nid');
  331. drupal_write_record('node_revision',$node,'nid');
  332. 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.');
  333. }
  334. /**
  335. * Implements node_delete
  336. * Deletes the data when the delete button on the node form is clicked
  337. */
  338. function tripal_bulk_loader_delete ($node) {
  339. $sql = "DELETE FROM {tripal_bulk_loader} WHERE nid = %d";
  340. db_query($sql, $node->nid);
  341. }
  342. /**
  343. * Implements node_update
  344. * Updates the data submitted by the node form on edit
  345. */
  346. function tripal_bulk_loader_update ($node) {
  347. // Update tripal_bulk_loader
  348. $sql = "UPDATE {tripal_bulk_loader} SET nid = %d, loader_name = '%s', template_id = %d, file = '%s', file_has_header = '%s' WHERE nid = %d";
  349. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->has_header, $node->nid);
  350. // Add a job if the user want to load the data
  351. global $user;
  352. if($node->job) {
  353. $job_args[0] =$node->loader_name;
  354. $job_args[1] = $node->template_id;
  355. $job_args[2] = $node->file;
  356. if (is_readable($node->file)) {
  357. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  358. tripal_add_job("Bulk Load: $fname",'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  359. } else {
  360. drupal_set_message("Can not open $node->file. Job not scheduled.");
  361. }
  362. }
  363. }
  364. ///////////////////////////////////////////////////////////
  365. // Set Constants Form
  366. ///////////////////////////////////////////////////////////
  367. /**
  368. * Set constants (exposed fields in template)
  369. *
  370. * @param $form_state
  371. * The current state of the form
  372. * @param $node
  373. * The node to set constants for
  374. *
  375. * @return
  376. * A form array to be rendered by drupal_get_form()
  377. */
  378. function tripal_bulk_loader_set_constants_form ($form_state, $node) {
  379. $form = array();
  380. $form['nid'] = array(
  381. '#type' => 'hidden',
  382. '#value' => $node->nid
  383. );
  384. $form['exposed_fields'] = array(
  385. '#type' => 'fieldset',
  386. '#title' => t('Constant Values'),
  387. '#collapsible' => TRUE,
  388. '#collapsed' => ($node->template_id) ? FALSE : TRUE,
  389. '#prefix' => '<div id="set-constants">',
  390. '#suffix' => '</div>',
  391. );
  392. $form['exposed_fields']['explanation'] = array(
  393. '#type' => 'item',
  394. '#value' => t('The following fields are constants in the selected template that you need to set values for.')
  395. );
  396. // Add textifelds for exposed fields of the current template
  397. $exposed_fields = FALSE;
  398. $indexes = array();
  399. if ($node->template_id) {
  400. if (isset($node->template)) {
  401. foreach ($node->template->template_array as $record_id => $record) {
  402. foreach ($record['fields'] as $field_id => $field) {
  403. if ($field['exposed']) {
  404. $exposed_fields = TRUE;
  405. $indexes[$record_id][] = $field_id;
  406. switch($field['type']) {
  407. case 'table field':
  408. $form['exposed_fields'][$record_id.'-'.$field_id] = array(
  409. '#type' => 'textfield',
  410. '#title' => t($field['title']),
  411. '#description' => t($field['exposed_description']),
  412. '#default_value' => (isset($node->constants[$record_id][$field_id]['value'])) ? $node->constants[$record_id][$field_id]['value'] : $field['constant value'],
  413. );
  414. break;
  415. case 'constant':
  416. $form['exposed_fields'][$record_id.'-'.$field_id] = array(
  417. '#type' => 'textfield',
  418. '#title' => t($field['title']),
  419. '#description' => t('Enter the case-sensitive value of this constant for your spreadsheet'),
  420. '#default_value' => (isset($node->constants[$record_id][$field_id]['value'])) ? $node->constants[$record_id][$field_id]['value'] : $field['constant value'],
  421. );
  422. break;
  423. }
  424. $form['exposed_fields'][$record_id.'-'.$field_id.'-table'] = array(
  425. '#type' => 'hidden',
  426. '#value' => $record['table'],
  427. );
  428. $form['exposed_fields'][$record_id.'-'.$field_id.'-field'] = array(
  429. '#type' => 'hidden',
  430. '#value' => $field['field'],
  431. );
  432. $form['exposed_fields'][$record_id.'-'.$field_id.'-type'] = array(
  433. '#type' => 'hidden',
  434. '#value' => $field['type'],
  435. );
  436. }
  437. }
  438. }
  439. }
  440. }
  441. $form['template'] = array(
  442. '#type' => 'hidden',
  443. '#value' => serialize($node->template->template_array)
  444. );
  445. $form['exposed_fields']['indexes'] = array(
  446. '#type' => 'hidden',
  447. '#value' => serialize($indexes),
  448. );
  449. if (!$exposed_fields) {
  450. $form['exposed_fields']['explanation'] = array(
  451. '#type' => 'item',
  452. '#value' => t('There are no exposed fields for this template.')
  453. );
  454. }
  455. $form['exposed_fields']['submit'] = array(
  456. '#type' => 'submit',
  457. '#value' => t('Save Changes')
  458. );
  459. return $form;
  460. }
  461. /**
  462. * Validate that the values entered exist in the database
  463. * if indicated in hte template array
  464. */
  465. function tripal_bulk_loader_set_constants_form_validate ($form_state, $form) {
  466. $template = unserialize($form_state['template']['#value']);
  467. $indexes = unserialize($form_state['exposed_fields']['indexes']['#value']);
  468. foreach ($indexes as $record_id => $array) {
  469. foreach($array as $field_id) {
  470. if ($template[$record_id]['fields'][$field_id]['exposed_validate']) {
  471. $result = db_fetch_object(db_query(
  472. "SELECT 1 as valid FROM %s WHERE %s='%s'",
  473. $template[$record_id]['table'],
  474. $template[$record_id]['fields'][$field_id]['field'],
  475. $form_state['exposed_fields'][$record_id.'-'.$field_id]['#value']
  476. ));
  477. if (!$result->valid) {
  478. $msg = 'A '.$form_state['exposed_fields'][$record_id.'-'.$field_id]['#title'].' of "'.$form_state['exposed_fields'][$record_id.'-'.$field_id]['#value'].'" must already exist!';
  479. form_set_error($record_id.'-'.$field_id, $msg);
  480. } else {
  481. 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.');
  482. }
  483. }
  484. }
  485. }
  486. }
  487. /**
  488. * Insert/update the constants associated with this node
  489. */
  490. function tripal_bulk_loader_set_constants_form_submit ($form, $form_state) {
  491. // Insert/Update constants
  492. $template = unserialize($form_state['values']['template']);
  493. $indexes = unserialize($form_state['values']['indexes']);
  494. foreach ($indexes as $record_id => $array) {
  495. foreach($array as $field_id) {
  496. tripal_bulk_loader_update_constant(
  497. $form_state['values'][nid],
  498. $form_state['values'][$record_id.'-'.$field_id.'-table'],
  499. $form_state['values'][$record_id.'-'.$field_id.'-field'],
  500. $record_id,
  501. $field_id,
  502. $form_state['values'][$record_id.'-'.$field_id]
  503. );
  504. }
  505. }
  506. }
  507. /**
  508. * Inserts/Updates a tripal bulk loading job constant
  509. *
  510. * @param $nid
  511. * The node ID of the the tripal bulk loading job the constant is associated with
  512. * @param $table
  513. * The chado table the constant is associated with
  514. * @param $field
  515. * The chado field the constant is associated with
  516. * @param $record_id
  517. * The index in the template array for this record
  518. * @param $field_id
  519. * The index in the template array for this field
  520. *
  521. * NOTE: $template_array[$record_id]['table'] = $table and $template_array[$record_id]['fields'][$field_id]['field'] = $field
  522. * both are included as a means of double-checking the constant still is still in thesame place in the template array.
  523. * For example, that the template was not edited and the records moved around after the job was submitted but before it was run.
  524. *
  525. * @return
  526. * On success it returns the object (with primary key if inserted);
  527. * on failure it returns FALSE
  528. */
  529. function tripal_bulk_loader_update_constant ($nid, $table, $field, $record_id, $field_id, $value) {
  530. $record = array(
  531. 'nid'=>$nid,
  532. 'chado_table'=>$table,
  533. 'chado_field'=>$field,
  534. 'record_id'=>$record_id,
  535. 'field_id'=>$field_id,
  536. 'value'=>$value
  537. );
  538. // Check to see if already exists
  539. $exists = db_fetch_object(db_query(
  540. "SELECT constant_id FROM {tripal_bulk_loader_constants} WHERE nid=%d AND record_id=%d AND field_id=%d",
  541. $record['nid'],
  542. $record['record_id'],
  543. $record['field_id']
  544. ));
  545. if ($exists->constant_id) {
  546. $record['constant_id'] = $exists->constant_id;
  547. $status = drupal_write_record('tripal_bulk_loader_constants',$record,'constant_id');
  548. if ($status) {
  549. return $record;
  550. } else {
  551. return FALSE;
  552. }
  553. } else {
  554. $status = drupal_write_record('tripal_bulk_loader_constants',$record);
  555. if ($status) {
  556. return $record;
  557. } else {
  558. return FALSE;
  559. }
  560. }
  561. }
  562. ///////////////////////////////////////////////////////////
  563. /**
  564. * Preprocessor function for the tripal_bulk_loader template
  565. */
  566. function tripal_bulk_loader_preprocess_tripal_bulk_loader_template (&$variables) {
  567. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  568. $template = db_fetch_object(db_query($sql, $variables['template_id']));
  569. $template->template_array = unserialize($template->template_array);
  570. $variables['template'] = $template;
  571. }
  572. /**
  573. * Implements hook_job_describe_args()
  574. * Specifically to make viewing past tripal jobs more readable for jobs registered by this module
  575. *
  576. * @params $callback
  577. * The callback passed into tripal_add_job()
  578. * @param $args
  579. * The arguements passed into tripal_add_job()
  580. * @return
  581. * An array where keys are the human readable headers describing each arguement
  582. * and the value is the aguement passed in after formatting
  583. */
  584. function tripal_bulk_loader_job_describe_args($callback,$args){
  585. $new_args = array();
  586. if($callback == 'tripal_bulk_loader_load_data'){
  587. //1st arg is the nid for a bulk loader node
  588. $node = node_load($args[0]);
  589. $new_args['Bulk Loading Job'] = l($node->title, 'node/'.$args[0]);
  590. return $new_args;
  591. }
  592. }