'Advanced catalog settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('uc_advanced_catalog_settings_form'),
'access arguments' => array('administer catalog'),
'weight' => -5,
'type' => MENU_LOCAL_TASK,
'file' => 'uc_advanced_catalog.admin.inc',
);
return $items;
}
/**
* Implementation of hook_theme_registry_alter().
*/
function uc_advanced_catalog_theme_registry_alter(&$theme_registry) {
// override uc_catalog_browse function
$theme_registry['uc_catalog_browse']['function'] = 'uc_advanced_catalog_browse';
}
/**
* Implementation of hook_form_alter().
*/
function uc_advanced_catalog_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'uc_catalog_grid_settings_form' && variable_get('uc_advanced_catalog', FALSE)) {
drupal_set_message(t('You cannot use grid display if advanced catalog is enabled.'), 'warning' );
$form['uc_catalog_grid_display']['#disabled'] = TRUE;
}
}
/**
* Implementation of hook_theme().
*/
function uc_advanced_catalog_theme() {
return array(
'uc_advanced_catalog_settings_list_form' => array(
'file' => 'uc_advanced_catalog.admin.inc',
'arguments' => array('form' => NULL),
),
'uc_advanced_catalog_pager_form' => array(
'arguments' => array('form' => NULL),
),
);
}
/**
* Display a formatted catalog page.
*
* This function is a copy of original theme_uc_catalog_browse() with filter and pager options
*
* If the category has products in it, display them in a view. Subcategories
* are linked along the top of the page. If it does not have products, display
* subcategories in a grid with their images and subcategories.
*
* @param $tid
* Catalog term id from URL.
* @return
* Formatted HTML of the catalog page using view.
*/
function uc_advanced_catalog_browse($tid = 0) {
// original code from theme_uc_catalog_browse() on uc_catalog.pages.inc
drupal_add_css(drupal_get_path('module', 'uc_catalog') .'/uc_catalog.css');
$output = '';
$catalog = uc_catalog_get_page((int)$tid);
drupal_set_title(check_plain($catalog->name));
drupal_set_breadcrumb(uc_catalog_set_breadcrumb($catalog->tid));
$types = uc_product_types();
$links = array();
$child_list = array();
foreach ($catalog->children as $child) {
if (!empty($child->nodes)) {
$links[] = array('title' => $child->name . (variable_get('uc_catalog_breadcrumb_nodecount', FALSE) ? ' ('. $child->nodes .')' : ''), 'href' => uc_catalog_path($child),
'attributes' => array('rel' => 'tag'),
);
}
if (!empty($child->image)) {
$image = '
';
if (module_exists('imagecache')) {
$image .= l(theme('imagecache', 'uc_category', $child->image['filepath'], $child->name, $child->name), uc_catalog_path($child), array('html' => TRUE));
}
else {
$image .= l(theme('image', $child->image['filepath'], $child->name, $child->name), uc_catalog_path($child), array('html' => TRUE));
}
$image .= '
';
}
else {
$image = '';
}
$grandchildren = array();
$j = 0;
$max_gc_display = 3;
foreach ($child->children as $i => $grandchild) {
if ($j > $max_gc_display) {
break;
}
$g_child_nodes = 0;
foreach ($types as $type) {
$g_child_nodes += taxonomy_term_count_nodes($grandchild->tid, $type);
}
if ($g_child_nodes) {
$grandchildren[$i] = l($grandchild->name, uc_catalog_path($grandchild), array('class' => 'subcategory'));
$j++;
}
}
//$grandchildren = array_slice($grandchildren, 0, intval(count($grandchildren) / 2) + 1, TRUE);
if ($j > $max_gc_display) {
array_push($grandchildren, l(t('More...'), uc_catalog_path($child), array('class' => 'subcategory')));
}
if ($child->nodes) {
$cell_link = $image .''. l($child->name, uc_catalog_path($child)) .'';
if (variable_get('uc_catalog_show_subcategories', TRUE)) {
$cell_link .= "
". implode(', ', $grandchildren) ."\n";
}
$child_list[] = $cell_link;
}
}
if (!empty($catalog->image)) {
if (module_exists('imagecache')) {
$output .= theme('imagecache', 'uc_thumbnail', $catalog->image['filepath'], $catalog->name, $catalog->name, array('class' => 'category'));
}
else {
$output .= theme('image', $catalog->image['filepath'], $catalog->name, $catalog->name, array('class' => 'category'));
}
}
// ADDED: creating pager and session storage
drupal_add_css(drupal_get_path('module', 'uc_advanced_catalog') .'/uc_advanced_catalog.css');
drupal_add_js(drupal_get_path('module', 'uc_advanced_catalog') .'/uc_advanced_catalog.js');
// session and request handler for default values
$defaults = _uc_advanced_catalog_default();
// using existing view
$view = views_get_view('advanced_catalog');
$view->set_display($defaults['mode']);
$view->set_arguments(array($tid));
if ($defaults['limit'] == t('all')) {
$view->set_items_per_page(0);
}
else {
$view->set_items_per_page($defaults['limit']);
}
$view->set_use_pager(TRUE);
$view->get_total_rows = TRUE;
$view->execute();
$count = count($view->result);
if ($count > 0) {
if (count($links)) {
$output .= theme('links', $links, array('class' => 'links inline uc-categories')) ."
\n";
}
// give some information to filters
$variables = array(
'count' => $count,
'total' => $view->total_rows,
);
// creating pager
$filters_form = drupal_get_form('uc_advanced_catalog_pager_form', $defaults, $variables);
// output construction
// utility: taxonomy administration links
if(user_access('administer taxonomy') && ($catalog->tid != 0)) {
$output .= l(
t('Edit this taxonomy'),
'admin/content/taxonomy/edit/term/'.$catalog->tid,
array(
'query' => array('destination' => url('catalog/'.$catalog->tid))
)
);
}
$output .= ''. $catalog->description .'
';
if (variable_get('uc_advanced_catalog_sticky', FALSE)) {
$output .= views_embed_view('advanced_catalog', 'block_4', $tid);
}
$pager_display = variable_get('uc_advanced_catalog_pager', 'top-bottom');
if ($pager_display == 'top' || $pager_display == 'top-bottom') {
$output .= '';
$output .= $filters_form;
$output .= theme('pager');
$output .= '
';
}
// adding view
$output .= $view->preview();
// bottom pager
if ($pager_display == 'bottom' || $pager_display == 'top-bottom') {
$output .= '';
$output .= $filters_form;
$output .= '
';
}
}
else {
// original code
// Display table of child categories similar to an osCommerce site's front page.
$columns = variable_get('uc_catalog_category_columns', 3);
$cat_rows = array();
$row = array();
$i = 1;
foreach ($child_list as $cell) {
$row[] = array('data' => $cell, 'class' => 'category');
if ($i % $columns == 0) {
$cat_rows[] = $row;
$row = array();
}
$i++;
}
if (count($row) > 0 && count($row) < $columns) {
if (count($cat_rows) >= 1) {
$row = array_merge($row, array_fill(count($row), $columns - count($row), array('data' => ' ', 'class' => 'category')));
}
$cat_rows[] = $row;
}
$output .= $catalog->description;
$output .= theme('table', array(), $cat_rows, array('class' => 'category'));
// end of original code
}
return $output;
}
/**
* Implementation of hook_form().
*
* @param $form_state
* Array of form state (formAPI).
* @param $defaults
* Array of defaults and current filters values.
* @param $variables
* Array of variables of the view pass to filters.
* @return
* form array().
*/
function uc_advanced_catalog_pager_form($form_state, $defaults, $variables) {
$form = array();
// hook_catalog_filters
$filters = module_invoke_all('catalog_filters', $defaults, $variables);
foreach ($filters AS $values) {
if (is_array($values)) {
$form += _uc_advanced_catalog_filter_view($values, $defaults);
}
}
return $form;
}
/**
* Theme pager form display.
*/
function theme_uc_advanced_catalog_pager_form($form) {
$elements = array();
$i = 0;
$output = '';
foreach (element_children($form) AS $key => $element) {
// render system form hidden
if ($element == 'form_build_id' || $element == 'form_token' || $element == 'form_id') {
$output .= drupal_render($form[$element]);
}
else {
// advanced_catalog filters
$elements[$key] = $form[$element];
$i++;
}
}
$j = 0;
foreach ($elements AS $key => $element) {
$j++;
$class = 'catalog-filter '. $element['#id'];
$key == 0 ? $class .= ' first': '';
$i == $j ? $class .= ' last': '';
$output .= '
';
$output .= drupal_render($elements[$key]);
$output .= '
';
}
$output .= '
';
$output .= '
';
return $output;
}
/**
* Construct sortable line on filters settings form
*
* @param $values
* array() of form value.
* @return
* form array().
*/
function _uc_advanced_catalog_filter_edit($values = NULL) {
$form = array();
if ($values && is_array($values)) {
//edit mode, remove view informations
$values += $values['edit'];
unset($values['view'], $values['edit']);
// element construction
$name = $values['name'];
// enable filter col
$values += array(
'#type' => 'checkbox',
'#default_value' => variable_get('uc_advanced_catalog_'. $name, TRUE),
);
$form['title'] = _uc_advanced_catalog_element('uc_advanced_catalog_'. $name, $values);
// default col
if ($values['default'] && is_array($values['default'])) {
// adding default value from stored variable
$variable_name = 'uc_advanced_catalog_'. $name .'_default';
$values['default']['#default_value'] = variable_get($variable_name, $values['default']['#default_value']);
$form['default'][$variable_name] = $values['default'];
}
else {
$form['default'] = array('#value' => t('n/a'));
}
// options col
if ($values['options'] && is_array($values['options'])) {
if ($values['options']['#multiple'] == TRUE) {
// multiple options
$form['options'] = array();
foreach ($values['options'] AS $name => $options) {
if ($name != '#multiple') {
$name = 'uc_advanced_catalog_'. $values['name'] .'_option_'. $name;
$options['#default_value'] = variable_get($name, $options['#default_value']);
$form['options'] += _uc_advanced_catalog_element($name, $options);
}
}
}
else {
// single option
$variable_name = 'uc_advanced_catalog_'. $name .'_options';
$values['options']['#default_value'] = variable_get($variable_name, $values['options']['#default_value']);
$form['options'] = _uc_advanced_catalog_element($variable_name, $values['options']);
}
}
else {
$form['options'] = array('#value' => t('n/a'));
}
// sort col (hide by draggable)
$form['sort'] = array(
'#type' => 'weight',
'#default_value' => variable_get('uc_advanced_catalog_'. $values['name'] .'_weight', $values['#weight']),
);
// hidden system name (used to register filters)
$form['name'] = array(
'#type' => 'hidden',
'#value' => $values['name'],
);
}
return $form;
}
/**
* Construct filters on pager display.
*
* @param $values
* array() of form value.
* @param $defaults
* array() of defaults value.
* @return
* form array().
*/
function _uc_advanced_catalog_filter_view($values = NULL, $defaults = NULL) {
$data = array();
if (variable_get('uc_advanced_catalog_'. $values['name'], TRUE)) {
//view mode, remove edit informations
$values += $values['view'];
unset($values['edit'], $values['view']);
$options = array();
$url = base_path() . $_GET['q'];
// options
if ($values['#options']) {
foreach ($values['#options'] as $v) {
$options[$url .'?'. $values['name'] .'='. drupal_strtolower($v)] = $v;
}
}
$values['#options'] = $options;
// type
if ($values['#type'] == 'select') {
$values['#attributes'] = array('onchange' => 'setLocation(this.value)', 'class' => 'pager-'. $values['name'] .'-item');
unset($values['#value']);
$values['#default_value'] = $url .'?'. $values['name'] .'='. $defaults[$values['name']];
}
else {
unset($values['#attributes']);
$values['#attributes'] = array('class' => 'pager-'. $values['name'] .'-item');
}
// weight
$values['#weight'] = variable_get('uc_advanced_catalog_'. $values['name'] .'_weight', $values['#weight'] ? $values['#weight'] : 0);
// element construction
$data = _uc_advanced_catalog_element($values['name'], $values);
}
return $data;
}
/**
* Helper to build a single form element
*
* @param $names
* string name of the element.
* @param $values
* array() of values.
* @return
* element array().
*/
function _uc_advanced_catalog_element($name, $values) {
$element[$name] = array();
foreach ($values AS $key => $value) {
if (drupal_substr($key, 0, 1) == '#') {
$element[$name] += array($key => $value);
}
}
$element[$name]['#id'] = 'filter-'. $name;
return $element;
}
/**
* Get default or current values for filters state.
*
* @return
* default values array().
* priority: request values, session, default filter
*/
function _uc_advanced_catalog_default() {
$filters = variable_get('uc_advanced_catalog_registered', array('extra', 'order', 'sort', 'limit', 'mode'));
$defaults = array();
if ($filters) {
foreach ($filters AS $name) {
if ($_REQUEST[$name]) {
$defaults[$name] = $_REQUEST[$name];
$_SESSION['uc_advanced_catalog'][$name] = $defaults[$name];
}
elseif ($_SESSION['uc_advanced_catalog'][$name]) {
$defaults[$name] = $_SESSION['uc_advanced_catalog'][$name];
}
elseif (variable_get('uc_advanced_catalog_'. $name .'_default', FALSE)) {
$defaults[$name] = variable_get('uc_advanced_catalog_'. $name .'_default', '');
}
}
}
return $defaults;
}
/******************************************************************************
* Views Hooks *
******************************************************************************/
/**
* Implementing hook_views_api
*/
function uc_advanced_catalog_views_api() {
return array(
'api' => 2.0,
'path' => drupal_get_path('module', 'uc_advanced_catalog') .'/includes',
);
}