Skip to content

Instantly share code, notes, and snippets.

@engram-design
Last active September 12, 2023 12:01
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save engram-design/5fbe54ef0abb15e3ff6f667291098464 to your computer and use it in GitHub Desktop.
Save engram-design/5fbe54ef0abb15e3ff6f667291098464 to your computer and use it in GitHub Desktop.
ExpressionEngine PHP Export into JSON
<?php
$channel = $_GET['id'];
$content = array();
$fields_query = $this->EE->db->query("SELECT * FROM exp_channel_fields LEFT JOIN exp_channels ON exp_channel_fields.group_id = exp_channels.field_group WHERE exp_channels.channel_id = '$channel'");
$entries_query = $this->EE->db->query("SELECT * FROM exp_channel_data cd INNER JOIN exp_channel_titles ct ON cd.entry_id = ct.entry_id WHERE cd.channel_id = '$channel'");
$fields = $fields_query->result_array();
foreach ($entries_query->result_array() as $id => $row) {
$content[$id] = array();
$content[$id]['title'] = $row['title'];
$content[$id]['url_title'] = $row['url_title'];
$content[$id]['status'] = $row['status'];
$content[$id]['enabled'] = ($row['status'] == 'closed') ? 0 : 1;
$content[$id]['entry_date'] = ($row['entry_date']) ? date('Y-m-d H:i:s', $row['entry_date']) : '';
$content[$id]['expiration_date'] = ($row['expiration_date']) ? date('Y-m-d H:i:s', $row['expiration_date']) : '';
$content[$id]['author_id'] = $row['author_id'];
// Handle category
$categories = $this->EE->db->query("SELECT * FROM exp_category_posts c JOIN exp_categories cp ON c.cat_id = cp.cat_id WHERE entry_id = ".$row['entry_id'])->result_array();
if ($categories) {
foreach ($categories as $category) {
$content[$id]['category_' . $category['group_id']][] = $category['cat_name'];
}
}
foreach ($fields as $field) {
// Support for P&T Matrix field
if ($field['field_type'] == 'matrix') {
$query = $this->EE->db->query("SELECT * FROM exp_matrix_cols WHERE field_id=".$field['field_id']);
foreach ($query->result_array() as $matrix_col) {
$query = $this->EE->db->query("SELECT * FROM exp_matrix_data WHERE field_id=".$field['field_id']." AND entry_id=".$row['entry_id']." ORDER BY row_order");
foreach ($query->result_array() as $key => $matrix_row) {
// Support for File field
if ($matrix_col['col_type'] == 'file') {
$this->EE->load->library('file_field');
$fileInfo = $this->EE->file_field->parse_field($matrix_row['col_id_'.$matrix_col['col_id']]);
$content[$id][$field['field_name']][$key][$matrix_col['col_name']] = $fileInfo['url'];
} else {
// Any other field
$content[$id][$field['field_name']][$key][$matrix_col['col_name']] = $matrix_row['col_id_'.$matrix_col['col_id']];
}
}
}
// Support for P&T Playa field
} elseif ($field['field_type'] == 'playa') {
// Change from "[93] [my-category] My Category" = "My Category"
$value = preg_replace("/\[[^]]+\]/", '', $row['field_id_' . $field['field_id']]);
$content[$id][$field['field_name']] = trim($value);
// Support for File field
} elseif ($field['field_type'] == 'file') {
$this->EE->load->library('file_field');
$fileInfo = $this->EE->file_field->parse_field($row['field_id_' . $field['field_id']]);
$content[$id][$field['field_name']] = $fileInfo['url'];
// Support for P&T Assets field
} elseif ($field['field_type'] == 'assets') {
$value = $row['field_id_' . $field['field_id']];
$content[$id][$field['field_name']] = $value;
} else {
// Any other field
$content[$id][$field['field_name']] = $row['field_id_' . $field['field_id']];
}
}
}
echo json_encode($content);
?>
@Nav33d
Copy link

Nav33d commented Jul 18, 2017

@engram-design Is there a reason that image field is only returning the name of the image instead of the path like it is in the database {filedir_1}square/people-profile-pics/anna-staff.jpg.? It only returns anna-staff.jpg in that case.

Many thanks

@engram-design
Copy link
Author

