Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Модуль, отображающий список выбора города
- */
- function cityfilters_init()
- {
- drupal_add_library('system', 'jquery.cookie');
- drupal_add_css(drupal_get_path('module', 'cityfilters') . '/css/cityfilters_style.css');
- drupal_add_js(drupal_get_path('module', 'cityfilters') . '/js/cityfilters_jfunctions.js');
- }
- /**
- * Implementation of hook_menu()
- */
- function example_menu() {
- $items = array();
- $items['cityfilters/%ctools_js/select'] = array(
- 'title' => t('Select your city'),
- 'page callback' => 'cityfilters_callback',
- 'page arguments' => array(1),
- // 'access arguments' => array('access site-wide contact form'),
- 'delivery callback' => 'ajax_deliver',
- 'theme callback' => 'ajax_base_page_theme',
- );
- return $items;
- }
- /**
- * @param null $js
- * @return array
- */
- function cityfilters_callback($js = NULL) {
- // Если у пользователя выключен JavaScript в браузере, то отправляют его на обычную форму обратной связи.
- if (!$js) {
- drupal_goto('#');
- }
- // Подключаем библиотеку для работы с модальным окном с помощью ajax команд.
- ctools_include('modal');
- ctools_include('ajax');
- $commands = array();
- // Формируем форму в модальном окне.
- $commands = ctools_modal_command_display(t('Select your city'), 'привет!');
- // // Если форма была успешно отправлена, то выводим сообщение об этом и закрываем модальное окно.
- // if (!empty($form_state['executed'])) {
- // $commands = array();
- //
- // // Выводим сообщение об успешной отправке формы.
- // $commands[] = ajax_command_html('#messages-wrapper', theme('status_messages'));
- //
- // // Закрываем модальное окно.
- // $commands[] = ctools_modal_command_dismiss();
- // }
- $result = ajax_render( $commands);
- dpm($result);
- return $result;
- }
- function cityfilters_block_info()
- {
- $blocks['city_filter'] = array(
- 'info' => t('City filter'),
- 'cache' => DRUPAL_NO_CACHE,
- );
- $blocks['city_select_popup'] = array(
- 'info' => t('Select your City (Popup)'),
- 'cache' => DRUPAL_NO_CACHE,
- );
- return $blocks;
- }
- /**
- * @param string $delta
- * @return array|bool
- */
- function cityfilters_block_view($delta = '')
- {
- $block = array();
- if (user_access('access content')) {
- switch ($delta) {
- case 'city_filter':
- $block['subject'] = t('Select your city');
- $block['content'] = drupal_get_form('cityfilters_select_city_form');
- return $block;
- case 'city_select_popup':
- $block['subject'] = t('Select your city');
- // $block['content'] = cityfilters_select_popup();
- // В этой функции подключаются необходимые библиотеки, js и css файлы (если нужны), а так же устанавливаются
- // настройки для модального окна. Эту функцию мы опишем позже
- _example_include_modal();
- $block['content'] = ctools_modal_text_button(
- t('Contact'),
- 'cityfilters/nojs/select',
- t('Contact'),
- 'ctools-modal-select-city'
- );
- return $block;
- }
- }
- return false;
- }
- function _example_include_modal() {
- static $added = FALSE;
- if ($added == FALSE) {
- $added = TRUE;
- // Include the CTools tools that we need.
- ctools_include('modal');
- ctools_include('ajax');
- ctools_modal_add_js();
- // Создаем массив с настройками для модального окна.
- $example_style = array(
- 'example-contact-style' => array(
- 'modalSize' => array(
- 'type' => 'fixed', // Тип модального окна. фиксированный или резиновый.
- 'width' => 420, // Ширина модального окна.
- 'height' => 'auto', // Высота модального окна.
- ),
- 'modalOptions' => array(
- 'opacity' => (float) 0.3, // Прозрачность фона.
- 'background-color' => '#000000', // Цвет фона.
- ),
- 'closeText' => '', // Текст для закрытия модального окна.
- 'loadingText' => '', // Текст, отображаемый в момент загрузки модального окна.
- 'animation' => 'fadeIn', // Эффект появления модального окна.
- 'animationSpeed' => 'fast', // Скорость анимации.
- ),
- );
- // Подключаем настройки для модального окна.
- drupal_add_js($example_style, 'setting');
- }
- }
- /**
- * @return mixed
- */
- function cityfilters_select_popup()
- {
- $cities = cityfilters_get_cities();
- $build['your_city'] = [
- '#type' => 'link',
- '#title' => $cities[$_COOKIE['clicat_city']],
- ];
- return $build;
- }
- /**
- * Form constructor for city select form.
- *
- */
- function cityfilters_select_city_form($form, &$form_state)
- {
- $cities = cityfilters_get_cities();
- $form['widget'] = [
- '#type' => 'container',
- '#attributes' => ['class' => ['container-inline']],
- ];
- $form['widget']['city'] = [
- '#type' => 'select',
- '#title' => t('Выберите город'),
- '#title_display' => 'attribute',
- '#options' => $cities,
- '#default_value' => $_COOKIE['clicat_city'],
- '#required' => TRUE,
- ];
- $form['widget']['actions'] = ['#type' => 'actions'];
- $form['widget']['actions']['submit'] = [
- '#type' => 'submit',
- '#value' => t('Select'),
- '#id' => 'selectcity-submit',
- ];
- return $form;
- }
- function cityfilters_select_city_form_submit($form, &$form_state)
- {
- // установка coocies в JS.
- // dpm($_COOKIE);
- }
- /*
- * Выбираем список городов
- */
- function cityfilters_get_cities()
- {
- $query = db_select('node', 'n');
- $query->leftJoin('field_data_field_address', 'a', 'n.nid = a.entity_id');
- $query->fields('a', ['field_address_locality']);
- $query->addExpression('COUNT(*)', 'count');
- $query->condition('n.status', '1');
- $query->condition('n.type', 'clinic');
- $query->groupBy('a.field_address_locality');
- $query->orderBy('count', 'DESC');
- $all = $query->execute()->fetchAll();
- $cities = [];
- foreach ($all as $city) {
- $city_name = $city->field_address_locality . ' (' . $city->count . ')';
- $cities[$city->field_address_locality] = $city_name;
- }
- return $cities;
- }
- // $block['subject'] = t('City filter');
- // // Load the view.
- // $view = views_embed_view('catalog', 'attachment_cities');
- // if ($view) {
- // $block['content'] = $view;
- // }
- // else {
- // $block['content'] = t('The view is empty');
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement