tripal_bulk_loader.module 26 KB

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