engram-design commented Jul 19, 2017

@Nav33d This script assumes you'll copy all the assets from the site manually, and place them in your folders in Craft. Running Index Assets will scan the file system (for each asset source) and add them to the database. Now with the assets it Craft, you only need to supply the filename for Feed Me to find the correct asset in Craft.

Essentially, I didn't want to complicate this script by adding find/replace functionality to swap out the file directories with what they should be. I could add this in if you like.

EDIT: Actually, I'm not sure I follow. My own testing shows a File file with an image will output {filedir_2}JCW1115-695.jpg and not JCW1115-695.jpg

@engram-design
Copy link
Author

@Nav33d If you're interested, I've now added auto-processing of the URL to return its final URL, as opposed to just what's stored in EE.

@alexmglover
Copy link

How would you recommend handling the {page_xx} vars in the content?

This is great by the way, thank you.

@akukral
Copy link

akukral commented Feb 27, 2018

Is there a way to auto-process asset names in a WYGWAM or Rich Text filed the same way you did with the File?
I'm getting embedded images like:

<img src="{assets_16862:http://siteurl.com/press/sl_3.jpg}" style="width:794px;height:448px;”>

But would love to instead receive:

<img src=“http://siteurl.com/press/sl_3.jpg#asset:16862" style="width:794px;height:448px;”>

or even:

<img src=“http://siteurl.com/press/sl_3.jpg" style="width:794px;height:448px;”>

I also second @alexmglover question for recommendations for dealing with {page_xx} links. Unless you think a Craft global find and replace would work.

@nathanchicken
Copy link

We're running through this script to migrate from EE to Craft and this script has been very useful, thanks.

We had "grid" fields though, which weren't exporting properly.

I don't have much Expression Engine experience, so I don't know if I have done this 'right' or not, I just know my resulting JSON looks okay at this point. I thought it might be helpful to some people...

I am well aware it's going to fail with asset fields, or anything more than just a string-in-a-field, lucky for us we weren't using it for more than that.

} elseif ($field['field_type'] == 'grid') {
    
    $labelsQuery = $this->EE->db->query("SELECT * FROM exp_grid_columns WHERE field_id=".$field['field_id']);
    $labels = $labelsQuery->result_array();
    
    $q = "SELECT * FROM exp_channel_grid_field_".$field['field_id']." WHERE entry_id=".$row['entry_id']." ORDER BY row_order ASC";
    $fieldQuery = $this->EE->db->query( $q );
    
    $gridrows = $fieldQuery->result_array();
    
    $result = [];
    foreach ( $labels as $label_id => $label ) {
        
        $rowValues = [];
        
        foreach ( $gridrows as $id => $gridrow ) {
            $val = $gridrow[ 'col_id_' . $label['col_id'] ];
            $result[$id][ $label['col_label'] ] = $val;
        }
    }
    
    $content[$id][$field['field_name']] = $result;
}

@Rocketpilot
Copy link

@engram-design this looks very useful. I just demoed on a big content site and the only issue I'm seeing right now is that it won't output anything other than "0" for relationship fields. Even just the output text value would be better than that.

@jderdock
Copy link

jderdock commented Aug 2, 2019

@NathanEdwards have you successfully run this script on a post 2.x EE site? I can't get it to work in 5

@nathanchicken
Copy link

@jderdock afraid not - I don't think we run any EE sites any more.

@stuart-haas
Copy link

stuart-haas commented Oct 14, 2019

