Last active
August 21, 2024 08:51
-
-
Save engram-design/5fbe54ef0abb15e3ff6f667291098464 to your computer and use it in GitHub Desktop.
ExpressionEngine PHP Export into JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
User Export - id is the group id