tripal_bulk_loader.module 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <?php
  2. include('tripal_bulk_loader.loader.inc');
  3. include('tripal_bulk_loader.constants.inc');
  4. /**
  5. * Implements hook_init
  6. * Used to add stylesheets and javascript files to the header
  7. */
  8. function tripal_bulk_loader_init() {
  9. // Add javascript and style sheet
  10. drupal_add_css(drupal_get_path('theme', 'tripal') . '/css/tripal_bulk_loader.css');
  11. drupal_add_js(drupal_get_path('theme', 'tripal') . '/js/tripal_bulk_loader.js');
  12. }
  13. /**
  14. * Implements hook_menu
  15. */
  16. function tripal_bulk_loader_menu() {
  17. $items = array();
  18. // Show all loaders
  19. $items['tripal_bulk_loaders'] = array(
  20. 'title' => 'Tripal Bulk Loaders',
  21. 'description' => 'Tripal bulk loaders for loading tab-delimited file into chado database',
  22. 'page callback' => 'tripal_bulk_loader_list',
  23. 'access arguments' => array('access tripal_bulk_loader'),
  24. 'type' => MENU_NORMAL_ITEM,
  25. );
  26. // Bulk Loading Job Node
  27. $items['node/%node/constants/%/edit'] = array(
  28. 'title' => 'Edit Constant Set',
  29. 'description' => 'Edit a group of constants associated with the current bulk loader',
  30. 'page callback' => 'drupal_get_form',
  31. 'page arguments' => array('tripal_bulk_loader_edit_constant_set_form', 1, 3),
  32. 'access arguments' => array('administer site configuration'),
  33. 'type' => MENU_CALLBACK,
  34. 'file' => 'tripal_bulk_loader.constants.inc',
  35. );
  36. $items['node/%node/constants/%/delete'] = array(
  37. 'title' => 'Delete Constant Set',
  38. 'description' => 'Delete a group of constants associated with the current bulk loader',
  39. 'page callback' => 'drupal_get_form',
  40. 'page arguments' => array('tripal_bulk_loader_delete_constant_set_form', 1, 3),
  41. 'access arguments' => array('administer site configuration'),
  42. 'type' => MENU_CALLBACK,
  43. 'file' => 'tripal_bulk_loader.constants.inc',
  44. );
  45. // Admin page to create the template
  46. $items['admin/tripal/tripal_bulk_loader_template'] = array(
  47. 'title' => 'Bulk Loader Template',
  48. 'description' => 'Templates for loading tab-delimited data',
  49. 'page callback' => 'tripal_bulk_loader_admin_template',
  50. 'access arguments' => array('administer site configuration'),
  51. 'type' => MENU_NORMAL_ITEM,
  52. 'file' => 'tripal_bulk_loader.admin.inc',
  53. );
  54. // Create/Edit Template -------
  55. $items['admin/tripal/tripal_bulk_loader_template/create'] = array(
  56. 'title' => 'Create Bulk Loader Template',
  57. 'description' => 'Create loader template for loading tab-delimited data',
  58. 'page callback' => 'drupal_get_form',
  59. 'page arguments' => array('tripal_bulk_loader_modify_template_base_form', 'create'),
  60. 'access arguments' => array('administer site configuration'),
  61. 'type' => MENU_NORMAL_ITEM,
  62. 'file' => 'tripal_bulk_loader.admin.inc',
  63. );
  64. $items['admin/tripal/tripal_bulk_loader_template/edit'] = array(
  65. 'title' => 'Edit Bulk Loader Template',
  66. 'description' => 'Edit loader template for loading tab-delimited data',
  67. 'page callback' => 'drupal_get_form',
  68. 'page arguments' => array('tripal_bulk_loader_modify_template_base_form', 'edit'),
  69. 'access arguments' => array('administer site configuration'),
  70. 'type' => MENU_NORMAL_ITEM,
  71. 'file' => 'tripal_bulk_loader.admin.inc',
  72. );
  73. $items['admin/tripal/tripal_bulk_loader_template/edit_record'] = array(
  74. 'title' => 'Edit Template Record',
  75. 'description' => 'Edit a record in an existing tripal bulk loader template.',
  76. 'page callback' => 'drupal_get_form',
  77. 'page arguments' => array('tripal_bulk_loader_edit_template_record_form'),
  78. 'access arguments' => array('administer site configuration'),
  79. 'type' => MENU_CALLBACK,
  80. 'file' => 'tripal_bulk_loader.admin.inc',
  81. );
  82. $items['admin/tripal/tripal_bulk_loader_template/add_field'] = array(
  83. 'title' => 'Add Template Field',
  84. 'description' => 'Add a template field to an existing tripal bulk loader template.',
  85. 'page callback' => 'drupal_get_form',
  86. 'page arguments' => array('tripal_bulk_loader_add_template_field_form'),
  87. 'access arguments' => array('administer site configuration'),
  88. 'type' => MENU_CALLBACK,
  89. 'file' => 'tripal_bulk_loader.admin.inc',
  90. );
  91. $items['admin/tripal/tripal_bulk_loader_template/edit_field'] = array(
  92. 'title' => 'Edit Template Field',
  93. 'description' => 'Edit an existing field from a tripal bulk loader template.',
  94. 'page callback' => 'drupal_get_form',
  95. 'page arguments' => array('tripal_bulk_loader_edit_template_field_form'),
  96. 'access arguments' => array('administer site configuration'),
  97. 'type' => MENU_CALLBACK,
  98. 'file' => 'tripal_bulk_loader.admin.inc',
  99. );
  100. // Delete Template -----
  101. $items['admin/tripal/tripal_bulk_loader_template/delete'] = array(
  102. 'title' => 'Delete Bulk Loader Template',
  103. 'description' => 'Delete bulk loader template',
  104. 'page callback' => 'drupal_get_form',
  105. 'page arguments' => array('tripal_bulk_loader_delete_template_base_form'),
  106. 'access arguments' => array('administer site configuration'),
  107. 'type' => MENU_NORMAL_ITEM,
  108. 'file' => 'tripal_bulk_loader.admin.inc',
  109. );
  110. // Import/Export ---------
  111. $items['admin/tripal/tripal_bulk_loader_template/import'] = array(
  112. 'title' => 'Import Bulk Loader Template',
  113. 'description' => 'Import Loaders',
  114. 'page callback' => 'drupal_get_form',
  115. 'page arguments' => array('tripal_bulk_loader_import_export_template_form', 'import'),
  116. 'access arguments' => array('administer site configuration'),
  117. 'type' => MENU_NORMAL_ITEM,
  118. 'file' => 'tripal_bulk_loader.admin.inc',
  119. );
  120. $items['admin/tripal/tripal_bulk_loader_template/export'] = array(
  121. 'title' => 'Export Bulk Loader Template',
  122. 'description' => 'Export Loaders',
  123. 'page callback' => 'drupal_get_form',
  124. 'page arguments' => array('tripal_bulk_loader_import_export_template_form', 'export'),
  125. 'access arguments' => array('administer site configuration'),
  126. 'type' => MENU_NORMAL_ITEM,
  127. 'file' => 'tripal_bulk_loader.admin.inc',
  128. );
  129. // AHAH ---------
  130. $items['admin/tripal/tripal_bulk_loader_template/add_field_ahah'] = array(
  131. 'page callback' => 'tripal_bulk_loader_add_field_ahah',
  132. 'access arguments' => array('administer site configuration'),
  133. 'type' => MENU_CALLBACK,
  134. 'file' => 'tripal_bulk_loader.admin.inc',
  135. );
  136. $items['admin/tripal/tripal_bulk_loader_template/edit_field_ahah'] = array(
  137. 'page callback' => 'tripal_bulk_loader_edit_field_ahah',
  138. 'access arguments' => array('administer site configuration'),
  139. 'type' => MENU_CALLBACK,
  140. 'file' => 'tripal_bulk_loader.admin.inc',
  141. );
  142. return $items;
  143. }
  144. /**
  145. * Implements hook_theme
  146. */
  147. function tripal_bulk_loader_theme() {
  148. return array(
  149. 'tripal_bulk_loader_set_constants_form' => array(
  150. 'arguments' => array('form' => NULL),
  151. ),
  152. 'tripal_bulk_loader_template' => array(
  153. 'arguments' => array('template_id' => NULL),
  154. 'template' => 'tripal_bulk_loader_template'
  155. ),
  156. 'tripal_bulk_loader_modify_template_base_form' => array(
  157. 'arguments' => array('form' => NULL),
  158. 'template' => 'tripal_bulk_loader_modify_template_base_form',
  159. ),
  160. 'tripal_bulk_loader_edit_template_field_form' => array(
  161. 'arguments' => array('form' => NULL),
  162. 'template' => 'tripal_bulk_loader_edit_template_field_form',
  163. ),
  164. 'tripal_bulk_loader_add_template_field_form' => array(
  165. 'arguments' => array('form' => NULL),
  166. 'template' => 'tripal_bulk_loader_add_template_field_form',
  167. ),
  168. );
  169. }
  170. /**
  171. * Implements hook_access
  172. */
  173. function tripal_bulk_loader_access($op, $node, $account) {
  174. if ($op == 'create') {
  175. if (!user_access('create tripal_bulk_loader', $account)) {
  176. return FALSE;
  177. }
  178. }
  179. if ($op == 'update') {
  180. if (!user_access('edit tripal_bulk_loader', $account)) {
  181. return FALSE;
  182. }
  183. }
  184. if ($op == 'delete') {
  185. if (!user_access('delete tripal_bulk_loader', $account)) {
  186. return FALSE;
  187. }
  188. }
  189. if ($op == 'view') {
  190. if (!user_access('access tripal_bulk_loader', $account)) {
  191. return FALSE;
  192. }
  193. }
  194. return NULL;
  195. }
  196. /**
  197. * Implements hook_perm
  198. */
  199. function tripal_bulk_loader_perm() {
  200. return array(
  201. 'access tripal_bulk_loader',
  202. 'create tripal_bulk_loader',
  203. 'delete tripal_bulk_loader',
  204. 'edit tripal_bulk_loader',
  205. );
  206. }
  207. /**
  208. * Creates a listing page for all bulk loading jobs
  209. */
  210. function tripal_bulk_loader_list() {
  211. $num_results_per_page = 50;
  212. $output = '';
  213. $header = array('', 'Status', 'Loader', 'File');
  214. $rows = array();
  215. $query = 'SELECT * FROM {tripal_bulk_loader} l '
  216. .'LEFT JOIN {node} n ON n.nid = l.nid '
  217. .'LEFT JOIN {tripal_bulk_loader_template} t ON t.template_id = cast(l.template_id as integer)';
  218. $resource = pager_query($query, $num_results_per_page, 0, NULL);
  219. while ($r = db_fetch_object($resource)) {
  220. $row = array(
  221. l($r->title, 'node/' . $r->nid),
  222. $r->job_status,
  223. $r->name,
  224. $r->file
  225. );
  226. $rows[] = $row;
  227. }
  228. $output .= theme('table', $header, $rows);
  229. return $output;
  230. }
  231. //////////////////////////////////////////////////////////////////////////////////////////////
  232. // Node Functions
  233. //////////////////////////////////////////////////////////////////////////////////////////////
  234. /**
  235. * Implements hook_node_info
  236. */
  237. function tripal_bulk_loader_node_info() {
  238. $nodes = array();
  239. $nodes['tripal_bulk_loader'] = array(
  240. 'name' => t('Bulk Loading Job'),
  241. 'module' => 'tripal_bulk_loader',
  242. 'description' => t('A bulk loader for inserting tab-delimited data into chado database'),
  243. 'has_title' => TRUE,
  244. 'has_body' => FALSE,
  245. 'locked' => TRUE
  246. );
  247. return $nodes;
  248. }
  249. /**
  250. * Implements node_form
  251. * Used to gather the extra details stored with a Bulk Loading Job Node
  252. */
  253. function tripal_bulk_loader_form($node, $form_state) {
  254. $form = array();
  255. if (isset($form_state['values'])) {
  256. $node = $form_state['values'] + (array)$node;
  257. $node = (object) $node;
  258. }
  259. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  260. $results = db_query($sql);
  261. $templates = array();
  262. while ($template = db_fetch_object ($results)) {
  263. $templates [$template->template_id] = $template->name;
  264. }
  265. if (!$templates) {
  266. $form['label'] = array(
  267. '#type' => 'item',
  268. '#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."),
  269. '#weight' => -10,
  270. );
  271. return $form;
  272. }
  273. $form['loader'] = array(
  274. '#type' => 'fieldset',
  275. '#title' => t('Basic Details'),
  276. );
  277. $form['loader']['loader_name'] = array(
  278. '#type' => 'textfield',
  279. '#title' => t('Loading Job Name'),
  280. '#weight' => -10,
  281. '#required' => TRUE,
  282. '#default_value' => $node->loader_name
  283. );
  284. $form['loader']['template_id'] = array(
  285. '#type' => 'select',
  286. '#title' => t('Template'),
  287. '#description' => t('Please specify a template for this loader'),
  288. '#options' => $templates,
  289. '#weight' => -9,
  290. '#required' => TRUE,
  291. '#default_value' => $node->template_id,
  292. );
  293. $form['loader']['file']= array(
  294. '#type' => 'textfield',
  295. '#title' => t('Data File'),
  296. '#description' => t('Please specify the data file to be loaded.'),
  297. '#weight' => -8,
  298. '#default_value' => $node->file
  299. );
  300. $form['loader']['has_header'] = array(
  301. '#type' => 'radios',
  302. '#title' => t('File has a Header'),
  303. '#options' => array( 1 => 'Yes', 2 => 'No'),
  304. '#weight' => -7,
  305. '#default_value' => $node->file_has_header,
  306. );
  307. return $form;
  308. }
  309. /**
  310. * Implements node_load
  311. */
  312. function tripal_bulk_loader_load($node) {
  313. $sql = "SELECT * FROM {tripal_bulk_loader} WHERE nid = %d";
  314. $node = db_fetch_object(db_query($sql, $node->nid));
  315. $node->title = 'Bulk Loading Job: ' . $node->loader_name;
  316. // Add the loader template
  317. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  318. $results = db_fetch_object(db_query($sql, $node->template_id));
  319. $template = unserialize($results->template_array);
  320. $node->template = $results;
  321. $node->template->template_array = $template;
  322. // Add inserted records
  323. $sql = 'SELECT * FROM {tripal_bulk_loader_inserted} WHERE nid=%d';
  324. $resource = db_query($sql, $node->nid);
  325. while ($r = db_fetch_object($resource)) {
  326. $r->num_inserted = sizeof(preg_split('/,/', $r->ids_inserted));
  327. $node->inserted_records->{$r->table_inserted_into} = $r;
  328. }
  329. // Add exposed field list
  330. $node->exposed_fields = array();
  331. if ($template) {
  332. foreach ($template as $record_id => $record) {
  333. foreach ($record['fields'] as $field_id => $field) {
  334. if ($field['exposed']) {
  335. $node->exposed_fields[] = array(
  336. 'record_id' => $record_id,
  337. 'field_id' => $field_id,
  338. 'title' => $field['title'],
  339. );
  340. }
  341. }
  342. }
  343. if (empty($node->exposed_fields)) {
  344. $node->exposed_fields[] = array();
  345. }
  346. }
  347. // Add constants
  348. $sql = 'SELECT * FROM {tripal_bulk_loader_constants} WHERE nid=%d ORDER BY group_id, record_id, field_id';
  349. $resource = db_query($sql, $node->nid);
  350. while ($r = db_fetch_object($resource)) {
  351. $node->constants[$r->group_id][$r->record_id][$r->field_id] = array(
  352. 'constant_id' => $r->constant_id,
  353. 'group_id' => $r->group_id,
  354. 'chado_table' => $r->chado_table,
  355. 'chado_field' => $r->chado_field,
  356. 'record_id' => $r->record_id,
  357. 'field_id' => $r->field_id,
  358. 'value' => $r->value
  359. );
  360. }
  361. if (!$node->constants) {
  362. $node->constants[] = array();
  363. }
  364. return $node;
  365. }
  366. /**
  367. * Implements node_insert
  368. * Insert the data from the node form on Create content
  369. */
  370. function tripal_bulk_loader_insert($node) {
  371. // Insert into tripal_bulk_loader
  372. $sql = "INSERT INTO {tripal_bulk_loader} (nid, loader_name, template_id, file, file_has_header, job_status) VALUES (%d, '%s', %d, '%s', %d, '%s')";
  373. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->has_header, 'Initialized');
  374. // Update title
  375. $node->title =$node->loader_name;
  376. drupal_write_record('node', $node, 'nid');
  377. drupal_write_record('node_revision', $node, 'nid');
  378. drupal_set_message('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.');
  379. }
  380. /**
  381. * Implements node_delete
  382. * Deletes the data when the delete button on the node form is clicked
  383. */
  384. function tripal_bulk_loader_delete($node) {
  385. $sql = "DELETE FROM {tripal_bulk_loader} WHERE nid = %d";
  386. db_query($sql, $node->nid);
  387. }
  388. /**
  389. * Implements node_update
  390. * Updates the data submitted by the node form on edit
  391. */
  392. function tripal_bulk_loader_update($node) {
  393. // Update tripal_bulk_loader
  394. $sql = "UPDATE {tripal_bulk_loader} SET nid = %d, loader_name = '%s', template_id = %d, file = '%s', file_has_header = '%s' WHERE nid = %d";
  395. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->has_header, $node->nid);
  396. // Add a job if the user want to load the data
  397. global $user;
  398. if ($node->job) {
  399. $job_args[0] =$node->loader_name;
  400. $job_args[1] = $node->template_id;
  401. $job_args[2] = $node->file;
  402. if (is_readable($node->file)) {
  403. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  404. tripal_add_job("Bulk Load: $fname", 'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  405. }
  406. else {
  407. drupal_set_message(t("Can not open %file. Job not scheduled.", array('%file' => $node->file)));
  408. }
  409. }
  410. }
  411. ///////////////////////////////////////////////////////////
  412. /**
  413. * Preprocessor function for the tripal_bulk_loader template
  414. */
  415. function tripal_bulk_loader_preprocess_tripal_bulk_loader_template(&$variables) {
  416. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  417. $template = db_fetch_object(db_query($sql, $variables['template_id']));
  418. $template->template_array = unserialize($template->template_array);
  419. $variables['template'] = $template;
  420. }
  421. /**
  422. * Implements hook_job_describe_args()
  423. * Specifically to make viewing past tripal jobs more readable for jobs registered by this module
  424. *
  425. * @params $callback
  426. * The callback passed into tripal_add_job()
  427. * @param $args
  428. * The arguements passed into tripal_add_job()
  429. * @return
  430. * An array where keys are the human readable headers describing each arguement
  431. * and the value is the aguement passed in after formatting
  432. */
  433. function tripal_bulk_loader_job_describe_args($callback, $args) {
  434. $new_args = array();
  435. if ($callback == 'tripal_bulk_loader_load_data') {
  436. //1st arg is the nid for a bulk loader node
  437. $node = node_load($args[0]);
  438. $new_args['Bulk Loading Job'] = l($node->title, 'node/' . $args[0]);
  439. return $new_args;
  440. }
  441. }