Skip to content

Commit b11318d

Browse files
committed
add logs / unique keys for update
1 parent 403c8e3 commit b11318d

File tree

6 files changed

+129
-45
lines changed

6 files changed

+129
-45
lines changed

README.md

+5-13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Download and install the module through the Admin or Manually
99

1010
## How to use
1111

12+
### Quick Import
13+
14+
You can choose to run a _Quick Import_ which will act as a one-time import. Simply follow the steps in the Admin to complete.
15+
1216
### Profiles
1317

1418
Profiles can be used to setup a common import you will run multiple times. A Profile will store your destination Stream and field mapping to eliminate these steps in the future.
@@ -19,23 +23,11 @@ If you have not created a profile, do so now. A Profile will contain all of the
1923

2024
Once your profile is created, you can click on __Run__ which will propose you to select a File you've uploaded in Files Module, and will import it instantly.
2125

22-
#### Customization in mind
23-
24-
Sometimes you will wish to handle a specific fields and preprocess it before running the insert. So the module create a for you 2 helpers.
25-
The first one is for preprocessing data. All the fields of the stream you want import into are listed here.
26-
the second one is for post processing data. A unique function with the $data and $entry_id to make whatever you want.
27-
28-
### Tehcnical detail
29-
30-
The module will try to convert your file into a PHP array.
31-
If for some reason, the root of the array is not the items you want to loop in, you can customize it in the Files settings of the profiles.
32-
You'll need to check the rights for the directory : helpers/profiles.
33-
3426
## Improve it !
3527

3628
Feel free to improve this Free and awesome module :)
3729
It's in beta for now, but we all need import system in our favorite CMS !
3830

3931
## Issues
4032

41-
Please submit any issues back to the Github issue tracker: <https://github.com/bergeo-fr/streams_import>
33+
Please submit any issues back to the Github issue tracker: <https://github.com/bergeo-fr/streams_import>

controllers/admin_profiles.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public function edit($id)
261261
array(
262262
'title' => lang($this->namespace. ':tabs:' .'source_connection' ),
263263
'id' => 'general',
264-
'fields' => array('xml_path_loop'),
264+
'fields' => array('xml_path_loop','unique_keys'),
265265
));
266266

267267
echo $this->streams->cp->entry_form($this->section, $this->namespace, $mode = 'edit', $entry = $id, $view_override = true, $extra, $skips = array(),$this->_tabs);

details.php

+37-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class Module_Streams_import extends Module
1313
{
1414

15-
public $version = 0.6;
15+
public $version = 0.7;
1616
public $module_name = 'streams_import';
1717

1818

@@ -326,6 +326,25 @@ public function install()
326326
);
327327
$this->streams->fields->add_field($field);
328328
}
329+
$field_slug = "unique_keys";
330+
if ( $this->db->where('field_namespace', $this->module_name)->where('field_slug', $field_slug)->limit(1)->get('data_fields')->num_rows() == null )
331+
{
332+
$field = array(
333+
'name' => 'lang:' . $this->module_name . ':fields:' . $field_slug,
334+
'slug' => $field_slug,
335+
'namespace' => $this->module_name,
336+
'type' => 'text',
337+
'extra' =>array(
338+
"max_length"=>555,
339+
"default_value"=>"" ),
340+
'assign' => $stream_slug,
341+
'title_column' => false,
342+
'required' => false,
343+
'unique' => false
344+
);
345+
$this->streams->fields->add_field($field);
346+
}
347+
329348

330349
// Add Streams - equalities
331350
$stream_slug = "mapping";
@@ -453,7 +472,6 @@ public function install()
453472
$this->streams->fields->add_field($field);
454473
}
455474

456-
// Add Fields profiles
457475
$field_slug = "filename";
458476
if ( $this->db->where('field_namespace', $this->module_name)->where('field_slug', $field_slug)->limit(1)->get('data_fields')->num_rows() == null )
459477
{
@@ -468,7 +486,23 @@ public function install()
468486
'unique' => false
469487
);
470488
$this->streams->fields->add_field($field);
471-
}
489+
}
490+
491+
$field_slug = "log_detail";
492+
if ( $this->db->where('field_namespace', $this->module_name)->where('field_slug', $field_slug)->limit(1)->get('data_fields')->num_rows() == null )
493+
{
494+
$field = array(
495+
'name' => 'lang:' . $this->module_name . ':fields:' . $field_slug,
496+
'slug' => $field_slug,
497+
'namespace' => $this->module_name,
498+
'type' => 'textarea',
499+
'assign' => $stream_slug,
500+
'title_column' => false,
501+
'required' => false,
502+
'unique' => false
503+
);
504+
$this->streams->fields->add_field($field);
505+
}
472506
return true;
473507
}
474508

events.php