Here's an improvement to the assets field, which will get the full url, rather than just the file name.

                      $this->EE->load->library('assets_lib');

                      $col_id = $matrix_col['col_id'];

                      $assets_query = $this->EE->db->query(
                        "SELECT DISTINCT a.source_type, a.folder_id, a.file_name, a.file_id, af.source_id, af.filedir_id
                        FROM exp_assets_files AS a
                        INNER JOIN exp_assets_selections AS ae ON ae.file_id = a.file_id
                        INNER JOIN exp_assets_folders AS af ON af.folder_id = a.folder_id
                        WHERE ae.entry_id = '$entry_id' AND ae.col_id = '$col_id' LIMIT 1");

                      $assets_result = $assets_query->result_array();

                      if($assets_result) {

                        $assetId = $assets_result[0]['file_id'];

                        $assetUrl = $this->EE->assets_lib->get_file_url($assetId);
                      
                        $content[$id][$field['field_name']][$key][$matrix_col['col_name']] = $assetUrl;

                      }

@stigzelm
Copy link

Any tips on how to export the value from relationships field?

@stigzelm
Copy link

Ok, I figured out the relationship field part.

} elseif ($field['field_type'] == 'relationship') {

    // set a default emtpy array for the relationship
    $relationshipValue = array();

    // Fetch the relationship child id
    $query = $this->EE->db->query("SELECT child_id FROM exp_relationships WHERE field_id=".$field['field_id']." AND parent_id=".$row['entry_id']);

    // Loop through all child ids
    foreach ($query->result_array() as $key => $r_row) {
        // Fetch the entries with the child ids
        $r_entries_query = $this->EE->db->query("SELECT * FROM exp_channel_data cd INNER JOIN exp_channel_titles ct ON cd.entry_id = ct.entry_id WHERE cd.entry_id=".$r_row['child_id']);

        // loop through all fields related to the entries
        foreach ($r_entries_query->result_array() as $entry) {
            // Set the entry meta data
            $relationshipValue[$key]['title'] = $entry['title'];
            $relationshipValue[$key]['url_title'] = $entry['url_title'];
            $relationshipValue[$key]['status'] = $entry['status'];
            $relationshipValue[$key]['enabled'] = ($entry['status'] == 'closed') ? 0 : 1;
            $relationshipValue[$key]['entry_date'] = ($entry['entry_date']) ? date('Y-m-d H:i:s', $entry['entry_date']) : '';
            $relationshipValue[$key]['expiration_date'] = ($entry['expiration_date']) ? date('Y-m-d H:i:s', $entry['expiration_date']) : '';
            $relationshipValue[$key]['author_id'] = $entry['author_id'];

            $r_fields_query = $this->EE->db->query("SELECT * FROM exp_channel_fields LEFT JOIN exp_channels ON exp_channel_fields.group_id = exp_channels.field_group WHERE exp_channels.channel_id=".$entry['channel_id']);

            $r_fields = $r_fields_query->result_array();

            // loop through all fields, to get the correct information
            foreach ($r_fields as $f) {
                // add here additional checks for different field types
                $relationshipValue[$key][$f['field_name']] = $entry['field_id_'. $f['field_id']];
            }
        }
    }


    $content[$id][$field['field_name']] = $relationshipValue;
}

@samdking
Copy link

samdking commented Jan 28, 2020

We're running through this script to migrate from EE to Craft and this script has been very useful, thanks.

We had "grid" fields though, which weren't exporting properly.

I don't have much Expression Engine experience, so I don't know if I have done this 'right' or not, I just know my resulting JSON looks okay at this point. I thought it might be helpful to some people...

I am well aware it's going to fail with asset fields, or anything more than just a string-in-a-field, lucky for us we weren't using it for more than that.

} elseif ($field['field_type'] == 'grid') {
    
    $labelsQuery = $this->EE->db->query("SELECT * FROM exp_grid_columns WHERE field_id=".$field['field_id']);
    $labels = $labelsQuery->result_array();
    
    $q = "SELECT * FROM exp_channel_grid_field_".$field['field_id']." WHERE entry_id=".$row['entry_id']." ORDER BY row_order ASC";
    $fieldQuery = $this->EE->db->query( $q );
    
    $gridrows = $fieldQuery->result_array();
    
    $result = [];
    foreach ( $labels as $label_id => $label ) {
        
        $rowValues = [];
        
        foreach ( $gridrows as $id => $gridrow ) {
            $val = $gridrow[ 'col_id_' . $label['col_id'] ];
            $result[$id][ $label['col_label'] ] = $val;
        }
    }
    
    $content[$id][$field['field_name']] = $result;
}

