tripal_bulk_loader.module 14 KB

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