Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Added {category_group_id} and {category_group_name} variables; #2998 #4667

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

Open
wants to merge 1 commit into
base: 7.dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
71 changes: 51 additions & 20 deletions system/ee/ExpressionEngine/Addons/channel/mod.channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ public function fetch_categories()

foreach ($query->result_array() as $row) {
if ($val == $row['entry_id']) {
$this->temp_array[$row['cat_id']] = array($row['cat_id'], $row['parent_id'], $row['cat_name'], $row['cat_image'], $row['cat_description'], $row['group_id'], $row['cat_url_title']);
$this->temp_array[$row['cat_id']] = array($row['cat_id'], $row['parent_id'], $row['cat_name'], $row['cat_image'], $row['cat_description'], $row['group_id'], $row['cat_url_title'], $row['group_id'], $row['group_name'] ?? '');

foreach ($row as $k => $v) {
if (strpos($k, 'field') !== false) {
Expand Down Expand Up @@ -501,7 +501,7 @@ public function fetch_categories()

$categories = ee('Model')->get('Category', $cats)->all();
foreach ($categories as $cat) {
$this->temp_array[$cat->cat_id] = array($cat->cat_id, $cat->parent_id, $cat->cat_name, $cat->cat_image, $cat->cat_description, $cat->group_id, $cat->cat_url_title);
$this->temp_array[$cat->cat_id] = array($cat->cat_id, $cat->parent_id, $cat->cat_name, $cat->cat_image, $cat->cat_description, $cat->group_id, $cat->cat_url_title, $cat->group_id, $cat->group_name ?? '');
if ($cat->parent_id > 0 && ! isset($this->temp_array[$cat->parent_id])) {
$parents[$cat->parent_id] = '';
}
Expand Down Expand Up @@ -2927,7 +2927,7 @@ public function categories()

$this->cat_full_array = array_unique($this->cat_full_array);

$sql = "SELECT c.cat_id, c.parent_id, c.cat_name, c.cat_url_title, c.cat_image, c.cat_description {$field_sqla}
$sql = "SELECT c.cat_id, c.parent_id, c.cat_name, c.cat_url_title, c.cat_image, c.cat_description, c.group_id {$field_sqla}
FROM exp_categories AS c
{$field_sqlb}
WHERE c.cat_id IN (";
Expand All @@ -2946,7 +2946,7 @@ public function categories()
return ee()->TMPL->no_results();
}
} else {
$sql = "SELECT c.cat_name, c.cat_url_title, c.cat_image, c.cat_description, c.cat_id, c.parent_id {$field_sqla}
$sql = "SELECT c.cat_name, c.cat_url_title, c.cat_image, c.cat_description, c.cat_id, c.parent_id, c.group_id {$field_sqla}
FROM exp_categories AS c
{$field_sqlb}
WHERE c.group_id IN ('" . $group_ids_str . "') ";
Expand Down Expand Up @@ -2986,7 +2986,7 @@ public function categories()
continue;
}

$this->temp_array[$row['cat_id']] = array($row['cat_id'], $row['parent_id'], '1', $row['cat_name'], $row['cat_description'], $row['cat_image'], $row['cat_url_title']);
$this->temp_array[$row['cat_id']] = array($row['cat_id'], $row['parent_id'], '1', $row['cat_name'], $row['cat_description'], $row['cat_image'], $row['cat_url_title'], $row['group_id'], $row['group_name'] ?? '');

foreach ($row as $key => $val) {
if (strpos($key, 'field') !== false) {
Expand Down Expand Up @@ -3031,6 +3031,8 @@ public function categories()
'category_image' => (string) $val[5],
'category_id' => $val[0],
'parent_id' => $val[1],
'category_group_id' => $val[7],
'category_group_name' => $val[8],
'has_children' => in_array($val[0], $parent_ids),
'active' => ($active_cat == $val[0] || $active_cat == $val[6])
);
Expand All @@ -3053,15 +3055,19 @@ public function categories()
LD . 'category_description' . RD,
LD . 'category_image' . RD,
LD . 'category_id' . RD,
LD . 'parent_id' . RD
LD . 'parent_id' . RD,
LD . 'category_group_id' . RD,
LD . 'category_group_name' . RD
),
array(
ee()->functions->encode_ee_tags($cat_vars['category_name']),
$cat_vars['category_url_title'],
ee()->functions->encode_ee_tags($cat_vars['category_description']),
$cat_vars['category_image'],
$cat_vars['category_id'],
$cat_vars['parent_id']
$cat_vars['parent_id'],
$cat_vars['category_group_id'],
ee()->functions->encode_ee_tags($cat_vars['category_group_name'])
),
$chunk
);
Expand Down Expand Up @@ -3441,6 +3447,8 @@ public function category_archive()
'category_image' => (string) $row['cat_image'],
'category_id' => $row['cat_id'],
'parent_id' => $row['parent_id'],
'category_group_id' => $row['group_id'],
'category_group_name' => $row['group_name'] ?? '',
'has_children' => in_array($row['cat_id'], $parent_ids),
'active' => ($active_cat == $row['cat_id'] || $active_cat == $row['cat_url_title'])
);
Expand All @@ -3458,15 +3466,19 @@ public function category_archive()
LD . 'category_url_title' . RD,
LD . 'category_image' . RD,
LD . 'category_description' . RD,
LD . 'parent_id' . RD
LD . 'parent_id' . RD,
LD . 'category_group_id' . RD,
LD . 'category_group_name' . RD
),
array(
$cat_vars['category_id'],
ee()->functions->encode_ee_tags($cat_vars['category_name']),
$cat_vars['category_url_title'],
$cat_vars['category_image'],
ee()->functions->encode_ee_tags($cat_vars['category_description']),
$cat_vars['parent_id']
$cat_vars['parent_id'],
$cat_vars['category_group_id'],
ee()->functions->encode_ee_tags($cat_vars['category_group_name'])
),
$chunk
);
Expand Down Expand Up @@ -3668,7 +3680,7 @@ public function category_tree($cdata = array())

