-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsearch_api.admin.inc
executable file
·2411 lines (2232 loc) · 86.3 KB
/
search_api.admin.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Administration page callbacks for the Search API module.
*/
/**
* Page callback that shows an overview of defined servers and indexes.
*
* @see search_api_menu()
*/
function search_api_admin_overview() {
$base_path = backdrop_get_path('module', 'search_api');
backdrop_add_css($base_path . '/css/search_api.admin.css');
backdrop_add_js($base_path . '/js/search_api.admin.js');
$servers = search_api_server_load_multiple(FALSE);
$indexes = array();
// When any entity was not normally created in the database, then show status
// for all.
$show_config_status = FALSE;
foreach (search_api_index_load_multiple(FALSE) as $index) {
$indexes[$index->server][$index->machine_name] = $index;
if (!$show_config_status && $index->status != ENTITY_PLUS_CUSTOM) {
$show_config_status = TRUE;
}
}
// Show disabled servers after enabled ones.
foreach ($servers as $id => $server) {
if (!$server->enabled) {
unset($servers[$id]);
$servers[$id] = $server;
}
if (!$show_config_status && $server->status != ENTITY_PLUS_CUSTOM) {
$show_config_status = TRUE;
}
}
$rows = array();
$t_server = array(
'data' => t('Server'),
'colspan' => 2,
);
$t_index = t('Index');
$t_enabled['data'] = array(
'#theme' => 'image',
'#path' => $base_path . '/enabled.png',
'#alt' => t('enabled'),
'#title' => t('enabled'),
);
$t_enabled['class'] = array('search-api-status');
$t_disabled['data'] = array(
'#theme' => 'image',
'#path' => $base_path . '/disabled.png',
'#alt' => t('disabled'),
'#title' => t('disabled'),
);
$t_disabled['class'] = array('search-api-status');
$t_enable = t('Enable');
$pre_server = 'admin/config/search/search_api/server';
$pre_index = 'admin/config/search/search_api/index';
$enable = '/enable';
foreach ($servers as $server) {
$url = $pre_server . '/' . $server->machine_name;
$row = array();
$row[] = $server->enabled ? $t_enabled : $t_disabled;
if ($show_config_status) {
$row[] = theme('entity_plus_status', array('status' => $server->status));
}
$row[] = $t_server;
$row[] = l($server->name, $url);
$links = array();
// The "Enable" function has no menu link, since a token is required. We add
// it as the first link, since it will most likely be the most useful link
// for a disabled server. (Same for indexes below.)
if (!$server->enabled) {
$links[] = array(
'title' => $t_enable,
'href' => $url . $enable,
'query' => array('token' => backdrop_get_token($server->machine_name)),
);
}
$links = array_merge($links, menu_contextual_links('search-api-server', $pre_server, array($server->machine_name)));
$row[] = array(
'data' => array(
'#type' => 'dropbutton',
'#links' => $links,
),
);
$rows[] = _search_api_deep_copy($row);
if (!empty($indexes[$server->machine_name])) {
foreach ($indexes[$server->machine_name] as $index) {
$url = $pre_index . '/' . $index->machine_name;
$row = array();
$row[] = $index->enabled ? $t_enabled : $t_disabled;
if ($show_config_status) {
$row[] = theme('entity_plus_status', array('status' => $index->status));
}
$row[] = ' ';
$row[] = $t_index;
$row[] = l($index->name, $url);
$links = array();
if (!$index->enabled && $server->enabled) {
$links[] = array(
'title' => $t_enable,
'href' => $url . $enable,
'query' => array('token' => backdrop_get_token($index->machine_name)),
);
}
$links = array_merge($links, menu_contextual_links('search-api-index', $pre_index, array($index->machine_name)));
$row[] = array(
'data' => array(
'#type' => 'dropbutton',
'#links' => $links,
),
);
$rows[] = _search_api_deep_copy($row);
}
}
}
if (!empty($indexes[''])) {
foreach ($indexes[''] as $index) {
$url = $pre_index . '/' . $index->machine_name;
$row = array();
$row[] = $t_disabled;
if ($show_config_status) {
$row[] = theme('entity_plus_status', array('status' => $index->status));
}
$row[] = array(
'data' => $t_index,
'colspan' => 2,
);
$row[] = l($index->name, $url);
$links = menu_contextual_links('search-api-index', $pre_index, array($index->machine_name));
$row[] = array(
'data' => array(
'#type' => 'dropbutton',
'#links' => $links,
),
);
$rows[] = _search_api_deep_copy($row);
}
}
$header = array();
$header[] = t('Status');
if ($show_config_status) {
$header[] = t('Configuration');
}
$header[] = array(
'data' => t('Type'),
'colspan' => 2,
);
$header[] = t('Name');
$header[] = array('data' => t('Operations'));
return array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => array('class' => array('search-api-overview')),
'#empty' => t('There are no search servers or indexes defined yet.'),
);
}
/**
* Form callback showing a form for adding a server.
*/
function search_api_admin_add_server(array $form, array &$form_state) {
backdrop_set_title(t('Add server'));
$class = empty($form_state['values']['class']) ? '' : $form_state['values']['class'];
$form_state['server'] = entity_create('search_api_server', array());
if (empty($form_state['storage']['step_one'])) {
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Server name'),
'#description' => t('Enter the displayed name for the new server.'),
'#maxlength' => 50,
'#required' => TRUE,
);
$form['machine_name'] = array(
'#type' => 'machine_name',
'#maxlength' => 50,
'#machine_name' => array(
'exists' => 'search_api_server_load',
),
);
$form['enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enabled'),
'#description' => t('Select if the new server will be enabled after creation.'),
'#default_value' => TRUE,
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Server description'),
'#description' => t('Enter a description for the new server.'),
);
$form['class'] = array(
'#type' => 'select',
'#title' => t('Service class'),
'#description' => t('Choose a service class to use for this server.'),
'#options' => array('' => '< ' . t('Choose a service class') . ' >'),
'#required' => TRUE,
'#default_value' => $class,
'#ajax' => array(
'callback' => 'search_api_admin_add_server_ajax_callback',
'wrapper' => 'search-api-class-options',
),
);
}
elseif (!$class) {
$class = $form_state['storage']['step_one']['class'];
}
foreach (search_api_get_service_info() as $id => $info) {
if (empty($form_state['storage']['step_one'])) {
$form['class']['#options'][$id] = $info['name'];
}
if (!$class || $class != $id) {
continue;
}
$service = NULL;
if (class_exists($info['class'])) {
$service = new $info['class']($form_state['server']);
}
if (!($service instanceof SearchApiServiceInterface)) {
watchdog('search_api', t('Service class @id specifies an illegal class: @class', array('@id' => $id, '@class' => $info['class'])), NULL, WATCHDOG_ERROR);
continue;
}
$service_form = isset($form['options']['form']) ? $form['options']['form'] : array();
$service_form = $service->configurationForm($service_form, $form_state);
$form['options']['form'] = $service_form ? $service_form : array('#markup' => t('There are no configuration options for this service class.'));
$form['options']['class']['#type'] = 'value';
$form['options']['class']['#value'] = $class;
$form['options']['#type'] = 'fieldset';
$form['options']['#tree'] = TRUE;
$form['options']['#collapsible'] = TRUE;
$form['options']['#title'] = $info['name'];
$form['options']['#description'] = $info['description'];
}
$form['options']['#prefix'] = '<div id="search-api-class-options">';
$form['options']['#suffix'] = '</div>';
// If $info is not set, there are no service classes. Display an error message
// telling the user how to change that and return an empty form.
if (!isset($info)) {
backdrop_set_message(t('There are no service classes available for the Search API. Please install a <a href="@url">module that provides a service class</a> to proceed.', array('@url' => url('https://github.com/backdrop-contrib/search_api/wiki/Search-API-Documentation#service-class'))), 'error');
return array();
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Create server'),
);
return $form;
}
/**
* Form AJAX handler for search_api_admin_add_server().
*
* Just returns the "options" array of the already built form array.
*/
function search_api_admin_add_server_ajax_callback(array $form, array &$form_state) {
return $form['options'];
}
/**
* Form validation handler for adding a server.
*
* Validates the machine name and calls the service class' validation handler.
*/
function search_api_admin_add_server_validate(array $form, array &$form_state) {
if (!empty($form_state['values']['machine_name'])) {
$name = $form_state['values']['machine_name'];
if (is_numeric($name)) {
form_set_error('machine_name', t('The machine name must not be a pure number.'));
}
}
if (empty($form_state['values']['options']['class'])) {
return;
}
$class = $form_state['values']['options']['class'];
$info = search_api_get_service_info($class);
$service = NULL;
if (class_exists($info['class'])) {
$service = new $info['class']($form_state['server']);
}
if (!($service instanceof SearchApiServiceInterface)) {
form_set_error('class', t('There seems to be something wrong with the selected service class.'));
return;
}
$form_state['values']['options']['service'] = $service;
if (!empty($form_state['values']['options']['form'])) {
$service->configurationFormValidate($form['options']['form'], $form_state['values']['options']['form'], $form_state);
}
}
/**
* Form submission handler for adding a server.
*/
function search_api_admin_add_server_submit(array $form, array &$form_state) {
form_state_values_clean($form_state);
$values = $form_state['values'];
if (!empty($form_state['storage']['step_one'])) {
$values += $form_state['storage']['step_one'];
unset($form_state['storage']);
}
if (empty($values['options']) || ($values['class'] != $values['options']['class'])) {
unset($values['options']);
$form_state['storage']['step_one'] = $values;
$form_state['rebuild'] = TRUE;
backdrop_set_message(t('Please configure the used service.'));
return;
}
$options = isset($values['options']['form']) ? $values['options']['form'] : array();
unset($values['options']);
$form_state['server'] = $server = entity_create('search_api_server', $values);
$server->configurationFormSubmit($form['options']['form'], $options, $form_state);
$server->save();
$form_state['redirect'] = 'admin/config/search/search_api/server/' . $server->machine_name;
backdrop_set_message(t('The server was successfully created.'));
}
/**
* Page callback: Displays information about a server.
*
* @param SearchApiServer $server
* The server to display.
* @param string|null $action
* (optional) An action to execute for the server. One of 'enable', 'disable'
* or 'clear'.
*
* @see search_api_menu()
*/
function search_api_admin_server_view(SearchApiServer $server, $action = NULL) {
if (!empty($action)) {
if ($action == 'enable') {
if (isset($_GET['token']) && backdrop_valid_token($_GET['token'], $server->machine_name)) {
if ($server->update(array('enabled' => 1))) {
backdrop_set_message(t('The server was successfully enabled.'));
}
else {
backdrop_set_message(t('The server could not be enabled. Check the logs for details.'), 'error');
}
backdrop_goto('admin/config/search/search_api/server/' . $server->machine_name);
}
else {
return MENU_ACCESS_DENIED;
}
}
else {
$ret = backdrop_get_form('search_api_admin_confirm', 'server', $action, $server);
if (!empty($ret['actions'])) {
return $ret;
}
}
}
backdrop_set_title(search_api_admin_item_title($server));
$class = search_api_get_service_info($server->class);
$options = $server->viewSettings();
$indexes = array();
foreach (search_api_index_load_multiple(FALSE, array('server' => $server->machine_name)) as $index) {
if (!$indexes) {
$indexes['#theme'] = 'links';
$indexes['#attributes']['class'] = array('inline');
}
$indexes['#links'][] = array(
'title' => $index->name,
'href' => 'admin/config/search/search_api/index/' . $index->machine_name,
);
}
$render['view'] = array(
'#theme' => 'search_api_server',
'#id' => $server->id,
'#name' => $server->name,
'#machine_name' => $server->machine_name,
'#description' => $server->description,
'#enabled' => $server->enabled,
'#class_id' => $server->class,
'#class_name' => $class['name'],
'#class_description' => $class['description'],
'#indexes' => $indexes,
'#options' => $options,
'#status' => $server->status,
'#extra' => $server->getExtraInformation(),
);
$render['#attached']['css'][] = backdrop_get_path('module', 'search_api') . '/css/search_api.admin.css';
if ($server->enabled) {
$render['form'] = backdrop_get_form('search_api_server_status_form', $server);
}
return $render;
}
/**
* Returns HTML for displaying a server.
*
* @param array $variables
* An associative array containing:
* - id: The server's id.
* - name: The server's name.
* - machine_name: The server's machine name.
* - description: The server's description.
* - enabled: Boolean indicating whether the server is enabled.
* - class_id: The used service class' ID.
* - class_name: The used service class' display name.
* - class_description: The used service class' description.
* - indexes: A list of indexes associated with this server, either as an HTML
* string or a render array.
* - options: An HTML string or render array containing information about the
* server's service-specific settings.
* - status: The entity configuration status (in database, in code, etc.).
* - extra: An array of additional server information in the format specified
* by SearchApiAbstractService::getExtraInformation().
*
* @return string
* HTML for displaying a server.
*
* @ingroup themeable
*/
function theme_search_api_server(array $variables) {
$machine_name = $variables['machine_name'];
$description = $variables['description'];
$enabled = $variables['enabled'];
$class_id = $variables['class_id'];
$class_name = $variables['class_name'];
$indexes = $variables['indexes'];
$options = $variables['options'];
$status = $variables['status'];
$extra = $variables['extra'];
// First, output the index description if there is one set.
$output = '';
if ($description) {
$output .= '<p class="description">' . nl2br(check_plain($description)) . '</p>';
}
// Then, display a table summarizing the index's status.
$rows = array();
// Create a row template with references so we don't have to deal with the
// complicated structure for each individual row.
$row = array(
'data' => array(
array('header' => TRUE),
'',
),
'class' => array(''),
);
$label = & $row['data'][0]['data'];
$info = & $row['data'][1];
$class = & $row['class'][0];
if ($enabled) {
$class = 'ok';
$info = t('enabled (!disable_link)', array('!disable_link' => l(t('disable'), 'admin/config/search/search_api/server/' . $machine_name . '/disable')));
}
else {
$class = 'warning';
$info = t('disabled (!enable_link)', array('!enable_link' => l(t('enable'), 'admin/config/search/search_api/server/' . $machine_name . '/enable', array('query' => array('token' => backdrop_get_token($machine_name))))));
}
$label = t('Status');
$rows[] = _search_api_deep_copy($row);
$class = '';
$label = t('Service class');
if (module_exists('help')) {
$url_options['fragment'] = backdrop_clean_css_identifier($class_id);
$info = l($class_name, 'admin/help/search_api', $url_options);
}
else {
$info = check_plain($class_name);
}
$rows[] = _search_api_deep_copy($row);
if ($indexes) {
$label = t('Search indexes');
$info = render($indexes);
$rows[] = _search_api_deep_copy($row);
}
if ($options) {
$label = t('Service options');
$info = render($options);
$rows[] = _search_api_deep_copy($row);
}
if ($status != ENTITY_PLUS_CUSTOM) {
$label = t('Configuration status');
$info = theme('entity_plus_status', array('status' => $status));
$class = ($status == ENTITY_PLUS_OVERRIDDEN) ? 'warning' : 'ok';
$rows[] = _search_api_deep_copy($row);
$class = '';
}
if ($extra) {
foreach ($extra as $information) {
$label = $information['label'];
$info = $information['info'];
$class = !empty($information['status']) ? $information['status'] : '';
$rows[] = _search_api_deep_copy($row);
}
}
$theme['rows'] = $rows;
$theme['attributes']['class'][] = 'search-api-summary';
$theme['attributes']['class'][] = 'search-api-server-summary';
$theme['attributes']['class'][] = 'system-status-report';
$output .= theme('table', $theme);
return $output;
}
/**
* Form constructor for server operations.
*
* @param SearchApiServer $server
* The server for which the form is displayed.
*
* @ingroup forms
*
* @see search_api_server_status_form_submit()
*/
function search_api_server_status_form(array $form, array &$form_state, SearchApiServer $server) {
$form_state['server'] = $server;
$form['clear'] = array(
'#type' => 'submit',
'#value' => t('Delete all indexed data on this server'),
'#submit' => array('search_api_server_status_form_clear_submit'),
);
$count = $server->enabled ? search_api_server_tasks_count($server) : 0;
if ($count) {
$message = format_plural($count, '@count pending task must be executed before indexing.', '@count pending tasks must be executed before indexing.');
backdrop_set_message($message, 'warning', FALSE);
$form['execute_pending_tasks'] = array(
'#type' => 'submit',
'#value' => t('Execute all pending tasks on this server'),
'#submit' => array('search_api_server_status_form_execute_pending_tasks_submit'),
);
}
return $form;
}
/**
* Form submission handler for search_api_server_status_form().
*
* Used for the "Execute all pending tasks" button.
*/
function search_api_server_status_form_execute_pending_tasks_submit($form, &$form_state) {
$server_id = $form_state['server']->machine_name;
$form_state['redirect'] = "admin/config/search/search_api/server/$server_id/execute-tasks";
}
/**
* Form submission handler for search_api_server_status_form().
*
* Used for the "Delete all indexed data" button.
*/
function search_api_server_status_form_clear_submit(array $form, array &$form_state) {
$server_id = $form_state['server']->machine_name;
$form_state['redirect'] = "admin/config/search/search_api/server/$server_id/clear";
}
/**
* Form constructor for editing a server's settings.
*
* @param SearchApiServer $server
* The server to edit.
*
* @ingroup forms
*
* @see search_api_admin_server_edit_validate()
* @see search_api_admin_server_edit_submit()
*/
function search_api_admin_server_edit(array $form, array &$form_state, SearchApiServer $server) {
$form_state['server'] = $server;
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Server name'),
'#description' => t('Enter the displayed name for the server.'),
'#maxlength' => 50,
'#default_value' => $server->name,
'#required' => TRUE,
);
$form['enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enabled'),
'#default_value' => $server->enabled,
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Server description'),
'#description' => t('Enter a description for the new server.'),
'#default_value' => $server->description,
);
$class = search_api_get_service_info($server->class);
$service_options = array();
$service_options = $server->configurationForm($service_options, $form_state);
if ($service_options) {
$form['options']['form'] = $service_options;
}
$form['options']['#type'] = 'fieldset';
$form['options']['#tree'] = TRUE;
$form['options']['#collapsible'] = TRUE;
$form['options']['#title'] = $class['name'];
$form['options']['#description'] = $class['description'];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save settings'),
);
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#submit' => array('search_api_admin_form_delete_submit'),
'#limit_validation_errors' => array(),
);
return $form;
}
/**
* Form validation handler for search_api_admin_server_edit().
*
* @see search_api_admin_server_edit_submit()
*/
function search_api_admin_server_edit_validate(array $form, array &$form_state) {
if (!empty($form['options']['form']) && !empty($form_state['values']['options']['form'])) {
$form_state['server']->configurationFormValidate($form['options']['form'], $form_state['values']['options']['form'], $form_state);
}
}
/**
* Form submission handler for search_api_admin_server_edit().
*
* @see search_api_admin_server_edit_validate()
*/
function search_api_admin_server_edit_submit(array $form, array &$form_state) {
form_state_values_clean($form_state);
$values = $form_state['values'];
$server = $form_state['server'];
if (isset($values['options'])) {
$server->configurationFormSubmit($form['options']['form'], $values['options']['form'], $form_state);
}
unset($values['options']);
$server->update($values);
$form_state['redirect'] = 'admin/config/search/search_api/server/' . $server->machine_name;
backdrop_set_message(t('The search server was successfully edited.'));
}
/**
* Form submission handler for search_api_admin_server_edit().
*
* Handles the 'Delete' button on the server and index edit forms.
*
* @see search_api_admin_server_edit()
* @see search_api_admin_index_edit()
*/
function search_api_admin_form_delete_submit($form, &$form_state) {
$destination = array();
if (isset($_GET['destination'])) {
$destination = backdrop_get_destination();
unset($_GET['destination']);
}
if (isset($form_state['server'])) {
$server = $form_state['server'];
$form_state['redirect'] = array('admin/config/search/search_api/server/' . $server->machine_name . '/delete', array('query' => $destination));
}
elseif (isset($form_state['index'])) {
$index = $form_state['index'];
$form_state['redirect'] = array('admin/config/search/search_api/index/' . $index->machine_name . '/delete', array('query' => $destination));
}
}
/**
* Form constructor for adding an index.
*
* @ingroup forms
*
* @see search_api_admin_add_index_ajax_callback()
* @see search_api_admin_add_index_validate()
* @see search_api_admin_add_index_submit()
*/
function search_api_admin_add_index(array $form, array &$form_state) {
backdrop_set_title(t('Add index'));
$old_type = empty($form_state['values']['item_type']) ? '' : $form_state['values']['item_type'];
$form['#attached']['css'][] = backdrop_get_path('module', 'search_api') . '/css/search_api.admin.css';
$form['#tree'] = TRUE;
if (empty($form_state['step_one'])) {
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Index name'),
'#maxlength' => 50,
'#required' => TRUE,
);
$form['machine_name'] = array(
'#type' => 'machine_name',
'#maxlength' => 50,
'#machine_name' => array(
'exists' => 'search_api_index_load',
),
);
$form['item_type'] = array(
'#type' => 'select',
'#title' => t('Item type'),
'#description' => t('Select the type of items that will be indexed in this index. ' .
'This setting cannot be changed afterwards.'),
'#options' => array(),
'#required' => TRUE,
'#ajax' => array(
'callback' => 'search_api_admin_add_index_ajax_callback',
'wrapper' => 'search-api-datasource-options',
),
);
$form['datasource'] = array();
foreach (search_api_get_item_type_info() as $type => $info) {
$form['item_type']['#options'][$type] = $info['name'];
}
$form['enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enabled'),
'#description' => t('This will only take effect if you also select a server for the index.'),
'#default_value' => TRUE,
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Index description'),
);
$form['server'] = array(
'#type' => 'select',
'#title' => t('Server'),
'#description' => t('Select the server this index should reside on.'),
'#default_value' => '',
'#options' => array('' => t('< No server >')),
);
$servers = search_api_server_load_multiple(FALSE, array('enabled' => 1));
// List enabled servers first.
foreach ($servers as $server) {
if ($server->enabled) {
$form['server']['#options'][$server->machine_name] = $server->name;
}
}
foreach ($servers as $server) {
if (!$server->enabled) {
$form['server']['#options'][$server->machine_name] = t('@server_name (disabled)', array('@server_name' => $server->name));
}
}
$form['read_only'] = array(
'#type' => 'checkbox',
'#title' => t('Read only'),
'#description' => t('Do not write to this index or track the status of items in this index.'),
'#default_value' => FALSE,
);
$form['options']['index_directly'] = array(
'#type' => 'checkbox',
'#title' => t('Index items immediately'),
'#description' => t('Immediately index new or updated items instead of waiting for the next cron run. ' .
'This might have serious performance drawbacks and is generally not advised for larger sites.'),
'#default_value' => FALSE,
);
$form['options']['cron_limit'] = array(
'#type' => 'number',
'#title' => t('Cron batch size'),
'#description' => t('Set how many items will be indexed at once when indexing items during a cron run. ' .
'"0" means that no items will be indexed by cron for this index, "-1" means that cron should index all items at once.'),
'#default_value' => SEARCH_API_DEFAULT_CRON_LIMIT,
'#size' => 4,
'#attributes' => array('class' => array('search-api-cron-limit')),
'#min' => -1,
);
}
elseif (!$old_type) {
$old_type = $form_state['step_one']['item_type'];
}
if ($old_type) {
$datasource = search_api_get_datasource_controller($old_type);
$datasource_form = array();
$datasource_form = $datasource->configurationForm($datasource_form, $form_state);
if ($datasource_form) {
$form['datasource'] = $datasource_form;
$form['datasource']['#parents'] = array('options', 'datasource');
}
}
$form['datasource']['#prefix'] = '<div id="search-api-datasource-options">';
$form['datasource']['#suffix'] = '</div>';
$form['old_type'] = array(
'#type' => 'value',
'#value' => $old_type,
);
$form['datasource_config'] = array(
'#type' => 'value',
'#value' => !empty($datasource_form),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Create index'),
);
return $form;
}
/**
* AJAX submit callback for search_api_admin_add_index().
*
* Used for displaying the matching datasource configuration form for the
* selected item type.
*/
function search_api_admin_add_index_ajax_callback(array $form, array &$form_state) {
return $form['datasource'];
}
/**
* Form validation handler for search_api_admin_add_index().
*
* @see search_api_admin_add_index_submit()
*/
function search_api_admin_add_index_validate(array $form, array &$form_state) {
$values = $form_state['values'];
$name = $values['machine_name'];
if (is_numeric($name)) {
form_set_error('machine_name', t('The machine name must not be a pure number.'));
}
if (!$values['datasource_config'] || empty($values['item_type']) || $values['item_type'] != $values['old_type']) {
return;
}
$datasource = search_api_get_datasource_controller($values['item_type']);
$datasource->configurationFormValidate($form['datasource'], $form_state['values']['options']['datasource'], $form_state);
}
/**
* Form submission handler for search_api_admin_add_index().
*
* @see search_api_admin_add_index_validate()
*/
function search_api_admin_add_index_submit(array $form, array &$form_state) {
form_state_values_clean($form_state);
$values = $form_state['values'];
if (!empty($form_state['step_one'])) {
$values += $form_state['step_one'];
unset($form_state['step_one']);
}
// The type was changed (or the form submitted without JS for the first time).
// If the new type has a configuration form, we have to display it now.
$datasource = search_api_get_datasource_controller($values['item_type']);
if ($values['item_type'] != $values['old_type']) {
$datasource_form = array();
if ($datasource->configurationForm($datasource_form, $form_state)) {
unset($values['options']['datasource']);
$form_state['step_one'] = $values;
$form_state['rebuild'] = TRUE;
backdrop_set_message(t('Please specify further configuration options.'));
return;
}
}
// If the current type has a configuration form, call the datasource
// controller's config submit callback.
if ($values['datasource_config']) {
$datasource->configurationFormSubmit($form['datasource'], $values['options']['datasource'], $form_state);
}
// Validation of whether a server is set for the index is done in the
// SearchApiIndex::save() method.
search_api_index_insert($values);
backdrop_set_message(t('The index was successfully created. Please set up its indexed fields now.'));
$form_state['redirect'] = 'admin/config/search/search_api/index/' . $values['machine_name'] . '/fields';
}
/**
* Page callback for displaying an index's status.
*
* @param SearchApiIndex $index
* The index to display.
* @param string|null $action
* (optional) An action to execute for the index. One of "reindex", "clear",
* "enable" or "disable". For "disable", a confirm dialog will be shown.
*
* @see search_api_menu()
*/
function search_api_admin_index_view(SearchApiIndex $index, $action = NULL) {
if (!empty($action)) {
if ($action == 'enable') {
if (isset($_GET['token']) && backdrop_valid_token($_GET['token'], $index->machine_name)) {
if ($index->update(array('enabled' => 1))) {
backdrop_set_message(t('The index was successfully enabled.'));
}
else {
backdrop_set_message(t('The index could not be enabled. Check the logs for details.'), 'error');
}
backdrop_goto('admin/config/search/search_api/index/' . $index->machine_name);
}
else {
return MENU_ACCESS_DENIED;
}
}
else {
$ret = backdrop_get_form('search_api_admin_confirm', 'index', $action, $index);
if (!empty($ret['actions'])) {
return $ret;
}
}
}
$status = search_api_index_status($index);
try {
$server = $index->server();
}
catch (SearchApiException $e) {
$server = NULL;
$vars['%server'] = $index->server;
$message = t('The index has an unknown server (ID: %server) set. Please check the index settings.', $vars);
backdrop_set_message($message, 'error');
}
$ret['view'] = array(
'#theme' => 'search_api_index',
'#id' => $index->id,
'#name' => $index->name,
'#machine_name' => $index->machine_name,
'#description' => $index->description,
'#item_type' => $index->item_type,
'#datasource_config' => $index->datasource()->getConfigurationSummary($index),
'#enabled' => $index->enabled,
'#server' => $server,
'#options' => $index->options,
'#fields' => $index->getFields(),
'#indexed_items' => $status['indexed'],
'#on_server' => NULL,
'#total_items' => $status['total'],
'#status' => $index->status,
'#read_only' => $index->read_only,
);
try {
$ret['view']['#on_server'] = _search_api_get_items_on_server($index);
}
catch (SearchApiException $e) {
watchdog_exception('search_api', $e);
}
if ($index->enabled && !$index->read_only) {
$ret['form'] = backdrop_get_form('search_api_admin_index_status_form', $index, $status);
}
return $ret;
}
/**
* Returns HTML for a search index.
*
* @param array $variables
* An associative array containing:
* - id: The index's id.
* - name: The index' name.
* - machine_name: The index' machine name.
* - description: The index' description.
* - item_type: The type of items stored in this index.
* - datasource_config: A summary of the datasource's configuration.
* - enabled: Boolean indicating whether the index is enabled.
* - server: The server this index currently rests on, if any.
* - options: The index' options, like cron limit.
* - fields: All indexed fields of the index.
* - indexed_items: The number of items already indexed in their latest
* version on this index.
* - on_server: The number of items actually indexed on the server. NULL if
* the search for finding out the item count failed.
* - total_items: The total number of items that have to be indexed for this
* index.
* - status: The entity configuration status (in database, in code, etc.).