I think I managed to figure out why this doesn't work. The $id in the $gridrows loop was overwriting the previously set $id variable in the main entries loop. Changing the variable name appears to fix the problem for me:

} elseif ($field['field_type'] == 'grid') {
    
    $labelsQuery = $this->EE->db->query("SELECT * FROM exp_grid_columns WHERE field_id=".$field['field_id']);
    $labels = $labelsQuery->result_array();
    
    $q = "SELECT * FROM exp_channel_grid_field_".$field['field_id']." WHERE entry_id=".$row['entry_id']." ORDER BY row_order ASC";
    $fieldQuery = $this->EE->db->query( $q );
    
    $gridrows = $fieldQuery->result_array();
    
    $result = [];
    foreach ( $labels as $label_id => $label ) {
        
        $rowValues = [];
        
        foreach ( $gridrows as $grid_row_id => $gridrow ) {
            $val = $gridrow[ 'col_id_' . $label['col_id'] ];
            $result[$grid_row_id][ $label['col_label'] ] = $val;
        }
    }
    
    $content[$id][$field['field_name']] = $result;
}

@VarenSwaab
Copy link

VarenSwaab commented Feb 6, 2020

Thanks for the script but when I run it in an EE template I get the following result. To be clear, I have php enabled for the template and the channel number I'm exporting set correctly. I have a fairly straightforward setup.

{!-- ra:0000000068e29d88000000005e2dd788 --}

@jonXmack
Copy link

Most of our legacy EE sites use SEO Lite and I wanted to import the data into Craft so I just modified a couple of lines as follows

Line 8:

$entries_query = $this->EE->db->query("SELECT cd.*, ct.*, sc.title as seo_title, sc.keywords as seo_keywords, sc.description as seo_description FROM exp_channel_data cd INNER JOIN exp_channel_titles ct ON cd.entry_id = ct.entry_id INNER JOIN exp_seolite_content sc ON ct.entry_id = sc.entry_id WHERE cd.channel_id = '$channel'");

Lines 21-23:

$content[$id]['seo_title'] = $row['seo_title'];
$content[$id]['seo_keywords'] = $row['seo_keywords'];
$content[$id]['seo_description'] = $row['seo_description'];

@prdanelli
Copy link

prdanelli commented Feb 14, 2020

In order to give back and contribute to the community, i'm adding my version of this script. I made it into an EE plugin, because I was having issues with EE template caching so this way you just need to add {exp:export:common channel_id="<your channel id integer>"} and it'll output the json for you. It also means you don't need to enable PHP processing within EE.

I added the SEO lite columns posted above, as well as relationships and nested grid relationships.

Thank you to the original developer who write the script, it saved us a bunch of time and frustration.

Just save this file as pi.export.php add this plugin to your plugins directory inside of a directory called export. Then add the snippet above and you're good to go.

$plugin_info = [
  'pi_name' => 'Export',
  'pi_version' => '1.0',
  'pi_author' => '',
  'pi_author_url' => '',
  'pi_description' => 'Export EE to JSON',
  'pi_usage' => Export::usage()
];

class Export
{
  protected $ee;

  public static function usage() {
    print 'Add a template in your required group add the following code: {exp:export:common channel_id="4"}';
  }

  public function __construct() {
    $this->ee =& get_instance();
  }

  # A general purpose example of getting the data from EE into a JSON export
  public function common() {
    $content = [];

    foreach ($this->entry_data()->result_array() as $id => $row) {
      $content[$id] = $this->process_data($row);
    }

    $this->json_response($content);
  }

  # Helper method for a unified JSON resposne
  protected function json_response($content) {
    $this->set_header();

    # Name space the returned objects within their channel and return JSON
    echo json_encode([$this->channel_name() => $content]);
    die;
  }

  protected function set_header() {
    header("Content-type:application/json;charset=utf-8");
  }

  protected function entry_data() {
    $sql = "SELECT cd.*, ct.*, sc.title as seo_title, sc.keywords as seo_keywords, sc.description as seo_description
            FROM exp_channel_data cd INNER JOIN exp_channel_titles ct ON cd.entry_id = ct.entry_id
            INNER JOIN exp_seolite_content sc ON ct.entry_id = sc.entry_id
            WHERE cd.channel_id = ". $this->channel_id();

    return $this->ee->db->query($sql);
  }

  protected function channel_name() {
    $sql = "SELECT * FROM exp_channels where channel_id = '" . $this->channel_id() . "'";

    return $this->ee->db->query($sql)->row()->channel_name;
  }

  protected function channel_id() {
    return $this->ee->TMPL->fetch_param("channel_id");
  }