$this->cat_full_array = array_unique($this->cat_full_array);

$sql = "SELECT c.cat_id, c.parent_id, c.cat_name, c.cat_url_title, c.cat_image, c.cat_description {$field_sqla}
$sql = "SELECT c.cat_id, c.parent_id, c.cat_name, c.cat_url_title, c.cat_image, c.cat_description, c.group_id {$field_sqla}
FROM exp_categories AS c
{$field_sqlb}
WHERE c.cat_id IN (";
Expand Down Expand Up @@ -3727,7 +3739,7 @@ public function category_tree($cdata = array())
continue;
}

$this->cat_array[$row['cat_id']] = array($row['parent_id'], $row['cat_name'], $row['cat_image'], $row['cat_description'], $row['cat_url_title']);
$this->cat_array[$row['cat_id']] = array($row['parent_id'], $row['cat_name'], $row['cat_image'], $row['cat_description'], $row['cat_url_title'], $row['group_id'], $row['group_name'] ?? '');

foreach ($row as $key => $val) {
if (strpos($key, 'field') !== false) {
Expand Down Expand Up @@ -3814,6 +3826,8 @@ public function category_subtree($cdata = array(), $active_cat = null)
'category_image' => (string) $val[2],
'category_id' => $key,
'parent_id' => $val[0],
'category_group_id' => $val[5],
'category_group_name' => $val[6],
'has_children' => in_array($key, $parent_ids),
'active' => ($active_cat == $key || $active_cat == $val[4])
);
Expand All @@ -3835,15 +3849,19 @@ public function category_subtree($cdata = array(), $active_cat = null)
LD . 'category_url_title' . RD,
LD . 'category_image' . RD,
LD . 'category_description' . RD,
LD . 'parent_id' . RD
LD . 'parent_id' . RD,
LD . 'category_group_id' . RD,
LD . 'category_group_name' . RD
),
array(
$cat_vars['category_id'],
ee()->functions->encode_ee_tags($cat_vars['category_name']),
$cat_vars['category_url_title'],
$cat_vars['category_image'],
ee()->functions->encode_ee_tags($cat_vars['category_description']),
$cat_vars['parent_id']
$cat_vars['parent_id'],
$cat_vars['category_group_id'],
ee()->functions->encode_ee_tags($cat_vars['category_group_name'])
),
$chunk
);
Expand Down Expand Up @@ -4283,11 +4301,11 @@ public function category_heading()

