forked from SU-SWS/stanford_capx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stanford_capx.pages.inc
430 lines (344 loc) · 15.5 KB
/
stanford_capx.pages.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
/**
* @file
* @author [author] <[email]>
*/
use CAPx\APILib\HTTPClient;
use CAPx\Drupal\Importer\EntityImporter;
use CAPx\Drupal\Importer\EntityImporterBatch;
use CAPx\Drupal\Mapper\EntityMapper;
use CAPx\Drupal\Mapper\FieldCollectionMapper;
use CAPx\Drupal\Util\CAPx;
use CAPx\Drupal\Util\CAPxMapper;
use CAPx\Drupal\Util\CAPxImporter;
use CAPx\Drupal\Util\CAPxConnection;
use CAPx\Drupal\Organizations\Orgs;
// Pages
// /////////////////////////////////////////////////////////////////////////////
/**
* Stanford CAPx admin overview page menu callback.
* @return string
* HTML representation of this page.
*/
function stanford_capx_admin_config() {
$output = array();
// Management Menu.
$menu_block = stanford_capx_management_menu_block();
$output['content']['capx_menu_block'] = $menu_block;
// Connection Status Block.
$block = block_load('stanford_capx', 'connection_status');
$output['content']['connection_block'] = _block_get_renderable_array(_block_render_blocks(array($block)));
// Header
$output['content']['thetext']['#markup'] = "<h2>" . t('Importers') . "</h2>";
// The view.
$output['content']['overviewview']['#markup'] = views_embed_view('importers_overview_page');
return $output;
}
/**
* Stanford CAPX connect page menu callback.
* @return string
* HTML representation of this page.
*/
function stanford_capx_admin_config_connect() {
$output = array();
// Management Menu.
$menu_block = stanford_capx_management_menu_block();
$output['content']['capx_menu_block'] = $menu_block;
// The settings form found in stanford_capx.forms.inc
$form = drupal_get_form('stanford_capx_forms_connect_form');
$output['content']['config_form'] = $form;
return $output;
}
/**
* Settings page menu callback.
* @return string
* HTML representation of this page.
*/
function stanford_capx_admin_config_settings() {
$output = array();
// Management Menu.
$menu_block = stanford_capx_management_menu_block();
$output['content']['capx_menu_block'] = $menu_block;
$has_orgs = \CAPx\Drupal\Organizations\Orgs::checkOrgs();
if (!$has_orgs) {
drupal_set_message(t('The organization codes are not available. You should sync with the API now.'), 'warning');
}
// Organizations
// ---------------------------------------------------------------------------
$form = drupal_get_form("stanford_capx_config_settings_data_sync_form");
$output['content']["orgsform"] = $form;
// Settings form
// ---------------------------------------------------------------------------
$form = drupal_get_form('stanford_capx_config_settings_form');
$output["content"]["settingsform"] = $form;
return $output;
}
/**
* [stanford_capx_config_settings_data_sync_form description]
* @param [type] $form [description]
* @param [type] $form_state [description]
* @return [type] [description]
*/
function stanford_capx_config_settings_data_sync_form($form, $form_state) {
$form['orggroup'] = array(
'#type' => "fieldset",
'#title' => t("Organizations & Schema"),
'#description' => t("The CAPx module needs some information from the CAP API in order to function properly. Below are actions that require communication with the CAP API server and you must be connected before you can run these tasks."),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
// Organizations.
$form['orggroup']['orgcodeinfo']['#markup'] = "<h3>Organizations</h3>";
$form['orggroup']['orgcodeinfo']['#markup'] .= "<p>" . t("Organization codes come from the CAP API and are saved to a taxonomy. This taxonomy powers the organization code autocomplete in the import section.") . "</p>";
$form['orggroup']['orgcodeinfo']['#markup'] .= "<p>" . t("Last sync: @date", array("@date" => variable_get('capx_last_orgs_sync', 'never'))) . "</p>";
$form['orggroup']['orgcodeinfo']['#markup'] .= "<p>" . t("View all organization codes: !link", array("!link" => l(t('Organization taxonomy'), 'admin/structure/taxonomy/capx_organizations'))) . "</p>";
$form['orggroup']['orgcodeinfo']['#markup'] .= "<p>" . l(t('Get organization data'), 'admin/config/capx/organizations/sync', array("attributes" => array('class' => array('btn button')), 'query' => array('destination' => current_path()))) . "</p>";
// Schema.
$form['orggroup']['schemainfo']['#markup'] = "<br /><h3>Schema Information</h3>";
$form['orggroup']['schemainfo']['#markup'] .= "<p>" . t("The CAP API provides a schema of the information available. The CAPx module needs this in order to power the data browser that is found on a mapping page.") . "</p>";
$form['orggroup']['schemainfo']['#markup'] .= "<p>" . t("Last sync: @date", array("@date" => variable_get('capx_last_schema_sync', 'never'))) . "</p>";
$form['orggroup']['submit'] = array(
'#type' => 'submit',
'#value' => 'Get schema information',
'#op' => 'submit',
);
$form['#submit'][] = "stanford_capx_schema_refresh";
return $form;
}
/**
* Callback url and utility function for running the organizations sync.
* @return mixed
* redirects back to page user was on.
*/
function stanford_capx_organizations_sync() {
try {
watchdog('stanford_capx', 'Organization codes updated', array(), WATCHDOG_INFO);
variable_set("capx_last_orgs_sync", format_date(time()));
\CAPx\Drupal\Organizations\Orgs::syncOrganizations(TRUE);
}
catch(\Exception $e) {
$message = t("ERROR! Could not update organization data. Reason given: %reason", array("%reason" => $e->getMessage()));
watchdog('stanford_capx', $message, array(), WATCHDOG_ERROR);
drupal_set_message($message, "error");
drupal_goto(drupal_get_destination());
}
}
/**
* Stanford Capx help page menu callback.
* @return string
* HTML representation of this page.
*/
function stanford_capx_admin_config_help() {
$output = array();
// Management Menu.
$menu_block = stanford_capx_management_menu_block();
$output['content']['capx_menu_block'] = $menu_block;
$content = "<h2>" . t("Getting started") . "</h2>";
$content .= "<p>" . t("Importing content from CAP can be completed in 3 steps:") . "</p>";
$content .= "<ol><li>";
$content .= t("!connect - Connect to the CAP API using your authentication credentials.", array("!connect" => l(t("Connect"), "admin/config/capx/connect")));
$content .= "</li><li>";
$content .= t("!mapping - Create a mapping that links CAP fields to your own fields.", array("!mapping" => l(t("Mapping"), "admin/config/capx/mapper")));
$content .= "</li><li>";
$content .= t("!import - Choose which profiles you would like to import.", array("!import" => l(t("Import"), "admin/config/capx/importer")));
$content .= "</li></ol>";
$content .= "<p> </p>";
$content .= "<h2>" . t("Autentication credentials") . "</h2>";
$content .= "<p>" . t("Authentication credentials allow you to connect to the CAP API and import content into your website.") . "</p>";
$content .= "<p>" . t("To get authentication credentials, file a !helpsu to Administrative Applications/CAP Stanford Profiles.", array("!helpsu" => l(t("HelpSU request"), "https://helpsu.stanford.edu/helpsu/3.0/auth/helpsu-form?pcat=CAP&dtemplate=CAP-OAuth-Info"))) . "</p>";
$content .= "<p> </p>";
$content .= "<h2>" . t("CAP data - Choosing fields") . "</h2>";
$content .= "<p>" . t("If you need help determining which fields you need in your mapping, you can use the !caplink. The schema displays all fields in CAP and where they are nested. Yes, it is a ton of data, we know...", array("!caplink" => l(t("CAP Data Schema"), "admin/config/capx/data-browser"))) . "</p>";
$content .= "<p><strong>" . t("Common fields: ") . "</strong>";
$content .= t("A list of common fields can be found on the sidebar of any !mappingpage.", array("!mappingpage" => l(t("mapping page"), "admin/config/capx/mapper/new"))) . "</p>";
$content .= "<p> </p>";
$content .= "<h2>" . t("Resources for developers") . "</h2>";
$content .= "<p>" . t("Want to get involved in the development of the CAPx module? Check out the resources below:") . "</p>";
$content .= "<ol><li>";
$content .= l(t("CAPx module documentation"), "https://github.com/SU-SWS/stanford_capx/tree/7.x-1.x/docs");
$content .= "</li><li>";
$content .= l(t("CAPx GitHub repository"), "http://www.github.com/SU-SWS/stanford_capx");
$content .= "</li><li>";
$content .= l(t("CAPx issue queue"), "https://github.com/SU-SWS/stanford_capx/issues");
// $content .= "</li><li>";
// $content .= l(t("CAP API documentation"), "http://www.github.com/SU-SWS/stanford_capx");
$content .= "</li><li>";
$content .= l(t("Working group Jira project"), "https://stanfordits.atlassian.net/");
$content .= "</li></ol>";
$output['content']['textcontent']['#markup'] = $content;
return $output;
}
/**
* Stanford Capx mapper page menu callback.
* @return string
* HTML representation of this page.
*/
function stanford_capx_admin_config_mapper() {
$output = array();
// Management Menu.
$menu_block = stanford_capx_management_menu_block();
$output['content']['capx_menu_block'] = $menu_block;
// Body content.
$output['content']['body']['#markup'] = "<p>" . t("After you have connected to CAP, create a Mapping to link CAP’s fields with your fields") . ".</p>";
$output['content']['body']['#markup'] .= "<p><b>" . l(t('Create new mapping'), 'admin/config/capx/mapper/new', array('attributes' => array('class' => 'new-button button btn'))) . "</b></p>";
// The mapper view.
$output['content']['mapper_view']['#markup'] = views_embed_view('capx_mappers');
return $output;
}
/**
* Stanford Capx create new mapper page menu callback.
* @return string
* HTML representation of this page.
*/
function stanford_capx_admin_config_mapper_new() {
$output = array();
// Management Menu.
$menu_block = stanford_capx_management_menu_block();
$output['content']['capx_menu_block'] = $menu_block;
// Add some css.
$output['#attached']['css'][] = drupal_get_path('module', 'stanford_capx') . "/css/stanford_capx.admin.css";
// Data browser block.
$block = block_load('stanford_capx', 'data_browser_launch');
$output['content']['data_browser'] = _block_get_renderable_array(_block_render_blocks(array($block)));
// Get and render the mapper form.
$output['content']['mapper_form'] = drupal_get_form('stanford_capx_mapper_form');
return $output;
}
/**
* Stanford Capx edit mapper page menu callback.
* @return string
* HTML representation of this page.
*/
function stanford_capx_admin_config_mapper_edit($mapper_machine_name) {
$output = array();
$mapper = capx_cfe_load_by_machine_name($mapper_machine_name, 'mapper');
if (!$mapper) {
throw new Exception(t("Could not load a mapping with provided machine name. Please check url."));
}
// Management Menu.
$menu_block = stanford_capx_management_menu_block();
$output['content']['capx_menu_block'] = $menu_block;
// Add some css.
// drupal_add_css(drupal_get_path('module', 'stanford_capx') . "/css/stanford_capx.admin.css");
$output['#attached']['css'][] = drupal_get_path('module', 'stanford_capx') . "/css/stanford_capx.admin.css";
// Data browser block.
$block = block_load('stanford_capx', 'mapper_in_use');
$output['content']['mapper_in_use'] = _block_get_renderable_array(_block_render_blocks(array($block)));
// Data browser block.
$block = block_load('stanford_capx', 'data_browser_launch');
$output['content']['data_browser_launch'] = _block_get_renderable_array(_block_render_blocks(array($block)));
// Render form.
$output['content']['mapper_form'] = drupal_get_form('stanford_capx_mapper_form', $mapper);
return $output;
}
/**
* Importers page menu callback.
* @return string
* HTML representation of this page.
*/
function stanford_capx_admin_config_import() {
$output = array();
// Management Menu.
$menu_block = stanford_capx_management_menu_block();
$output['content']['capx_menu_block'] = $menu_block;
// Text markup.
$content = "<p>" . t("After you have Mapping configured, create an Importer to chose which profiles you would like to import.") . "</p>";
$content .= "<p>" . t("Importers allow you to chose CAP profiles in bulk by Organizations, or Workgroups, or SunetIDs.") . "</p>";
$content .= "<p>" . l(t('Create new importer'), 'admin/config/capx/importer/new', array('attributes' => array('class' => 'new-button button btn'))) . "</p>";
$output['content']['body']['#markup'] = $content;
// The importer view.
$output['content']['importer_view']['#markup'] = views_embed_view('capx_importers');
return $output;
}
/**
* Create new importer page menu callback.
* @return string
* HTML representation of this page.
*/
function stanford_capx_admin_config_import_new() {
$output = array();
// Management Menu.
$menu_block = stanford_capx_management_menu_block();
$output['content']['capx_menu_block'] = $menu_block;
// Get and render the importer form.
$form = drupal_get_form('stanford_capx_importer_form');
$output['content']['importer_form'] = $form;
return $output;
}
/**
* Edit importer configuration form page menu callback.
* @return string
* HTML representation of this page.
*/
function stanford_capx_admin_config_import_edit($importer_machine_name) {
$output = array();
$importer = CAPxImporter::loadImporter($importer_machine_name);
if (!$importer) {
throw new Exception(t("Could not load an importer with provided machine name. Please check url."));
}
// Management Menu.
$menu_block = stanford_capx_management_menu_block();
$output['content']['capx_menu_block'] = $menu_block;
// The in use block.
$output['content']["in_use_block"] = _block_get_renderable_array(_block_render_blocks(array(block_load('stanford_capx', 'importer_in_use'))));
// Render form.
$output['content']["importer_form"] = drupal_get_form('stanford_capx_importer_form', $importer);
return $output;
}
/**
* Runs a batch on a single importer.
*/
function stanford_capx_sync_importer($machine_name) {
// Find by the name that was passed in.
$importer = CAPxImporter::loadEntityImporter($machine_name);
// Nothing to see here.
if (!$importer) {
$vars = array(
'%name' => $machine_name,
'!log' => l(t('log messages'), 'admin/reports/dblog'),
);
drupal_set_message(t('There was an issue loading the importer with %name machine name. Check !log.', $vars), 'error');
}
if (!$importer->valid()) {
drupal_set_message(t('There was an issue running the importer. Settings did not validate. Please check the error logs for more information.'), 'error');
}
else {
// Set the batch items.
$importer->createBatch();
// Run the show!
batch_process(drupal_get_destination());
}
}
/**
* Runs a batch on a single orphanator.
*
* @param string $machine_name
* The machine name of the entity importer to run an orphan check on.
*
*/
function stanford_capx_orphan_importer($machine_name) {
// Sanity...
$machine_name = check_plain($machine_name);
// Get the orphanator object.
$orphanator = CAPxImporter::getEntityOrphanator($machine_name);
// Nothing to see must throw up.
if (!$orphanator) {
throw new Exception(t("Could not find an entity importer orphanator by that name."));
}
// Set the limit.
$orphanator->setLimit(variable_get('stanford_capx_batch_limit', 100));
// Set the batch items.
$orphanator->batch();
// Call batch.
batch_process(drupal_get_destination());
}
/**
* Runs a batch on all importers.
*/
function stanford_capx_sync_all_importers() {
$importers = CAPxImporter::getAllEntityImporters();
foreach ($importers as $importer) {
$importer->createBatch();
}
batch_process(drupal_get_destination());
}