  # Shared general processing, returns an array, included grid, asset and regular field data
  protected function process_data($row) {
    $content = [];
    $content['ID'] = $row["entry_id"];
    $content['title'] = $row['title'];
    $content['url_title'] = $row['url_title'];
    $content['status'] = $row['status'];
    $content['enabled'] = ($row['status'] == 'closed') ? 0 : 1;
    $content['entry_date'] = ($row['entry_date']) ? date('Y-m-d H:i:s', $row['entry_date']) : '';
    $content['expiration_date'] = ($row['expiration_date']) ? date('Y-m-d H:i:s', $row['expiration_date']) : '';
    $content['author_id'] = $row['author_id'];
    $content['seo_title'] = $row['seo_title'];
    $content['seo_keywords'] = $row['seo_keywords'];
    $content['seo_description'] = $row['seo_description'];

    // Handle category
    $sql = "SELECT * FROM exp_category_posts c JOIN exp_categories cp ON c.cat_id = cp.cat_id WHERE entry_id = ".$row['entry_id'];
    $categories = $this->ee->db->query($sql)->result_array();

    if ($categories) {
      foreach ($categories as $category) {
        $content['category_' . $category['group_id']][] = $category['cat_name'];
      }
    }

    $sql = "SELECT * FROM exp_channel_fields
    LEFT JOIN exp_channels ON exp_channel_fields.group_id = exp_channels.field_group
    WHERE exp_channels.channel_id = '" . $this->channel_id() . "'";

    $fields_query = $this->ee->db->query($sql);
    $fields = $fields_query->result_array();

    foreach ($fields as $field) {
      // File field
      if ($field['field_type'] == 'file') {
        $this->ee->load->library('file_field');

        $fileInfo = $this->ee->file_field->parse_field($row['field_id_' . $field['field_id']]);
        $content[$field['field_name']] = basename($fileInfo['url']);

      // Grids
      } elseif ($field['field_type'] == 'grid') {
        $content[$field['field_name']] = $this->grid_values($field['field_id'], $row['entry_id']);

      // Relationships
      } elseif ($field['field_type'] == 'relationship') {
        $content[$id][$field['field_name']] = $this->relationship_values($field['field_id'], $row['entry_id']);

      // Any other field
      } else {
        $content[$field['field_name']] = $row['field_id_' . $field['field_id']];
      }
    }

    return $content;
  }

  protected function grid_values($field_id, $entry_id) {
    $query = $this->ee->db->query("SELECT * FROM exp_grid_columns WHERE field_id = " . $field_id);
    $labels = $query->result_array();

    $sql = "SELECT * FROM exp_channel_grid_field_" . $field_id . " WHERE entry_id=" . $entry_id . " ORDER BY row_order ASC";
    $query = $this->ee->db->query($sql);
    $grid_rows = $query->result_array();

    $result = [];
    foreach ($labels as $label_id => $label) {
      foreach ($grid_rows as $grid_row_id => $grid_row) {

        if ($label["col_type"] == "relationship") {
          $allow_multiple = json_decode($label["col_settings"])->allow_multiple;
          $relationship = $this->relationship_values($label["col_id"], $grid_row["entry_id"]);

          $value = "";
          if ($allow_multiple === 1) {
            $value = $relationship;

          // Relationships are reordered on update, so they should match the order of the query result
          } elseif (count($relationship) > 0) {
            $value = $relationship[$grid_row_id];
          }

        } else {
          $value = $grid_row['col_id_' . $label['col_id']];
        }

        $result[$grid_row_id][$label['col_label']] = $value;
      }
    }

    return $result;
  }