+33-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ class Events_Streams_import {
1515
public function __construct()
1616
{
1717
$this->CI =& get_instance();
18-
18+
19+
// Load up our classes
20+
$this->CI->load->driver('Streams');
21+
$this->CI->load->helper(array('common_import','streams_import/streams_import'));
22+
$this->CI->load->config('streams_import/streams_import_c');
23+
1924
// Register CRON module shtuff
2025
Events::register('cron_process_test', array($this, 'import_pics')); // cron/test
2126
Events::register('cron_process_test', array($this, 'import')); // cron/test
@@ -34,12 +39,14 @@ public function __construct()
3439
public function import_pics()
3540
{
3641

37-
$this->CI->load->helper('streams_import/streams_import');
38-
$query=$this->CI->db->get_where('files', array('is_downloaded' => 0), $limit=10, $offset=0); //DL 10 files a time
42+
$query=$this->CI->db->get_where('files', array('is_downloaded' => 0), $limit=50, $offset=0); //DL 10 files a time
3943

4044
//donwload them all
4145
foreach ($query->result() as $single_file) {
4246
$raw = file_get_contents($single_file->src);
47+
if ($raw)
48+
{
49+
4350
$full_path="uploads/default/files/$single_file->filename";
4451
write_file($full_path, $raw);
4552

@@ -57,6 +64,10 @@ public function import_pics()
5764
);
5865
$this->CI->db->where('id',$single_file->id);
5966
$this->CI->db->update('files',$query);
67+
}else{
68+
echo "src :".$single_file->src;
69+
echo "<br/> no downloadé <br/>";
70+
}
6071

6172

6273
}
@@ -68,10 +79,25 @@ public function import_pics()
6879
public function import()
6980
{
7081

71-
// Load up our classes
72-
$this->CI->load->driver('Streams');
73-
$this->CI->load->helper('common_import');
74-
$this->CI->load->config('streams_import/streams_import_c');
82+
//Steps for automatic import
83+
// 0- Purge
84+
// 1- check in the config the path for the profiles.
85+
// 2- Load all the profiles where "auto = true"
86+
// 3- check if a file is awaiting for import process
87+
// 4- Import it :)
88+
89+
90+
91+
//Purge
92+
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+
}
99+
100+
die();
75101

76102

77103
/*

language/english/streams_import_lang.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,14 @@
77
* @author PyroCMS Community
88
* @website https://github.com/bergeo-fr/streams_import
99
*/
10-
11-
// CodeIgniter Reference Object
12-
$ci = &get_instance();
13-
10+
$ci =& get_instance();
1411
$lang = array(
1512
# titles
1613
'streams_import:title:profiles:index' => 'Profiles',
1714
'streams_import:title:profiles:create' => 'Create a new profile',
1815
'streams_import:title:profiles:edit' => 'Edit the profile',
19-
'streams_import:title:profiles:quick_import' => 'Quick Import',
20-
'streams_import:title:profiles:quick_import_mapping' => 'Quick Import: Mapping',
16+
'streams_import:title:logs:index' => 'Logs',
2117
'streams_import:title:profiles:quick_import_success' => 'Quick Import: Success',
22-
'streams_import:title:logs:index' => 'Logs',
2318

2419
# messages
2520
'streams_import:messages:profiles:create:success' => 'New profile created with success.',
@@ -32,12 +27,14 @@
3227
'streams_import:messages:import:failure' => 'Import fail. Sorry about that :(',
3328
'streams_import:messages:mapping:save:success' => 'Mapping saved',
3429
'streams_import:messages:mapping:save:failure' => 'Mapping Failed',
30+
'streams_import:messages:logs:delete:success' => 'Logs deleted succesfully',
31+
'streams_import:messages:logs:delete:failure' => 'Logs could not be deleted succesfully',
3532

3633
# fields
3734
'streams_import:fields:profile_name' => 'Profile Name',
3835
'streams_import:fields:profile' => 'Profile ID',
3936
'streams_import:fields:entry_number' => 'Entry',
40-
'streams_import:fields:stream_field' => 'Stream Field ID',
37+
'streams_import:fields:stream_field' => 'Stream Field ID',
4138
'streams_import:fields:equalities' => 'Associations',
4239
'streams_import:fields:stream_identifier' => 'Stream',
4340
'streams_import:fields:eol' => 'EOL',
@@ -65,6 +62,9 @@
6562
'streams_import:fields:password_instructions' => '',
6663
'streams_import:fields:xml_path_loop' => 'Loop Path',
6764
'streams_import:fields:xml_path_loop_instructions' => 'The file will be converted into an array. You have to provide the node where the system have to loop to import. <a onClick="window.open(\'/admin/streams_import/profiles/raw_data/'.$ci->uri->segment(5).'\', \'NOM\', \'scrollbars=yes,width=550,height=600\')"target=\'_blank\'>See the raw data</a>',
65+
'streams_import:fields:filename' => 'File\'s name/ID',
66+
'streams_import:fields:profile_rel_logs' => 'Profile',
67+
'streams_import:fields:profile_slug' => 'Profile Slug',
6868

6969
# buttons
7070
'profiles:button:add' => 'Add a profile',
@@ -85,4 +85,4 @@
8585
'streams_import:misc_csv_file' => 'Pick a file',
8686
'streams_import:misc_instructions_csv_file' => 'or upload one using Files Manager.',
8787

88-
);
88+
);

libraries/streams_import.php

+44-12
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,10 @@ public function process_import($profile_id, $file_id = 0)
7474
$log_id= $this->register_logs($file_id, $profile_id);
7575

7676
$profile = $this->ci->streams_import_m->get_profile_object($profile_id);
77-
//Import helper
78-
$profile_slug=create_slug($profile->profile_name);
7977

8078
// _helper.php' Preproccess and post process
81-
$this->ci->load->helper('profiles/'.$profile_slug . '_pre');
82-
$this->ci->load->helper('profiles/'.$profile_slug . '_post');
79+
$this->ci->load->helper('profiles/'.$profile->profile_slug . '_pre');
80+
$this->ci->load->helper('profiles/'.$profile->profile_slug . '_post');
8381

8482

8583
$data_array = $this->file_to_array($file_id, $profile);
@@ -105,13 +103,19 @@ public function process_import($profile_id, $file_id = 0)
105103
foreach ($fields as $field)
106104
{
107105
$formated_fields[$field->field_id] = $field->field_slug; // ex : $formated_fields[59] = 'name'
108-
}
106+
//we save the folder field type because the update will be screwed if we don't unset it in the entry_data
107+
if($field->field_type == 'folder')
108+
{
109+
$fields_folder[]=$field->field_slug;
110+
}
111+
}
109112

110113

111114

112115

113116
// Soemtimes, the entries are not in the root of the array. You can deal with it in editing the profil and adding the path to the entries where we shoudl loop
114117

118+
115119
if ($profile->xml_path_loop)
116120
{
117121
//Get the text between [] into array if there's more than 1 !
@@ -125,7 +129,6 @@ public function process_import($profile_id, $file_id = 0)
125129
}
126130

127131

128-
129132
// Detect and get the top level of our data_array => DOESNT WORK.
130133
//$data_array = $this->array_to_top_level($data_array);
131134

@@ -140,23 +143,52 @@ public function process_import($profile_id, $file_id = 0)
140143
{
141144

142145
// Build the function name
143-
$preprocess=$profile_slug .'_'.$map['stream_field'].'_sim_preprocess';
146+
$preprocess=$profile->profile_slug .'_'.$map['stream_field'].'_sim_preprocess';
144147

145148
// Process the value
146-
//if the entry_number is 'preprocess' then we call the preprocessor with null because nodata is passed out and they should be hardcoded
147-
$processed_value = ($map['entry_number']!='preprocess')?$preprocess($entry[$map['entry_number']]):$preprocess(null);
149+
//if the entry_number is 'preprocess' then we call the preprocessor with the full entry set because nodata is passed out and they should be hardcoded
150+
$processed_value = ($map['entry_number']!='preprocess')?$preprocess($entry[$map['entry_number']]):$preprocess($entry);
148151
$insert_data[$map['stream_field']] = (empty($processed_value))?null:$processed_value;
149152

150153
// Debug
151154
//print_r($insert_data);die;
152155
}
153-
if (($entry_id = $this->ci->streams->entries->insert_entry($insert_data, $stream->stream_slug, $stream->stream_namespace, $skips = array(), $extra = array())) === false)
156+
157+
//Ok now : INSERT OR UPDATE ?
158+
//Check if the profile fields "unique_keys" is setted up
159+
if(!empty($profile->unique_keys))
160+
{
161+
//Oh... so you want to update ? explode the keys !
162+
$keys = explode(',', str_replace(' ', '', $profile->unique_keys));
163+
foreach ($keys as $key) {
164+
# code...
165+
$this->ci->db->where($key,$insert_data[$key]);
166+
}
167+
168+
$update = $this->ci->db->limit(1)->get($stream->stream_namespace.'_'.$stream->stream_slug)->row();
169+
}
170+
171+
172+
if(!empty($update->id))
173+
{
174+
$skips = array();
175+
//unset the folder fields.
176+
foreach ($fields_folder as $slug) {
177+
# code...
178+
unset($insert_data[$slug]);
179+
$skips[] = $slug;
180+
}
181+
//var_dump($insert_data);
182+
183+
$this->ci->streams->entries->update_entry($update->id,$insert_data, $stream->stream_slug, $stream->stream_namespace,$skips, $extra = array());
184+
}
185+
elseif (($entry_id = $this->ci->streams->entries->insert_entry($insert_data, $stream->stream_slug, $stream->stream_namespace, $skips = array(), $extra = array())) === false)
154186
{
155187
continue;
156188
}
157189

158190
//call the post process function
159-
$post_process=$profile_slug .'_'.$stream->stream_slug.'_'.'sim_postprocess';
191+
$post_process=$profile->profile_slug .'_'.$stream->stream_slug.'_'.'sim_postprocess';
160192

161193
$post_process($stream, $entry_id, $entry);
162194
//die();
@@ -318,7 +350,7 @@ public function post_process_array($data = null, $key =null, $array = array())
318350
and isset($evaluate[2]) and is_array($evaluate[2])
319351
)
320352
{
321-
return $this->post_process_array($value,$key, &$array);
353+
return $this->post_process_array($value,$key, $array);
322354
}
323355
else
324356
{

0 commit comments

Comments
 (0)