tripal_bulk_loader.module 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. <?php
  2. /**
  3. * @file
  4. * The functions for the Tripal bulk loader.
  5. *
  6. * @defgroup tripal_bulk_loader Tripal Bulk Loader Module
  7. * @ingroup tripal_modules
  8. */
  9. include('includes/tripal_bulk_loader.loader.inc');
  10. include('includes/tripal_bulk_loader.constants.inc');
  11. // Administration
  12. include('includes/tripal_bulk_loader.admin.inc');
  13. include('includes/tripal_bulk_loader.admin.templates.inc');
  14. // API
  15. include('api/tripal_bulk_loader.api.templates.inc');
  16. /**
  17. * Implements hook_init
  18. * Used to add stylesheets and javascript files to the header
  19. *
  20. * @ingroup tripal_bulk_loader
  21. */
  22. function tripal_bulk_loader_init() {
  23. // Add javascript and style sheet
  24. drupal_add_css(drupal_get_path('theme', 'tripal') . '/css/tripal_bulk_loader.css');
  25. drupal_add_js(drupal_get_path('theme', 'tripal') . '/js/tripal_bulk_loader.js');
  26. }
  27. /**
  28. * Implements hook_menu
  29. *
  30. * @ingroup tripal_bulk_loader
  31. */
  32. function tripal_bulk_loader_menu() {
  33. $items = array();
  34. // Bulk Loading Job Node
  35. $items['node/%node/constants/%/edit'] = array(
  36. 'title' => 'Edit Constant Set',
  37. 'description' => 'Edit a group of constants associated with the current bulk loader',
  38. 'page callback' => 'drupal_get_form',
  39. 'page arguments' => array('tripal_bulk_loader_edit_constant_set_form', 1, 3),
  40. 'access arguments' => array('administer tripal_bulk_loader'),
  41. 'type' => MENU_CALLBACK,
  42. );
  43. $items['node/%node/constants/%/delete'] = array(
  44. 'title' => 'Delete Constant Set',
  45. 'description' => 'Delete a group of constants associated with the current bulk loader',
  46. 'page callback' => 'drupal_get_form',
  47. 'page arguments' => array('tripal_bulk_loader_delete_constant_set_form', 1, 3),
  48. 'access arguments' => array('administer tripal_bulk_loader'),
  49. 'type' => MENU_CALLBACK,
  50. );
  51. // Admin pages -----------------
  52. $items['admin/tripal/loaders/bulk'] = array(
  53. 'title' => 'Bulk Loader',
  54. 'description' => 'Templates for loading tab-delimited data',
  55. 'access arguments' => array('administer tripal_bulk_loader'),
  56. 'type' => MENU_NORMAL_ITEM,
  57. );
  58. $items['admin/tripal/loaders/bulk/configure'] = array(
  59. 'title' => 'Configure',
  60. 'description' => 'Configuration of global options related to bulk loading jobs',
  61. 'page callback' => 'drupal_get_form',
  62. 'page arguments' => array('tripal_bulk_loader_configuration_form'),
  63. 'access arguments' => array('administer tripal_bulk_loader'),
  64. 'weight' => 8,
  65. 'type' => MENU_NORMAL_ITEM,
  66. );
  67. $items['admin/tripal/loaders/bulk/help'] = array(
  68. 'title' => 'Help',
  69. 'description' => "A description of the Tripal Bulk Loader module including a short description of it's usage.",
  70. 'page callback' => 'tripal_bulk_loader_admin_manage_templates',
  71. 'access arguments' => array('administer tripal_bulk_loader'),
  72. 'weight' => 10,
  73. 'type' => MENU_NORMAL_ITEM,
  74. );
  75. $items['admin/tripal/loaders/bulk/jobs'] = array(
  76. 'title' => 'Bulk Loader Jobs',
  77. 'description' => 'Listing of Bulk Loading Jobs',
  78. 'page callback' => 'tripal_bulk_loader_admin_jobs',
  79. 'access arguments' => array('administer tripal_bulk_loader'),
  80. 'weight' => -10,
  81. 'type' => MENU_NORMAL_ITEM,
  82. );
  83. // Create/Edit Template --------
  84. $items['admin/tripal/loaders/bulk/template/create'] = array(
  85. 'title' => 'Create Template',
  86. 'description' => 'Create loader template for loading tab-delimited data',
  87. 'page callback' => 'drupal_get_form',
  88. 'page arguments' => array('tripal_bulk_loader_modify_template_base_form', 'create'),
  89. 'access arguments' => array('administer tripal_bulk_loader'),
  90. 'weight' => -8,
  91. 'type' => MENU_NORMAL_ITEM,
  92. );
  93. $items['admin/tripal/loaders/bulk/template/%tblid/edit'] = array(
  94. 'title' => 'Edit Template',
  95. 'description' => 'Edit loader template for loading tab-delimited data',
  96. 'page callback' => 'drupal_get_form',
  97. 'page arguments' => array('tripal_bulk_loader_modify_template_base_form', 'edit',5),
  98. 'access arguments' => array('administer tripal_bulk_loader'),
  99. 'type' => MENU_CALLBACK,
  100. );
  101. $items['admin/tripal/loaders/bulk/template/edit'] = array(
  102. 'title' => 'Edit Template',
  103. 'description' => 'Edit loader template for loading tab-delimited data',
  104. 'page callback' => 'drupal_get_form',
  105. 'page arguments' => array('tripal_bulk_loader_modify_template_base_form', 'edit',FALSE),
  106. 'access arguments' => array('administer tripal_bulk_loader'),
  107. 'weight' => -6,
  108. 'type' => MENU_NORMAL_ITEM,
  109. );
  110. // Add/Edit Record -----
  111. $items['admin/tripal/loaders/bulk/template/%tblid/add_record'] = array(
  112. 'title' => 'Add Template Field',
  113. 'description' => 'Add a template field to an existing tripal bulk loader template.',
  114. 'page callback' => 'drupal_get_form',
  115. 'page arguments' => array('tripal_bulk_loader_add_template_field_form',5),
  116. 'access arguments' => array('administer tripal_bulk_loader'),
  117. 'type' => MENU_CALLBACK,
  118. );
  119. $items['admin/tripal/loaders/bulk/template/%tblid/edit_record/%tblid'] = array(
  120. 'title' => 'Edit Template Record',
  121. 'description' => 'Edit a record in an existing tripal bulk loader template.',
  122. 'page callback' => 'drupal_get_form',
  123. 'page arguments' => array('tripal_bulk_loader_edit_template_record_form',5,7),
  124. 'access arguments' => array('administer tripal_bulk_loader'),
  125. 'type' => MENU_CALLBACK,
  126. );
  127. $items['admin/tripal/loaders/bulk/template/%tblid/delete_record/%tblid'] = array(
  128. 'title' => 'Delete Template Record',
  129. 'description' => 'Delete a record in an existing tripal bulk loader template.',
  130. 'page callback' => 'drupal_get_form',
  131. 'page arguments' => array('tripal_bulk_loader_delete_template_record_form',5,7),
  132. 'access arguments' => array('administer tripal_bulk_loader'),
  133. 'type' => MENU_CALLBACK,
  134. );
  135. $items['admin/tripal/loaders/bulk/template/%tblid/duplicate_record/%tblid'] = array(
  136. 'title' => 'Duplicate Template Record',
  137. 'description' => 'Duplicate a record in an existing tripal bulk loader template.',
  138. 'page callback' => 'drupal_get_form',
  139. 'page arguments' => array('tripal_bulk_loader_duplicate_template_record_form',5,7),
  140. 'access arguments' => array('administer tripal_bulk_loader'),
  141. 'type' => MENU_CALLBACK,
  142. );
  143. // Add/Edit Field ------
  144. $items['admin/tripal/loaders/bulk/template/%tblid/add_field/%tblid'] = array(
  145. 'title' => 'Add Template Field',
  146. 'description' => 'Add a template field to an existing tripal bulk loader template.',
  147. 'page callback' => 'drupal_get_form',
  148. 'page arguments' => array('tripal_bulk_loader_add_template_field_form',5,7),
  149. 'access arguments' => array('administer tripal_bulk_loader'),
  150. 'type' => MENU_CALLBACK,
  151. );
  152. $items['admin/tripal/loaders/bulk/template/%tblid/edit_field/%tblid/%tblid'] = array(
  153. 'title' => 'Edit Template Field',
  154. 'description' => 'Edit an existing field from a tripal bulk loader template.',
  155. 'page callback' => 'drupal_get_form',
  156. 'page arguments' => array('tripal_bulk_loader_edit_template_field_form',5,7,8),
  157. 'access arguments' => array('administer tripal_bulk_loader'),
  158. 'type' => MENU_CALLBACK,
  159. );
  160. $items['admin/tripal/loaders/bulk/template/%tblid/delete_field/%tblid/%tblid'] = array(
  161. 'title' => 'Delete Template Field',
  162. 'description' => 'Delete an existing field from a tripal bulk loader template.',
  163. 'page callback' => 'drupal_get_form',
  164. 'page arguments' => array('tripal_bulk_loader_delete_template_field_form',5,7,8),
  165. 'access arguments' => array('administer tripal_bulk_loader'),
  166. 'type' => MENU_CALLBACK,
  167. );
  168. // Delete Template -----
  169. $items['admin/tripal/loaders/bulk/manage_templates/delete'] = array(
  170. 'title' => 'Delete Template',
  171. 'description' => 'Delete bulk loader template',
  172. 'page callback' => 'drupal_get_form',
  173. 'page arguments' => array('tripal_bulk_loader_delete_template_base_form'),
  174. 'access arguments' => array('administer tripal_bulk_loader'),
  175. 'weight' => -4,
  176. 'type' => MENU_NORMAL_ITEM,
  177. );
  178. // Import/Export ---------
  179. $items['admin/tripal/loaders/bulk/manage_templates/import'] = array(
  180. 'title' => 'Import Template',
  181. 'description' => 'Import Loaders',
  182. 'page callback' => 'drupal_get_form',
  183. 'page arguments' => array('tripal_bulk_loader_import_export_template_form', 'import'),
  184. 'access arguments' => array('administer tripal_bulk_loader'),
  185. 'weight' => 2,
  186. 'type' => MENU_NORMAL_ITEM,
  187. );
  188. $items['admin/tripal/loaders/bulk/manage_templates/export'] = array(
  189. 'title' => 'Export Template',
  190. 'description' => 'Export Loaders',
  191. 'page callback' => 'drupal_get_form',
  192. 'page arguments' => array('tripal_bulk_loader_import_export_template_form', 'export'),
  193. 'access arguments' => array('administer tripal_bulk_loader'),
  194. 'weight' => 4,
  195. 'type' => MENU_NORMAL_ITEM,
  196. );
  197. // AHAH ---------
  198. $items['admin/tripal/loaders/bulk/edit_field_ahah'] = array(
  199. 'page callback' => 'tripal_bulk_loader_edit_field_ahah',
  200. 'access arguments' => array('administer tripal_bulk_loader'),
  201. 'type' => MENU_CALLBACK,
  202. );
  203. return $items;
  204. }
  205. function tblid_to_arg($arg, $map, $index) {
  206. if (preg_match('/^\d+$/', $arg)) {
  207. return $arg;
  208. }
  209. }
  210. /**
  211. * Implements hook_theme
  212. *
  213. * @ingroup tripal_bulk_loader
  214. */
  215. function tripal_bulk_loader_theme() {
  216. return array(
  217. 'node' => array(
  218. 'variables' => array('node' => NULL),
  219. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  220. ),
  221. 'tripal_bulk_loader_set_constants_form' => array(
  222. 'function' => 'theme_tripal_bulk_loader_set_constants_form',
  223. 'variables' => array('form' => NULL),
  224. ),
  225. 'tripal_bulk_loader_template' => array(
  226. 'variables' => array('template_id' => NULL),
  227. 'template' => 'tripal_bulk_loader_template',
  228. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  229. ),
  230. 'tripal_bulk_loader_modify_template_base_form' => array(
  231. 'arguments' => array('form' => NULL),
  232. 'template' => 'tripal_bulk_loader_modify_template_base_form',
  233. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  234. 'render element' => 'form'
  235. ),
  236. /**
  237. 'tripal_bulk_loader_edit_template_field_form' => array(
  238. 'variables' => array('form' => NULL),
  239. 'template' => 'tripal_bulk_loader_edit_template_field_form',
  240. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  241. ),
  242. 'tripal_bulk_loader_add_template_field_form' => array(
  243. 'variables' => array('form' => NULL),
  244. 'template' => 'tripal_bulk_loader_add_template_field_form',
  245. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  246. ),
  247. */
  248. 'tripal_bulk_loader_admin' => array(
  249. 'template' => 'tripal_bulk_loader_admin',
  250. 'variables' => array(NULL),
  251. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  252. ),
  253. );
  254. }
  255. /**
  256. * Implement hook_access().
  257. *
  258. * This hook allows node modules to limit access to the node types they define.
  259. *
  260. * @param $op
  261. * The operation to be performed
  262. *
  263. * @param $node
  264. * The node on which the operation is to be performed, or, if it does not yet exist, the
  265. * type of node to be created
  266. *
  267. * @param $account
  268. * A user object representing the user for whom the operation is to be performed
  269. *
  270. * @return
  271. * If the permission for the specified operation is not set then return FALSE. If the
  272. * permission is set then return NULL as this allows other modules to disable
  273. * access. The only exception is when the $op == 'create'. We will always
  274. * return TRUE if the permission is set.
  275. * @ingroup tripal_bulk_loader
  276. */
  277. function tripal_bulk_loader_node_access($node, $op, $account) {
  278. if ($op == 'create') {
  279. if (!user_access('create tripal_bulk_loader', $account)) {
  280. return FALSE;
  281. }
  282. return TRUE;
  283. }
  284. if ($op == 'update') {
  285. if (!user_access('edit tripal_bulk_loader', $account)) {
  286. return FALSE;
  287. }
  288. }
  289. if ($op == 'delete') {
  290. if (!user_access('delete tripal_bulk_loader', $account)) {
  291. return FALSE;
  292. }
  293. }
  294. if ($op == 'view') {
  295. if (!user_access('access tripal_bulk_loader', $account)) {
  296. return FALSE;
  297. }
  298. }
  299. return NULL;
  300. }
  301. /**
  302. * Implements hook_perm
  303. *
  304. * @ingroup tripal_bulk_loader
  305. */
  306. function tripal_bulk_loader_permission() {
  307. return array(
  308. 'access tripal_bulk_loader',
  309. 'create tripal_bulk_loader',
  310. 'delete tripal_bulk_loader',
  311. 'edit tripal_bulk_loader',
  312. 'administer tripal_bulk_loader',
  313. );
  314. }
  315. //////////////////////////////////////////////////////////////////////////////////////////////
  316. // Node Functions
  317. //////////////////////////////////////////////////////////////////////////////////////////////
  318. /**
  319. * Implements hook_node_info
  320. *
  321. * @ingroup tripal_bulk_loader
  322. */
  323. function tripal_bulk_loader_node_info() {
  324. $nodes = array();
  325. $nodes['tripal_bulk_loader'] = array(
  326. 'name' => t('Bulk Loading Job'),
  327. 'base' => 'tripal_bulk_loader',
  328. 'description' => t('A bulk loader for inserting tab-delimited data into chado database'),
  329. 'has_title' => TRUE,
  330. 'has_body' => FALSE,
  331. 'locked' => TRUE
  332. );
  333. return $nodes;
  334. }
  335. /**
  336. * Implements node_form
  337. * Used to gather the extra details stored with a Bulk Loading Job Node
  338. *
  339. * @ingroup tripal_bulk_loader
  340. */
  341. function tripal_bulk_loader_form($node, $form_state) {
  342. $form = array();
  343. if (isset($form_state['values'])) {
  344. $node = $form_state['values'] + (array)$node;
  345. $node = (object) $node;
  346. }
  347. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  348. $results = db_query($sql)->execute();
  349. $templates = array();
  350. foreach ($results as $template) {
  351. $templates [$template->template_id] = $template->name;
  352. }
  353. if (!$templates) {
  354. $form['label'] = array(
  355. '#type' => 'item',
  356. '#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."),
  357. '#weight' => -10,
  358. );
  359. return $form;
  360. }
  361. $form['loader'] = array(
  362. '#type' => 'fieldset',
  363. '#title' => t('Basic Details'),
  364. );
  365. $form['loader']['loader_name'] = array(
  366. '#type' => 'textfield',
  367. '#title' => t('Loading Job Name'),
  368. '#weight' => -10,
  369. '#required' => TRUE,
  370. '#default_value' => $node->loader_name
  371. );
  372. $form['loader']['template_id'] = array(
  373. '#type' => 'select',
  374. '#title' => t('Template'),
  375. '#description' => t('Please specify a template for this loader'),
  376. '#options' => $templates,
  377. '#weight' => -9,
  378. '#required' => TRUE,
  379. '#default_value' => $node->template_id,
  380. );
  381. $form['loader']['file']= array(
  382. '#type' => 'textfield',
  383. '#title' => t('Data File'),
  384. '#description' => t('Please specify the data file to be loaded. This must be a tab-delimited text file with UNIX line endings.'),
  385. '#weight' => -8,
  386. '#default_value' => $node->file,
  387. '#maxlength' => 1024,
  388. );
  389. $form['loader']['has_header'] = array(
  390. '#type' => 'radios',
  391. '#title' => t('File has a Header'),
  392. '#options' => array( 1 => 'Yes', 2 => 'No'),
  393. '#weight' => -7,
  394. '#default_value' => $node->file_has_header,
  395. );
  396. $form['loader']['keep_track_inserted'] = array(
  397. '#type' => 'radios',
  398. '#title' => t('Keep track of inserted record IDs'),
  399. '#description' => t('This enables the ability to revert an entire loading job even if '
  400. .'it completed successfully. Furthermore, it displays the number of records '
  401. .'successfully inserted into each table.'),
  402. '#options' => array( 1 => 'Yes', 0 => 'No'),
  403. '#weight' => -7,
  404. '#default_value' => (isset($node->keep_track_inserted)) ? $node->keep_track_inserted : variable_get('tripal_bulk_loader_keep_track_inserted', FALSE),
  405. );
  406. return $form;
  407. }
  408. /**
  409. * Implements node_load
  410. *
  411. * @ingroup tripal_bulk_loader
  412. */
  413. function tripal_bulk_loader_load($node) {
  414. $sql = "SELECT * FROM {tripal_bulk_loader} WHERE nid = :nid";
  415. $node = db_query($sql, array(':nid' => $node->nid))->fetchObject();
  416. $node->title = 'Bulk Loading Job: ' . $node->loader_name;
  417. // Add job details
  418. $progress = tripal_bulk_loader_progess_file_get_progress($node->job_id);
  419. $sql = "SELECT * FROM {tripal_jobs} WHERE job_id=:job_id";
  420. $node->job = db_query($sql, array(':job_id' => $node->job_id))->fetchObject();
  421. // Add the loader template
  422. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
  423. $results = db_query($sql, array(':template' => $node->template_id))->fetchObject();
  424. $template = unserialize($results->template_array);
  425. $node->template = $results;
  426. $node->template->template_array = $template;
  427. // Add inserted records
  428. $sql = 'SELECT * FROM {tripal_bulk_loader_inserted} WHERE nid=:nid';
  429. $resource = db_query($sql, array(':nid' => $node->nid))->execute();
  430. foreach ($resource as $r) {
  431. $r->num_inserted = sizeof(preg_split('/,/', $r->ids_inserted));
  432. $node->inserted_records->{$r->table_inserted_into} = $r;
  433. }
  434. // Add exposed field list
  435. $node->exposed_fields = array();
  436. if ($template) {
  437. foreach ($template as $record_id => $record) {
  438. foreach ($record['fields'] as $field_id => $field) {
  439. if ($field['exposed']) {
  440. $node->exposed_fields[] = array(
  441. 'record_id' => $record_id,
  442. 'field_id' => $field_id,
  443. 'title' => $field['title'],
  444. );
  445. }
  446. }
  447. }
  448. if (empty($node->exposed_fields)) {
  449. $node->exposed_fields[] = array();
  450. }
  451. }
  452. // Add constants
  453. $sql = 'SELECT * FROM {tripal_bulk_loader_constants} WHERE nid=:nid ORDER BY group_id, record_id, field_id';
  454. $resource = db_query($sql, array(':nid' => $node->nid))->execute();
  455. foreach ($resource as $r) {
  456. $node->constants[$r->group_id][$r->record_id][$r->field_id] = array(
  457. 'constant_id' => $r->constant_id,
  458. 'group_id' => $r->group_id,
  459. 'chado_table' => $r->chado_table,
  460. 'chado_field' => $r->chado_field,
  461. 'record_id' => $r->record_id,
  462. 'field_id' => $r->field_id,
  463. 'value' => $r->value
  464. );
  465. }
  466. if (!$node->constants) {
  467. $node->constants[] = array();
  468. }
  469. return $node;
  470. }
  471. /**
  472. * Implements node_insert
  473. * Insert the data from the node form on Create content
  474. *
  475. * @ingroup tripal_bulk_loader
  476. */
  477. function tripal_bulk_loader_insert($node) {
  478. // Insert into tripal_bulk_loader
  479. $sql = "INSERT INTO {tripal_bulk_loader} (nid, loader_name, template_id, file, file_has_header, job_status, keep_track_inserted) VALUES (:nid, :loader, :template, :file, :header, :status, :ids)";
  480. db_query($sql,
  481. array(':nid' => $node->nid,
  482. ':loader' => $node->loader_name,
  483. ':template' => $node->template_id,
  484. ':file' => $node->file,
  485. ':header' => $node->has_header,
  486. ':status' => 'Initialized',
  487. ':ids' => $node->keep_track_inserted
  488. )
  489. )->execute();
  490. // Update title
  491. $node->title =$node->loader_name;
  492. drupal_write_record('node', $node, 'nid');
  493. drupal_write_record('node_revision', $node, 'nid');
  494. 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.'));
  495. }
  496. /**
  497. * Implements node_delete
  498. * Deletes the data when the delete button on the node form is clicked
  499. *
  500. * @ingroup tripal_bulk_loader
  501. */
  502. function tripal_bulk_loader_delete($node) {
  503. $sql = "DELETE FROM {tripal_bulk_loader} WHERE nid = :nid";
  504. db_query($sql, array(':nid' => $node->nid))->execute();
  505. }
  506. /**
  507. * Implements node_update
  508. * Updates the data submitted by the node form on edit
  509. *
  510. * @ingroup tripal_bulk_loader
  511. */
  512. function tripal_bulk_loader_update($node) {
  513. // Update tripal_bulk_loader
  514. $sql = "UPDATE {tripal_bulk_loader} SET nid = :nid, loader_name = :loader, template_id = :template, file = :file, file_has_header = :header, keep_track_inserted = :ids WHERE nid = :wherenid";
  515. db_query($sql,
  516. array(':nid' => $node->nid,
  517. ':loader' => $node->loader_name,
  518. ':template' => $node->template_id,
  519. ':file' => $node->file,
  520. ':header' => $node->has_header,
  521. ':ids' => $node->keep_track_inserted,
  522. ':wherenid' => $node->nid
  523. )
  524. )->execute();
  525. // Add a job if the user want to load the data
  526. global $user;
  527. if ($node->job) {
  528. $job_args[0] =$node->loader_name;
  529. $job_args[1] = $node->template_id;
  530. $job_args[2] = $node->file;
  531. if (is_readable($node->file)) {
  532. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  533. tripal_add_job("Bulk Load: $fname", 'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  534. }
  535. else {
  536. drupal_set_message(t("Can not open %file. Job not scheduled.", array('%file' => $node->file)));
  537. }
  538. }
  539. }
  540. ///////////////////////////////////////////////////////////
  541. /**
  542. * Preprocessor function for the tripal_bulk_loader template
  543. *
  544. * @ingroup tripal_bulk_loader
  545. */
  546. function tripal_bulk_loader_preprocess_tripal_bulk_loader_template(&$variables) {
  547. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  548. $template = db_query($sql, array(':template' => $variables['template_id']))->fetchObject();
  549. $template->template_array = unserialize($template->template_array);
  550. $variables['template'] = $template;
  551. }
  552. /**
  553. * Get the progress of the current constant set from the progress file
  554. *
  555. * When transactions are used, database updates to drupal cannot be made. Thus a separate
  556. * method to keep track of progress was implemented: save a period to the file for each
  557. * record successfully inserted; each line in the file represents a processed line.
  558. *
  559. * @param $job_id
  560. * The id of the tripal job to check the progress of
  561. * @param $node
  562. * The tripal_bulk_loader node associated with the job
  563. *
  564. * @return
  565. * An array with the following keys:
  566. * num_lines = the number of lines in the file processed so far
  567. * total_lines = the total number of lines in the input file
  568. * percent_file = the percent the input file has been loaded
  569. * num_records = the number of records successfully inserted
  570. *
  571. * @ingroup tripal_bulk_loader
  572. */
  573. function tripal_bulk_loader_progess_file_get_progress($job_id, $update_progress = TRUE) {
  574. $filename = '/tmp/tripal_bulk_loader_progress-' . $job_id . '.out';
  575. if (!file_exists($filename)) {
  576. return (object) array();
  577. }
  578. $num_lines = trim(`wc --lines < $filename`);
  579. $num_records = trim(`grep -o "." $filename | wc --lines`);
  580. $job = db_query("SELECT j.*, b.file, b.file_has_header, c.num as num_constant_sets
  581. FROM {tripal_jobs} j
  582. LEFT JOIN {tripal_bulk_loader} b ON b.job_id=j.job_id
  583. LEFT JOIN (
  584. SELECT nid, count(distinct(group_id)) as num
  585. FROM {tripal_bulk_loader_constants}
  586. GROUP BY nid
  587. ) c ON c.nid=b.nid
  588. WHERE j.job_id=:job", array(':job' =>$job_id))->execute();
  589. if ($job->num_constant_sets) {
  590. $num_constant_sets_loaded = round($job->progress / (100 / $job->num_constant_sets), 4);
  591. // If the next constant set has started loading
  592. if ($job->num_constant_sets != $num_constant_sets_loaded) {
  593. // total lines in input file
  594. $total_lines = trim(`wc --lines < $job->file`);
  595. if ($job->file_has_header) {
  596. $total_lines--;
  597. }
  598. // percent of the current constant set loaded
  599. $percent = round($num_lines/$total_lines * 100, 2);
  600. // percent of the total job = (<# fully loaded constant sets> * 100 )
  601. // + <percent of current constant set>
  602. // / <total number of constant sets>
  603. $total_percent = (($num_constant_sets_loaded * 100) + $percent) / $job->num_constant_sets;
  604. // update the progress of the job
  605. if ($update_progress AND ($percent != 0 OR $percent != 100)) {
  606. tripal_job_set_progress($job_id, round($total_percent, 0));
  607. }
  608. }
  609. }
  610. return (object) array(
  611. 'num_lines' => $num_lines,
  612. 'total_lines' => $total_lines,
  613. 'percent_file' => $percent,
  614. 'num_constant_sets_loaded' => $num_constant_sets_loaded,
  615. 'total_percent' => $total_percent,
  616. 'num_records' => $num_records
  617. );
  618. }
  619. /**
  620. * Implements hook_job_describe_args()
  621. * Specifically to make viewing past tripal jobs more readable for jobs registered by this module
  622. *
  623. * @params $callback
  624. * The callback passed into tripal_add_job()
  625. * @param $args
  626. * The arguements passed into tripal_add_job()
  627. * @return
  628. * An array where keys are the human readable headers describing each arguement
  629. * and the value is the aguement passed in after formatting
  630. *
  631. * @ingroup tripal_bulk_loader
  632. */
  633. function tripal_bulk_loader_job_describe_args($callback, $args) {
  634. $new_args = array();
  635. if ($callback == 'tripal_bulk_loader_load_data') {
  636. //1st arg is the nid for a bulk loader node
  637. $node = node_load($args[0]);
  638. $new_args['Bulk Loading Job'] = l($node->title, 'node/' . $args[0]);
  639. return $new_args;
  640. }
  641. }
  642. /**
  643. * Implements hook_coder_ignore().
  644. * Defines the path to the file (tripal_bulk_loader.coder_ignores.txt) where ignore rules for coder are stored
  645. *
  646. * @ingroup tripal_bulk_loader
  647. */
  648. function tripal_bulk_loader_coder_ignore() {
  649. return array(
  650. 'path' => drupal_get_path('module', 'tripal_bulk_loader'),
  651. 'line prefix' => drupal_get_path('module', 'tripal_bulk_loader'),
  652. );
  653. }