コード例 #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
 public function raw_data($profile_id)
 {
     $current_profile = $this->streams->entries->get_entry($profile_id, $this->stream_slug, $this->namespace, TRUE);
     $data_array = $this->streams_import->file_to_array(get_fileid_by_profileid($current_profile->id), $current_profile);
     echo "<pre>";
     var_dump($data_array);
     echo "</pre>";
 }