tripal_bulk_loader.module 27 KB

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