-
Notifications
You must be signed in to change notification settings - Fork 0
/
aksearch_learning.admin.inc
executable file
·114 lines (102 loc) · 4.55 KB
/
aksearch_learning.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
<?php /** * @file * Administration page callbacks for the aksearch_learning module. */ /** * Form builder. Configure aksearch_learning. * *
@ingroup forms * @see system_settings_form(). */ function aksearch_learning_admin_settings() {
global $base_url;
$s = $base_url . '/' . drupal_get_path('module', 'aksearch_learning') .'/default/config/';
//drupal_add_js('//code.jquery.com/jquery-1.11.0.min.js', 'external');
drupal_add_js('var ak_conf_url = "'. $s .'";', 'inline');
drupal_add_js(drupal_get_path('module', 'aksearch_learning') .'/aksearch_learning.js');
drupal_add_css(drupal_get_path('module', 'aksearch_learning') .'/aksearch_learning.css');
//Getting aksearch_learning type node ids
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(':type' => 'aksearch_learning'));
//array to hold the nids
$nids = array();
foreach ($result as $row) {
$nids[$row->nid] = $row->nid;
}
reset($nids);
$facets_url = 'http://api.akstem.com/search-api/v1/akif/?q=*&facets=set&facet_size=100';
$result = drupal_http_request($facets_url);
//human and nachine names json file
$mappings = drupal_get_path('module','aksearch_learning').'/default/config/facets_mappings.json';
$mappingsJson = file_get_contents($mappings);
//decode json file and get the 'set' element
$mappingsData = drupal_json_decode($mappingsJson);
$mappingsData = $mappingsData['set'];
//create mapings array with machine name as key and human name as value
$mapArr = array();
foreach ($mappingsData as $value) {
$mapArr[$value['machine']] = $value['human'] ;
}
//get the facet terms from decoded json
$terms = drupal_json_decode($result->data);
$terms = $terms['facets']['set']['terms'];
//if there is human name, save human name as value instead of machine name
foreach ($terms as $term) {
if(array_key_exists($term['term'], $mapArr))
$options[$term['term']] = $mapArr[$term['term']] . '(' . $term['count'] . ')';
else
$options[$term['term']] = $term['term']. '(' . $term['count'] . ')';
}
$form['ak_enable_jquery'] = array(
'#type' => 'select',
'#title' => t('Include jQuery in search pages'),
'#options' => array('0' => 'No' , '1' => 'Yes' ),
'#default_value' => variable_get('ak_enable_jquery', '0'),
'#description' => t('If already using jQuery version >= 1.7 set this option to "No"'),
);
//building the admin form from previous created arrays nids[],options[]
$form['ak_node_id'] = array(
'#type' => 'select',
'#title' => t('Select the node for which you want to configure'),
'#options' => $nids,
'#default_value' => variable_get('ak_node_id', key($nids)),
'#description' => t('Select the node id to save the configuration.'),
);
$form['aksearch_learning_facets'] = array(
'#type' => 'checkboxes',
'#title' => t('Users may search using these facets collections'),
'#options' => $options,
'#default_value' => variable_get('aksearch_learning_facets', array('aglrgfsp')),
'#description' => t('Please select the desired facets collections'),
);
//hardcoded values as options(no json config file)
$form['aksearch_learning_facets_f'] = array(
'#type' => 'checkboxes',
'#title' => t('Users may search using these facets'),
'#options' => array('set' => 'set', 'language' => 'language' , 'controlled.classification.AGROVOC' => 'classification' , 'controlled.type.value' => 'Document Type' ),
'#default_value' => variable_get('aksearch_learning_facets_f', array('language')),
'#description' => t('Please select the desired facets'),
);
$form['#submit'][] = 'aksearch_learning_admin_settings_submit';
return system_settings_form($form);
}
function aksearch_learning_admin_settings_submit($form, $form_state) {
$enabled = array();
$enabled2 = array();
// Loop through each of the checkboxes shown on the form.
foreach ($form_state['values']['aksearch_learning_facets'] as $key => $value) {
if($value){
$enabled[] = $key;
}
} // End foreach loop.
foreach ($form_state['values']['aksearch_learning_facets_f'] as $key => $value) {
if($value){
$enabled2[] = $key;
}
} // End foreach loop.
$conf = drupal_get_path('module', 'aksearch_learning').'/default/config/conf.json';
//getting original conf file and set the new data.. but instead of writing back to disk save configuration in database for a node - config relationship
$jsonString = file_get_contents($conf);
$data = json_decode($jsonString);
$data->facets = $enabled2;
$data->limit_facets->set = $enabled;
//save data as json
$newData = drupal_json_encode($data);
$num_updated = db_update('aksearch_learning_conf')
->fields(array(
'conf' => $newData,
))
->condition('nid', $form_state['values']['ak_node_id'], '=')
->execute();
}