Skip to content
This repository has been archived by the owner on Jan 9, 2021. It is now read-only.

Commit

Permalink
Closed #24
Browse files Browse the repository at this point in the history
Addresses an issue where Grid Fields are appearing on all entry of the
same content_type
  • Loading branch information
cosmomathieu committed Apr 22, 2016
1 parent a061409 commit c097939
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ class Grid_fields
$post = '',
$gridData = [],
$CI,
$entry_id = '',
$grid_col_data = 'grid_col_data';

public function __construct($data = [])
{
$this->post = $data;
$this->post = $data;
$this->CI = get_instance();
$this->entry_id = $this->CI->uri->segment(6);
}

/**
Expand Down Expand Up @@ -90,6 +92,7 @@ private function save($gridData, $row_order = [])
foreach($row_data as $grid_col_id => $col_data) {
}
$data = [
'entry_id' => $this->entry_id,
'grid_col_id' => $grid_col_id,
'row_data' => $col_data,
];
Expand Down
42 changes: 38 additions & 4 deletions application/modules/content/content_fields/models/grid_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ private function build_table()
$content_fields_array = ($this->session->userdata('content_fields')) ? $this->session->userdata('content_fields') : [];

// Get all elligible fields
$content_fields = $this->db->select('*')->where('content_type_id', $content_type_id)->get('content_fields');

for($i=0;$i<count($content_field_array = $content_fields->result());$i++) {
$content_fields = $this->db->select('*')
->where('content_type_id', $content_type_id)
->where('content_field_type_id', 16)
->get('content_fields');
$content_field_array = $content_fields->result();

for($i=0; $i<count($content_field_array); $i++) {
if( ! array_key_exists($i, $content_fields_array)) {
$content_fields_array[] = $content_field_array[$i]->id;
$this->session->set_userdata(['content_fields' => $content_fields_array]);
Expand All @@ -86,6 +90,7 @@ private function build_table()

// Get the table rows
$this->db->select('*, grid_col_data.id');
$this->db->where('grid_col_data.entry_id', $entry_id);
$this->db->where('grid_cols.content_field_id', $content_field_array[$i]->id);
$this->db->join('grid_cols', 'grid_cols.id = grid_col_data.grid_col_id', 'left');
$this->db->order_by("grid_col_data.row_order", 'asc');
Expand Down Expand Up @@ -321,6 +326,28 @@ function renumberRows(tableRow = '') {
showNoRowExist();
}
});
// -----------------------------------------------------------
// ckeditor config
var grid_ckeditor_config = {
toolbar : [
{ name: 'basicstyles', items : [ 'Undo','Redo','-','Bold','Italic','Underline','Strike' ] },
{ name: 'styles', items : [ 'Format' ] },
{ name: 'paragraph', items : [ 'NumberedList','BulletedList','-','Blockquote','- ','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock' ] },
{ name: 'tools', items : [ 'Maximize' ] },
'/',
{ name: 'links', items : [ 'Link','Unlink','Anchor' ] },
{ name: 'insert', items : [ 'HorizontalRule', 'ShowBlocks', '-', 'Source' ] }
],
entities : true,
// height : '150px',
};
$('textarea.ckeditor_grid_textarea').each(function(index) {
grid_ckeditor_config.height = $(this).height();
CKEDITOR.replace($(this).attr('name'), grid_ckeditor_config);
});
});";

$this->template->add_script($script);
Expand All @@ -334,7 +361,14 @@ private function format_field_type($type, $grid_col_id, $options, $row_data, $gr
{
$field = '';
switch($type) {
case 3 : $field = '
case 1 : $field = '
<td style="width: auto;" class="matrix matrix-text">
<textarea style="overflow: hidden; min-height: 14px;" class="matrix-textarea ckeditor_grid_textarea" height="150" name="grid_col_data['.$grid_col_data_id.']['.$grid_col_id.']" dir="ltr">'. $row_data .'</textarea>
<div class="matrix-charsleft-container"><div class="matrix-charsleft">'.$options.'</div></div>
</td>';
break;

case 3 : $field = '
<td style="width: auto;" class="matrix matrix-text">
<textarea style="overflow: hidden; min-height: 14px;" class="matrix-textarea" name="grid_col_data['.$grid_col_data_id.']['.$grid_col_id.']" dir="ltr">'. $row_data .'</textarea>
<div class="matrix-charsleft-container"><div class="matrix-charsleft">'.$options.'</div></div>
Expand Down
25 changes: 12 additions & 13 deletions application/modules/content/libraries/Grid_fields_library.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ public function __construct()
* @access public
* @return array
*/
public function get_data($content_fields)
public function get_data($entry_id, $content_fields)
{
$data = []; // Final data to be returned to the caller

// var_dump($content_fields); die();

foreach($content_fields as $key => $content_field) {
// Get field names associated with the content field
Expand All @@ -73,30 +75,29 @@ public function get_data($content_fields)
if ($grid_cols)
{
// Get the table rows
$query = $this->db->select('short_tag, grid_col_data.row_data');
$query = $this->db->where('grid_cols.content_field_id', $content_field->id)
$query = $this->db->select('short_tag, grid_col_data.row_data')
->where('grid_cols.content_field_id', $content_field->id)
->where('grid_col_data.entry_id', $entry_id)
->join('grid_cols', 'grid_cols.id = grid_col_data.grid_col_id', 'left')
->order_by("grid_col_data.row_order", 'asc')
->get('grid_col_data');

$grid_rows = $query->result();
$grid_data = [];
$break = $grid_cols * 2;
$count = 1;
$array = [];
$fields = [];
$rows = 3;
$new_array = [];
// @note: This should count the relating total grid_cols
// to the entry being called.
$rows = 3;

foreach($grid_rows as $row => $data_set) {
foreach($data_set as $key => $value) {
if($count % 2) {
$label = $value;
} else {
$new_array[$label] = $value;
$grid_data[$label] = $value;
if($count % $rows === 0) {
$data[$content_field->short_tag][] = $new_array;
$new_array = [];
$data[$content_field->short_tag][] = $grid_data;
$grid_data = [];
}
}
$count++;
Expand All @@ -105,8 +106,6 @@ public function get_data($content_fields)
}
}

// var_dump($data); die();

return $data;
}
}
8 changes: 3 additions & 5 deletions application/modules/content/models/entries_cache_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,12 @@ function get_content_array()
*/
$CI->load->library('grid_fields_library');
$Grid_Fields_library = new Grid_Fields_library();
$grid_content_array = $Grid_Fields_library->get_data($this->content_fields);

// var_dump($grid_content_array); die();
$grid_content_array = $Grid_Fields_library->get_data($this->id, $this->content_fields);

if ( ! empty($grid_content_array)) {
$this->content_array = $grid_content_array;
$this->content_array = array_replace($this->content_array, $grid_content_array);
}

return $this->content_array;
}
}
Expand Down

0 comments on commit c097939

Please sign in to comment.