tripal_bulk_loader.module 17 KB

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