tripal_bulk_loader.module 9.4 KB

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