tripal_bulk_loader.module 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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_template_field_form','create_record',5,FALSE),
  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_template_field_form','create',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_template_field_form','edit',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. return $items;
  198. }
  199. function tblid_to_arg($arg, $map, $index) {
  200. if (preg_match('/^\d+$/', $arg)) {
  201. return $arg;
  202. }
  203. }
  204. /**
  205. * Implements hook_views_api()
  206. *
  207. * Purpose: Essentially this hook tells drupal that there is views support for
  208. * for this module which then includes tripal_views.views.inc where all the
  209. * views integration code is
  210. *
  211. * @ingroup tripal_views
  212. */
  213. function tripal_bulk_loader_views_api() {
  214. return array(
  215. 'api' => 3.0,
  216. );
  217. }
  218. /**
  219. * Implements hook_theme
  220. *
  221. * @ingroup tripal_bulk_loader
  222. */
  223. function tripal_bulk_loader_theme() {
  224. return array(
  225. 'node' => array(
  226. 'variables' => array('node' => NULL),
  227. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  228. ),
  229. 'tripal_bulk_loader_set_constants_form' => array(
  230. 'function' => 'theme_tripal_bulk_loader_set_constants_form',
  231. 'variables' => array('form' => NULL),
  232. ),
  233. 'tripal_bulk_loader_template' => array(
  234. 'variables' => array('template_id' => NULL),
  235. 'template' => 'tripal_bulk_loader_template',
  236. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  237. ),
  238. 'tripal_bulk_loader_modify_template_base_form' => array(
  239. 'arguments' => array('form' => NULL),
  240. 'template' => 'tripal_bulk_loader_modify_template_base_form',
  241. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  242. 'render element' => 'form'
  243. ),
  244. /**
  245. 'tripal_bulk_loader_edit_template_field_form' => array(
  246. 'variables' => array('form' => NULL),
  247. 'template' => 'tripal_bulk_loader_edit_template_field_form',
  248. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  249. ),
  250. 'tripal_bulk_loader_add_template_field_form' => array(
  251. 'variables' => array('form' => NULL),
  252. 'template' => 'tripal_bulk_loader_add_template_field_form',
  253. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  254. ),
  255. */
  256. 'tripal_bulk_loader_admin' => array(
  257. 'template' => 'tripal_bulk_loader_admin',
  258. 'variables' => array(NULL),
  259. 'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
  260. ),
  261. );
  262. }
  263. /**
  264. * Implement hook_access().
  265. *
  266. * This hook allows node modules to limit access to the node types they define.
  267. *
  268. * @param $op
  269. * The operation to be performed
  270. *
  271. * @param $node
  272. * The node on which the operation is to be performed, or, if it does not yet exist, the
  273. * type of node to be created
  274. *
  275. * @param $account
  276. * A user object representing the user for whom the operation is to be performed
  277. *
  278. * @return
  279. * If the permission for the specified operation is not set then return FALSE. If the
  280. * permission is set then return NULL as this allows other modules to disable
  281. * access. The only exception is when the $op == 'create'. We will always
  282. * return TRUE if the permission is set.
  283. * @ingroup tripal_bulk_loader
  284. */
  285. function tripal_bulk_loader_node_access($node, $op, $account) {
  286. if ($op == 'create') {
  287. if (!user_access('create tripal_bulk_loader', $account)) {
  288. return FALSE;
  289. }
  290. return TRUE;
  291. }
  292. if ($op == 'update') {
  293. if (!user_access('edit tripal_bulk_loader', $account)) {
  294. return FALSE;
  295. }
  296. }
  297. if ($op == 'delete') {
  298. if (!user_access('delete tripal_bulk_loader', $account)) {
  299. return FALSE;
  300. }
  301. }
  302. if ($op == 'view') {
  303. if (!user_access('access tripal_bulk_loader', $account)) {
  304. return FALSE;
  305. }
  306. }
  307. return NULL;
  308. }
  309. /**
  310. * Implements hook_perm
  311. *
  312. * @ingroup tripal_bulk_loader
  313. */
  314. function tripal_bulk_loader_permission() {
  315. return array(
  316. 'access tripal_bulk_loader',
  317. 'create tripal_bulk_loader',
  318. 'delete tripal_bulk_loader',
  319. 'edit tripal_bulk_loader',
  320. 'administer tripal_bulk_loader',
  321. );
  322. }
  323. //////////////////////////////////////////////////////////////////////////////////////////////
  324. // Node Functions
  325. //////////////////////////////////////////////////////////////////////////////////////////////
  326. /**
  327. * Implements hook_node_info
  328. *
  329. * @ingroup tripal_bulk_loader
  330. */
  331. function tripal_bulk_loader_node_info() {
  332. $nodes = array();
  333. $nodes['tripal_bulk_loader'] = array(
  334. 'name' => t('Bulk Loading Job'),
  335. 'base' => 'tripal_bulk_loader',
  336. 'description' => t('A bulk loader for inserting tab-delimited data into chado database'),
  337. 'has_title' => TRUE,
  338. 'has_body' => FALSE,
  339. 'locked' => TRUE
  340. );
  341. return $nodes;
  342. }
  343. /**
  344. * Implements node_form
  345. * Used to gather the extra details stored with a Bulk Loading Job Node
  346. *
  347. * @ingroup tripal_bulk_loader
  348. */
  349. function tripal_bulk_loader_form($node, $form_state) {
  350. $form = array();
  351. if (isset($form_state['values'])) {
  352. $node = $form_state['values'] + (array)$node;
  353. $node = (object) $node;
  354. }
  355. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  356. $results = db_query($sql)->execute();
  357. $templates = array();
  358. foreach ($results as $template) {
  359. $templates [$template->template_id] = $template->name;
  360. }
  361. if (!$templates) {
  362. $form['label'] = array(
  363. '#type' => 'item',
  364. '#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."),
  365. '#weight' => -10,
  366. );
  367. return $form;
  368. }
  369. $form['loader'] = array(
  370. '#type' => 'fieldset',
  371. '#title' => t('Basic Details'),
  372. );
  373. $form['loader']['loader_name'] = array(
  374. '#type' => 'textfield',
  375. '#title' => t('Loading Job Name'),
  376. '#weight' => -10,
  377. '#required' => TRUE,
  378. '#default_value' => $node->loader_name
  379. );
  380. $form['loader']['template_id'] = array(
  381. '#type' => 'select',
  382. '#title' => t('Template'),
  383. '#description' => t('Please specify a template for this loader'),
  384. '#options' => $templates,
  385. '#weight' => -9,
  386. '#required' => TRUE,
  387. '#default_value' => $node->template_id,
  388. );
  389. $form['loader']['file']= array(
  390. '#type' => 'textfield',
  391. '#title' => t('Data File'),
  392. '#description' => t('Please specify the data file to be loaded. This must be a tab-delimited text file with UNIX line endings.'),
  393. '#weight' => -8,
  394. '#default_value' => $node->file,
  395. '#maxlength' => 1024,
  396. );
  397. $form['loader']['has_header'] = array(
  398. '#type' => 'radios',
  399. '#title' => t('File has a Header'),
  400. '#options' => array( 1 => 'Yes', 2 => 'No'),
  401. '#weight' => -7,
  402. '#default_value' => $node->file_has_header,
  403. );
  404. $form['loader']['keep_track_inserted'] = array(
  405. '#type' => 'radios',
  406. '#title' => t('Keep track of inserted record IDs'),
  407. '#description' => t('This enables the ability to revert an entire loading job even if '
  408. .'it completed successfully. Furthermore, it displays the number of records '
  409. .'successfully inserted into each table.'),
  410. '#options' => array( 1 => 'Yes', 0 => 'No'),
  411. '#weight' => -7,
  412. '#default_value' => (isset($node->keep_track_inserted)) ? $node->keep_track_inserted : variable_get('tripal_bulk_loader_keep_track_inserted', FALSE),
  413. );
  414. return $form;
  415. }
  416. /**
  417. * Implements node_load
  418. *
  419. * @ingroup tripal_bulk_loader
  420. */
  421. function tripal_bulk_loader_load($node) {
  422. $sql = "SELECT * FROM {tripal_bulk_loader} WHERE nid = :nid";
  423. $node = db_query($sql, array(':nid' => $node->nid))->fetchObject();
  424. $node->title = 'Bulk Loading Job: ' . $node->loader_name;
  425. // Add job details
  426. $progress = tripal_bulk_loader_progess_file_get_progress($node->job_id);
  427. $sql = "SELECT * FROM {tripal_jobs} WHERE job_id=:job_id";
  428. $node->job = db_query($sql, array(':job_id' => $node->job_id))->fetchObject();
  429. // Add the loader template
  430. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
  431. $results = db_query($sql, array(':template' => $node->template_id))->fetchObject();
  432. $template = unserialize($results->template_array);
  433. $node->template = $results;
  434. $node->template->template_array = $template;
  435. // Add inserted records
  436. $sql = 'SELECT * FROM {tripal_bulk_loader_inserted} WHERE nid=:nid';
  437. $resource = db_query($sql, array(':nid' => $node->nid))->execute();
  438. foreach ($resource as $r) {
  439. $r->num_inserted = sizeof(preg_split('/,/', $r->ids_inserted));
  440. $node->inserted_records->{$r->table_inserted_into} = $r;
  441. }
  442. // Add exposed field list
  443. $node->exposed_fields = array();
  444. if ($template) {
  445. foreach ($template as $record_id => $record) {
  446. foreach ($record['fields'] as $field_id => $field) {
  447. if ($field['exposed']) {
  448. $node->exposed_fields[] = array(
  449. 'record_id' => $record_id,
  450. 'field_id' => $field_id,
  451. 'title' => $field['title'],
  452. );
  453. }
  454. }
  455. }
  456. if (empty($node->exposed_fields)) {
  457. $node->exposed_fields[] = array();
  458. }
  459. }
  460. // Add constants
  461. $sql = 'SELECT * FROM {tripal_bulk_loader_constants} WHERE nid=:nid ORDER BY group_id, record_id, field_id';
  462. $resource = db_query($sql, array(':nid' => $node->nid))->execute();
  463. foreach ($resource as $r) {
  464. $node->constants[$r->group_id][$r->record_id][$r->field_id] = array(
  465. 'constant_id' => $r->constant_id,
  466. 'group_id' => $r->group_id,
  467. 'chado_table' => $r->chado_table,
  468. 'chado_field' => $r->chado_field,
  469. 'record_id' => $r->record_id,
  470. 'field_id' => $r->field_id,
  471. 'value' => $r->value
  472. );
  473. }
  474. if (!$node->constants) {
  475. $node->constants[] = array();
  476. }
  477. return $node;
  478. }
  479. /**
  480. * Implements node_insert
  481. * Insert the data from the node form on Create content
  482. *
  483. * @ingroup tripal_bulk_loader
  484. */
  485. function tripal_bulk_loader_insert($node) {
  486. // Insert into tripal_bulk_loader
  487. $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)";
  488. db_query($sql,
  489. array(':nid' => $node->nid,
  490. ':loader' => $node->loader_name,
  491. ':template' => $node->template_id,
  492. ':file' => $node->file,
  493. ':header' => $node->has_header,
  494. ':status' => 'Initialized',
  495. ':ids' => $node->keep_track_inserted
  496. )
  497. )->execute();
  498. // Update title
  499. $node->title =$node->loader_name;
  500. drupal_write_record('node', $node, 'nid');
  501. drupal_write_record('node_revision', $node, 'nid');
  502. 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.'));
  503. }
  504. /**
  505. * Implements node_delete
  506. * Deletes the data when the delete button on the node form is clicked
  507. *
  508. * @ingroup tripal_bulk_loader
  509. */
  510. function tripal_bulk_loader_delete($node) {
  511. $sql = "DELETE FROM {tripal_bulk_loader} WHERE nid = :nid";
  512. db_query($sql, array(':nid' => $node->nid))->execute();
  513. }
  514. /**
  515. * Implements node_update
  516. * Updates the data submitted by the node form on edit
  517. *
  518. * @ingroup tripal_bulk_loader
  519. */
  520. function tripal_bulk_loader_update($node) {
  521. // Update tripal_bulk_loader
  522. $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";
  523. db_query($sql,
  524. array(':nid' => $node->nid,
  525. ':loader' => $node->loader_name,
  526. ':template' => $node->template_id,
  527. ':file' => $node->file,
  528. ':header' => $node->has_header,
  529. ':ids' => $node->keep_track_inserted,
  530. ':wherenid' => $node->nid
  531. )
  532. )->execute();
  533. // Add a job if the user want to load the data
  534. global $user;
  535. if ($node->job) {
  536. $job_args[0] =$node->loader_name;
  537. $job_args[1] = $node->template_id;
  538. $job_args[2] = $node->file;
  539. if (is_readable($node->file)) {
  540. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  541. tripal_add_job("Bulk Load: $fname", 'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  542. }
  543. else {
  544. drupal_set_message(t("Can not open %file. Job not scheduled.", array('%file' => $node->file)));
  545. }
  546. }
  547. }
  548. ///////////////////////////////////////////////////////////
  549. /**
  550. * Preprocessor function for the tripal_bulk_loader template
  551. *
  552. * @ingroup tripal_bulk_loader
  553. */
  554. function tripal_bulk_loader_preprocess_tripal_bulk_loader_template(&$variables) {
  555. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  556. $template = db_query($sql, array(':template' => $variables['template_id']))->fetchObject();
  557. $template->template_array = unserialize($template->template_array);
  558. $variables['template'] = $template;
  559. }
  560. /**
  561. * Get the progress of the current constant set from the progress file
  562. *
  563. * When transactions are used, database updates to drupal cannot be made. Thus a separate
  564. * method to keep track of progress was implemented: save a period to the file for each
  565. * record successfully inserted; each line in the file represents a processed line.
  566. *
  567. * @param $job_id
  568. * The id of the tripal job to check the progress of
  569. * @param $node
  570. * The tripal_bulk_loader node associated with the job
  571. *
  572. * @return
  573. * An array with the following keys:
  574. * num_lines = the number of lines in the file processed so far
  575. * total_lines = the total number of lines in the input file
  576. * percent_file = the percent the input file has been loaded
  577. * num_records = the number of records successfully inserted
  578. *
  579. * @ingroup tripal_bulk_loader
  580. */
  581. function tripal_bulk_loader_progess_file_get_progress($job_id, $update_progress = TRUE) {
  582. $filename = '/tmp/tripal_bulk_loader_progress-' . $job_id . '.out';
  583. if (!file_exists($filename)) {
  584. return (object) array();
  585. }
  586. $num_lines = trim(`wc --lines < $filename`);
  587. $num_records = trim(`grep -o "." $filename | wc --lines`);
  588. $job = db_query("SELECT j.*, b.file, b.file_has_header, c.num as num_constant_sets
  589. FROM {tripal_jobs} j
  590. LEFT JOIN {tripal_bulk_loader} b ON b.job_id=j.job_id
  591. LEFT JOIN (
  592. SELECT nid, count(distinct(group_id)) as num
  593. FROM {tripal_bulk_loader_constants}
  594. GROUP BY nid
  595. ) c ON c.nid=b.nid
  596. WHERE j.job_id=:job", array(':job' =>$job_id))->execute();
  597. if ($job->num_constant_sets) {
  598. $num_constant_sets_loaded = round($job->progress / (100 / $job->num_constant_sets), 4);
  599. // If the next constant set has started loading
  600. if ($job->num_constant_sets != $num_constant_sets_loaded) {
  601. // total lines in input file
  602. $total_lines = trim(`wc --lines < $job->file`);
  603. if ($job->file_has_header) {
  604. $total_lines--;
  605. }
  606. // percent of the current constant set loaded
  607. $percent = round($num_lines/$total_lines * 100, 2);
  608. // percent of the total job = (<# fully loaded constant sets> * 100 )
  609. // + <percent of current constant set>
  610. // / <total number of constant sets>
  611. $total_percent = (($num_constant_sets_loaded * 100) + $percent) / $job->num_constant_sets;
  612. // update the progress of the job
  613. if ($update_progress AND ($percent != 0 OR $percent != 100)) {
  614. tripal_job_set_progress($job_id, round($total_percent, 0));
  615. }
  616. }
  617. }
  618. return (object) array(
  619. 'num_lines' => $num_lines,
  620. 'total_lines' => $total_lines,
  621. 'percent_file' => $percent,
  622. 'num_constant_sets_loaded' => $num_constant_sets_loaded,
  623. 'total_percent' => $total_percent,
  624. 'num_records' => $num_records
  625. );
  626. }
  627. /**
  628. * Implements hook_job_describe_args()
  629. * Specifically to make viewing past tripal jobs more readable for jobs registered by this module
  630. *
  631. * @params $callback
  632. * The callback passed into tripal_add_job()
  633. * @param $args
  634. * The arguements passed into tripal_add_job()
  635. * @return
  636. * An array where keys are the human readable headers describing each arguement
  637. * and the value is the aguement passed in after formatting
  638. *
  639. * @ingroup tripal_bulk_loader
  640. */
  641. function tripal_bulk_loader_job_describe_args($callback, $args) {
  642. $new_args = array();
  643. if ($callback == 'tripal_bulk_loader_load_data') {
  644. //1st arg is the nid for a bulk loader node
  645. $node = node_load($args[0]);
  646. $new_args['Bulk Loading Job'] = l($node->title, 'node/' . $args[0]);
  647. return $new_args;
  648. }
  649. }
  650. /**
  651. * Implements hook_coder_ignore().
  652. * Defines the path to the file (tripal_bulk_loader.coder_ignores.txt) where ignore rules for coder are stored
  653. *
  654. * @ingroup tripal_bulk_loader
  655. */
  656. function tripal_bulk_loader_coder_ignore() {
  657. return array(
  658. 'path' => drupal_get_path('module', 'tripal_bulk_loader'),
  659. 'line prefix' => drupal_get_path('module', 'tripal_bulk_loader'),
  660. );
  661. }