tripal_bulk_loader.module 26 KB

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