/**
  * Performs the full import of all regular (non-Matrix) Playa channel fields
  * into native Relationships fields
  *
  * @return	array	Array of new Playa field IDs
  */
 public function do_import()
 {
     ee()->load->library('pm_import_common');
     $playas = $this->get_playa_fields();
     $playa_relationships = ee()->pm_import_common->get_playa_relationships();
     ee()->load->library('api');
     ee()->api->instantiate('channel_fields');
     ee()->load->model('addons_model');
     $new_field_ids = array();
     $original_site_id = ee()->config->item('site_id');
     foreach ($playas as $playa) {
         // Let's be explicit about what's making up this new field
         $new_relationship_field = array('site_id' => $playa['site_id'], 'group_id' => $playa['group_id'], 'field_label' => $playa['field_label'], 'field_name' => ee()->pm_import_common->get_unique_field_name($playa['field_name'], '_relate', $playa['site_id']), 'field_type' => 'relationship', 'field_instructions' => $playa['field_instructions'], 'field_required' => $playa['field_required'], 'field_search' => $playa['field_search'], 'field_order' => 0);
         // Hack to prevent site ID mismatch error in API channel fields
         ee()->config->config['site_id'] = $new_relationship_field['site_id'];
         $field_id = ee()->api_channel_fields->update_field($new_relationship_field);
         ee()->config->config['site_id'] = $original_site_id;
         $new_field_settings = PlayaConverter::convertSettings(unserialize(base64_decode($playa['field_settings'])));
         $new_relationships = array();
         if (isset($playa_relationships[$playa['field_id']])) {
             $new_relationships = PlayaConverter::convertPlayaRelationships($playa_relationships[$playa['field_id']], $field_id);
         }
         // Set new field settings from translated Playa settings
         ee()->db->set('field_settings', base64_encode(serialize($new_field_settings)))->where('field_id', $field_id)->update('channel_fields');
         if (!empty($new_relationships)) {
             if (ee()->addons_model->module_installed('publisher') && ee()->db->table_exists('publisher_relationships')) {
                 ee()->db->insert_batch('publisher_relationships', $new_relationships);
                 $new_relationships = PlayaConverter::filterPublisherRelationships($new_relationships, ee()->config->item('publisher_default_language_id') ?: 1);
             }
             // Finally, import Playa relationships
             ee()->db->insert_batch('relationships', $new_relationships);
         }
         $new_field_ids[] = $field_id;
     }
     return $new_field_ids;
 }
 /**
  * Import Playa data from Matrix columns
  *
  * @param	array	Assocative array of Matrix field IDs to Grid field IDs
  * @param	array	Assocative array of Matrix column IDs to Grid column IDs
  * @return	void
  */
 private function import_playa_data($matrix_to_grid_fields, $matrix_to_grid_cols)
 {
     if (!ee()->db->table_exists('playa_relationships')) {
         return;
     }
     $playa_relationships = ee()->db->where('parent_col_id IS NOT NULL')->get('playa_relationships')->result_array();
     if (count($playa_relationships)) {
         $new_relationships = PlayaConverter::convertPlayaRelationshipsForGrid($playa_relationships, $matrix_to_grid_fields, $matrix_to_grid_cols);
         if (ee()->addons_model->module_installed('publisher') && ee()->db->table_exists('publisher_relationships')) {
             ee()->db->insert_batch('publisher_relationships', $new_relationships);
             $new_relationships = PlayaConverter::filterPublisherRelationships($new_relationships, ee()->config->item('publisher_default_language_id') ?: 1);
         }
         ee()->db->insert_batch('relationships', $new_relationships);
     }
 }
 /**
  * Test PlayaConverter::convertPlayaRelationshipsForGrid() method
  */
 public function testPublisher()
 {
     $data = array(0 => array('rel_id' => '2058', 'parent_entry_id' => '394', 'parent_field_id' => '40', 'parent_col_id' => NULL, 'parent_row_id' => NULL, 'parent_var_id' => NULL, 'parent_is_draft' => '0', 'child_entry_id' => '278', 'rel_order' => '1', 'publisher_lang_id' => 1, 'publisher_status' => 'open'), 1 => array('rel_id' => '2057', 'parent_entry_id' => '394', 'parent_field_id' => '40', 'parent_col_id' => NULL, 'parent_row_id' => NULL, 'parent_var_id' => NULL, 'parent_is_draft' => '0', 'child_entry_id' => '245', 'rel_order' => '0', 'publisher_lang_id' => 2, 'publisher_status' => 'open'), 2 => array('rel_id' => '786', 'parent_entry_id' => '410', 'parent_field_id' => '40', 'parent_col_id' => NULL, 'parent_row_id' => NULL, 'parent_var_id' => NULL, 'parent_is_draft' => '0', 'child_entry_id' => '349', 'rel_order' => '1', 'publisher_lang_id' => 3, 'publisher_status' => 'draft'), 3 => array('rel_id' => '785', 'parent_entry_id' => '410', 'parent_field_id' => '40', 'parent_col_id' => NULL, 'parent_row_id' => NULL, 'parent_var_id' => NULL, 'parent_is_draft' => '0', 'child_entry_id' => '315', 'rel_order' => '0', 'publisher_lang_id' => 1, 'publisher_status' => 'open'));
     $expected = array(0 => array('parent_id' => '394', 'field_id' => 120, 'child_id' => '278', 'order' => '1', 'publisher_lang_id' => 1, 'publisher_status' => 'open'), 1 => array('parent_id' => '394', 'field_id' => 120, 'child_id' => '245', 'order' => '0', 'publisher_lang_id' => 2, 'publisher_status' => 'open'), 2 => array('parent_id' => '410', 'field_id' => 120, 'child_id' => '349', 'order' => '1', 'publisher_lang_id' => 3, 'publisher_status' => 'draft'), 3 => array('parent_id' => '410', 'field_id' => 120, 'child_id' => '315', 'order' => '0', 'publisher_lang_id' => 1, 'publisher_status' => 'open'));
     $new_data = PlayaConverter::convertPlayaRelationships($data, 120);
     $this->assertEquals($expected, $new_data, 'Should preserve Publisher columns');
     $expected = array(0 => array('parent_id' => '394', 'field_id' => 120, 'child_id' => '278', 'order' => '1'), 3 => array('parent_id' => '410', 'field_id' => 120, 'child_id' => '315', 'order' => '0'));
     $new_data = PlayaConverter::filterPublisherRelationships($new_data, 1);
     $this->assertEquals($expected, $new_data, 'Should filter out certain relationships based on Publisher data');
     $matrix_to_grid_fields = array(61 => 133);
     $matrix_to_grid_cols = array(23 => 38);
     $data = array(0 => array('rel_id' => '2953', 'parent_entry_id' => '800', 'parent_field_id' => '61', 'parent_col_id' => '23', 'parent_row_id' => '2210', 'parent_var_id' => NULL, 'parent_is_draft' => '0', 'child_entry_id' => '1200', 'rel_order' => '0', 'publisher_lang_id' => 1, 'publisher_status' => 'open'), 1 => array('rel_id' => '2952', 'parent_entry_id' => '800', 'parent_field_id' => '61', 'parent_col_id' => '23', 'parent_row_id' => '2209', 'parent_var_id' => NULL, 'parent_is_draft' => '0', 'child_entry_id' => '1199', 'rel_order' => '0', 'publisher_lang_id' => 2, 'publisher_status' => 'open'), 2 => array('rel_id' => '2951', 'parent_entry_id' => '800', 'parent_field_id' => '61', 'parent_col_id' => '23', 'parent_row_id' => '2202', 'parent_var_id' => NULL, 'parent_is_draft' => '0', 'child_entry_id' => '1198', 'rel_order' => '0', 'publisher_lang_id' => 3, 'publisher_status' => 'draft'), 3 => array('rel_id' => '2950', 'parent_entry_id' => '800', 'parent_field_id' => '61', 'parent_col_id' => '23', 'parent_row_id' => '2201', 'parent_var_id' => NULL, 'parent_is_draft' => '0', 'child_entry_id' => '1197', 'rel_order' => '0', 'publisher_lang_id' => 1, 'publisher_status' => 'open'));
     $expected = array(0 => array('parent_id' => '800', 'field_id' => 38, 'child_id' => '1200', 'order' => '0', 'grid_field_id' => 133, 'grid_col_id' => 38, 'grid_row_id' => '2210', 'publisher_lang_id' => 1, 'publisher_status' => 'open'), 1 => array('parent_id' => '800', 'field_id' => 38, 'child_id' => '1199', 'order' => '0', 'grid_field_id' => 133, 'grid_col_id' => 38, 'grid_row_id' => '2209', 'publisher_lang_id' => 2, 'publisher_status' => 'open'), 2 => array('parent_id' => '800', 'field_id' => 38, 'child_id' => '1198', 'order' => '0', 'grid_field_id' => 133, 'grid_col_id' => 38, 'grid_row_id' => '2202', 'publisher_lang_id' => 3, 'publisher_status' => 'draft'), 3 => array('parent_id' => '800', 'field_id' => 38, 'child_id' => '1197', 'order' => '0', 'grid_field_id' => 133, 'grid_col_id' => 38, 'grid_row_id' => '2201', 'publisher_lang_id' => 1, 'publisher_status' => 'open'));
     $new_data = PlayaConverter::convertPlayaRelationshipsForGrid($data, $matrix_to_grid_fields, $matrix_to_grid_cols);
     $this->assertEquals($expected, $new_data, 'Should preserve Publisher columns in Matrix relationships');
     $expected = array(0 => array('parent_id' => '800', 'field_id' => 38, 'child_id' => '1200', 'order' => '0', 'grid_field_id' => 133, 'grid_col_id' => 38, 'grid_row_id' => '2210'), 3 => array('parent_id' => '800', 'field_id' => 38, 'child_id' => '1197', 'order' => '0', 'grid_field_id' => 133, 'grid_col_id' => 38, 'grid_row_id' => '2201'));
     $new_data = PlayaConverter::filterPublisherRelationships($new_data, 1);
     $this->assertEquals($expected, $new_data, 'Should filter out certain Matrix relationships based on Publisher data');
 }