tripal_bulk_loader.chado_node.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /**
  3. * @file
  4. * Tripal Bulk Loader Node functionality (jobs)
  5. */
  6. /**
  7. * Implements hook_node_info
  8. *
  9. * @ingroup tripal_bulk_loader
  10. */
  11. function tripal_bulk_loader_node_info() {
  12. $nodes = array();
  13. $nodes['tripal_bulk_loader'] = array(
  14. 'name' => t('Bulk Loading Job'),
  15. 'base' => 'tripal_bulk_loader',
  16. 'description' => t('A bulk loader for inserting tab-delimited data into chado database'),
  17. 'has_title' => TRUE,
  18. 'has_body' => FALSE,
  19. 'locked' => TRUE
  20. );
  21. return $nodes;
  22. }
  23. /**
  24. * Implements node_form
  25. * Used to gather the extra details stored with a Bulk Loading Job Node
  26. *
  27. * @ingroup tripal_bulk_loader
  28. */
  29. function tripal_bulk_loader_form($node, $form_state) {
  30. $form = array();
  31. if (isset($form_state['values'])) {
  32. $node = $form_state['values'] + (array)$node;
  33. $node = (object) $node;
  34. }
  35. $results = db_select('tripal_bulk_loader_template', 't')
  36. ->fields('t', array('template_id','name'))
  37. ->execute();
  38. $templates = array();
  39. foreach ($results as $template) {
  40. $templates [$template->template_id] = $template->name;
  41. }
  42. if (!$templates) {
  43. $form['label'] = array(
  44. '#type' => 'item',
  45. '#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."),
  46. '#weight' => -10,
  47. );
  48. return $form;
  49. }
  50. $form['loader'] = array(
  51. '#type' => 'fieldset',
  52. '#title' => t('Basic Details'),
  53. );
  54. $form['loader']['loader_name'] = array(
  55. '#type' => 'textfield',
  56. '#title' => t('Loading Job Name'),
  57. '#weight' => -10,
  58. '#required' => TRUE,
  59. '#default_value' => (isset($node->loader_name)) ? $node->loader_name : ''
  60. );
  61. $form['loader']['template_id'] = array(
  62. '#type' => 'select',
  63. '#title' => t('Template'),
  64. '#description' => t('Please specify a template for this loader'),
  65. '#options' => $templates,
  66. '#weight' => -9,
  67. '#required' => TRUE,
  68. '#default_value' => (isset($node->template_id)) ? $node->template_id : current($templates),
  69. );
  70. $form['loader']['file']= array(
  71. '#type' => 'textfield',
  72. '#title' => t('Data File'),
  73. '#description' => t('Please specify the data file to be loaded. This must be a tab-delimited text file with UNIX line endings.'),
  74. '#weight' => -8,
  75. '#default_value' => (isset($node->file)) ? $node->file : '',
  76. '#maxlength' => 1024,
  77. );
  78. $form['loader']['has_header'] = array(
  79. '#type' => 'radios',
  80. '#title' => t('File has a Header'),
  81. '#options' => array( 1 => 'Yes', 2 => 'No'),
  82. '#weight' => -7,
  83. '#default_value' => (isset($node->file_has_header)) ? $node->file_has_header : 1,
  84. );
  85. $form['loader']['keep_track_inserted'] = array(
  86. '#type' => 'radios',
  87. '#title' => t('Keep track of inserted record IDs'),
  88. '#description' => t('This enables the ability to revert an entire loading job even if '
  89. .'it completed successfully. Furthermore, it displays the number of records '
  90. .'successfully inserted into each table.'),
  91. '#options' => array( 1 => 'Yes', 0 => 'No'),
  92. '#weight' => -7,
  93. '#default_value' => (isset($node->keep_track_inserted)) ? $node->keep_track_inserted : variable_get('tripal_bulk_loader_keep_track_inserted', 0),
  94. );
  95. return $form;
  96. }
  97. /**
  98. * Implements node_load
  99. *
  100. * D7 Changes: now loads all $nodes at once so need to add loops
  101. * @ingroup tripal_bulk_loader
  102. */
  103. function tripal_bulk_loader_load($nodes) {
  104. // Loading Job Details
  105. // Add fields from the tripal_bulk_loader
  106. $result = db_select('tripal_bulk_loader', 'tbl')
  107. ->fields('tbl')
  108. ->condition('nid',array_keys($nodes),'IN')
  109. ->execute();
  110. foreach ($result as $record) {
  111. $nodes[$record->nid]->loader_name = $record->loader_name;
  112. $nodes[$record->nid]->template_id = $record->template_id;
  113. $nodes[$record->nid]->file = $record->file;
  114. $nodes[$record->nid]->job_id = $record->job_id;
  115. $nodes[$record->nid]->job_status = $record->job_status;
  116. $nodes[$record->nid]->file_has_header = $record->file_has_header;
  117. $nodes[$record->nid]->keep_track_inserted = $record->keep_track_inserted;
  118. $nodes[$record->nid]->exposed_fields = array();
  119. $nodes[$record->nid]->constants = array();
  120. }
  121. // Job Details
  122. // Add fields from tripal_jobs
  123. $result = db_query('SELECT tbl.nid, tj.* FROM {tripal_jobs} tj '
  124. . 'LEFT JOIN {tripal_bulk_loader} tbl ON tbl.job_id=tj.job_id '
  125. . 'WHERE tbl.nid IN (:nids)',
  126. array(':nids' => array_keys($nodes))
  127. );
  128. foreach ($result as $record) {
  129. $nodes[$record->nid]->job = $record;
  130. }
  131. // Add the Loader Template
  132. // Add fields from tripal_bulk_loader_template
  133. $result = db_query('SELECT tbl.nid, tblt.* FROM {tripal_bulk_loader_template} tblt '
  134. . 'LEFT JOIN {tripal_bulk_loader} tbl ON tbl.template_id=tblt.template_id '
  135. . 'WHERE tbl.nid IN (:nids)',
  136. array(':nids' => array_keys($nodes))
  137. );
  138. foreach ($result as $dbrecord) {
  139. $nodes[$dbrecord->nid]->template = $dbrecord;
  140. $nodes[$dbrecord->nid]->template->template_array = unserialize($dbrecord->template_array);
  141. // Add exposed field list
  142. $template = $nodes[$dbrecord->nid]->template->template_array;
  143. $nodes[$dbrecord->nid]->exposed_fields = array();
  144. if ($template) {
  145. foreach ($template as $record_id => $record) {
  146. foreach ($record['fields'] as $field_id => $field) {
  147. if (isset($field['exposed'])) {
  148. if ($field['exposed']) {
  149. $nodes[$dbrecord->nid]->exposed_fields[] = array(
  150. 'record_id' => $record_id,
  151. 'field_id' => $field_id,
  152. 'title' => $field['title'],
  153. );
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. // Add inserted records
  161. // Add fields from tripal_bulk_loader_inserted
  162. $result = db_query('SELECT tbli.* FROM {tripal_bulk_loader_inserted} tbli '
  163. . 'WHERE tbli.nid IN (:nids)',
  164. array(':nids' => array_keys($nodes))
  165. );
  166. foreach ($result as $record) {
  167. $record->num_inserted = sizeof(preg_split('/,/', $record->ids_inserted));
  168. $nodes[$record->nid]->inserted_records->{$record->table_inserted_into} = $record;
  169. }
  170. // Add constants
  171. // Add fields from tripal_bulk_loader_constants
  172. $result = db_query('SELECT tblc.* FROM {tripal_bulk_loader_constants} tblc '
  173. . 'WHERE tblc.nid IN (:nids) '
  174. . 'ORDER BY group_id, record_id, field_id',
  175. array(':nids' => array_keys($nodes))
  176. );
  177. foreach ($result as $record) {
  178. $nodes[$record->nid]->constants[$record->group_id][$record->record_id][$record->field_id] = array(
  179. 'constant_id' => $record->constant_id,
  180. 'group_id' => $record->group_id,
  181. 'chado_table' => $record->chado_table,
  182. 'chado_field' => $record->chado_field,
  183. 'record_id' => $record->record_id,
  184. 'field_id' => $record->field_id,
  185. 'value' => $record->value
  186. );
  187. }
  188. }
  189. /**
  190. * Implements node_insert
  191. * Insert the data from the node form on Create content
  192. *
  193. * D7 Changes: seems to need db_insert; not recommended to change $node
  194. * @ingroup tripal_bulk_loader
  195. */
  196. function tripal_bulk_loader_insert($node) {
  197. $node->title = $node->loader_name;
  198. db_insert('tripal_bulk_loader')->fields(array(
  199. 'nid' => $node->nid,
  200. 'loader_name' => $node->loader_name,
  201. 'template_id' => $node->template_id,
  202. 'file' => $node->file,
  203. 'file_has_header' => $node->has_header,
  204. 'job_status' => 'Initialized',
  205. 'keep_track_inserted' => $node->keep_track_inserted
  206. ))->execute();
  207. drupal_set_message(t('After reviewing the details, please Submit this Job (by clicking the "Submit Job" button below). No data will be loaded until the submitted job is reached in the queue.'));
  208. }
  209. /**
  210. * Implements node_delete
  211. * Deletes the data when the delete button on the node form is clicked
  212. *
  213. * @ingroup tripal_bulk_loader
  214. */
  215. function tripal_bulk_loader_delete($node) {
  216. $tables = array();
  217. $tables[] = 'tripal_bulk_loader';
  218. $tables[] = 'tripal_bulk_loader_constants';
  219. $tables[] = 'tripal_bulk_loader_inserted';
  220. foreach($tables as $table) {
  221. db_delete($table)
  222. ->condition('nid',$node->nid)
  223. ->execute();
  224. }
  225. }
  226. /**
  227. * Implements node_update
  228. * Updates the data submitted by the node form on edit
  229. *
  230. * D7 Changes: db_update is much easier
  231. * @ingroup tripal_bulk_loader
  232. */
  233. function tripal_bulk_loader_update($node) {
  234. // Update tripal_bulk_loader
  235. db_update('tripal_bulk_loader')->fields(array(
  236. 'nid' => $node->nid,
  237. 'loader_name' => $node->loader_name,
  238. 'template_id' => $node->template_id,
  239. 'file' => $node->file,
  240. 'file_has_header' => $node->has_header,
  241. 'keep_track_inserted' => $node->keep_track_inserted
  242. ))->condition('nid',$node->nid)->execute();
  243. // Add a job if the user want to load the data
  244. /**
  245. No job checkbox in the form
  246. global $user;
  247. if ($node->job) {
  248. $job_args[0] =$node->loader_name;
  249. $job_args[1] = $node->template_id;
  250. $job_args[2] = $node->file;
  251. if (is_readable($node->file)) {
  252. $fname = preg_replace("/.*\/(.*)/", "$1", $node->file);
  253. tripal_add_job("Bulk Load: $fname", 'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  254. }
  255. else {
  256. drupal_set_message(t("Can not open %file. Job not scheduled.", array('%file' => $node->file)));
  257. }
  258. }
  259. */
  260. }
  261. /**
  262. * Implement hook_access().
  263. *
  264. * This hook allows node modules to limit access to the node types they define.
  265. *
  266. * @param $op
  267. * The operation to be performed
  268. *
  269. * @param $node
  270. * The node on which the operation is to be performed, or, if it does not yet exist, the
  271. * type of node to be created
  272. *
  273. * @param $account
  274. * A user object representing the user for whom the operation is to be performed
  275. *
  276. * @return
  277. * If the permission for the specified operation is not set then return FALSE. If the
  278. * permission is set then return NULL as this allows other modules to disable
  279. * access. The only exception is when the $op == 'create'. We will always
  280. * return TRUE if the permission is set.
  281. * @ingroup tripal_bulk_loader
  282. */
  283. function tripal_bulk_loader_node_access($node, $op, $account) {
  284. if ($op == 'create') {
  285. if (!user_access('create tripal_bulk_loader', $account)) {
  286. return NODE_ACCESS_DENY;
  287. }
  288. return NODE_ACCESS_ALLOW;
  289. }
  290. if ($op == 'update') {
  291. if (!user_access('edit tripal_bulk_loader', $account)) {
  292. return NODE_ACCESS_DENY;
  293. }
  294. return NODE_ACCESS_ALLOW;
  295. }
  296. if ($op == 'delete') {
  297. if (!user_access('delete tripal_bulk_loader', $account)) {
  298. return NODE_ACCESS_DENY;
  299. }
  300. return NODE_ACCESS_ALLOW;
  301. }
  302. if ($op == 'view') {
  303. if (!user_access('access tripal_bulk_loader', $account)) {
  304. return NODE_ACCESS_DENY;
  305. }
  306. return NODE_ACCESS_ALLOW;
  307. }
  308. return NODE_ACCESS_IGNORE;
  309. }