Skip to content

Commit cbbceb8

Browse files
committed
fixes
1 parent f88e2fe commit cbbceb8

File tree

5 files changed

+91
-39
lines changed

5 files changed

+91
-39
lines changed

config/routes.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @website https://github.com/bergeo-fr/streams_import
99
*/
1010
$route['streams_import/admin/profiles(/:any)?'] = 'admin_profiles$1';
11+
$route['streams_import/admin/logs(/:num)?'] = 'admin_logs/index/$1';
1112
$route['streams_import/admin/logs(/:any)?'] = 'admin_logs$1';
1213
$route['streams_import/admin(/:any)?'] = 'admin_profiles$1';
1314

config/streams_import_c.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
/*
44
* We some security so we can call the importer on the front end
55
*/
6-
$config['streams_import:hash'] = 'a874de040c2439d710d1b8e6c7bd59a0';
7-
$config['streams_import:unzip_folder'] = "uploads/default/files/temp_unzip/";
6+
$config['streams_import:hash'] = 'a874de040c2439d710d1b8e6c7bd59a0';
7+
$config['streams_import:unzip_folder'] = "uploads/default/files/temp_unzip/";
8+
$config['streams_import:archives_folder'] = "uploads/default/files/archives/";
9+
$config['streams_import:anomalies_folder'] = "uploads/default/files/anomalies/";
10+
$config['streams_import:profiles_directory'] = "uploads/default/ftp/profiles/";

controllers/admin_logs.php

+21-7
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,24 @@ public function __construct()
4141

4242
}
4343

44-
public function index()
44+
public function index($offset)
4545
{
4646
$extra =
4747
array(
4848
'title' => lang($this->namespace.':title:'.$this->section.':index'),
4949
'buttons' => array(
50-
array(
51-
'label' => lang('global:delete'),
52-
'url' => 'admin/'.$this->namespace.'/'.$this->section.'/delete/-entry_id-',
53-
'confirm' => true
54-
))
50+
array(
51+
'label' => lang('global:delete'),
52+
'url' => 'admin/'.$this->namespace.'/'.$this->section.'/delete/-entry_id-',
53+
'confirm' => true
54+
),
55+
array(
56+
'label' => lang('global:view'),
57+
'url' => 'admin/'.$this->namespace.'/'.$this->section.'/view/-entry_id-',
58+
'confirm' => false
59+
))
5560
);
56-
echo $this->streams->cp->entries_table($this->section, $this->namespace, $pagination = null, $pagination_uri = null, $view_override = true, $extra);
61+
echo $this->streams->cp->entries_table($this->section, $this->namespace, $pagination = "50", $pagination_uri = "admin/streams_import/logs", $view_override = true, $extra);
5762
}
5863

5964
public function create()
@@ -68,6 +73,15 @@ public function edit ($id)
6873
redirect('admin/'.$this->namespace.'/'.$this->section);
6974
}
7075

