tripal_bulk_loader.module 9.6 KB

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