Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add relationships support for grids #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion system/expressionengine/third_party/json/pi.json.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Json
protected $entries_grid_cols;
protected $entries_rel_data;
protected $entries_relationship_data;
protected $entries_grid_relationship_data;
protected $entries_playa_data;
protected $entries_channel_files_data;
protected $image_manipulations = array();
Expand Down Expand Up @@ -431,7 +432,12 @@ protected function entries_grid($entry_id, $field, $field_data)

foreach ($this->entries_grid_cols[$field['field_id']] as $col_id => $col)
{
$row[$col['col_name']] = $grid_row['col_id_'.$col_id];
$val = $grid_row['col_id_' . $col_id];
if ($col['col_type'] == 'relationship')
{
$val = $this->entries_grid_relationship($col_id, $row['row_id'], $entry_id);
}
$row[$col['col_name']] = $val;
}

$data[] = $row;
Expand Down Expand Up @@ -502,6 +508,42 @@ protected function entries_relationship($entry_id, $field, $field_data)
return array();
}

protected function entries_grid_relationship($grid_col_id, $grid_row_id, $entry_id)
{
if (is_null($this->entries_grid_relationship_data))
{
$query = ee()->db->select('parent_id, child_id, grid_field_id, grid_col_id, grid_row_id')
->where_in('parent_id', $this->entries_entry_ids)
->where('grid_col_id', $grid_col_id)
->order_by('order', 'asc')
->get('relationships');

foreach ($query->result_array() as $row)
{
if ( ! isset($this->entries_grid_relationship_data[$grid_col_id][$row['parent_id']][$row['grid_row_id']]))
{
$this->entries_grid_relationship_data[$grid_col_id][$row['parent_id']][$row['grid_row_id']] = array();
}

if ( ! isset($this->entries_grid_relationship_data[$grid_col_id][$row['parent_id']][$row['grid_row_id']]))
{
$this->entries_grid_relationship_data[$grid_col_id][$row['parent_id']][$row['grid_row_id']] = array();
}

$this->entries_grid_relationship_data[$grid_col_id][$row['parent_id']][$row['grid_row_id']][] = (int) $row['child_id'];
}

$query->free_result();
}

if (isset($this->entries_grid_relationship_data[$grid_col_id][$entry_id][$grid_row_id]))
{
return $this->entries_grid_relationship_data[$grid_col_id][$entry_id][$grid_row_id];
}

return array();
}

protected function entries_playa($entry_id, $field, $field_data)
{
if (is_null($this->entries_playa_data))
Expand Down Expand Up @@ -1058,6 +1100,7 @@ protected function initialize($which = NULL)
$this->entries_matrix_rows = NULL;
$this->entries_rel_data = NULL;
$this->entries_relationship_data = NULL;
$this->entries_grid_relationship_data = NULL;
$this->entries_playa_data = NULL;
$this->entries_channel_files_data = NULL;
break;
Expand Down