Skip to content

Instantly share code, notes, and snippets.

@St0iK
Created September 12, 2015 22:48
Show Gist options
  • Save St0iK/98f240b138fdbb287515 to your computer and use it in GitHub Desktop.
Save St0iK/98f240b138fdbb287515 to your computer and use it in GitHub Desktop.
<?php
/**
* Implementation of hook_views_post_execute()
*
* @param view $view
*/
function vw_product_listing_sort_views_post_execute(&$view) {
// Check that we are on the correct View
if ($view->name == "shop" && $view->current_display == 'page') {
// Check if we have 'searchPath' specified in our request
// If it exists it means we have an ajax call from facet filters
if (isset($_GET['searchPath'])) {
// Chech ig we have enabled facets
if (isset($_GET['enabled_facets'])) {
watchdog('vw_product_listing_sort', "Facets are enabled. We have an ajax call.");
foreach ($_GET['enabled_facets'] as $key => $value) {
$_GET['enabled_facets'][$key] = str_replace('/', '', str_replace('-', ' ', $value));
}
$enabled_facets = $_GET['enabled_facets'];
if ($enabled_facets) {
watchdog("vw_product_listing_sort", '<pre>' . print_r($enabled_facets, TRUE) . '</pre>');
$term_name = _find_term_name_used($enabled_facets);
$gender_name = _find_gender_name_used($enabled_facets);
$total_term_dempth = _find_total_term_dempth($enabled_facets);
if ($total_term_dempth > 1) {
$term_name = NULL;
}
watchdog("vw_product_listing_sort", 'Term name is: <pre>' . print_r($term_name, TRUE) . '</pre>');
watchdog("vw_product_listing_sort", 'Gender name is: <pre>' . print_r($gender_name, TRUE) . '</pre>');
watchdog("vw_product_listing_sort", 'Total term depth is: <pre>' . print_r($total_term_dempth, TRUE) . '</pre>');
}
}
}
else {
// We have a normal page request (not ajax facet filtered)
// ex. https://www.viviennewestwood.com/shop/mens/accessories/shoes
$arguments = arg();
// Find the gender within the arguments
$gender_name = _find_gender_name_used($arguments);
// get the term name that will be used for sorting
$term_name = _find_term_with_max_depth($arguments);
if ($term_name) {
$term_uuid = _get_term_uuid($term_name);
watchdog("vw_product_listing_sort", 'term_uuid: <pre>' . print_r($term_uuid, TRUE) . '</pre>');
}
watchdog("vw_product_listing_sort", 'Term name is: <pre>' . print_r($term_name, TRUE) . '</pre>');
watchdog("vw_product_listing_sort", 'Gender name is: <pre>' . print_r($gender_name, TRUE) . '</pre>');
}
if ($term_uuid) {
watchdog("vw_product_listing_sort", 'UUID was found...');
$current_page = isset($view->query->pager->current_page) ? $view->query->pager->current_page : 0;
$from = $current_page * 18;
$to = $current_page + 18;
// Get all products sorted for this category UUID
$results = db_select('product_listing_weights', 'p')->fields('p', array('entity_id', 'weight'));
$results->join('node', 'nnd', 'p.entity_id = nnd.nid');
$results->condition('status', 1);
$results->condition('category_uuid', $term_uuid);
$results->condition('gender', $gender_name)->orderBy('weight')->range($from, $to);
$results = $results->execute()->fetchAll();
watchdog("vw_product_listing_sort", 'Results from db:<pre>' . print_r($results, TRUE) . '</pre>');
// If this category has been sorted in the admin area
if ($results) {
watchdog("vw_product_listing_sort", 'Before: <pre>' . print_r($view->result, TRUE) . '</pre>');
// Attach that weight to the results of the view
foreach ($view->result as $key => $result) {
$result->entity = $results[$key]->entity_id;
if (is_null($results[$key]->weight)) {
// We set weight to a large number for results that are missing this value
// Could be new products, or products that have been recently published
$result->weight = 9999;
}
else {
$result->weight = $results[$key]->weight;
}
}
watchdog("vw_product_listing_sort", 'After: <pre>' . print_r($view->result, TRUE) . '</pre>');
}
}
}
}
/**
* Returns the Term UUID based on a term name
*
* @param string $term_name
*/
function _get_term_uuid($term_name) {
if (!empty($term_name)) {
$term = taxonomy_get_term_by_name(str_replace('-', ' ', $term_name),'category');
if(!$term){
$term = taxonomy_get_term_by_name(str_replace('-', ' ', $term_name),'label');
}
if (!empty($term)) {
$term_array = reset($term);
return $term_array->uuid;
}
}
return FALSE;
}
/**
* Implements hook_menu().
*/
function vw_product_listing_sort_menu() {
$items['admin/commerce/product_sorting/%'] = array(
'title' => 'Sort category products',
'description' => 'Admin ui to sort products by on each category',
'page callback' => 'drupal_get_form',
'page arguments' => array('vw_product_listing_sort_fields_form', 3),
'access arguments' => array('administer search_api'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Form: Form Table for sorting products within the category
*/
function vw_product_listing_sort_fields_form($form, &$form_state, $term_id) {
drupal_add_css(drupal_get_path('module', 'vw_product_listing_sort') . '/css/custom.css', array('group' => CSS_DEFAULT, 'every_page' => TRUE));
drupal_add_js(drupal_get_path('module', 'vw_product_listing_sort') . '/js/custom.js');
// Get session stored values
$session_active = $_SESSION['vw_product_listing_sort']['active'];
$session_published = $_SESSION['vw_product_listing_sort']['published'];
$session_gender = $_SESSION['vw_product_listing_sort']['gender'];
// Set default values
$default_active = isset($_SESSION['vw_product_listing_sort']['active']) ? $_SESSION['vw_product_listing_sort']['active'] : 'all';
$default_published = isset($_SESSION['vw_product_listing_sort']['published']) ? $_SESSION['vw_product_listing_sort']['published'] : 'all';
$default_gender = isset($_SESSION['vw_product_listing_sort']['gender']) ? $_SESSION['vw_product_listing_sort']['gender'] : 'all';
$form_state['storage']['active'] = $default_active;
$form_state['storage']['published'] = $default_published;
$form_state['storage']['gender'] = $default_gender;
$default_gender = isset($form_state['storage']['gender']) ? $form_state['storage']['gender'] : $default_gender;
$gender_vocab = taxonomy_vocabulary_machine_name_load("gender");
$gender_taxonomy_terms = taxonomy_get_tree($gender_vocab->vid);
$gender_taxonomy_options = array();
$gender_taxonomy_options['all'] = "all";
foreach ($gender_taxonomy_terms as $term) {
$gender_taxonomy_options[$term->tid] = $term->name;
}
// Get weights for products on this category
$results = db_select('product_listing_weights', 'p')->fields('p')->condition('category_uuid', $term_info->uuid)->condition('gender', $gender_taxonomy_options[$default_gender])->orderBy('weight')->execute()->fetchAll();
// Get Term info
$term_info = taxonomy_term_load($term_id);
// Get the nodes for the taxonomy term
$taxonomy_select_nodes = _get_selected_nodes($term_id, $term_info->vocabulary_machine_name);
// Grab previous sorting value
// and build node_weight_info using that
if (empty($results)) {
$results = array();
foreach ($taxonomy_select_nodes as $node_id) {
$result = new stdClass();
$node = node_load($node_id);
$result->weight = $node->field_product_sort_index["und"][0]["value"];
$result->entity_id = $node_id;
$result->id = NULL;
$results[] = $result;
}
}
// We built an array that contains the weights foreach node
$node_weight_info = array();
foreach ($results as $key => $result) {
$node_weight_info[$result->entity_id]['weight'] = $result->weight;
$node_weight_info[$result->entity_id]['id'] = $result->id;
}
// Load their details
$node_info = node_load_multiple($taxonomy_select_nodes);
$taxonomy_select_nodes_to_sort = array();
foreach ($taxonomy_select_nodes as $key => $node) {
$taxonomy_select_nodes_to_sort[$key]['entity_id'] = $node;
$taxonomy_select_nodes_to_sort[$key]['weight'] = $node_weight_info[$node]['weight'];
}
// Sort array based on weight key
usort($taxonomy_select_nodes_to_sort,
function ($a, $b) {
return $a['weight'] - $b['weight'];
}
);
$taxonomy_select_nodes_sorted = array();
foreach ($taxonomy_select_nodes_to_sort as $key => $value) {
$taxonomy_select_nodes_sorted[] = $value['entity_id'];
}
// create a parent element and use our custom theme
$form['fields'] = array(
'#prefix' => '<div id="vw_sort_fields">',
'#suffix' => '</div>',
'#tree' => TRUE,
'#theme' => 'vw_product_listing_sort_theme_name',
);
$form['find'] = array(
'#weight' => -13,
'#type' => 'submit',
'#value' => t("Filter"),
'#submit' => array('custom_submit_for_find_button'),
);
$form['reset'] = array(
'#weight' => -13,
'#type' => 'submit',
'#value' => t("Reset"),
'#submit' => array('custom_submit_for_reset_button'),
);
$form["published"] = array(
'#weight' => -14,
"#type" => "select",
"#title" => t("Published"),
'#default_value' => isset($form_state['storage']['published']) ? $form_state['storage']['published'] : $default_published,
"#options" => array(
'all' => "- Any -",
"1" => t("Yes"),
"0" => t("No"),
),
);
$form["active"] = array(
'#weight' => -14,
"#type" => "select",
"#title" => t("Active"),
'#default_value' => isset($form_state['storage']['active']) ? $form_state['storage']['active'] : $default_active,
"#options" => array(
'all' => "- Any -",
"1" => t("Yes"),
"0" => t("No"),
),
);
$form["gender"] = array(
'#weight' => -14,
"#type" => "select",
"#title" => t("Gender"),
'#default_value' => isset($form_state['storage']['gender']) ? $form_state['storage']['gender'] : $default_gender,
"#options" => $gender_taxonomy_options,
"#access" => $term_info->vocabulary_machine_name == 'category' ? TRUE : FALSE, // we hide Gender for other vocabs
);
$form['info_text'] = array(
'#weight' => -12,
'#markup' => '<h1>' . $term_info->name . '</h1>',
);
if (empty($results)) {
$form['status_text'] = array(
'#weight' => -11,
'#markup' => '<h1>Status</h1><p style="color:red">Products for this category havent been sorted yet</p>',
);
}
// To make the fieldset collapsible
$form['bulk_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Bulk Operations'),
// Added
'#collapsible' => TRUE,
// Added
'#collapsed' => TRUE,
);
$form['bulk_fieldset']["bulk_operations"] = array(
'#weight' => 10,
"#type" => "select",
"#title" => t("Action"),
'#default_value' => isset($form_state['storage']['bulk_operations']) ? $form_state['storage']['bulk_operations'] : 'all',
"#options" => array(
'all' => "- Select -",
"unassign_from_category" => t("Remove product from category"),
),
);
$form['bulk_fieldset']['term_id'] = array(
'#type' => 'hidden',
'#default_value' => $term_id,
'#title' => $term_id,
);
$form['bulk_fieldset']['vocabulary_machine_name'] = array(
'#type' => 'hidden',
'#default_value' => $term_info->vocabulary_machine_name,
'#title' => $term_info->vocabulary_machine_name,
);
$form['bulk_fieldset']['apply'] = array(
'#type' => 'submit',
'#weight' => 99,
'#value' => t("Apply"),
'#submit' => array('custom_submit_for_apply_button'),
);
// create the form elements for each item
foreach ($taxonomy_select_nodes_sorted as $key => $item) {
// We $node_info[$item]->field_product_gender; care about "Product" content tye;
// Term might be also applied to multiple things
// continue to next iteration if content type is not a product
if ($node_info[$item]->type != "product") {
continue;
}
// Get product variations
// If all of them ar disabled then the product status is Disabled
// and it is not saleable
$product_is_active = _get_product_status($node_info[$item]->field_product['und']);
// Published filter
if (isset($form_state['storage']['published']) && $form_state['storage']['published'] != "all") {
if ($node_info[$item]->status != $form_state['storage']['published']) {
continue;
}
}
// Active filter
if (isset($form_state['storage']['active']) && $form_state['storage']['active'] != "all") {
if ($product_is_active != $form_state['storage']['active']) {
continue;
}
}
// Gender filter
if (isset($form_state['storage']['gender']) && $form_state['storage']['gender'] != "all") {
$gender_tids = array();
foreach ($node_info[$item]->field_product_gender['und'] as $gender_tid) {
$gender_tids[] = $gender_tid;
}
$gender_tids = multitosingle($gender_tids);
if (!in_array($form_state['storage']['gender'], $gender_tids)) {
continue;
}
}
$form['fields'][$key]['name'] = array(
'#type' => 'item',
'#title' => "<a href='/" . drupal_get_path_alias("node/" . $item) . "'>" . $node_info[$item]->title . "</a>". "-- ".$item,
);
$form['fields'][$key]['published'] = array(
'#type' => 'item',
'#title' => $node_info[$item]->status == 1 ? 'Yes' : 'No',
);
$form['fields'][$key]['active'] = array(
'#type' => 'item',
'#title' => $product_is_active == 1 ? 'Yes' : 'No',
);
$form['fields'][$key]['checkbox'] = array(
'#type' => 'checkbox',
);
$form['fields'][$key]['entity_id'] = array(
'#type' => 'textfield',
'#title' => $item,
);
$form['fields'][$key]['entity_id'] = array(
'#type' => 'hidden',
'#default_value' => $item,
'#title' => $item,
);
$form['fields'][$key]['gender'] = array(
'#type' => 'hidden',
'#default_value' => isset($form_state['storage']['gender']) ? $form_state['storage']['gender'] : $default_gender,
'#title' => $item,
);
$form['fields'][$key]['category_uuid'] = array(
'#type' => 'hidden',
'#default_value' => $term_info->uuid,
'#title' => $term_info->uuid,
);
$form['fields'][$key]['id'] = array(
'#type' => 'hidden',
'#default_value' => $node_weight_info[$item]['id'],
'#title' => $node_weight_info[$item]['id'],
);
$form['fields'][$key]['weight'] = array(
'#type' => 'textfield',
'#default_value' => $node_weight_info[$item]['weight'],
'#size' => 3,
// needed for table dragging
'#attributes' => array('class' => array('rank-weight')),
);
}
$form['submit'] = array(
'#weight' => 999,
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* Implements hook_theme().
*/
function vw_product_listing_sort_theme($existing, $type, $theme, $path) {
return array(
'vw_product_listing_sort_theme_name' => array(
'render element' => 'element',
),
);
}
function theme_vw_product_listing_sort_theme_name($vars) {
$element = $vars['element'];
// needed for table dragging
drupal_add_tabledrag('form_id', 'order', 'sibling', 'rank-weight');
$header = array(
'name' => t('Product title'),
'published' => t('Published'),
'active' => t('Active'),
'checkbox' => t('Remove from category'),
'weight' => t('Rank'),
);
$rows = array();
foreach (element_children($element) as $key) {
$row = array();
$row['data'] = array();
foreach ($header as $fieldname => $title) {
$row['data'][] = drupal_render($element[$key][$fieldname]);
// needed for table dragging
$row['class'] = array('draggable');
}
$rows[] = $row;
}
return theme('table', array(
'header' => $header,
'rows' => $rows,
// needed for table dragging
'attributes' => array('id' => 'form_id'),
));
}
/**
* Handles form submission
*/
function vw_product_listing_sort_fields_form_submit($form, &$form_state) {
$fields = array();
$gender_vocab = taxonomy_vocabulary_machine_name_load("gender");
$gender_taxonomy_terms = taxonomy_get_tree($gender_vocab->vid);
$gender_taxonomy_options = array();
$gender_taxonomy_options['all'] = "all";
foreach ($gender_taxonomy_terms as $term) {
$gender_taxonomy_options[$term->tid] = strtolower($term->name);
}
foreach ($form_state['values']['fields'] as $item) {
var_dump($gender_taxonomy_options[$item['gender']]);
$items[] = array(
'id' => $item['id'],
'category_uuid' => $item['category_uuid'],
'gender' => $gender_taxonomy_options[$item['gender']],
'entity_id' => $item['entity_id'],
'weight' => $item['weight'],
);
}
// Sort entities by weight
if (!empty($items)) {
usort($items, '_creode_arraysort');
}
/*================================================
= Delete existing entries =
================================================*/
$delete = db_delete('product_listing_weights')->condition('gender', $item['gender'])->condition('category_uuid', $item['category_uuid'])->execute();
/*===== End of Delete existing entries ======*/
foreach ($items as $item) {
$data = array(
'category_uuid' => $item['category_uuid'],
'gender' => $item['gender'],
'entity_id' => $item['entity_id'],
'weight' => $item['weight'],
);
// Check if 'id' is empty
// to determine if you are going to update or insert a record
if (empty($item['id'])) {
drupal_write_record("product_listing_weights", $data);
}
else {
$data['id'] = $item['id'];
drupal_write_record("product_listing_weights", $data, 'id');
}
}
$form_state['storage']['published'] = $form_state['values']['published'];
$form_state['storage']['active'] = $form_state['values']['active'];
$form_state['storage']['gender'] = $form_state['values']['gender'];
$form_state['rebuild'] = TRUE;
drupal_set_message(t('Ordering have been saved.'));
$_SESSION['vw_product_listing_sort']['active'] = $form_state['storage']['published'];
$_SESSION['vw_product_listing_sort']['published'] = $form_state['storage']['active'];
$_SESSION['vw_product_listing_sort']['gender'] = $form_state['storage']['gender'];
drupal_goto(current_path());
}
/**
* Sort an Array based on weight key value
*/
function _creode_arraysort($a, $b) {
if (isset($a['weight']) && isset($b['weight'])) {
return $a['weight'] < $b['weight'] ? -1 : 1;
}
return 0;
}
/**
* Sort an Object based on weight property
*/
function _creode_objectsort($a, $b) {
if ($a->weight == $b->weight) {
return 0;
}
return ($a->weight < $b->weight) ? -1 : 1;
}
/**
* function to get the selected nodes based on term id,ids
* Returns Flat array of node ids belong to the given term ids.
*
* @param
* $tid takes term id can be a single term or array of terms.
*
* @return
* $nid/$nids returns node ids array or sinly node id directly mapped to the given taxonomy term.
*/
function _get_selected_nodes($term_id, $term_vocab_machine_name) {
$taxonomy_get_children = taxonomy_get_children($term_id);
if (empty($taxonomy_get_children)) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')->entityCondition('bundle', 'product')->fieldCondition('field_product_' . $term_vocab_machine_name, 'tid', $term_id);
$result = $query->execute();
return array_keys($result['node']);
}
return get_nodes_directly_mapped_to_term($taxonomy_get_children, $term_id, $term_vocab_machine_name);
}
/**
* function to get all the node ids directly mapped to the given taxonomy terms.
* Returns Flat array of node ids belong to the given term ids.
*
* @param
* $tid takes term id can be a single term or array of terms.
*
* @return
* $nid/$nids returns node ids array or sinly node id directly mapped to the given taxonomy term.
*/
function get_nodes_directly_mapped_to_term($tid, $original_term_id = NULL, $vocabulary_machine_name) {
$nids = array();
if (is_array($tid) && !empty($tid)) {
$term_info = taxonomy_term_load($original_term_id);
$tid[] = $term_info;
foreach ($tid as $key => $value) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')->entityCondition('bundle', 'product')->fieldCondition('field_product_' . $vocabulary_machine_name, 'tid', $value->tid);
$result = $query->execute();
$nids[] = array_keys($result['node']);
}
$flat_nids = multitosingle($nids);
return array_unique($flat_nids);
}
else {
$nid = taxonomy_select_nodes($tid, FALSE);
return $nid;
}
}
/**
* function to convert mutlidimension arrays to flat arrays.
* Returns Flat arrays.
*
* @param
* $input takes input array as multudimension array.
* $output takes output array as paramter in the recursive call.
*
* @return
* $output return flat array format for the input multidimenional array
*/
function multitosingle($input, $output = array()) {
ksort($input);
foreach ($input as $value) {
$key = count($output);
if (is_array($value)) {
$output = multitosingle($value, $output);
}
else {
$output[$key] = $value;
}
}
return $output;
}
/**
* [_get_product_status description]
*
* @param [type] $field_products [description]
*
* @return [type] [description]
*/
function _get_product_status($field_products) {
$product_is_active = TRUE;
if (!empty($field_products)) {
foreach ($field_products as $product_id) {
$commerce_product = commerce_product_load($product_id);
if (!$commerce_product->status) {
$product_is_active = FALSE;
}
else {
$product_is_active = TRUE;
}
}
}
return $product_is_active;
}
/**
* [custom_submit_for_find_button description]
*
* @param [type] $form [description]
* @param [type] &$form_state [description]
*
* @return [type] [description]
*/
function custom_submit_for_find_button($form, &$form_state) {
$form_state['storage']['published'] = $form_state['values']['published'];
$form_state['storage']['active'] = $form_state['values']['active'];
$form_state['storage']['gender'] = $form_state['values']['gender'];
$_SESSION['vw_product_listing_sort']['active'] = $form_state['storage']['published'];
$_SESSION['vw_product_listing_sort']['published'] = $form_state['storage']['active'];
$_SESSION['vw_product_listing_sort']['gender'] = $form_state['storage']['gender'];
$form_state['rebuild'] = TRUE;
drupal_goto(current_path());
}
/**
* [custom_submit_for_apply_button description]
*
* @param [type] $form [description]
* @param [type] &$form_state [description]
*
* @return [type] [description]
*/
function custom_submit_for_apply_button($form, &$form_state) {
$term_id = $form_state['values']['term_id'];
$vocabulary_machine_name = $form_state['values']['vocabulary_machine_name'];
if ($term_id && $form_state['values']['bulk_operations'] == 'unassign_from_category') {
foreach ($form_state['values']['fields'] as $item) {
if ($item['checkbox'] == 1) {
// Get the node
$node = node_load($item['entity_id']);
// Get the Category field
$field_name = 'field_product_' . $vocabulary_machine_name;
$field = &$node->$field_name[LANGUAGE_NONE];
foreach ($field as $key => $term) {
if ($term['tid'] == $term_id) {
unset($field[$key]);
node_save($node);
}
}
}
// end of check for checkbox value
}
// end foreach item
}
// end of check for term_id and bulk_operations
$form_state['storage']['published'] = $form_state['values']['published'];
$form_state['storage']['active'] = $form_state['values']['active'];
$form_state['storage']['gender'] = $form_state['values']['gender'];
$_SESSION['vw_product_listing_sort']['active'] = $form_state['storage']['published'];
$_SESSION['vw_product_listing_sort']['published'] = $form_state['storage']['active'];
$_SESSION['vw_product_listing_sort']['gender'] = $form_state['storage']['gender'];
drupal_set_message(t('Product(s) has been removed from the category successfully'), "status");
$form_state['rebuild'] = TRUE;
drupal_goto(current_path());
}
/**
* [custom_submit_for_reset_button description]
*
* @param [type] $form [description]
* @param [type] &$form_state [description]
*
* @return [type] [description]
*/
function custom_submit_for_reset_button($form, &$form_state) {
unset($form_state['storage']['published']);
unset($form_state['storage']['active']);
unset($form_state['storage']['gender']);
$form_state['rebuild'] = TRUE;
}
function _find_term_name_used($enabled_facets) {
$terms_found = "";
foreach ($enabled_facets as $enabled_facet) {
$term = taxonomy_get_term_by_name(str_replace('-', ' ', $enabled_facet),'category');
if(!$term){
$term = taxonomy_get_term_by_name(str_replace('-', ' ', $enabled_facet),'label');
}
if ($term) {
$term_reseted = reset($term);
$terms_found = $term_reseted->name;
}
}
return strtolower($terms_found);
}
function _find_gender_name_used($enabled_facets) {
$counted = 0;
// Check how many genders we have on the url
foreach ($enabled_facets as $enabled_facet) {
if ($enabled_facet == "mens" || $enabled_facet == "womens" || $enabled_facet == "unisex") {
$counted++;
}
}
// If we have only one
if ($counted == 1) {
foreach ($enabled_facets as $enabled_facet) {
if ($enabled_facet == "mens" || $enabled_facet == "womens" || $enabled_facet == "unisex") {
return $enabled_facet;
}
}
}
else {
return "all";
}
}
/**
* Returns the depth of a term based on how many parents it has
* @param [int] $tid term id
* @return [int] terms depth
*/
function term_depth($tid) {
return count(taxonomy_get_parents_all($tid));
}
/**
* [_find_total_term_dempth description]
* @param [type] $enabled_facets [description]
* @param [type] $vocabulary_machine_name [description]
* @return [type] [description]
*/
function _find_total_term_dempth($enabled_facets, $vocabulary_machine_name) {
$counted = 0;
foreach ($enabled_facets as $enabled_facet) {
$term = taxonomy_get_term_by_name(str_replace('-', ' ', $enabled_facet),'category');
if(!$term){
$term = taxonomy_get_term_by_name(str_replace('-', ' ', $enabled_facet),'label');
}
if ($term) {
$term_reseted = reset($term);
$terms_depth = term_depth($term_reseted->tid);
}
if ($terms_depth == 1) {
$counted++;
}
}
return $counted;
}
function _find_term_with_max_depth($arguments) {
$max_depth = 0;
$term_with_max_depth = "";
foreach ($arguments as $argument) {
$term = taxonomy_get_term_by_name(str_replace('-', ' ', $argument),'category');
if(!$term){
$term = taxonomy_get_term_by_name(str_replace('-', ' ', $argument),'label');
}
if ($term) {
$term_reseted = reset($term);
$terms_depth = term_depth($term_reseted->tid);
if ($terms_depth > $max) {
$max = $terms_depth;
$term_with_max_depth = $term_reseted->name;
}
}
}
return $term_with_max_depth;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment