Пример #1
0
 /**
  * Process an Import
  * 
  * @param int  $profile_id  Profile ID
  * @param int  $file_id     The ID of the file we are importing
  * @return bool
  */
 public function process_import($profile_id, $file_id = 0)
 {
     //Put the lock ON.
     Settings::set('stream_import_lock_info', '1|' . $profile_id . '|' . $file_id . '|' . date('Y-m-d_H:i:s', now()));
     // Get the file example if file_id is not set
     if ($file_id == 0) {
         $file_id = get_fileid_by_profileid($profile_id);
     }
     //Log that shit !
     $log_id = $this->register_logs($file_id, $profile_id);
     $profile = $this->ci->streams_import_m->get_profile_object($profile_id);
     // _helper.php' Preproccess and post process
     $this->ci->load->helper('profiles/' . $profile->profile_slug . '_pre');
     $this->ci->load->helper('profiles/' . $profile->profile_slug . '_post');
     $data_array = $this->file_to_array($file_id, $profile);
     // get the mapping
     $params = array('stream' => 'mapping', 'namespace' => $this->namespace, 'where' => " profile_relation_stream = " . $profile_id);
     $mapping = $this->ci->streams->entries->get_entries($params);
     // Debug
     //print_r($mapping);die;
     //get the fields
     $stream = $this->ci->streams->stream_obj($profile->stream_identifier);
     $fields = $this->ci->streams_m->get_stream_fields($profile->stream_identifier);
     //prepare the array.
     foreach ($fields as $field) {
         $formated_fields[$field->field_id] = $field->field_slug;
         // ex : $formated_fields[59] = 'name'
         //we save the folder field type because the update will be screwed if we don't unset it in the entry_data
         if ($field->field_type == 'folder') {
             $fields_folder[] = $field->field_slug;
         }
     }
     // 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
     /*	if ($this->debug):
     			echo "<pre>";
     			var_dump($data_array);
     			echo "</pre>";
     		endif;*/
     if ($profile->xml_path_loop) {
         //Get the text between [] into array if there's more than 1 !
         $matches = get_values_between_brackets($profile->xml_path_loop);
         //we parse the array until we got the xpath as the root
         foreach ($matches as $key) {
             //echo $key;
             $data_array = $data_array["{$key}"];
         }
     }
     // Detect and get the top level of our data_array => DOESNT WORK.
     //$data_array = $this->array_to_top_level($data_array);
     // Debug
     //print_r($data_array);die;
     // Build the batch
     foreach ($data_array as $entry) {
         //
         $insert_data = array();
         foreach ($mapping['entries'] as $map) {
             // Build the function name
             $preprocess = $profile->profile_slug . '_' . $map['stream_field'] . '_sim_preprocess';
             // Process the value
             //if empty don't insert
             if (empty($entry[$map['entry_number']]) and $map['entry_number'] != 'preprocess') {
                 //echo $map['entry_number'];
                 continue;
             }
             //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
             $processed_value = $map['entry_number'] != 'preprocess' ? $preprocess($entry[$map['entry_number']]) : $preprocess($entry);
             $insert_data[$map['stream_field']] = empty($processed_value) ? null : $processed_value;
             // Debug
             //print_r($insert_data);die;
         }
         //Log that shit.
         $data_log = array();
         foreach ($insert_data as $key => $value) {
             $data_log[] = $key . ',' . $value;
         }
         //if one of the data is corrupted, we do not insert anything
         if (in_array("{{do_no_insert}}", $insert_data, $strict = TRUE)) {
             //Log that shit.
             $this->enrich_log($log_id, "INSTABLE DATA: We don't insert: " . implode('|', $data_log));
             continue;
         }
         //Ok now : INSERT OR UPDATE ?
         //Check if the profile fields "unique_keys" is setted up
         if (!empty($profile->unique_keys)) {
             //Oh... so you want to update ? explode the keys !
             $keys = explode(',', str_replace(' ', '', $profile->unique_keys));
             foreach ($keys as $key) {
                 # code...
                 $this->ci->db->where($key, $insert_data[$key]);
             }
             $update = $this->ci->db->limit(1)->get($stream->stream_namespace . '_' . $stream->stream_slug)->row();
         }
         $action = "INSERT ";
         // this is for log
         if (!empty($update->id)) {
             $skips = array();
             //unset the folder fields.
             foreach ($fields_folder as $slug) {
                 # code...
                 unset($insert_data[$slug]);
                 $skips[] = $slug;
             }
             //var_dump($insert_data);
             $action = "UPDATE ";
             // this is for log
             $this->ci->streams->entries->update_entry($update->id, $insert_data, $stream->stream_slug, $stream->stream_namespace, $skips, $extra = array());
         } elseif ($entry_id = $this->ci->streams->entries->insert_entry($insert_data, $stream->stream_slug, $stream->stream_namespace, $skips = array(), $extra = array())) {
             //call the post process function
             $post_process = $profile->profile_slug . '_' . $stream->stream_slug . '_' . 'sim_postprocess';
             $post_process($stream, $entry_id, $entry);
         } else {
             continue;
         }
         //Log that shit.
         $this->enrich_log($log_id, $action . implode('|', $data_log));
         //kill the variables
         $entry = null;
         $insert_data = null;
         //die();
     }
     //kill the variables
     $data_array = null;
     $this->register_logs($file_id, $profile_id, $log_id);
     //release the lock
     Settings::set('stream_import_lock_info', '0|' . $profile_id . '|' . $file_id . '|' . date('Y-m-d_H:i:s', now()));
     return true;
 }
Пример #2
0
 /**
  * Profile Mapping screen
  * 
  * @param $id  Profile ID
  */
 public function mapping($id)
 {
     if ($this->input->post()) {
         // We delete all existing data for this mapping, and we will overwrite everything
         $this->db->where('profile_relation_stream', $this->input->post('profileID'))->delete('streams_import_mapping');
         //Go generate and save the mapping
         $source = $this->input->post('source');
         $dest = $this->input->post('destination');
         for ($i = 0; $i < $this->input->post('counter'); $i++) {
             if (isset($dest[$i]) && !empty($dest[$i]) && isset($source[$i]) && !empty($source[$i])) {
                 # code...
                 $insert_data[] = array('stream_field' => $dest[$i], 'entry_number' => $source[$i], 'ordering_count' => $i, 'created' => date('Y-m-d H:i:s'), 'created_by' => $this->current_user->id, 'profile_relation_stream' => $this->input->post('profileID'));
             }
             //var_dump($insert_data);
         }
         // Import them
         batch_insert_update('streams_import_mapping', $insert_data, array('ordering_count'));
         $this->session->set_flashdata('success', lang('streams_import:messages:mapping:save:success'));
         redirect('/admin/streams_import/profiles');
     }
     // get the profile we're editing.
     $current_profile = $this->streams_import_m->get_profile_object($id);
     //get the stream's fields we want import into
     $data->fields = $this->get_stream_fields_list($current_profile->stream_identifier);
     //$data->field_count = count((array) $data->fields);	//number of fields we have for this stream.
     //Go for the magic ! Convert the example file to a PHP array.
     $data_array = $this->streams_import->file_to_array(get_fileid_by_profileid($current_profile->id), $current_profile);
     //	if we work with XML and path loop is define, so we can load the node to loop
     if ($current_profile->source_format == 'XML' && $current_profile->xml_path_loop) {
         $matches = get_values_between_brackets($current_profile->xml_path_loop);
         //we fetch the array until we got the xpath as the root
         foreach ($matches as $key) {
             $data_array = $data_array["{$key}"];
         }
     }
     //processing the output if the array
     $data->csv_dropdown = $this->streams_import->post_process_array($data_array);
     array_unshift($data->csv_dropdown, '------');
     $data->csv_dropdown['preprocess'] = 'will be preprocessed';
     // Debug
     //print_r($data->csv_dropdown);die;
     $this->template->build('admin/profiles/mapping', $data);
 }