Tripal Plant PopGen Submit
config.php
Go to the documentation of this file.
1 <?php
2 
19 function tpps_admin_settings(array $form, array &$form_state) {
20 
21  $authors = variable_get('tpps_author_files_dir', 'tpps_authors');
22  $photos = variable_get('tpps_study_photo_files_dir', 'tpps_study_photos');
23  $accession = variable_get('tpps_accession_files_dir', 'tpps_accession');
24  $genotype = variable_get('tpps_genotype_files_dir', 'tpps_genotype');
25  $phenotype = variable_get('tpps_phenotype_files_dir', 'tpps_phenotype');
26  $cartogratree_env = variable_get('tpps_cartogratree_env', FALSE);
27 
28  $form['tpps_maps_api_key'] = array(
29  '#type' => 'textfield',
30  '#title' => t('TPPS Google Maps API Key'),
31  '#default_value' => variable_get('tpps_maps_api_key', NULL),
32  );
33 
34  $form['tpps_ncbi_api_key'] = array(
35  '#type' => 'textfield',
36  '#title' => t('TPPS NCBI EUtils API Key'),
37  '#default_value' => variable_get('tpps_ncbi_api_key', NULL),
38  );
39 
40  $form['tpps_geocode_api_key'] = array(
41  '#type' => 'textfield',
42  '#title' => t('TPPS OpenCage Geocoding API Key'),
43  '#default_value' => variable_get('tpps_geocode_api_key', NULL),
44  );
45 
46  $form['tpps_unpublished_days_threshold'] = array(
47  '#type' => 'textfield',
48  '#title' => t('Number of days before an unpublished study gets highlighted in TPPS Admin panel'),
49  '#default_value' => variable_get('tpps_unpublished_days_threshold', 180),
50  );
51 
52  $form['tpps_gps_epsilon'] = array(
53  '#type' => 'textfield',
54  '#title' => t('TPPS GPS Epsilon'),
55  '#default_value' => variable_get('tpps_gps_epsilon', .001),
56  '#description' => t('This is the amount of error TPPS should allow for when trying to match plants. An epsilon value of 1 is around 100km, and an epsilon value of .001 is around 100 m.'),
57  );
58 
59  $form['tpps_zenodo_api_key'] = array(
60  '#type' => 'textfield',
61  '#title' => t('TPPS Zenodo API Key'),
62  '#default_value' => variable_get('tpps_zenodo_api_key', NULL),
63  );
64 
65  $form['tpps_zenodo_prefix'] = array(
66  '#type' => 'textfield',
67  '#title' => t('TPPS Zenodo Prefix'),
68  '#default_value' => variable_get('tpps_zenodo_prefix', ''),
69  '#description' => t('For testing and development purposes. Set this field to "sandbox." to create dois in the Zenodo sandbox rather than the real site. Please keep in mind that you will need a separate API key for sandbox.zenodo.org.'),
70  );
71 
72  $form['tpps_admin_email'] = array(
73  '#type' => 'textfield',
74  '#title' => t('TPPS Admin Email Address'),
75  '#default_value' => variable_get('tpps_admin_email', ''),
76  );
77 
78  $form['tpps_refresh_views_job_frequency'] = array(
79  '#type' => 'textfield',
80  '#title' => t('TPPS Refresh Views Cron Job Frequency'),
81  '#default_value' => variable_get('tpps_refresh_views_job_frequency', '0 */12 * * *'),
82  '#description' => t("This should be a valid crontab schedule string. If you don't know what that is or would like more information, see") . ' ' . l(t('here'), 'http://man7.org/linux/man-pages/man5/crontab.5.html') . t(". Please also note that if you change this value, you will need to clear your site's cache in order for the change to take effect."),
83  );
84 
85  $form['tpps_delayed_submissions_job_frequency'] = array(
86  '#type' => 'textfield',
87  '#title' => t('TPPS Delayed Submissions Cron Job Frequency'),
88  '#default_value' => variable_get('tpps_delayed_submissions_job_frequency', '0 */6 * * *'),
89  '#description' => t("This should be a valid crontab schedule string. If you don't know what that is or would like more information, see") . ' ' . l(t('here'), 'http://man7.org/linux/man-pages/man5/crontab.5.html') . t(". Please also note that if you change this value, you will need to clear your site's cache in order for the change to take effect."),
90  );
91 
92  $form['tpps_cartogratree_env'] = array(
93  '#type' => 'checkbox',
94  '#title' => t('Use environmental layers from CartograPlant'),
95  '#default_value' => $cartogratree_env,
96  '#description' => t("If CartograPlant is installed, TPPS can add an optional field to the environment section for environment layers, using the data pulled in through CartograPlant."),
97  );
98 
99  if (module_exists('cartogratree') and db_table_exists('cartogratree_groups') and db_table_exists('cartogratree_layers')) {
100  $form['tpps_ct_api_key'] = array(
101  '#type' => 'textfield',
102  '#title' => t('TPPS CartograPlant API Key'),
103  '#default_value' => variable_get('tpps_ct_api_key', NULL),
104  );
105 
106  $form['layer_groups'] = array(
107  '#type' => 'fieldset',
108  '#title' => 'CartograPlant Environmental Layer Groups:',
109  '#description' => t('Please select which layer groups will contain environmental data that is relevant to TPPS. TPPS will use the selected groups to decide which layers to present as environmental options to the users.'),
110  '#states' => array(
111  'visible' => array(
112  ':input[name="tpps_cartogratree_env"]' => array('checked' => TRUE),
113  ),
114  ),
115  );
116 
117  $results = db_select('cartogratree_groups', 'g')
118  ->fields('g', array('group_name', 'group_id'))
119  ->execute();
120 
121  while (($result = $results->fetchObject())) {
122  $form['layer_groups']["tpps_layer_group_{$result->group_id}"] = array(
123  '#type' => 'checkbox',
124  '#title' => $result->group_name,
125  '#default_value' => variable_get("tpps_layer_group_{$result->group_id}", FALSE),
126  );
127  }
128  }
129 
130  $form['tpps_record_group'] = array(
131  '#type' => 'textfield',
132  '#title' => t('TPPS Record max group'),
133  '#default_value' => variable_get('tpps_record_group', 10000),
134  '#description' => t('Some files are very large. TPPS tries to submit as many entries together as possible, in order to speed up the process of writing data to the database. However, very large size entries can cause errors within the Tripal Job daemon. This number is the maximum number of entries that may be submitted at once. Larger numbers will make the process faster, but are more likely to cause errors. Defaults to 10,000.'),
135  );
136 
137  $form['tpps_local_genome_dir'] = array(
138  '#type' => 'textfield',
139  '#title' => t('Reference Genome file directory:'),
140  '#default_value' => variable_get('tpps_local_genome_dir', NULL),
141  '#description' => t('The directory of local genome files on your web server. If left blank, tpps will skip the searching for local genomes step in the tpps genotype section. Local genome files should be organized according to the following structure: <br>[file directory]/[species code]/[version number]/[genome data] where: <br>&emsp;&emsp;[file directory] is the full path to the genome files provided above <br>&emsp;&emsp;[species code] is the 4-letter standard species code - this must match the species code entry in the "chado.organismprop" table<br>&emsp;&emsp;[version number] is the reference genome version, of the format "v#.#"<br>&emsp;&emsp;[genome data] is the actual reference genome files - these can be any format or structure<br>More information is available <a href="https://tpps.rtfd.io/en/latest/config.html" target="blank">here</a>.'),
142  );
143 
144  $form['tpps_author_files_dir'] = array(
145  '#type' => 'textfield',
146  '#title' => t('Author files:'),
147  '#default_value' => $authors,
148  '#description' => t("Currently points to @path.", array('@path' => drupal_realpath("public://$authors"))),
149  '#prefix' => t('<h1>File Upload locations</h1>All file locations are relative to the "public://" file stream. Your current "public://" file stream points to "@path".<br><br>', array('@path' => drupal_realpath('public://'))),
150  );
151 
152  $form['tpps_study_photo_files_dir'] = array(
153  '#type' => 'textfield',
154  '#title' => t('Study photo files:'),
155  '#default_value' => $photos,
156  '#description' => t("Currently points to @path.", array('@path' => drupal_realpath("public://$photos"))),
157  );
158 
159  $form['tpps_tree_pics_files_dir'] = array(
160  '#type' => 'textfield',
161  '#title' => t('Plant Pictures directory:'),
162  '#default_value' => variable_get('tpps_tree_pics_files_dir', NULL),
163  '#description' => t("The directory of plant pictures on your web server. If you do not have any plant pictures on your web server, you can leave this field blank. Currently points to @path.", array('@path' => drupal_realpath("public://" . variable_get('tpps_tree_pics_files_dir', NULL)))),
164  );
165 
166  $form['tpps_accession_files_dir'] = array(
167  '#type' => 'textfield',
168  '#title' => t('Plant Accession files:'),
169  '#default_value' => $accession,
170  '#description' => t("Currently points to @path.", array('@path' => drupal_realpath("public://$accession"))),
171  );
172 
173  $form['tpps_genotype_files_dir'] = array(
174  '#type' => 'textfield',
175  '#title' => t('Genotype files:'),
176  '#default_value' => $genotype,
177  '#description' => t("Currently points to @path.", array('@path' => drupal_realpath("public://$genotype"))),
178  );
179 
180  $form['tpps_phenotype_files_dir'] = array(
181  '#type' => 'textfield',
182  '#title' => t('Phenotype files:'),
183  '#default_value' => $phenotype,
184  '#description' => t("Currently points to @path.", array('@path' => drupal_realpath("public://$phenotype"))),
185  );
186 
187  $form['tpps_update_old_submissions'] = array(
188  '#type' => 'checkbox',
189  '#title' => t('Update Old TPPS Submissions'),
190  '#description' => t('If you save configuration with this option enabled, TPPS will search for all older TPPS Submissions that are no longer compatible with newer versions of TPPS, and will make them compatible again. This works best after using the "tpps/update" tool from the "update_old_submissions" branch on the TPPS gitlab.'),
191  '#default_value' => variable_get('tpps_update_old_submissions', NULL),
192  );
193 
194  return system_settings_form($form);
195 }
196 
202 function tpps_admin_settings_validate($form, &$form_state) {
203  foreach ($form_state['values'] as $key => $value) {
204  if (substr($key, -10) == '_files_dir') {
205  $location = "public://$value";
206  if (!file_prepare_directory($location, FILE_CREATE_DIRECTORY)) {
207  form_set_error("$key", t("Error: path must be valid and current user must have permissions to access that path."));
208  }
209  }
210  elseif ($key == 'tpps_admin_email') {
211  if (!valid_email_address($value)) {
212  form_set_error("$key", t("Error: please enter a valid email address."));
213  }
214  }
215  elseif ($key == 'tpps_cartogratree_env') {
216  if (!empty($value) and !module_exists('cartogratree')) {
217  form_set_error("$key", t("Error: The CartograPlant module is not installed."));
218  }
219  elseif (!empty($value) and (!db_table_exists('cartogratree_groups') or !db_table_exists('cartogratree_layers') or !db_table_exists('cartogratree_fields'))) {
220  form_set_error("$key", t("Error: TPPS was unable to find the required CartograPlant tables for environmental layers."));
221  }
222  }
223  elseif ($key == 'tpps_zenodo_prefix') {
224  if ($value and $value != 'sandbox.') {
225  form_set_error("$key", t("Error: Zenodo Prefix must either be empty or 'sandbox.'"));
226  }
227  }
228  elseif ($key == 'tpps_unpublished_days_threshold') {
229  if (empty($value) or !preg_match('/^[0-9]+$/', $value)) {
230  form_set_error("$key", t("Error: please enter a valid number of days"));
231  }
232  }
233  elseif ($key == 'tpps_update_old_submissions' and !empty($value)) {
234  $update = TRUE;
235  }
236  }
237 
238  if (!empty($update) and !form_get_errors()) {
240  }
241 }
242 
253  $query = db_select('variable', 'v')
254  ->fields('v')
255  ->condition('name', db_like('tpps_complete_') . '%', 'LIKE')
256  ->execute();
257 
258  while (($result = $query->fetchObject())) {
259  $mail = substr($result->name, 14, -7);
260  $user = user_load_by_mail($mail);
261  $state = unserialize($result->value);
262  if (!empty($user)) {
263  $uid = $user->uid;
264  }
265  else {
266  $uid = 21;
267  }
268  $accession = $state['accession'];
269  $dbxref_id = $state['dbxref_id'];
270  $status = $state['status'];
271  db_insert('tpps_submission')
272  ->fields(array(
273  'uid' => $uid,
274  'status' => $status,
275  'accession' => $accession,
276  'dbxref_id' => $dbxref_id,
277  'submission_state' => serialize($state),
278  ))
279  ->execute();
280  variable_del($result->name);
281  }
282 }
tpps_admin_settings(array $form, array &$form_state)
Definition: config.php:19
tpps_admin_settings_validate($form, &$form_state)
Definition: config.php:202
tpps_update_old_submissions()
Definition: config.php:252