tripal_bulk_loader.module 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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' => 'tripal_bulk_loader_admin_template_add',
  36. 'access arguments' => array('administer site configuration'),
  37. 'type' => MENU_NORMAL_ITEM,
  38. 'file' => 'tripal_bulk_loader.admin.inc',
  39. );
  40. $items['admin/tripal/tripal_bulk_loader_template/add/chado_column'] = array(
  41. 'page callback' => 'tripal_bulk_loader_chado_column_ajax',
  42. 'page arguments' => array(5),
  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/delete'] = array(
  48. 'title' => 'Delete Bulk Loader Template',
  49. 'description' => 'Delete bulk loader template',
  50. 'page callback' => 'tripal_bulk_loader_admin_template_delete',
  51. 'access arguments' => array('administer site configuration'),
  52. 'type' => MENU_NORMAL_ITEM,
  53. 'file' => 'tripal_bulk_loader.admin.inc',
  54. );
  55. return $items;
  56. }
  57. /*******************************************************************************
  58. * tripal_bulk_loader_list
  59. */
  60. function tripal_bulk_loader_list () {
  61. return "Loaders";
  62. }
  63. /*******************************************************************************
  64. * tripal_bulk_loader_access
  65. */
  66. function tripal_bulk_loader_access($op, $node, $account){
  67. if ($op == 'create') {
  68. return user_access('create tripal_bulk_loader', $account);
  69. }
  70. if ($op == 'update') {
  71. if (user_access('edit tripal_bulk_loader', $account)) {
  72. return TRUE;
  73. }
  74. }
  75. if ($op == 'delete') {
  76. if (user_access('delete tripal_bulk_loader', $account)) {
  77. return TRUE;
  78. }
  79. }
  80. if ($op == 'view') {
  81. if (user_access('access tripal_bulk_loader', $account)) {
  82. return TRUE;
  83. }
  84. }
  85. return FALSE;
  86. }
  87. /*******************************************************************************
  88. * tripal_bulk_loader_perm
  89. */
  90. function tripal_bulk_loader_perm(){
  91. return array(
  92. 'access tripal_bulk_loader',
  93. 'create tripal_bulk_loader',
  94. 'delete tripal_bulk_loader',
  95. 'edit tripal_bulk_loader',
  96. );
  97. }
  98. /*******************************************************************************
  99. * tripal_bulk_loader_node_info
  100. */
  101. function tripal_bulk_loader_node_info() {
  102. $nodes = array();
  103. $nodes['tripal_bulk_loader'] = array(
  104. 'name' => t('Bulk Loader'),
  105. 'module' => 'tripal_bulk_loader',
  106. 'description' => t('A bulk loader for inserting tab-delimited data into chado database'),
  107. 'has_title' => TRUE,
  108. 'has_body' => FALSE,
  109. 'locked' => TRUE
  110. );
  111. return $nodes;
  112. }
  113. /*******************************************************************************
  114. * tripal_bulk_loader_form
  115. */
  116. function tripal_bulk_loader_form ($node){
  117. $form = array();
  118. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  119. $results = db_query($sql);
  120. $templates = array ();
  121. while ($template = db_fetch_object ($results)) {
  122. $templates [$template->template_id] = $template->name;
  123. }
  124. if (!$templates) {
  125. $form['label'] = array(
  126. '#type' => 'item',
  127. '#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."),
  128. '#weight' => 0,
  129. );
  130. return $form;
  131. }
  132. $form['loader_name'] = array(
  133. '#type' => 'textfield',
  134. '#title' => t('Loader Name'),
  135. '#weight' => -3,
  136. '#required' => TRUE,
  137. '#default_value' => $node->loader_name
  138. );
  139. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  140. $results = db_query($sql);
  141. $templates = array ();
  142. while ($template = db_fetch_object ($results)) {
  143. $templates [$template->template_id] = $template->name;
  144. }
  145. $form['template_id'] = array(
  146. '#type' => 'select',
  147. '#title' => t('Template'),
  148. '#description' => t('Please specify a template for this loader'),
  149. '#options' => $templates,
  150. '#weight' => -2,
  151. '#required' => TRUE,
  152. '#default_value' => $node->template_id
  153. );
  154. $form['file']= array(
  155. '#type' => 'textfield',
  156. '#title' => t('Data File'),
  157. '#description' => t('Please specify the data file to be loaded.'),
  158. '#weight' => -1,
  159. '#default_value' => $node->file
  160. );
  161. $form['job']= array(
  162. '#type' => 'checkbox',
  163. '#title' => t('Submit a job to load the data file using selected template'),
  164. '#weight' => 0
  165. );
  166. return $form;
  167. }
  168. /*******************************************************************************
  169. * tripal_bulk_loader_theme
  170. */
  171. function tripal_bulk_loader_theme() {
  172. return array(
  173. 'tripal_bulk_loader_node_form' => array(
  174. 'arguments' => array('form' => NULL),
  175. ),
  176. );
  177. }
  178. /*******************************************************************************
  179. * theme_tripal_bulk_loader_node_form
  180. */
  181. function theme_tripal_bulk_loader_node_form($form) {
  182. // Do not show [Save] and [Preview] buttons if loader template is not available
  183. if($form['label']['#type'] == "item") {
  184. unset($form['buttons']);
  185. }
  186. unset($form['menu']);
  187. unset($form['revision_information']);
  188. unset($form['author']);
  189. unset($form['path']);
  190. unset($form['comment_settings']);
  191. unset($form['options']);
  192. return drupal_render($form);
  193. }
  194. /*******************************************************************************
  195. * tripal_bulk_loader_load
  196. */
  197. function tripal_bulk_loader_load($node){
  198. $sql = "SELECT * FROM {tripal_bulk_loader} WHERE nid = %d";
  199. $properties = db_fetch_object(db_query($sql, $node->nid));
  200. return $properties;
  201. }
  202. /*******************************************************************************
  203. * tripal_bulk_loader_view
  204. */
  205. function tripal_bulk_loader_view ($node, $teaser = FALSE, $page = FALSE) {
  206. if (!$teaser) {
  207. $node = node_prepare($node, $teaser);
  208. }
  209. return $node;
  210. }
  211. /*******************************************************************************
  212. * tripal_bulk_loader_insert
  213. */
  214. function tripal_bulk_loader_insert ($node) {
  215. $sql = "INSERT INTO {tripal_bulk_loader} (nid, loader_name, template_id, file) VALUES (%d, '%s', %d, '%s')";
  216. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file);
  217. $node->title =$node->loader_name;
  218. drupal_write_record('node',$node,'nid');
  219. drupal_write_record('node_revision',$node,'nid');
  220. // Add a job if the user want to load the data
  221. global $user;
  222. if($node->job) {
  223. $job_args[0] =$node->loader_name;
  224. $job_args[1] = $node->template_id;
  225. $job_args[2] = $node->file;
  226. if (is_readable($node->file)) {
  227. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  228. tripal_add_job("Bulk Load: $fname",'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  229. } else {
  230. drupal_set_message("Can not open $node->file. Job not scheduled.");
  231. }
  232. }
  233. }
  234. /*******************************************************************************
  235. * tripal_bulk_loader_delete
  236. */
  237. function tripal_bulk_loader_delete ($node) {
  238. $sql = "DELETE FROM {tripal_bulk_loader} WHERE nid = %d";
  239. db_query($sql, $node->nid);
  240. }
  241. /*******************************************************************************
  242. * tripal_bulk_loader_update
  243. */
  244. function tripal_bulk_loader_update ($node) {
  245. $sql = "UPDATE {tripal_bulk_loader} SET nid = %d, loader_name = '%s', template_id = %d, file = '%s' WHERE nid = %d";
  246. db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->nid);
  247. // Add a job if the user want to load the data
  248. global $user;
  249. if($node->job) {
  250. $job_args[0] =$node->loader_name;
  251. $job_args[1] = $node->template_id;
  252. $job_args[2] = $node->file;
  253. if (is_readable($node->file)) {
  254. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  255. tripal_add_job("Bulk Load: $fname",'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  256. } else {
  257. drupal_set_message("Can not open $node->file. Job not scheduled.");
  258. }
  259. }
  260. }
  261. /*******************************************************************************
  262. * tripal_bulk_loader_load_data
  263. */
  264. function tripal_bulk_loader_load_data ($loader_name, $template_id, $file) {
  265. }