tripal_bulk_loader.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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' => 'Create loader template 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. $items['admin/tripal/tripal_bulk_loader_template/add'] = array(
  35. 'title' => 'Create Bulk Loader Template',
  36. 'description' => 'Create loader template for loading tab-delimited data',
  37. 'page callback' => 'drupal_get_form',
  38. 'page arguments' => array('tripal_bulk_loader_create_template_base_form'),
  39. 'access arguments' => array('administer site configuration'),
  40. 'type' => MENU_NORMAL_ITEM,
  41. 'file' => 'tripal_bulk_loader.admin.inc',
  42. );
  43. // Edit Template------
  44. $items['admin/tripal/tripal_bulk_loader_template/edit'] = array(
  45. 'title' => 'Edit Bulk Loader Template',
  46. 'description' => 'Create loader template for loading tab-delimited data',
  47. 'page callback' => 'drupal_get_form',
  48. 'page arguments' => array('tripal_bulk_loader_edit_template_base_form'),
  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/add_field'] = array(
  54. 'title' => 'Add Template Field',
  55. 'description' => 'Add a template field to an existing tripal bulk loader template.',
  56. 'page callback' => 'drupal_get_form',
  57. 'page arguments' => array('tripal_bulk_loader_add_template_field_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/edit/edit_field'] = array(
  63. 'title' => 'Edit Template Field',
  64. 'description' => 'Edit an existing field from a tripal bulk loader template.',
  65. 'page callback' => 'drupal_get_form',
  66. 'page arguments' => array('tripal_bulk_loader_edit_template_field_form'),
  67. 'access arguments' => array('administer site configuration'),
  68. 'type' => MENU_CALLBACK,
  69. 'file' => 'tripal_bulk_loader.admin.inc',
  70. );
  71. // Delete Template -----
  72. $items['admin/tripal/tripal_bulk_loader_template/delete'] = array(
  73. 'title' => 'Delete Bulk Loader Template',
  74. 'description' => 'Delete bulk loader template',
  75. 'page callback' => 'drupal_get_form',
  76. 'page arguments' => array('tripal_bulk_loader_delete_template_base_form'),
  77. 'access arguments' => array('administer site configuration'),
  78. 'type' => MENU_NORMAL_ITEM,
  79. 'file' => 'tripal_bulk_loader.admin.inc',
  80. );
  81. // AHAH ---------
  82. $items['admin/tripal/tripal_bulk_loader_template/add_field_ahah'] = array(
  83. 'page callback' => 'tripal_bulk_loader_add_field_ahah',
  84. 'access arguments' => array('administer site configuration'),
  85. 'type' => MENU_CALLBACK,
  86. 'file' => 'tripal_bulk_loader.admin.inc',
  87. );
  88. $items['admin/tripal/tripal_bulk_loader_template/edit_field_ahah'] = array(
  89. 'page callback' => 'tripal_bulk_loader_edit_field_ahah',
  90. 'access arguments' => array('administer site configuration'),
  91. 'type' => MENU_CALLBACK,
  92. 'file' => 'tripal_bulk_loader.admin.inc',
  93. );
  94. return $items;
  95. }
  96. /**
  97. * tripal_bulk_loader_list
  98. */
  99. function tripal_bulk_loader_list () {
  100. return "Loaders";
  101. }
  102. /**
  103. * tripal_bulk_loader_access
  104. */
  105. function tripal_bulk_loader_access($op, $node, $account){
  106. if ($op == 'create') {
  107. return user_access('create tripal_bulk_loader', $account);
  108. }
  109. if ($op == 'update') {
  110. if (user_access('edit tripal_bulk_loader', $account)) {
  111. return TRUE;
  112. }
  113. }
  114. if ($op == 'delete') {
  115. if (user_access('delete tripal_bulk_loader', $account)) {
  116. return TRUE;
  117. }
  118. }
  119. if ($op == 'view') {
  120. if (user_access('access tripal_bulk_loader', $account)) {
  121. return TRUE;
  122. }
  123. }
  124. return FALSE;
  125. }
  126. /**
  127. * tripal_bulk_loader_perm
  128. */
  129. function tripal_bulk_loader_perm(){
  130. return array(
  131. 'access tripal_bulk_loader',
  132. 'create tripal_bulk_loader',
  133. 'delete tripal_bulk_loader',
  134. 'edit tripal_bulk_loader',
  135. );
  136. }
  137. /**
  138. * tripal_bulk_loader_node_info
  139. */
  140. function tripal_bulk_loader_node_info() {
  141. $nodes = array();
  142. $nodes['tripal_bulk_loader'] = array(
  143. 'name' => t('Bulk Loading Job'),
  144. 'module' => 'tripal_bulk_loader',
  145. 'description' => t('A bulk loader for inserting tab-delimited data into chado database'),
  146. 'has_title' => TRUE,
  147. 'has_body' => FALSE,
  148. 'locked' => TRUE
  149. );
  150. return $nodes;
  151. }
  152. /**
  153. * tripal_bulk_loader_form
  154. */
  155. function tripal_bulk_loader_form ($node){
  156. $form = array();
  157. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  158. $results = db_query($sql);
  159. $templates = array ();
  160. while ($template = db_fetch_object ($results)) {
  161. $templates [$template->template_id] = $template->name;
  162. }
  163. if (!$templates) {
  164. $form['label'] = array(
  165. '#type' => 'item',
  166. '#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."),
  167. '#weight' => 0,
  168. );
  169. return $form;
  170. }
  171. $form['loader_name'] = array(
  172. '#type' => 'textfield',
  173. '#title' => t('Loading Job Name'),
  174. '#weight' => -3,
  175. '#required' => TRUE,
  176. '#default_value' => $node->loader_name
  177. );
  178. $form['template_id'] = array(
  179. '#type' => 'select',
  180. '#title' => t('Template'),
  181. '#description' => t('Please specify a template for this loader'),
  182. '#options' => $templates,
  183. '#weight' => -2,
  184. '#required' => TRUE,
  185. '#default_value' => $node->template_id
  186. );
  187. $form['file']= array(
  188. '#type' => 'textfield',
  189. '#title' => t('Data File'),
  190. '#description' => t('Please specify the data file to be loaded.'),
  191. '#weight' => -1,
  192. '#default_value' => $node->file
  193. );
  194. return $form;
  195. }
  196. /**
  197. * tripal_bulk_loader_theme
  198. */
  199. function tripal_bulk_loader_theme() {
  200. return array(
  201. 'tripal_bulk_loader_node_form' => array(
  202. 'arguments' => array('form' => NULL),
  203. ),
  204. 'tripal_bulk_loader_template' => array(
  205. 'arguments'=> array('template_id' => NULL),
  206. 'template' => 'tripal_bulk_loader_template'
  207. ),
  208. 'tripal_bulk_loader_edit_template_base_form' => array(
  209. 'arguments' => array('form' => NULL),
  210. 'template' => 'tripal_bulk_loader_edit_template_base_form',
  211. ),
  212. );
  213. }
  214. /**
  215. * theme_tripal_bulk_loader_node_form
  216. */
  217. function theme_tripal_bulk_loader_node_form($form) {
  218. // Do not show [Save] and [Preview] buttons if loader template is not available
  219. if($form['label']['#type'] == "item") {
  220. unset($form['buttons']);
  221. }
  222. return drupal_render($form);
  223. }
  224. /**
  225. * tripal_bulk_loader_load
  226. */
  227. function tripal_bulk_loader_load($node){
  228. $sql = "SELECT * FROM {tripal_bulk_loader} WHERE nid = %d";
  229. $node = db_fetch_object(db_query($sql, $node->nid));
  230. $node->title = 'Bulk Loading Job: '.$node->loader_name;
  231. // Add the loader template
  232. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  233. $results = db_fetch_object(db_query($sql, $node->template_id));
  234. $template = unserialize($results->template_array);
  235. $node->template = $results;
  236. $node->template->template_array = $template;
  237. return $node;
  238. }
  239. /**
  240. * tripal_bulk_loader_insert
  241. */
  242. function tripal_bulk_loader_insert ($node) {
  243. $sql = "INSERT INTO {tripal_bulk_loader} (nid, loader_name, template_id, file) VALUES (%d, '%s', %d, '%s')";
  244. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file);
  245. $node->title =$node->loader_name;
  246. drupal_write_record('node',$node,'nid');
  247. drupal_write_record('node_revision',$node,'nid');
  248. // Add a job if the user want to load the data
  249. global $user;
  250. if($node->job) {
  251. $job_args[0] =$node->loader_name;
  252. $job_args[1] = $node->template_id;
  253. $job_args[2] = $node->file;
  254. if (is_readable($node->file)) {
  255. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  256. tripal_add_job("Bulk Load: $fname",'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  257. } else {
  258. drupal_set_message("Can not open $node->file. Job not scheduled.");
  259. }
  260. }
  261. }
  262. /**
  263. * tripal_bulk_loader_delete
  264. */
  265. function tripal_bulk_loader_delete ($node) {
  266. $sql = "DELETE FROM {tripal_bulk_loader} WHERE nid = %d";
  267. db_query($sql, $node->nid);
  268. }
  269. /**
  270. * tripal_bulk_loader_update
  271. */
  272. function tripal_bulk_loader_update ($node) {
  273. $sql = "UPDATE {tripal_bulk_loader} SET nid = %d, loader_name = '%s', template_id = %d, file = '%s' WHERE nid = %d";
  274. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->nid);
  275. // Add a job if the user want to load the data
  276. global $user;
  277. if($node->job) {
  278. $job_args[0] =$node->loader_name;
  279. $job_args[1] = $node->template_id;
  280. $job_args[2] = $node->file;
  281. if (is_readable($node->file)) {
  282. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  283. tripal_add_job("Bulk Load: $fname",'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  284. } else {
  285. drupal_set_message("Can not open $node->file. Job not scheduled.");
  286. }
  287. }
  288. }
  289. /**
  290. * Preprocessor function for the tripal_bulk_loader template
  291. */
  292. function tripal_bulk_loader_preprocess_tripal_bulk_loader_template (&$variables) {
  293. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  294. $template = db_fetch_object(db_query($sql, $variables['template_id']));
  295. $template->template_array = unserialize($template->template_array);
  296. $variables['template'] = $template;
  297. }
  298. /**
  299. * Implements hook_job_describe_args()
  300. * Specifically to make viewing past tripal jobs more readable for jobs registered by this module
  301. *
  302. * @params $callback
  303. * The callback passed into tripal_add_job()
  304. * @param $args
  305. * The arguements passed into tripal_add_job()
  306. * @return
  307. * An array where keys are the human readable headers describing each arguement
  308. * and the value is the aguement passed in after formatting
  309. */
  310. function tripal_bulk_loader_job_describe_args($callback,$args){
  311. $new_args = array();
  312. if($callback == 'tripal_bulk_loader_load_data'){
  313. //1st arg is the nid for a bulk loader node
  314. $node = node_load($args[0]);
  315. $new_args['Bulk Loading Job'] = l($node->title, 'node/'.$args[0]);
  316. return $new_args;
  317. }
  318. }