list($field_sqla, $field_sqlb) = $this->generateCategoryFieldSQL($gquery->row('group_id'));
} else {
$field_sqla = '';
$field_sqlb = '';
// no category fields, but we still might need the category group name
list($field_sqla, $field_sqlb) = $this->generateCategoryFieldSQL();
}

$query = ee()->db->query("SELECT c.cat_name, c.parent_id, c.cat_url_title, c.cat_description, c.cat_image {$field_sqla}
$query = ee()->db->query("SELECT c.cat_name, c.parent_id, c.cat_url_title, c.cat_description, c.cat_image, c.group_id {$field_sqla}
FROM exp_categories AS c
{$field_sqlb}
WHERE c.cat_id = '" . ee()->db->escape_str($cat_id) . "'");
Expand All @@ -4306,7 +4324,9 @@ public function category_heading()
'category_description' => $query->row('cat_description'),
'category_image' => (string) $query->row('cat_image'),
'category_id' => $cat_id,
'parent_id' => $query->row('parent_id')
'parent_id' => $query->row('parent_id'),
'category_group_id' => $query->row('group_id'),
'category_group_name' => $query->row('group_name') ?? ''
);

// add custom fields for conditionals prep
Expand All @@ -4325,15 +4345,19 @@ public function category_heading()
LD . 'category_url_title' . RD,
LD . 'category_image' . RD,
LD . 'category_description' . RD,
LD . 'parent_id' . RD
LD . 'parent_id' . RD,
LD . 'category_group_id' . RD,
LD . 'category_group_name' . RD
),
array(
$cat_vars['category_id'],
ee()->functions->encode_ee_tags($cat_vars['category_name']),
$cat_vars['category_url_title'],
$cat_vars['category_image'],
ee()->functions->encode_ee_tags($cat_vars['category_description']),
$cat_vars['parent_id']
$cat_vars['parent_id'],
$cat_vars['category_group_id'],
ee()->functions->encode_ee_tags($cat_vars['category_group_name'])
),
ee()->TMPL->tagdata
);
Expand Down Expand Up @@ -5210,6 +5234,13 @@ public function combo_loader()
private function generateCategoryFieldSQL($group_ids = '')
{
if ($this->enable['category_fields'] !== true) {
// make sure we get category group name if it's not already in the query
if (isset(ee()->TMPL->var_single['category_group_name'])) {
$field_sqla = ", cg.group_name ";
$field_sqlb = " LEFT JOIN exp_category_groups AS cg ON cg.group_id = c.group_id ";
return array($field_sqla, $field_sqlb);
}

return array('', '');
}

Expand All @@ -5232,7 +5263,7 @@ private function generateCategoryFieldSQL($group_ids = '')

$this->cacheCategoryFieldModels();

$field_sqla = ", cg.field_html_formatting, fd.* ";
$field_sqla = ", cg.group_name, cg.field_html_formatting, fd.* ";
$field_sqlb = " LEFT JOIN exp_category_field_data AS fd ON fd.cat_id = c.cat_id
LEFT JOIN exp_category_groups AS cg ON cg.group_id = c.group_id ";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ public function replace($tagdata, EE_Channel_data_parser $obj, $cat_chunk)
// $v[4] = cat_description
// $v[5] = group_id
// $v[6] = cat_url_title
// $v[7] = group_id
// $v[8] = group_name

ee()->load->library('file_field');
$cat_image = ee()->file_field->parse_field($v[3]);
Expand All @@ -179,6 +181,8 @@ public function replace($tagdata, EE_Channel_data_parser $obj, $cat_chunk)
'category_group' => (isset($v[5])) ? $v[5] : '',
'category_image' => (isset($v[3])) ? (string) $v[3] : '',
'category_id' => $v[0],
'category_group_id' => $v[7],
'category_group_name' => $v[8],
'parent_id' => $v[1],
'active' => ($active_cat == $v[0] || $active_cat == $v[6])
);
Expand Down
6 changes: 6 additions & 0 deletions tests/cypress/cypress/integration/channel/categories.ee6.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,9 @@ context('Categories', () => {
cy.get('#category-one .category_description').invoke('text').then((text) => {
expect(text).equal('one description')
})
cy.get('#category-one .category_group_name').invoke('text').then((text) => {
expect(text).equal('News Categories')
})
cy.get('#category-one .custom_textfield').invoke('text').then((text) => {
expect(text).equal('one textfield')
})
Expand Down Expand Up @@ -577,6 +580,9 @@ context('Categories', () => {
cy.get('#category-two .category_description').invoke('text').then((text) => {
expect(text).equal('two description')
})
cy.get('#category-two .category_group_name').invoke('text').then((text) => {
expect(text).equal('News Categories')
})
cy.get('#category-two .custom_textfield').invoke('text').then((text) => {
expect(text).equal('two textfield')
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<div class="category_name">{category_name}</div>
<div class="category_url_title">{category_url_title}</div>
<div class="category_description">{category_description}</div>
<div class="category_group_name">{category_group_name}</div>
<div class="custom_textfield">{custom_textfield}</div>
<div class="custom_textarea">{custom_textarea}</div>
<div class="custom_dropdown">{custom_dropdown}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ <h3>default-linear</h3>
<div class="category_name">{category_name}</div>
<div class="category_url_title">{category_url_title}</div>
<div class="category_description">{category_description}</div>
<div class="category_group_name">{category_group_name}</div>
<div class="custom_textfield">{custom_textfield}</div>
<div class="custom_textarea">{custom_textarea}</div>
<div class="custom_dropdown">{custom_dropdown}</div>
Expand All @@ -28,6 +29,7 @@ <h3>default-nested</h3>
<div class="category_name">{category_name}</div>
<div class="category_url_title">{category_url_title}</div>
<div class="category_description">{category_description}</div>
<div class="category_group_name">{category_group_name}</div>
<div class="custom_textfield">{custom_textfield}</div>
<div class="custom_textarea">{custom_textarea}</div>
<div class="custom_dropdown">{custom_dropdown}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<div class="category_name" style="font-weight: bold;">{category_name}</div>
<div class="category_url_title">{category_url_title}</div>
<div class="category_description">{category_description}</div>
<div class="category_group_name">{category_group_name}</div>
<div class="custom_textfield">{custom_textfield}</div>
<div class="custom_textarea">{custom_textarea}</div>
<div class="custom_dropdown">{custom_dropdown}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<div class="category_name">{category_name}</div>
<div class="category_url_title">{category_url_title}</div>
<div class="category_description">{category_description}</div>
<div class="category_group_name">{category_group_name}</div>
<div class="custom_textfield">{custom_textfield}</div>
<div class="custom_textarea">{custom_textarea}</div>
<div class="custom_dropdown">{custom_dropdown}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<div class="category_name">{category_name}</div>
<div class="category_url_title">{category_url_title}</div>
<div class="category_description">{category_description}</div>
<div class="category_group_name">{category_group_name}</div>
<div class="custom_textfield">{custom_textfield}</div>
<div class="custom_textarea">{custom_textarea}</div>
<div class="custom_dropdown">{custom_dropdown}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<div class="category_name">{category_name}</div>
<div class="category_url_title">{category_url_title}</div>
<div class="category_description">{category_description}</div>
<div class="category_group_name">{category_group_name}</div>
<div class="custom_textfield">{custom_textfield}</div>
<div class="custom_textarea">{custom_textarea}</div>
<div class="custom_dropdown">{custom_dropdown}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<div class="category_name">{category_name}</div>
<div class="category_url_title">{category_url_title}</div>
<div class="category_description">{category_description}</div>
<div class="category_group_name">{category_group_name}</div>
<div class="custom_textfield">{custom_textfield}</div>
<div class="custom_textarea">{custom_textarea}</div>
<div class="custom_dropdown">{custom_dropdown}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<div class="category_name">{category_name}</div>
<div class="category_url_title">{category_url_title}</div>
<div class="category_description">{category_description}</div>
<div class="category_group_name">{category_group_name}</div>
<div class="custom_textfield">{custom_textfield}</div>
<div class="custom_textarea">{custom_textarea}</div>
<div class="custom_dropdown">{custom_dropdown}</div>
Expand Down
Loading