tripal_bulk_loader.module 9.0 KB

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