76+
public function view ($id)
77+
{
78+
$query = $this->streams->entries->get_entry($id,$this->section, $this->namespace,false);
79+
80+
echo "<pre>";
81+
var_dump($query);
82+
}
83+
84+
7185
public function delete($id)
7286
{
7387
if($this->streams->entries->delete_entry($id, $this->section, $this->namespace)){

events.php

+42-30
Original file line numberDiff line numberDiff line change
@@ -75,37 +75,42 @@ public function import_pics()
7575
}
7676

7777

78+
public function purge()
79+
{
80+
81+
82+
//Purge
83+
//SELECT listings older than 30 days (updated OR created.) Limit it. It's really slow. maximum should be 50
84+
$sql =" SELECT id FROM default_listing_homes WHERE (updated is null and (TO_DAYS(NOW()) - TO_DAYS(created)) > 30 ) OR ( (TO_DAYS(NOW()) - TO_DAYS(updated)) >30 ) LIMIT 10";
85+
86+
$entries = $this->CI->db->query($sql)->result();
87+
foreach ($entries as $entry) {
88+
$this->CI->streams->entries->delete_entry($entry->id, 'homes', 'listing');
89+
}
90+
}
7891

7992
public function import()
8093
{
81-
94+
die();
8295
//Steps for automatic import
83-
// 0- Purge
8496
// 1- check in the config the path for the profiles.
8597
// 2- Load all the profiles where "auto = true"
8698
// 3- check if a file is awaiting for import process
8799
// 4- Import it :)
88100

89101

90102

91-
//Purge
92103

93-
$sql =" SELECT id FROM default_listing_homes WHERE (updated is null and (TO_DAYS(NOW()) - TO_DAYS(created)) > 30 ) OR ( (TO_DAYS(NOW()) - TO_DAYS(updated)) >30 ) LIMIT 60";
94-
echo $sql;
95-
$entries = $this->CI->db->query($sql)->result();
96-
foreach ($entries as $entry) {
97-
$this->CI->streams->entries->delete_entry($entry->id, 'homes', 'listing');
98-
}
99104

100-
die();
101105

102106

103107
/*
104108
* Set up our root directory to scan
105109
*/
106110

107-
$base = UPLOAD_PATH.'ftp/profiles/';
111+
$base = $this->CI->config->item('streams_import:profiles_directory');
108112

113+
$exit_at_the_end = false;
109114

110115

111116
/*
@@ -116,29 +121,20 @@ public function import()
116121
$profiles = $this->CI->streams->entries->get_entries(
117122
array(
118123
'stream' => 'profiles',
119-
'namespace' => 'streams_import',
124+
'namespace' => 'streams_import'
120125
)
121126
);
122127

123-
foreach ($profiles['entries'] as &$profile)
124-
{
125-
$profile = array(
126-
'slug' => slugify($profile['profile_name'], ''),
127-
'id' => $profile['id'],
128-
);
129-
}
130-
131-
132-
133128
/*
134129
* Scan
135130
*/
136131

137132
foreach ($profiles['entries'] as $profile)
138133
{
139-
$path = $base.$profile['slug'];
134+
$path = $base.$profile['profile_slug'].'/data/';
140135

141136

137+
var_dump($path);
142138
// Does the folder exist?
143139
if (is_dir($path))
144140
{
@@ -160,39 +156,55 @@ public function import()
160156

161157

162158
// Check for the achive folder
163-
if (!is_dir($path.'/archive')) mkdir($path.'/archive');
159+
$archive = $this->CI->config->item('streams_import:archives_folder').$profile['profile_slug'];
160+
161+
162+
if (!is_dir($this->CI->config->item('streams_import:archives_folder')))
163+
{ mkdir($this->CI->config->item('streams_import:archives_folder')); }
164+
if (!is_dir($archive))
165+
{ mkdir($archive); }
164166

165167

166168
// Move the files to archive
167169
foreach ($files as $file)
168170
{
169-
171+
$exit_at_the_end = true;
170172
// Try importing the file
171-
if(import_file($path.'/'.$file,$file,$folder_id = 18,$owner = 17,$description = '')) // $folder_id = 18 is "import" and $owner = 17 is "[email protected]"
173+
if(import_file($path.$file,$file,$folder_id = 18,$owner = 17,$description = 'file to import')) // $folder_id = 18 is "import" and $owner = 17 is "[email protected]"
172174
{
175+
//echo "success ".$path.$file;
173176
##
174177
## There needs to be a public import method
175178
##
176179
## Something like website.com/streams_import/import/PROFILE/FILE/SECURITY_HASH
177180
##
178181

179182
// File ID
180-
$file = $this->CI->db->select()->where('filename', $file)->limit(1)->get('files')->row(0);
183+
$file_id = $this->CI->db->select()->where('filename', $file)->limit(1)->get('files')->row(0);
181184

182185
// Try this
183-
echo file_get_contents(site_url('streams_import/import/'.$this->CI->config->item('streams_import:hash').'/'.$profile['id'].'/'.$file->id));die;
186+
$hit = site_url('streams_import/import/'.$this->CI->config->item('streams_import:hash').'/'.$profile['id'].'/'.$file_id->id);
187+
//var_dump($hit);
188+
echo file_get_contents($hit);
184189
//echo site_url('streams_import/import/'.$this->CI->config->item('streams_import:hash').'/'.$profile['id'].'/'.$file->id);die;
185190

186191
// Clean that snatch like a dirty carpet
187-
rename($path.'/'.$file, $path.'/archive/'.now().'.'.$file);
192+
193+
rename($path.$file, $archive.'/'.now().'.'.$file);
188194
}
189195
}
196+
if ($exit_at_the_end)
197+
{
198+
//We want to process one profile per time.
199+
die();
200+
}
190201
}
191202
else
192203
{
204+
//echo "ano";
193205
// Who knows?
194206
// Maybe we'll need it next time.
195-
mkdir($path);
207+
rename($path.$file, $archive.'/'.$file);
196208
}
197209
}
198210
}

libraries/streams_import.php

+22
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ public function process_import($profile_id, $file_id = 0)
169169
}
170170

171171

172+
$action = "INSERT "; // this is for log
172173
if(!empty($update->id))
173174
{
174175
$skips = array();
@@ -179,6 +180,7 @@ public function process_import($profile_id, $file_id = 0)
179180
$skips[] = $slug;
180181
}
181182
//var_dump($insert_data);
183+
$action = "UPDATE "; // this is for log
182184

183185
$this->ci->streams->entries->update_entry($update->id,$insert_data, $stream->stream_slug, $stream->stream_namespace,$skips, $extra = array());
184186
}
@@ -187,6 +189,14 @@ public function process_import($profile_id, $file_id = 0)
187189
continue;
188190
}
189191

192+
//Log that shit.
193+
$data_log = array();
194+
foreach($insert_data as $key=>$value) {
195+
$data_log[] = $key.','.$value;
196+
}
197+
;
198+
$this->enrich_log($log_id,$action.implode('|',$data_log));
199+
190200
//call the post process function
191201
$post_process=$profile->profile_slug .'_'.$stream->stream_slug.'_'.'sim_postprocess';
192202

@@ -438,6 +448,18 @@ public function register_logs($filename, $profile_id, $id=null)
438448

439449
}
440450

451+
public function enrich_log($id,$content){
452+
453+
$log = $this->ci->db->get_where('streams_import_logs', array('id'=>$id))->row();
454+
$entry_data = array(
455+
'log_detail' => $log->log_detail.br(2).$content
456+
);
457+
$skips = array('filename','profile_rel_logs');
458+
return $this->ci->streams->entries->update_entry($id, $entry_data = array(), 'logs', 'streams_import',$skips, $extra = array());
459+
460+
461+
}
462+
441463

442464
}
443465

0 commit comments

Comments
 (0)