  protected function relationship_values($field_id, $parent_id) {
    $content = [];

    // Fetch the relationship child id - which is the ID of the entry it relates to.
    // Each grid is destroyed and recreated on update, so ordering by relationship_id is the grid order
    $sql = "SELECT child_id FROM exp_relationships
            WHERE field_id = " . $field_id . "
            AND parent_id=" . $parent_id . "
            ORDER BY relationship_id"; #
    $query = $this->ee->db->query($sql);

    foreach ($query->result_array() as $key => $r_row) {
      // Fetch the entries with the child ids
      $sql = "SELECT * FROM exp_channel_data cd
              INNER JOIN exp_channel_titles ct ON cd.entry_id = ct.entry_id
              WHERE cd.entry_id = " . $r_row['child_id'];
      $r_entries_query = $this->ee->db->query($sql);

      // loop through all fields related to the entries
      foreach ($r_entries_query->result_array() as $entry) {
        $content[$key]['ID'] = $entry['entry_id'];
        $content[$key]['title'] = $entry['title'];
        $content[$key]['url_title'] = $entry['url_title'];
        $content[$key]['status'] = $entry['status'];
        $content[$key]['enabled'] = ($entry['status'] == 'closed') ? 0 : 1;
        $content[$key]['entry_date'] = $entry['entry_date'] ? date('Y-m-d H:i:s', $entry['entry_date']) : '';
        $content[$key]['expiration_date'] = $entry['expiration_date'] ? date('Y-m-d H:i:s', $entry['expiration_date']) : '';
        $content[$key]['author_id'] = $entry['author_id'];

        $sql = "SELECT * FROM exp_channel_fields
                LEFT JOIN exp_channels ON exp_channel_fields.group_id = exp_channels.field_group
                WHERE exp_channels.channel_id = ".$entry['channel_id'];
        $r_fields_query = $this->ee->db->query($sql);

        $r_fields = $r_fields_query->result_array();

        // loop through all fields, to get the correct information
        foreach ($r_fields as $f) {
          $content[$key][$f['field_name']] = $entry['field_id_'. $f['field_id']];
        }
      }
    }

    return $content;
  }
}

@doctorgloom
Copy link

I know this is a longshot, but I'm helping with a site that is running EE 1.7.3 (ancient!) and to make matters worse used two old extensions to build out their catalog (both by Mark Huot, File Type and Multirelationship). This export solution won't work because it is for EE2, and I tried updating to EE2 and running it but it won't work because of those old Extensions (which break things in EE2). The goal is to move to Craft.

So, I was wondering if anyone had any suggestions? And like I said, I know it's a longshot.

@prdanelli
Copy link

prdanelli commented Oct 29, 2020 via email

@CaptainOfTheSky
Copy link

CaptainOfTheSky commented Aug 9, 2021

Hey everyone, not sure how many people are still doing this but I thought I might add my edits for whoever it might help. I found that files/and assets, particularly one's using aws' S3 buckets, didn't work well. I also had issues with categories not working. There is also a seperate template for Members/Users which is the second snippet

<?php

$channel = $_GET['id'];
$content = array();

$fields_query = $this->EE->db->query("SELECT * FROM exp_channel_fields LEFT JOIN exp_channels ON exp_channel_fields.group_id = exp_channels.field_group WHERE exp_channels.channel_id = '$channel'");

$entries_query = $this->EE->db->query("SELECT * FROM exp_channel_data cd INNER JOIN exp_channel_titles ct ON cd.entry_id = ct.entry_id WHERE cd.channel_id = '$channel'");

$fields = $fields_query->result_array();

