Tripal Plant PopGen Submit
tpps.api.inc
Go to the documentation of this file.
1 <?php
2 
13 require_once 'tpps.submission.api.inc';
14 require_once 'tpps.queries.api.inc';
15 
33 function tpps_api_paths() {
34  $items = array();
35 
36  // tpps/api/refresh_views calls tpps_api_refresh_views which will
37  // update the tpps specific views used in particular for details pages
38  $items['tpps/api/refresh_views'] = array(
39  'title' => 'TPPS Refresh Views',
40  'page callback' => 'tpps_api_refresh_views',
41  'page arguments' => array(),
42  'access arguments' => array('access content'),
43  'delivery callback' => 'drupal_json_output',
44  'type' => MENU_CALLBACK,
45  );
46 
47  // tpps/api/check_accession calls tpps_api_check_accession on a provided
48  // accession number. It will return TRUE for accessions with a valid
49  // TPPS submission and FALSE for all other accessions.
50  $items['tpps/api/check_accession/%'] = array(
51  'title' => 'TPPS Check Accession',
52  'page callback' => 'tpps_api_check_accession',
53  'page arguments' => array(3),
54  'access callback' => 'tpps_access',
55  'access arguments' => array('access content'),
56  'delivery callback' => 'drupal_json_output',
57  'type' => MENU_CALLBACK,
58  );
59 
60  // tpps/api/submission calls tpps_load_submission on a provided accession.
61  // If a valid TPPS submission exists for the accession, it will return the
62  // form_state of the submission.
63  $items['tpps/api/submission/%'] = array(
64  'title' => 'TPPS Raw Submissions',
65  'page callback' => 'tpps_load_submission',
66  'page arguments' => array(3),
67  'access callback' => 'tpps_access',
68  'access arguments' => array('access content'),
69  'delivery callback' => 'drupal_json_output',
70  'type' => MENU_CALLBACK,
71  );
72 
73  // tpps/api/submissions calls tpps_api_submission_query. If no query
74  // conditions are provided, it will return information about all of the
75  // existing valid TPPS submissions. Otherwise, it will filter the submissions
76  // based on the provided conditions. Please see the tpps_api_submission_query
77  // function for more details.
78  $items['tpps/api/submissions'] = array(
79  'title' => 'TPPS Submissions Query',
80  'page callback' => 'tpps_api_submission_query',
81  'page arguments' => array(),
82  'access callback' => 'tpps_access',
83  'access arguments' => array('access content'),
84  'delivery callback' => 'drupal_json_output',
85  'type' => MENU_CALLBACK,
86  );
87 
88  // tpps/api/submission_info calls tpps_api_submission_info. It will return
89  // data on multiple TPPS submissions based on accessions provided in the
90  // API query. The data returned can be specified using the data query
91  // argument. Please see the tpps_api_submission_info function for more
92  // details.
93  $items['tpps/api/submission_info'] = array(
94  'title' => 'TPPS Submission Data',
95  'page callback' => 'tpps_api_submission_info',
96  'page arguments' => array(),
97  'access callback' => 'tpps_access',
98  'access arguments' => array('access content'),
99  'delivery callback' => 'drupal_json_output',
100  'type' => MENU_CALLBACK,
101  );
102 
103  $items['tpps/api/files/%'] = array(
104  'title' => 'TPPS Submission Files Download',
105  'page callback' => 'tpps_api_compress_files',
106  'page arguments' => array(3),
107  'access callback' => 'tpps_access',
108  'access arguments' => array('access tpps details'),
109  'type' => MENU_NORMAL_ITEM,
110  );
111 
112  return $items;
113 }
114 
131 function tpps_api_call($type, $id = NULL, array $query = array()) {
132  global $base_url;
133  $path = $base_url . "/tpps/api/$type";
134  if (!empty($id)) {
135  $path .= "/$id";
136  }
137  if (!empty($query)) {
138  $args = array();
139  foreach ($query as $key => $val) {
140  $args[] = "$key=$val";
141  }
142  $path .= "?" . implode("&", $args);
143  }
144  $response = file_get_contents($path);
145  return json_decode($response);
146 }
147 
160 function tpps_api_check_accession($accession = NULL) {
161  if (empty($accession)) {
162  return FALSE;
163  }
164 
165  return tpps_load_submission($accession) ? TRUE : FALSE;
166 }
167 
174 function tpps_api_compress_files($accession = NULL) {
175  $state = tpps_load_submission($accession);
176  if (empty($state)) {
177  throw new Exception('Invalid state accession number');
178  }
179  ob_end_clean();
180  $zip_name = "{$accession}_files.zip";
181  if (!file_exists(drupal_realpath(TPPS_TEMP_ZIP))) {
182  mkdir(drupal_realpath(TPPS_TEMP_ZIP));
183  }
184  $zip_loc = drupal_realpath(TPPS_TEMP_ZIP) . '/' . $zip_name;
185 
186  $files = $state['files'];
187  foreach ($files as $fid) {
188  $file_str[] = str_replace(' ', '\\ ', drupal_realpath(file_load($fid)->uri));
189  }
190 
191  if (file_exists(drupal_realpath($zip_loc))) {
192  unlink($zip_loc);
193  }
194 
195  $file_str = implode(" ", $file_str);
196  $cmd = "zip -j $zip_loc $file_str";
197  exec($cmd);
198  drupal_add_http_header('Content-Type', 'application/zip');
199  drupal_add_http_header('Content-Disposition', 'attachment; filename="' . $zip_name . '"');
200  drupal_add_http_header('Content-length', filesize($zip_loc));
201  drupal_add_http_header('Pragma', "no-cache");
202  drupal_add_http_header('Expires', '0');
203 
204  readfile("$zip_loc");
205  exit;
206 }
207 
208 
210  db_query('refresh materialized view chado.tpps_search_genotype_name;');
211  db_query('refresh materialized view chado.tpps_search_genotype_marker;');
212  return TRUE;
213 }
const TPPS_TEMP_ZIP
Definition: tpps.module:17
tpps_api_paths()
Definition: tpps.api.inc:33
tpps_api_compress_files($accession=NULL)
Definition: tpps.api.inc:174
tpps_api_call($type, $id=NULL, array $query=array())
Definition: tpps.api.inc:131
tpps_load_submission($accession, $state=TRUE)
Definition: submissions.inc:27
tpps_api_refresh_views()
Definition: tpps.api.inc:209
tpps_api_check_accession($accession=NULL)
Definition: tpps.api.inc:160