Skip to content

Commit

Permalink
Better handling of nested categories
Browse files Browse the repository at this point in the history
Uses the data returned by api_channel_categories->category_tree to
determine nesting (why did I not see this before!)
  • Loading branch information
Nathan Pitman committed Jul 30, 2014
1 parent 484c7f8 commit 554a351
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion system/third_party/nf_categories_field/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
if ( ! defined('NF_CF_NAME'))
{
define('NF_CF_NAME', 'Categories Field');
define('NF_CF_VERSION', '1.0.1');
define('NF_CF_VERSION', '1.0.2');
define('NF_CF_DOCS', 'http://github.com/ninefour/categories_field.ft.ee_addon');
}

Expand Down
25 changes: 13 additions & 12 deletions system/third_party/nf_categories_field/ft.nf_categories_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,24 @@ private function _display_field($data, $cell = FALSE)
ee()->api->instantiate('channel_categories');

foreach($this->settings['groups'] AS $group_id) {

// Get data for this group
$this->EE->db->select('group_id, site_id, group_name, sort_order, can_edit_categories');
$this->EE->db->where('group_id',$group_id);
$groups[$group_id] = $this->EE->db->get('category_groups')->row();

// Append categories data
$groups[$group_id]->categories = ee()->api_channel_categories->category_tree($group_id,$data,'c');

/* Returns:
'0' => (int) Category ID,
'1' => (string) Category Name,
'2' => (int) Category Group ID,
'3' => (string) Category Group Name,
'4' => (bool) Selected,
'5' => (int) Depth Nested in the Tree,
'6' => (int) Category Parent ID
*/
}

if (ee()->input->get('entry_id')) {
Expand All @@ -287,7 +299,6 @@ private function _display_field($data, $cell = FALSE)

foreach($groups AS $group) {

$level = 1;
$current_parent_id = 0;

if ($this->settings['category_group_names']) {
Expand All @@ -303,16 +314,6 @@ private function _display_field($data, $cell = FALSE)
$selected_primary = NULL;
$selected_primary_label = "";

// Indentation/Level
if ($row[6] == 0) {
// Parent
$level = 1;
} if (($row[6] > 0) AND ($row[6]>$current_parent_id)) {
// Children
$current_parent_id = $row[6];
$level++;
}

if (is_array($data)) {
// If validation on the publish form fires the returned data is an array
$selected = in_array($row[0], $data) ? 1 : 0;
Expand Down Expand Up @@ -360,7 +361,7 @@ private function _display_field($data, $cell = FALSE)
$selected_primary_input = form_radio($field_name.'[]', 'p'.$row[0], $selected_primary);
}

$out .= '<label class="level_' . $level . ' ' . $class . '">'
$out .= '<label class="level_' . $row[5] . ' ' . $class . '">'
. form_checkbox($field_name.'[]', $row[0], $selected)
. NBS .'<span>'. $row[1] . $selected_primary_label . '</span>' . $selected_primary_input . '</span></label>';

Expand Down

0 comments on commit 554a351

Please sign in to comment.