-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeslib.module
More file actions
182 lines (156 loc) · 4.39 KB
/
geslib.module
File metadata and controls
182 lines (156 loc) · 4.39 KB
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
<?php
// $Id: geslib.module
/**
* @file
* Importacion/exportacion de datos con geslib.
*/
/**
* Implementation of hook_perm().
*/
function geslib_perm() {
return array( 'import geslib files', 'geslib content import', 'geslib report page', 'geslib delete report line');
}
/**
* Implementation of hook_menu().
*/
function geslib_menu() {
$items = array();
# Define import form
$items['admin/store/geslib_import'] = array(
'title' => 'Import Geslib File',
'description' => 'Import Geslib File.',
'page callback' => 'drupal_get_form',
'page arguments' => array('geslib_import_form'),
'access callback' => 'user_access',
'access arguments' => array('geslib_content import'),
'type' => MENU_NORMAL_ITEM,
);
# Define reports page
$items['admin/store/reports/geslib_report'] = array(
'title' => 'Imported Geslib Files Report',
'description' => 'Imported Geslib Files Report',
'page callback' => 'geslib_files_report',
'access callback' => 'user_access',
'access arguments' => array('geslib report page'),
);
# Delete log line
$items['geslib/delete_log'] = array(
'title' => 'Delete log line',
'page callback' => 'geslib_delete_log',
'page arguments' => array(2, 3),
'access callback' => 'user_access',
'access arguments' => array('geslib delete report line'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_cron().
*/
/**
* Los crons no pueden durar mas de 240s => desactivado
function geslib_cron() {
$path = variable_get('geslib_upload_path', NULL);
if ($path && is_readable($path)) {
try {
$reader = new geslib_api($path,0);
$files = $reader->get_pending_files();
foreach($files as $file) {
$reader->read_file($file);
$reader->process_elements();
drush_log(dt('Geslib elements imported sucessfully from') ." ". $file, 'ok');
}
} catch(Exception $e) {
drush_log(dt($e->getMessage()), 'error');
}
}
}
*/
/**
* Implements hook_theme().
*/
function geslib_theme() {
return array(
'geslib_report' => array(
'template' => 'geslib_report',
'arguments' => array('files' => NULL),
),
);
}
/**
* Report imported files page
*/
function geslib_files_report() {
$list = array();
# Get all geslib logs
$results = db_query("SELECT * FROM {geslib_log} ORDER BY start_date DESC LIMIT 30");
while ($record = db_fetch_object($results)) {
$list[] = $record;
}
return theme('geslib_report', $list);
}
/**
* Delete log line
*/
function geslib_delete_log($log_line) {
$valor = intval($log_line);
if ($valor > 0) {
// Remove from database
$results = db_query("DELETE FROM {geslib_log} WHERE id = %d", $valor);
// En drupal7
// $results = db_delete('geslib_log')->condition('id', $valor)->execute;
drupal_set_message("Eliminada entrada de log.");
}
unset($_REQUEST['destination']);
drupal_goto('admin/store/reports/geslib_report');
}
/**
* Form: Import form.
*/
function geslib_import_form($form_state) {
$form = array();
# Esto es necesario para poder hacer el upload del fichero
#$form['#attributes'] = array('enctype' => "multipart/form-data");
$form['#attributes']['enctype'] = "multipart/form-data";
$form['geslib_file'] = array(
'#type' => 'file',
'#title' => t('Upload Geslib file'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Import Geslib File')
);
if( user_access('geslib_content import') ) {
return $form;
} else {
return array();
}
}
/**
* Validation callback for "admin/store/geslib_import":
*/
function geslib_import_form_validate($form, &$form_state) {
$fieldName = 'geslib_file';
// attempt to save the uploaded file
$file = file_save_upload($fieldName, array(), NULL, FILE_EXISTS_REPLACE);
// set error if file was not uploaded
if (!$file) {
form_set_error($fieldName, 'Error uploading file. Contact system administrator.');
} else {
// set files to form_state, to process when form is submitted
$form_state['values']['file'] = $file;
}
}
/**
* Submit callback for "admin/store/geslib_import":
*/
function geslib_import_form_submit($form, &$form_state) {
$file = $form_state['values']['file'];
if(!$file) {
drupal_set_message(t('ERROR uploading the file.'));
} else {
$reader = new geslib_api( $file->filepath );
$reader->process_elements();
}
file_delete($file->filepath);
}