View source
<?php
class current_search_export_ui extends ctools_export_ui {
function list_form(&$form, &$form_state) {
parent::list_form($form, $form_state);
$form['top row']['submit'] = $form['bottom row']['submit'];
$form['top row']['reset'] = $form['bottom row']['reset'];
$form['bottom row']['#access'] = FALSE;
return;
}
function list_build_row($item, &$form_state, $operations) {
parent::list_build_row($item, $form_state, $operations);
}
function edit_execute_form(&$form_state) {
$output = parent::edit_execute_form($form_state);
if (!empty($form_state['executed'])) {
$clicked = $form_state['clicked_button']['#value'];
if (t('Add item') == $clicked || t('Save and edit') == $clicked) {
$options = array();
if (!empty($_GET['destination'])) {
$options['query']['destination'] = $_GET['destination'];
unset($_GET['destination']);
}
$op = $form_state['op'];
$name = $form_state['values']['name'];
$path = 'add' != $op ? $_GET['q'] : 'admin/settings/current_search/list/' . $name . '/edit';
$this->plugin['redirect'][$op] = array(
$path,
$options,
);
}
}
return $output;
}
function edit_page($js, $input, $item, $step = NULL) {
drupal_set_title($this
->get_page_title('edit', $item));
if (!empty($this->plugin['use wizard'])) {
$cached = $this
->edit_cache_get($item, 'edit');
if (!empty($cached)) {
$item = $cached;
}
}
$form_state = array(
'plugin' => $this->plugin,
'object' => &$this,
'ajax' => $js,
'item' => $item,
'op' => 'edit',
'form type' => 'edit',
'rerender' => TRUE,
'no_redirect' => TRUE,
'step' => $step,
'function args' => func_get_args(),
);
$output = $this
->edit_execute_form($form_state);
if (!empty($form_state['executed'])) {
$export_key = $this->plugin['export']['key'];
$args = (array) $this->plugin['redirect']['edit'];
$args[0] = str_replace('%ctools_export_ui', $form_state['item']->{$export_key}, $args[0]);
call_user_func_array('drupal_goto', $args);
}
return $output;
}
function add_page($js, $input, $step = NULL) {
drupal_set_title($this
->get_page_title('add'));
if (!empty($this->plugin['use wizard']) && !empty($step)) {
$item = $this
->edit_cache_get(NULL, 'add');
}
if (empty($item)) {
$item = ctools_export_crud_new($this->plugin['schema']);
}
$form_state = array(
'plugin' => $this->plugin,
'object' => &$this,
'ajax' => $js,
'item' => $item,
'op' => 'add',
'form type' => 'add',
'rerender' => TRUE,
'no_redirect' => TRUE,
'step' => $step,
'function args' => func_get_args(),
);
$output = $this
->edit_execute_form($form_state);
if (!empty($form_state['executed'])) {
$export_key = $this->plugin['export']['key'];
$args = (array) $this->plugin['redirect']['add'];
$args[0] = str_replace('%ctools_export_ui', $form_state['item']->{$export_key}, $args[0]);
call_user_func_array('drupal_goto', $args);
}
return $output;
}
}
function current_search_settings_form(&$form, &$form_state) {
$item =& $form_state['item'];
$form['info']['#weight'] = -30;
if (empty($item->settings)) {
$item->settings = current_search_get_defaults();
}
$form['info']['label'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => $item->label,
'#description' => t('The human-readable name of the current search block configuration.'),
'#required' => TRUE,
'#maxlength' => 255,
'#size' => 30,
);
$form['info']['name'] = array(
'#type' => 'textfield',
'#title' => t('Machine name'),
'#default_value' => $item->name,
'#maxlength' => 32,
'#description' => t('The machine readable name of the current search block configuration. This value can only contain letters, numbers, and underscores.'),
);
$form['info']['searcher'] = array(
'#type' => 'select',
'#title' => t('Search page'),
'#options' => current_search_get_searcher_options(),
'#description' => t('The search page this configuration will be active on.'),
'#default_value' => current_search_get_default_searcher(),
'#access' => empty($item->name),
);
$form['buttons']['#access'] = FALSE;
$form['actions'] = array(
'#type' => 'fieldset',
'#weight' => 100,
);
if (isset($_GET['destination']) && !url_is_external($_GET['destination'])) {
$submit_text = t('Save and go back to search page');
$cancel_title = t('Return to the search page without saving configuration changes.');
$url = drupal_parse_url($_GET['destination']);
}
else {
$submit_text = t('Save and go back to list');
$cancel_title = t('Return to the list without saving configuration changes.');
$url = array();
}
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save and edit'),
);
$form['actions']['submit_list'] = array(
'#type' => 'submit',
'#value' => $submit_text,
);
$cancel_options = array(
'title' => $cancel_title,
);
if ($url) {
$cancel_options += array(
'query' => $url['query'],
);
}
$form['actions']['cancel'] = array(
'#value' => l('Cancel', !$url ? 'admin/settings/current_search' : $url['path'], $cancel_options),
);
if (empty($item->name)) {
$description = t('Add a new current search block configuration.');
}
else {
$description = t('Add new items to the current search block or configure existing ones.');
}
$form['description'] = array(
'#prefix' => '<div class="current-search-description">',
'#suffix' => '</div>',
'#value' => $description,
'#weight' => -90,
);
drupal_add_css(drupal_get_path('module', 'current_search') . '/current_search.css');
if (empty($form['info']['name']['#default_value'])) {
return;
}
$plugins = array_map('check_plain', current_search_get_plugins());
$form['plugins_title'] = array(
'#type' => 'item',
'#title' => t('Add item to block'),
);
$form['plugins'] = array(
'#tree' => TRUE,
'#prefix' => '<div class="clearfix">',
'#suffix' => '</div>',
);
$form['plugins']['plugin'] = array(
'#type' => 'select',
'#title' => t('Type'),
'#options' => $plugins,
'#prefix' => '<div class="current-search-setting current-search-plugin">',
'#suffix' => '</div>',
);
$form['plugins']['item_label'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#default_value' => '',
'#required' => FALSE,
'#size' => 30,
'#prefix' => '<div class="current-search-setting current-search-label">',
'#suffix' => '</div>',
);
$form['plugins']['item_name'] = array(
'#type' => 'textfield',
'#title' => t('Machine name'),
'#default_value' => '',
'#maxlength' => 32,
'#required' => FALSE,
'#description' => t('The machine readable name of the item being added to the current search block. This value can only contain letters, numbers, and underscores.'),
);
$form['plugins']['actions'] = array(
'#type' => 'actions',
'#prefix' => '<div class="current-search-setting current-search-button">',
'#suffix' => '</div>',
);
$form['plugins']['actions']['add_item'] = array(
'#type' => 'submit',
'#value' => t('Add item'),
'#submit' => array(
'current_search_add_item_submit',
),
'#validate' => array(
'current_search_add_item_validate',
),
);
$form['plugin_sort'] = array(
'#type' => 'item',
'#access' => !empty($item->settings['items']),
'#title' => t('Item display order'),
'#theme' => 'current_search_sort_settings_table',
'#current_search' => $item->settings,
'#tree' => TRUE,
);
foreach ($item->settings['items'] as $name => $settings) {
$form['plugin_sort'][$name]['item'] = array(
'#value' => check_plain($settings['label']),
);
$form['plugin_sort'][$name]['remove'] = array(
'#value' => l('Remove item', 'admin/settings/current_search/item/' . $item->name . '/delete/' . $name),
);
$form['plugin_sort'][$name]['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight for @title', array(
'@title' => $settings['label'],
)),
'#title_display' => 'invisible',
'#delta' => 50,
'#default_value' => isset($settings['weight']) ? $settings['weight'] : 0,
'#attributes' => array(
'class' => 'current-search-sort-weight',
),
);
}
$form['plugin_settings_title'] = array(
'#type' => 'item',
'#access' => !empty($item->settings['items']),
'#title' => t('Item settings'),
);
$form['plugin_settings'] = array(
'#type' => 'fieldset',
'#tree' => TRUE,
);
$has_settings = FALSE;
foreach ($item->settings['items'] as $name => $settings) {
if ($class = ctools_plugin_load_class('current_search', 'items', $settings['id'], 'handler')) {
$plugin = new $class($name, $item);
$form['plugin_settings'][$name] = array(
'#type' => 'fieldset',
'#title' => check_plain($settings['label']),
'#collapsible' => TRUE,
'#group' => 'settings',
'#tree' => TRUE,
);
$form['plugin_settings'][$name]['id'] = array(
'#type' => 'value',
'#value' => $settings['id'],
);
$form['plugin_settings'][$name]['label'] = array(
'#type' => 'value',
'#value' => $settings['label'],
);
$plugin
->settingsForm($form['plugin_settings'][$name], $form_state);
$has_settings = TRUE;
}
}
if (!$has_settings) {
unset($form['plugin_settings']);
}
$form['advanced_settings_title'] = array(
'#type' => 'item',
'#title' => t('Advanced settings'),
);
$form['advanced_settings'] = array(
'#tree' => TRUE,
);
$form['advanced_settings']['empty_searches'] = array(
'#type' => 'radios',
'#title' => t('Display settings'),
'#options' => array(
CURRENT_SEARCH_DISPLAY_KEYS => t('Display only when keywords are entered.'),
CURRENT_SEARCH_DISPLAY_ALWAYS => t('Display on empty searches where no keywords are entered.'),
CURRENT_SEARCH_DISPLAY_FILTERS => t('Display only when one or more facet items are active.'),
CURRENT_SEARCH_DISPLAY_KEYS_FILTERS => t('Display when either keywords are entered one or more facet items are active.'),
),
'#default_value' => $item->settings['advanced']['empty_searches'],
'#description' => t('This setting determines when the current search block will be displayed.'),
);
}
function theme_current_search_sort_settings_table($variables) {
$output = '';
$header = array(
'item' => t('Item'),
'weight' => t('weight'),
);
$rows = array();
foreach ($variables['#current_search']['items'] as $name => $settings) {
$rows[$name] = array(
'class' => 'draggable',
'data' => array(
drupal_render($variables[$name]['item']),
drupal_render($variables[$name]['weight']),
array(
'data' => drupal_render($variables[$name]['remove']),
'class' => 'current-search-remove-link',
),
),
);
}
$table_id = 'current-search-sort-settings';
drupal_add_tabledrag($table_id, 'order', 'sibling', 'current-search-sort-weight');
$output .= theme('table', $header, $rows, array(
'id' => $table_id,
));
return $output;
}
function current_search_add_item_validate($form, &$form_state) {
if (empty($form_state['values']['plugins']['item_name'])) {
$vars = array(
'!name' => 'Item name',
);
form_set_error('item_name', t('!name field is required', $vars));
}
if (empty($form_state['values']['plugins']['item_label'])) {
$vars = array(
'!name' => 'Machine-readable name',
);
form_set_error('item_label', t('!name field is required', $vars));
}
}
function current_search_add_item_submit($form, &$form_state) {
$item =& $form_state['item'];
$item->settings += current_search_get_defaults();
$id = $form_state['values']['plugins']['plugin'];
$name = $form_state['values']['plugins']['item_name'];
$label = $form_state['values']['plugins']['item_label'];
if ($class = ctools_plugin_load_class('current_search', 'items', $id, 'handler')) {
$plugin = new $class($name);
$item->settings['items'][$name] = $plugin
->getDefaultSettings() + array(
'id' => $id,
'label' => $label,
);
}
}
function current_search_settings_form_submit($form, &$form_state) {
$item =& $form_state['item'];
$item->settings += current_search_get_defaults();
if (!empty($form_state['values']['plugin_settings'])) {
$item->label = $form_state['values']['label'];
if (!empty($form_state['values']['plugin_settings'])) {
foreach ($form_state['values']['plugin_settings'] as $name => $settings) {
if (is_array($settings)) {
$item->settings['items'][$name] = $settings + array(
'weight' => $form_state['values']['plugin_sort'][$name]['weight'],
);
}
}
uasort($item->settings['items'], 'facetapi_sort_weight');
$item->settings['advanced'] = $form_state['values']['advanced_settings'];
}
}
else {
if (!empty($form_state['values']['searcher'])) {
$name = $form_state['values']['name'];
$searcher = $form_state['values']['searcher'];
current_search_set_block_searcher($name, $searcher);
}
}
}
function current_search_delete_item_form(&$form_state, stdClass $item, $name) {
$form['#current_search'] = array(
'item' => $item,
'name' => $name,
);
$form['text'] = array(
'#value' => '<p>' . t('Are you sure you want to remove the item %name from the current search block configuration?.', array(
'%name' => $name,
)) . '</p>',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Remove item'),
);
$form['actions']['cancel'] = array(
'#value' => l('Cancel', 'admin/settings/current_search/list/' . $item->name . '/edit', array(
'title' => t('Go back to current search block configuration'),
)),
);
$form['#submit'][] = 'current_search_delete_item_form_submit';
return $form;
}
function current_search_delete_item_form_submit($form, &$form_state) {
$name = $form['#current_search']['name'];
$item = $form['#current_search']['item'];
if (isset($item->settings['items'][$name])) {
$label = $item->settings['items'][$name]['label'];
drupal_set_message(t('@label has been removed.', array(
'@label' => $label,
)));
unset($item->settings['items'][$name]);
ctools_export_crud_save('current_search', $item);
}
$form_state['redirect'] = 'admin/settings/current_search/list/' . $item->name . '/edit';
}
function current_search_config_exists($name) {
$configs = ctools_export_crud_load_all('current_search');
return isset($configs[$name]);
}
function current_search_item_exists($name, &$element, &$form_state) {
$item =& $form_state['item'];
return isset($item->settings['items'][$name]);
}
function current_search_get_defaults() {
return array(
'items' => array(),
'advanced' => array(
'empty_searches' => 0,
),
);
}