foreach ($entries_query->result_array() as $id => $row) {
    $content[$id] = array();
    $content[$id]['title'] = $row['title'];
    $content[$id]['url_title'] = $row['url_title'];
    $content[$id]['status'] = $row['status'];
    $content[$id]['enabled'] = ($row['status'] == 'closed') ? 0 : 1;
    $content[$id]['entry_date'] = ($row['entry_date']) ? date('Y-m-d H:i:s', $row['entry_date']) : '';
    $content[$id]['expiration_date'] = ($row['expiration_date']) ? date('Y-m-d H:i:s', $row['expiration_date']) : '';
    $content[$id]['author_id'] = $row['author_id'];
    
    // Handle category
    $categories = $this->EE->db->query("SELECT * FROM exp_category_posts c JOIN exp_categories cp ON c.cat_id = cp.cat_id WHERE entry_id = ".$row['entry_id'])->result_array();

    if ($categories) {
        foreach ($categories as $category) {
            $content[$id]['category_' . $category['group_id']][] = $category['cat_name'];
        }
    }
    
    foreach ($fields as $field) {
        // Support for P&T Matrix field
        if ($field['field_type'] == 'matrix') {
            $query = $this->EE->db->query("SELECT * FROM exp_matrix_cols WHERE field_id=".$field['field_id']);

            foreach ($query->result_array() as $matrix_col) {
                $query = $this->EE->db->query("SELECT * FROM exp_matrix_data WHERE field_id=".$field['field_id']." AND entry_id=".$row['entry_id']." ORDER BY row_order");

                foreach ($query->result_array() as $key => $matrix_row) {
                    // Support for File field
                    if ($matrix_col['col_type'] == 'file') {
                        $this->EE->load->library('file_field');
                        $fileInfo = $this->EE->file_field->parse_field($matrix_row['col_id_'.$matrix_col['col_id']]);
                        
                        $content[$id][$field['field_name']][$key][$matrix_col['col_name']] = $fileInfo['url'];
                    } else {
                        // Any other field
                    	$content[$id][$field['field_name']][$key][$matrix_col['col_name']] = $matrix_row['col_id_'.$matrix_col['col_id']];
                    }
                }
            }
        // Support for P&T Playa field
        } elseif ($field['field_type'] == 'playa') {
            $productList = array();
            $lines = explode("\n", $row['field_id_' . $field['field_id']]);
            if ($field['field_label'] == 'Products'){            
                foreach($lines as $line){
                    $matches = array();
                    preg_match_all("/(?<=\[)([^\[\]]*)(?=\])/", $line, $matches);
                    $product = $matches[0]; //= ~["29","10-resources-to-teach-stem-in-afterschool"]
                    if(count($product) == 0){ break; }
                    array_push($productList, $product[1]);
                }

                // var_dump($matches);
                $content[$id][$field['field_name']] = $productList;
            }
            else {
                // Change from "[93] [my-category] My Category" = "My Category"   
                foreach($lines as $line){    
                    array_push($productList, preg_replace("/\[[^]]+\]\s*/", '', $line));
                }
                // $content[$id][$field['field_name']] = trim($value);
                $content[$id][$field['field_name']] = $productList;
            }
            
        // Support for File field   
        } elseif ($field['field_type'] == 'file') {
            $url;
            if($field['field_name'] == 'product_thumbnail' || $field['field_name'] == 'product_detail_image') {
                $url = preg_replace('/{.*}/', '', $row['field_id_' . $field['field_id']]);
            }
            else {
                $this->EE->load->library('file_field');
                $fileInfo = $this->EE->file_field->parse_field($row['field_id_' . $field['field_id']]);
                $url = $fileInfo['url'];
            }
            $content[$id][$field['field_name']] = $url;
        //Channel Images
        } elseif ($field['field_type'] == 'channel_images') {
            $imageQuery = $this->EE->db->query("SELECT * FROM exp_channel_images WHERE field_id=".$field['field_id']." and entry_id=".$row['entry_id']);
            $images = array();

            foreach ($imageQuery->result_array() as $iid => $image) {
                $images[$iid] = array();

                $images[$iid]['Title'] = $image['title'];
                $images[$iid]['URL'] = '/' . $image['filename'];
                $images[$iid]['Caption'] = $image['description'];
            }
            $content[$id][$field['field_name']] = $images;

        // Support for P&T Assets field
        } elseif ($field['field_type'] == 'assets') {
            $value = $row['field_id_' . $field['field_id']];
            $content[$id][$field['field_name']] =$value;
            
        } else {
            // Any other field
            $content[$id][$field['field_name']] = $row['field_id_' . $field['field_id']];
        }
    }
}
    
echo json_encode($content);

?>

User Export - id is the group id

<?php

$group = $_GET['id'];
$content = array();

$fields_query = $this->EE->db->query("SELECT * FROM exp_member_fields;");

$entries_query = $this->EE->db->query("
    SELECT m.group_id, m.username, m.screen_name, m.email, md.* FROM exp_member_data md
    INNER JOIN exp_members m ON md.member_id = m.member_id 
    WHERE m.group_id = '$group';");

$fields = $fields_query->result_array();

foreach ($entries_query->result_array() as $id => $row) {
    $content[$id] = array();
    $content[$id]['username'] = $row['username'];
    $content[$id]['screen_name'] = $row['screen_name'];
    $content[$id]['email'] = $row['email'];
    
    foreach ($fields as $field) {
        $content[$id][$field['m_field_name']] = $row['m_field_id_' . $field['m_field_id']];
    }
}
    
echo json_encode($content);

?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment