tripal_bulk_loader.module 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php
  2. include('tripal_bulk_loader.loader.inc');
  3. include('tripal_bulk_loader.info_hooks.inc');
  4. /**
  5. * tripal_bulk_loader_init
  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. * tripal_bulk_loader_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. * tripal_bulk_loader_list
  126. */
  127. function tripal_bulk_loader_list () {
  128. return "Loaders";
  129. }
  130. /**
  131. * tripal_bulk_loader_access
  132. */
  133. function tripal_bulk_loader_access($op, $node, $account){
  134. if ($op == 'create') {
  135. return user_access('create tripal_bulk_loader', $account);
  136. }
  137. if ($op == 'update') {
  138. if (user_access('edit tripal_bulk_loader', $account)) {
  139. return TRUE;
  140. }
  141. }
  142. if ($op == 'delete') {
  143. if (user_access('delete tripal_bulk_loader', $account)) {
  144. return TRUE;
  145. }
  146. }
  147. if ($op == 'view') {
  148. if (user_access('access tripal_bulk_loader', $account)) {
  149. return TRUE;
  150. }
  151. }
  152. return FALSE;
  153. }
  154. /**
  155. * tripal_bulk_loader_perm
  156. */
  157. function tripal_bulk_loader_perm(){
  158. return array(
  159. 'access tripal_bulk_loader',
  160. 'create tripal_bulk_loader',
  161. 'delete tripal_bulk_loader',
  162. 'edit tripal_bulk_loader',
  163. );
  164. }
  165. /**
  166. * tripal_bulk_loader_node_info
  167. */
  168. function tripal_bulk_loader_node_info() {
  169. $nodes = array();
  170. $nodes['tripal_bulk_loader'] = array(
  171. 'name' => t('Bulk Loading Job'),
  172. 'module' => 'tripal_bulk_loader',
  173. 'description' => t('A bulk loader for inserting tab-delimited data into chado database'),
  174. 'has_title' => TRUE,
  175. 'has_body' => FALSE,
  176. 'locked' => TRUE
  177. );
  178. return $nodes;
  179. }
  180. /**
  181. * tripal_bulk_loader_form
  182. */
  183. function tripal_bulk_loader_form ($node){
  184. $form = array();
  185. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  186. $results = db_query($sql);
  187. $templates = array ();
  188. while ($template = db_fetch_object ($results)) {
  189. $templates [$template->template_id] = $template->name;
  190. }
  191. if (!$templates) {
  192. $form['label'] = array(
  193. '#type' => 'item',
  194. '#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."),
  195. '#weight' => -10,
  196. );
  197. return $form;
  198. }
  199. $form['loader_name'] = array(
  200. '#type' => 'textfield',
  201. '#title' => t('Loading Job Name'),
  202. '#weight' => -10,
  203. '#required' => TRUE,
  204. '#default_value' => $node->loader_name
  205. );
  206. $form['template_id'] = array(
  207. '#type' => 'select',
  208. '#title' => t('Template'),
  209. '#description' => t('Please specify a template for this loader'),
  210. '#options' => $templates,
  211. '#weight' => -9,
  212. '#required' => TRUE,
  213. '#default_value' => $node->template_id
  214. );
  215. $form['file']= array(
  216. '#type' => 'textfield',
  217. '#title' => t('Data File'),
  218. '#description' => t('Please specify the data file to be loaded.'),
  219. '#weight' => -8,
  220. '#default_value' => $node->file
  221. );
  222. $form['has_header'] = array(
  223. '#type' => 'radios',
  224. '#title' => 'File has a Header',
  225. '#options' => array( 't' => 'Yes', 'f' => 'No'),
  226. '#weight' => -7,
  227. '#default_value' => $node->file_has_header,
  228. );
  229. return $form;
  230. }
  231. /**
  232. * tripal_bulk_loader_theme
  233. */
  234. function tripal_bulk_loader_theme() {
  235. return array(
  236. 'tripal_bulk_loader_node_form' => array(
  237. 'arguments' => array('form' => NULL),
  238. ),
  239. 'tripal_bulk_loader_template' => array(
  240. 'arguments'=> array('template_id' => NULL),
  241. 'template' => 'tripal_bulk_loader_template'
  242. ),
  243. 'tripal_bulk_loader_modify_template_base_form' => array(
  244. 'arguments' => array('form' => NULL),
  245. 'template' => 'tripal_bulk_loader_modify_template_base_form',
  246. ),
  247. );
  248. }
  249. /**
  250. * theme_tripal_bulk_loader_node_form
  251. */
  252. function theme_tripal_bulk_loader_node_form($form) {
  253. // Do not show [Save] and [Preview] buttons if loader template is not available
  254. if($form['label']['#type'] == "item") {
  255. unset($form['buttons']);
  256. }
  257. return drupal_render($form);
  258. }
  259. /**
  260. * tripal_bulk_loader_load
  261. */
  262. function tripal_bulk_loader_load($node){
  263. $sql = "SELECT * FROM {tripal_bulk_loader} WHERE nid = %d";
  264. $node = db_fetch_object(db_query($sql, $node->nid));
  265. $node->title = 'Bulk Loading Job: '.$node->loader_name;
  266. // Add the loader template
  267. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  268. $results = db_fetch_object(db_query($sql, $node->template_id));
  269. $template = unserialize($results->template_array);
  270. $node->template = $results;
  271. $node->template->template_array = $template;
  272. return $node;
  273. }
  274. /**
  275. * tripal_bulk_loader_insert
  276. */
  277. function tripal_bulk_loader_insert ($node) {
  278. $sql = "INSERT INTO {tripal_bulk_loader} (nid, loader_name, template_id, file, file_has_header) VALUES (%d, '%s', %d, '%s', %d)";
  279. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->has_header);
  280. $node->title =$node->loader_name;
  281. drupal_write_record('node',$node,'nid');
  282. drupal_write_record('node_revision',$node,'nid');
  283. }
  284. /**
  285. * tripal_bulk_loader_delete
  286. */
  287. function tripal_bulk_loader_delete ($node) {
  288. $sql = "DELETE FROM {tripal_bulk_loader} WHERE nid = %d";
  289. db_query($sql, $node->nid);
  290. }
  291. /**
  292. * tripal_bulk_loader_update
  293. */
  294. function tripal_bulk_loader_update ($node) {
  295. $sql = "UPDATE {tripal_bulk_loader} SET nid = %d, loader_name = '%s', template_id = %d, file = '%s', file_has_header = '%s' WHERE nid = %d";
  296. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->has_header, $node->nid);
  297. // Add a job if the user want to load the data
  298. global $user;
  299. if($node->job) {
  300. $job_args[0] =$node->loader_name;
  301. $job_args[1] = $node->template_id;
  302. $job_args[2] = $node->file;
  303. if (is_readable($node->file)) {
  304. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  305. tripal_add_job("Bulk Load: $fname",'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  306. } else {
  307. drupal_set_message("Can not open $node->file. Job not scheduled.");
  308. }
  309. }
  310. }
  311. /**
  312. * Preprocessor function for the tripal_bulk_loader template
  313. */
  314. function tripal_bulk_loader_preprocess_tripal_bulk_loader_template (&$variables) {
  315. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  316. $template = db_fetch_object(db_query($sql, $variables['template_id']));
  317. $template->template_array = unserialize($template->template_array);
  318. $variables['template'] = $template;
  319. }
  320. /**
  321. * Implements hook_job_describe_args()
  322. * Specifically to make viewing past tripal jobs more readable for jobs registered by this module
  323. *
  324. * @params $callback
  325. * The callback passed into tripal_add_job()
  326. * @param $args
  327. * The arguements passed into tripal_add_job()
  328. * @return
  329. * An array where keys are the human readable headers describing each arguement
  330. * and the value is the aguement passed in after formatting
  331. */
  332. function tripal_bulk_loader_job_describe_args($callback,$args){
  333. $new_args = array();
  334. if($callback == 'tripal_bulk_loader_load_data'){
  335. //1st arg is the nid for a bulk loader node
  336. $node = node_load($args[0]);
  337. $new_args['Bulk Loading Job'] = l($node->title, 'node/'.$args[0]);
  338. return $new_args;
  339. }
  340. }