-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_analysis.module
291 lines (256 loc) · 8.75 KB
/
csv_analysis.module
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
<?php
// $Id$
/**
* Implementation of hook_init
*/
function csv_analysis_init() {
$mpath = drupal_get_path('module', 'csv_analysis');
}
/**
* Implementation of hook_menu
*/
function csv_analysis_menu() {
$items['csv_analysis/csv_export'] = array(
'page callback' => 'csv_analysis_export',
'title' => 'CSV Export',
'type' => MENU_CALLBACK,
'access arguments' => array('csv analysis access'),
'access callback' => 'user_access',
);
return $items;
}
/**
* Function building the csv file and returning it to the client
*/
function csv_analysis_export(){
$csv_output = array();
$terms = array();
$vocabularies = array();
// Load all nodes
$results = db_query("SELECT nid, title, type from {node}");
$csv_output[0]['nid'] = 'nid';
$csv_output[0]['title'] = 'title';
$csv_output[0]['type'] = 'type';
foreach ($results as $row) {
$node = node_load($row->nid);
$csv_output[$node->nid]['nid'] = $node->nid;
$csv_output[$node->nid]['title'] = $node->title;
$csv_output[$node->nid]['type'] = $node->type;
$node_array = (array)$node;
foreach ($node_array as $key => $value) {
if(isset($value['und'])) {
foreach ($value['und'] as $candiate) {
if(isset($candiate['tid'])) {
if(!isset($terms[$candiate['tid']])){
$terms[$candiate['tid']] = taxonomy_term_load($candiate['tid']);
}
$term = $terms[$candiate['tid']];
if(!isset($vocabularies[$term->vid])) {
$vocabularies[$term->vid] = taxonomy_vocabulary_load($term->vid);
$csv_output[0][$term->vid] = t($vocabularies[$term->vid]->name);
}
$vocabulary = $vocabularies[$term->vid];
$csv_output[$node->nid][$vocabulary->vid][] = t($term->name);
}
}
}
}
}
//Build CSV
$csv = '';
foreach ($csv_output as $row) {
$row_output = '';
foreach ($csv_output[0] as $column => $value){
if(isset($row[$column])) {
$field_output = '';
if (is_array($row[$column])) {
foreach ($row[$column] as $term) {
if(!empty($field_output)){
$field_output .= ',';
}
$field_output.= $term;
}
}
else {
$field_output = $row[$column];
}
if(!empty($row_output)) {
$row_output .= ';';
}
$row_output .= $field_output;
}
else
{
if(!empty($row_output)) {
$row_output .= ';';
}
}
}
if(!empty($csv)){
$csv .= "\n";
}
$csv .= $row_output;
}
$application="text/csv";
header( "Content-Type: ".$application."; charset=ISO-8859-1" );
header( "Content-Disposition: attachment; filename=\"yrp-export.csv\"");
header( "Content-Description: csv File" );
header( "Pragma: no-cache" );
header( "Expires: 0" );
echo utf8_decode($csv); //Output has to be ISO-8859-1. Otherwise Excel is not displaying special characters the right way
exit();
}
/**
* Function building the csv file and returning it to the client
*/
function csv_analysis_export_old(){
//Setting for multiple terms
$multiterms = variable_get('csv_analysis_multiterms', 'all');
$output = "nid;title;type";
//get all vocuabularies
$vocabularies = taxonomy_get_vocabularies();
foreach($vocabularies as $key => $voc){
$output.=";".csv_analysis_csv_escape($voc->name);
}
$output.="\n";
$results = db_query("SELECT nid, title, type from {node}");
foreach ($results as $row) {
$rowCSV = array(); //Stores all datasets for current node (can be multiple if node is attached to multiple terms in a single vocabulary)
//basic information for each node
$rowCSV[0] = $row->nid.";".csv_analysis_csv_escape($row->title).";".csv_analysis_csv_escape($row->type);
$node = node_load($row->nid);
//get taxonomy terms for node
//$terms = taxonomy_node_get_terms($node);
//$node = node_load(5000);
$node_array = (array)$node;
$terms = array();
foreach ($node_array as $key => $value) {
if(isset($value['und'])) {
foreach ($value['und'] as $candiate) {
if(isset($candiate['tid'])) {
$terms[$candiate['tid']] = taxonomy_term_load($candiate['tid']);
//print_r($candiate['tid'] . " - ");
}
}
}
}
// print_r($terms);
// exit;
$foundTerms = array();
foreach($terms as $key => $term){ //Determine all terms matching current vocabulary
$foundTerms[$term->vid][$term->tid] = $term->tid; //Add all terms of vocabulary
}
foreach($vocabularies as $key => $voc){
$newRow = array();
foreach($rowCSV as $key => $val){ //Iterate all rows up to now
if(count($foundTerms[$voc->vid]) > 0){
foreach($foundTerms[$voc->vid] as $tKey => $tVal){
if($multiterms == "comma"){
if(empty($newRow[0]))
$newRow[0] = $val.";".$tVal; //First term
else
$newRow[0].=",".$tVal; //Attach further terms separated by comma
}
elseif($multiterms == "first"){
$newRow[] = $val.";".$tVal;
break; //Just add the first term then break inner foreach. Outer foreach in this case always just consists of one entry.
}
else {
$newRow[] = $val.";".$tVal; //If multiple terms in current voc create douplicates of every exisiting row
}
}
}
else {
$newRow[]=$val.";"; //if no term found leave this field blank
}
}
$rowCSV = $newRow; //multiplied lines are set as the base for the next vocabulary
} //End: Iteration over vocabularies
foreach($rowCSV as $val){
$output.=$val."\n"; //for every
}
} //End: Iteration over all nodes
$application="text/csv";
header( "Content-Type: ".$application."; charset=ISO-8859-1" );
header( "Content-Disposition: attachment; filename=\"yrp-export.csv\"");
header( "Content-Description: csv File" );
header( "Pragma: no-cache" );
header( "Expires: 0" );
echo utf8_decode($output); //Output has to be ISO-8859-1. Otherwise Excel is not displaying special characters the right way
exit();
}
/**
* Escape function for csv strings
* @param String $string unescaped csv
* @return String Escaped csv string
*/
function csv_analysis_csv_escape($string){
return '"'.str_replace ( '"' , '""' , $string ).'"';
}
/**
* Implementation of hook_block_view().
* @param integer $delta code to identify the block
*/
function csv_analysis_block_view($delta = '') {
$block = array(
'subject' => t('CSV Export for analysis'),
'content' => csv_analysis_view_block());
return $block;
}
/**
* Implementation of hook_block_info().
*/
function csv_analysis_block_info() {
// Generate listing of blocks from this module, for the admin/block page
$block = array();
$block[0]["info"] = t('CSV Export for analysis');
return $block;
}
/**
* Implementation of hook_block_configure().
* @param integer $delta code to identify the block
*/
function csv_analysis_block_configure($delta = '') {
$form['multiterms'] = array(
'#type' => 'select',
'#title' => t('Procedure for multiple terms assigned from vocabulary'),
'#options' => array("first" => t('Take frist term'), "all" => t('All combinations of terms'), "comma" => t('Comma seperated list of terms')),
'#default_value' => variable_get('csv_analysis_multiterms', 'all'),
'#description' => t('This option determines the behaviour of the csv export if there are multiple terms from the same vocabulary attached to the node. It can either just the first item be exported, all combinations of terms be calculated (e.g. one voc has 2 terms assigned and another 3 terms would result in 6 unique rows to be exported), or all attached terms are written comma separated in the same field.'),
);
return $form;
}
/**
* Implementation of hook_block_save().
* @param integer $delta code to identify the block
* @param array $edit edited content
*/
function csv_analysis_block_save($delta = '', $edit = array()) {
variable_set('csv_analysis_multiterms', $edit['multiterms']);
}
/**
* Gives the content of the block
* Basically just the link to the export
*
*/
function csv_analysis_view_block(){
ob_start();
?>
<div id="csv_analysis">
<?php echo '<a href="' . url("csv_analysis/csv_export") . '">'.t("Export nodes for analysis").'</a>. '.t("The CSV file will contain all nodes stored in the system with their assigned vocabulary terms."); ?>
</div>
<?php
$out = ob_get_clean();
return $out;
}
/**
* Implementation of hook_permission()
*/
function csv_analysis_permission(){
return array(
'csv analysis access' => array(
'title' => t('csv analysis access'),
'description' => t('Execute the csv export'),
),
);
}