tripal_bulk_loader.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /*******************************************************************************
  3. * tripal_bulk_loader_init
  4. */
  5. function tripal_bulk_loader_init(){
  6. // Add javascript and style sheet
  7. drupal_add_css(drupal_get_path('theme', 'tripal').'/css/tripal_bulk_loader.css');
  8. drupal_add_js(drupal_get_path('theme', 'tripal').'/js/tripal_bulk_loader.js');
  9. }
  10. /*******************************************************************************
  11. * tripal_bulk_loader_menu
  12. */
  13. function tripal_bulk_loader_menu() {
  14. $items = array();
  15. // Show all loaders
  16. $items['/tripal_bulk_loaders'] = array(
  17. 'title' => 'Tripal Bulk Loaders',
  18. 'description' => 'Tripal bulk loaders for loading tab-delimited file into chado database',
  19. 'page callback' => 'tripal_bulk_loader_list',
  20. 'access arguments' => array('access tripal_bulk_loader'),
  21. 'type' => MENU_NORMAL_ITEM,
  22. );
  23. // Admin page to create the template
  24. $items['admin/tripal/tripal_bulk_loader_template'] = array(
  25. 'title' => 'Bulk Loader Template',
  26. 'description' => 'Create loader template for loading tab-delimited data',
  27. 'page callback' => 'tripal_bulk_loader_admin_template',
  28. 'access arguments' => array('administer site configuration'),
  29. 'type' => MENU_NORMAL_ITEM,
  30. 'file' => 'tripal_bulk_loader.admin.inc',
  31. );
  32. $items['admin/tripal/tripal_bulk_loader_template/add'] = array(
  33. 'title' => 'Create Bulk Loader Template',
  34. 'description' => 'Create loader template for loading tab-delimited data',
  35. 'page callback' => 'drupal_get_form',
  36. 'page arguments' => array('tripal_bulk_loader_create_template_base_form'),
  37. 'access arguments' => array('administer site configuration'),
  38. 'type' => MENU_NORMAL_ITEM,
  39. 'file' => 'tripal_bulk_loader.admin.inc',
  40. );
  41. $items['admin/tripal/tripal_bulk_loader_template/add/chado_column_ahah'] = array(
  42. 'page callback' => 'tripal_bulk_loader_add_chado_column_ahah',
  43. 'access arguments' => array('administer site configuration'),
  44. 'type' => MENU_CALLBACK,
  45. 'file' => 'tripal_bulk_loader.admin.inc',
  46. );
  47. $items['admin/tripal/tripal_bulk_loader_template/add/field_type_ahah'] = array(
  48. 'page callback' => 'tripal_bulk_loader_add_field_type_ahah',
  49. 'access arguments' => array('administer site configuration'),
  50. 'type' => MENU_CALLBACK,
  51. 'file' => 'tripal_bulk_loader.admin.inc',
  52. );
  53. //tripal_bulk_loader_edit_template_base_form
  54. $items['admin/tripal/tripal_bulk_loader_template/edit'] = array(
  55. 'title' => 'Edit Bulk Loader Template',
  56. 'description' => 'Edit bulk loader template',
  57. 'page callback' => 'drupal_get_form',
  58. 'page arguments' => array('tripal_bulk_loader_edit_template_base_form'),
  59. 'access arguments' => array('administer site configuration'),
  60. 'type' => MENU_NORMAL_ITEM,
  61. 'file' => 'tripal_bulk_loader.admin.inc',
  62. );
  63. $items['admin/tripal/tripal_bulk_loader_template/edit/edit_fields_ahah'] = array(
  64. 'page callback' => 'tripal_bulk_loader_edit_fields_ahah',
  65. 'access arguments' => array('administer site configuration'),
  66. 'type' => MENU_CALLBACK,
  67. 'file' => 'tripal_bulk_loader.admin.inc',
  68. );
  69. $items['admin/tripal/tripal_bulk_loader_template/edit/chado_column_ahah'] = array(
  70. 'page callback' => 'tripal_bulk_loader_edit_chado_column_ahah',
  71. 'access arguments' => array('administer site configuration'),
  72. 'type' => MENU_CALLBACK,
  73. 'file' => 'tripal_bulk_loader.admin.inc',
  74. );
  75. $items['admin/tripal/tripal_bulk_loader_template/edit/field_type_ahah'] = array(
  76. 'page callback' => 'tripal_bulk_loader_edit_field_type_ahah',
  77. 'access arguments' => array('administer site configuration'),
  78. 'type' => MENU_CALLBACK,
  79. 'file' => 'tripal_bulk_loader.admin.inc',
  80. );
  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. return $items;
  91. }
  92. /*******************************************************************************
  93. * tripal_bulk_loader_list
  94. */
  95. function tripal_bulk_loader_list () {
  96. return "Loaders";
  97. }
  98. /*******************************************************************************
  99. * tripal_bulk_loader_access
  100. */
  101. function tripal_bulk_loader_access($op, $node, $account){
  102. if ($op == 'create') {
  103. return user_access('create tripal_bulk_loader', $account);
  104. }
  105. if ($op == 'update') {
  106. if (user_access('edit tripal_bulk_loader', $account)) {
  107. return TRUE;
  108. }
  109. }
  110. if ($op == 'delete') {
  111. if (user_access('delete tripal_bulk_loader', $account)) {
  112. return TRUE;
  113. }
  114. }
  115. if ($op == 'view') {
  116. if (user_access('access tripal_bulk_loader', $account)) {
  117. return TRUE;
  118. }
  119. }
  120. return FALSE;
  121. }
  122. /*******************************************************************************
  123. * tripal_bulk_loader_perm
  124. */
  125. function tripal_bulk_loader_perm(){
  126. return array(
  127. 'access tripal_bulk_loader',
  128. 'create tripal_bulk_loader',
  129. 'delete tripal_bulk_loader',
  130. 'edit tripal_bulk_loader',
  131. );
  132. }
  133. /*******************************************************************************
  134. * tripal_bulk_loader_node_info
  135. */
  136. function tripal_bulk_loader_node_info() {
  137. $nodes = array();
  138. $nodes['tripal_bulk_loader'] = array(
  139. 'name' => t('Bulk Loading Job'),
  140. 'module' => 'tripal_bulk_loader',
  141. 'description' => t('A bulk loader for inserting tab-delimited data into chado database'),
  142. 'has_title' => TRUE,
  143. 'has_body' => FALSE,
  144. 'locked' => TRUE
  145. );
  146. return $nodes;
  147. }
  148. /*******************************************************************************
  149. * tripal_bulk_loader_form
  150. */
  151. function tripal_bulk_loader_form ($node){
  152. $form = array();
  153. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  154. $results = db_query($sql);
  155. $templates = array ();
  156. while ($template = db_fetch_object ($results)) {
  157. $templates [$template->template_id] = $template->name;
  158. }
  159. if (!$templates) {
  160. $form['label'] = array(
  161. '#type' => 'item',
  162. '#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."),
  163. '#weight' => 0,
  164. );
  165. return $form;
  166. }
  167. $form['loader_name'] = array(
  168. '#type' => 'textfield',
  169. '#title' => t('Loading Job Name'),
  170. '#weight' => -3,
  171. '#required' => TRUE,
  172. '#default_value' => $node->loader_name
  173. );
  174. $form['template_id'] = array(
  175. '#type' => 'select',
  176. '#title' => t('Template'),
  177. '#description' => t('Please specify a template for this loader'),
  178. '#options' => $templates,
  179. '#weight' => -2,
  180. '#required' => TRUE,
  181. '#default_value' => $node->template_id
  182. );
  183. $form['file']= array(
  184. '#type' => 'textfield',
  185. '#title' => t('Data File'),
  186. '#description' => t('Please specify the data file to be loaded.'),
  187. '#weight' => -1,
  188. '#default_value' => $node->file
  189. );
  190. return $form;
  191. }
  192. /*******************************************************************************
  193. * tripal_bulk_loader_theme
  194. */
  195. function tripal_bulk_loader_theme() {
  196. return array(
  197. 'tripal_bulk_loader_node_form' => array(
  198. 'arguments' => array('form' => NULL),
  199. ),
  200. 'tripal_bulk_loader_template' => array(
  201. 'arguments'=> array('template_id' => NULL),
  202. 'template' => 'tripal_bulk_loader_template'
  203. ),
  204. 'tripal_bulk_loader_edit_template_base_form' => array(
  205. 'arguments' => array('form' => NULL),
  206. 'template' => 'tripal_bulk_loader_edit_template_base_form',
  207. ),
  208. );
  209. }
  210. /*******************************************************************************
  211. * theme_tripal_bulk_loader_node_form
  212. */
  213. function theme_tripal_bulk_loader_node_form($form) {
  214. // Do not show [Save] and [Preview] buttons if loader template is not available
  215. if($form['label']['#type'] == "item") {
  216. unset($form['buttons']);
  217. }
  218. return drupal_render($form);
  219. }
  220. /*******************************************************************************
  221. * tripal_bulk_loader_load
  222. */
  223. function tripal_bulk_loader_load($node){
  224. $sql = "SELECT * FROM {tripal_bulk_loader} WHERE nid = %d";
  225. $node = db_fetch_object(db_query($sql, $node->nid));
  226. $node->title = 'Bulk Loading Job: '.$node->loader_name;
  227. // Add the loader template
  228. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  229. $results = db_fetch_object(db_query($sql, $node->template_id));
  230. $template = unserialize($results->template_array);
  231. $node->template = $results;
  232. $node->template->template_array = $template;
  233. return $node;
  234. }
  235. /*******************************************************************************
  236. * tripal_bulk_loader_insert
  237. */
  238. function tripal_bulk_loader_insert ($node) {
  239. $sql = "INSERT INTO {tripal_bulk_loader} (nid, loader_name, template_id, file) VALUES (%d, '%s', %d, '%s')";
  240. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file);
  241. $node->title =$node->loader_name;
  242. drupal_write_record('node',$node,'nid');
  243. drupal_write_record('node_revision',$node,'nid');
  244. // Add a job if the user want to load the data
  245. global $user;
  246. if($node->job) {
  247. $job_args[0] =$node->loader_name;
  248. $job_args[1] = $node->template_id;
  249. $job_args[2] = $node->file;
  250. if (is_readable($node->file)) {
  251. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  252. tripal_add_job("Bulk Load: $fname",'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  253. } else {
  254. drupal_set_message("Can not open $node->file. Job not scheduled.");
  255. }
  256. }
  257. }
  258. /*******************************************************************************
  259. * tripal_bulk_loader_delete
  260. */
  261. function tripal_bulk_loader_delete ($node) {
  262. $sql = "DELETE FROM {tripal_bulk_loader} WHERE nid = %d";
  263. db_query($sql, $node->nid);
  264. }
  265. /*******************************************************************************
  266. * tripal_bulk_loader_update
  267. */
  268. function tripal_bulk_loader_update ($node) {
  269. $sql = "UPDATE {tripal_bulk_loader} SET nid = %d, loader_name = '%s', template_id = %d, file = '%s' WHERE nid = %d";
  270. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->nid);
  271. // Add a job if the user want to load the data
  272. global $user;
  273. if($node->job) {
  274. $job_args[0] =$node->loader_name;
  275. $job_args[1] = $node->template_id;
  276. $job_args[2] = $node->file;
  277. if (is_readable($node->file)) {
  278. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  279. tripal_add_job("Bulk Load: $fname",'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  280. } else {
  281. drupal_set_message("Can not open $node->file. Job not scheduled.");
  282. }
  283. }
  284. }
  285. /*******************************************************************************
  286. * tripal_bulk_loader_load_data
  287. */
  288. function tripal_bulk_loader_load_data ($loader_name, $template_id, $file) {
  289. }
  290. function tripal_bulk_loader_preprocess_tripal_bulk_loader_template (&$variables) {
  291. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  292. $template = db_fetch_object(db_query($sql, $variables['template_id']));
  293. $template->template_array = unserialize($template->template_array);
  294. $variables['template'] = $template;
  295. }