Skip to content

Commit

Permalink
Primary category changes
Browse files Browse the repository at this point in the history
Adds support for primary_category_parent_id field and improves handling
of primary categories. Previously if a primary category was set then
the same ‘normal’ category was not also set. This fixes that from a UI
and code perspective.
  • Loading branch information
Nathan Pitman committed Aug 12, 2014
1 parent 7dcc4d4 commit cbab67c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ $(document).ready(function() {
}
});

// Ensure that primary categories are checked too
$('.nf_category_field input[type="radio"]').click(function(){
$(this).parent().find('input[type="checkbox"]').prop('checked', true);
});

});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 33 additions & 3 deletions system/third_party/nf_categories_field/ft.nf_categories_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,11 @@ public function save($data) {
$primary_cat = NULL;

// Find our primary cat (if one is set)
foreach($data AS $selected_cat) {
foreach($data AS $selected_cat_key=>$selected_cat_value) {
// Ooh, look here, that's our primary cat
if (substr( $selected_cat, 0, 1 ) === "p") {
$primary_cat = $selected_cat;
if (substr( $selected_cat_value, 0, 1 ) === "p") {
$primary_cat = $selected_cat_value;
$data[$selected_cat_key] = ltrim($selected_cat_value,'p');
}
}

Expand Down Expand Up @@ -597,6 +598,35 @@ function replace_primary_category_name($data, $params = array(), $tagdata = FALS
return $primary_cat_name;
}

// {field_name:primary_category_parent_id}
function replace_primary_category_parent_id($data, $params = array(), $tagdata = FALSE)
{

// Establish Settings
$settings = (isset($this->settings['nf_categories_field'])) ? $this->settings['nf_categories_field'] : $this->settings;
$settings = $this->_default_settings($settings);

$primary_cat_name = FALSE;
// array_filter removes empty nodes, array_values re-indexes
$categories = array_values(array_filter(explode($settings['delimiter'], $data)));

if (substr( $categories[0], 0, 1 ) === "p") {
$primary_cat_id = ltrim($categories[0],'p');
$primary_cat = $this->_get_category_data(array($primary_cat_id));

if ($primary_cat[0]) {
// If there's a parent return it's ID, else return this categories ID
if ($primary_cat[0]['category_parent_id']==0) {
return $primary_cat[0]['category_id'];
} else {
return $primary_cat[0]['category_parent_id'];
}
}
}

return;
}

/**
* Given a list of category IDs, returns
* @param Array $cat_ids Array of category IDs
Expand Down

0 comments on commit cbab67c

Please sign in to comment.