Skip to content

Instantly share code, notes, and snippets.

@cyberlex404
Created October 21, 2017 07:58
Show Gist options
  • Select an option

  • Save cyberlex404/416ec8d052f294411267975f8f7501ea to your computer and use it in GitHub Desktop.

Select an option

Save cyberlex404/416ec8d052f294411267975f8f7501ea to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: Lex
* Date: 20.10.2017
* Time: 11:47
*/
/**
* Implements hook_menu().
*/
function show_order_menu() {
// Module settings form
$items['admin/config/show-order'] = array(
'title' => 'Order settings',
'position' => 'left',
'weight' => -100,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer site configuration'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/config/show-order/settings'] = array(
'title' => 'Distance price',
'page callback' => 'drupal_get_form',
'page arguments' => array('show_order_settings_form'),
'access arguments' => array('administer site configuration'),
'file' => 'show_order.settings.inc',
);
$items['order/%ctools_js'] = array(
'title' => t('Order'),
'page arguments' => array(1),
'access callback' => TRUE,
'page callback' => 'show_order_form_callback',
'type' => MENU_CALLBACK,
);
// Страница для вывода ссылки на всплывающее окно.
$items['first-popup'] = array(
'title' => 'My first popup',
'access callback' => TRUE,
'page callback' => 'my_popup_page',
'type' => MENU_CALLBACK,
);
return $items;
}
function show_order_form_callback($js = NULL) {
$args = func_get_args();
$form_id = 'show_order_form';
// Remove $js from the arguments.
array_shift($args);
// dpm($args);
$form_state = [];
if (!empty($args) && isset($args[0])) {
$form_state['build_info']['show'] = $args[0];
$form_state['build_info']['args'] = [];
}
$showItems = _show_order_shows();
$distancePrice = variable_get('show_order_distance', []);
$setting = _show_order_js_settings($showItems);
$setting['distance_price'] = $distancePrice;
$form_state['build_info']['args']['show_items'] = $showItems;
$form_state['build_info']['args']['setting'] = $setting;
$form = drupal_build_form($form_id, $form_state);
//$form = drupal_get_form('show_order_form', $args);
return $form;
}
function show_order_form($form, $form_state) {
$showId = isset($form_state['build_info']['show']) ? $form_state['build_info']['show']: FALSE;
$setting = $form_state['build_info']['args']['setting'];
$showItems = $form_state['build_info']['args']['show_items'];
// Название шоу
$form['#attached']['js'][] = [
'type' => 'setting',
'data' => [
'show_order' => $setting,
]
];
$form['#attached']['js'][] = array(
'data' => drupal_get_path('module', 'show_order') . '/js/select_update.js',
'type' => 'file',
);
$form_state['build_info']['calc_data'] = $setting;
// dpm($setting ,' s');
// dpm($form_state , '$form_state');
$form['#prefix'] = '<div id="show-order-form-wrapper">';
$form['#suffix'] = '</div>';
$form['order_info'] = [
'#type' => 'container',
'#weight' => 1,
'#attributes' => [
'class' => ['order-info', 'row'],
],
];
$form['order'] = [
'#type' => 'container',
'#weight' => 2,
'#attributes' => [
'class' => ['show-order-fields', 'row'],
],
];
$form['order']['base'] = [
'#type' => 'container',
'#weight' => 1,
'#tree' => TRUE,
'#attributes' => [
'class' => ['show-base', 'col-sm-4'],
],
];
$form['order']['timepromo'] = [
'#type' => 'container',
'#weight' => 2,
'#tree' => TRUE,
'#attributes' => [
'class' => ['time-promo', 'col-sm-4'],
],
];
$form['order']['contact'] = [
'#type' => 'container',
'#weight' => 3,
'#tree' => TRUE,
'#attributes' => [
'class' => ['order-contact', 'col-sm-4'],
],
];
$form['preorder'] = [
'#type' => 'container',
'#weight' => 3,
'#attributes' => [
'class' => ['pre-order-info', 'row'],
],
];
$form['actionw'] = [
'#type' => 'container',
'#weight' => 4,
'#attributes' => [
'class' => ['action-wrapper', 'row'],
],
];
$form['features'] = [
'#type' => 'container',
'#weight' => 5,
'#tree' => TRUE,
];
$form['order']['base']['show'] = array(
'#title' => t('Show name'),
'#type' => 'select',
'#weight' => 1,
'#options' => _show_order_show_select($showItems),
'#empty_option' => t('-Select show-'),
'#empty_value' => -1,
'#required' => TRUE,
'#ajax' => [
'callback' => 'show_order_on_select_show_callback',
'wrapper' => 'show-order-form-wrapper',
],
);
// Amount of children
$form['order']['base']['children'] = array(
'#title' => t('Amount of children'),
'#type' => 'select',
'#weight' => 3,
'#default_value' => 'to15',
'#options' => [
'to15' => t('to 15'),
'15-30' => t('15-30'),
'over30' => t('over 30'),
],
'#empty_option' => t('-Select-'),
'#empty_value' => -1,
);
$form['order']['base']['duration'] = array(
'#title' => t('Duration'),
'#type' => 'select',
'#weight' => 2,
'#options' => [
],
'#empty_option' => t('-Select show-'),
'#empty_value' => -1,
'#default_value' => -1,
'#ajax' => [
'callback' => 'show_order_on_select_duration_callback',
],
'#prefix' => '<div class="base-duration-wrapper">',
'#suffix' => '</div>',
);
$form['order']['base']['mkad'] = array(
'#title' => t('Distance from MKAD'),
'#type' => 'select',
'#weight' => 4,
'#ajax' => [
'callback' => 'show_order_on_select_duration_callback',
],
'#options' => [
'inside' => 'inside MKAD',
'to10' => 'to 10 km',
'to20' => 'to 20 km',
'to30' => 'to 30 km',
'to40' => 'to 40 km',
'to50' => 'to 50 km',
'to60' => 'to 60 km',
'to70' => 'to 70 km',
'to80' => 'to 80 km',
],
'#empty_option' => t('-Select show-'),
'#empty_value' => -1,
);
$form['order']['timepromo']['date'] = array(
'#title' => t('Date'),
'#type' => 'date_select',
'#date_format' => 'd m Y',
'#date_year_range' => '0:+1',
'#date_label_position' => 'within',
'#date_increment' => 10,
'#default_value' => 'now',
'#datepicker_options' => [],
'#prefix' => '<div class="clearfix date-date">',
'#suffix' => '</div>',
);
$form['order']['timepromo']['time'] = array(
'#title' => t('Time'),
'#type' => 'date_select',
'#date_format' => 'H:i',
'#date_year_range' => '0:+1',
'#date_label_position' => 'within',
'#date_increment' => 10,
'#default_value' => 'now',
'#prefix' => '<div class="clearfix date-time">',
'#suffix' => '</div>',
);
$form['order']['timepromo']['promo'] = array(
'#title' => t('Do you have a promotional code?'),
'#type' => 'textfield',
);
$form['order']['timepromo']['agreement'] = [
'#type' => 'checkbox',
'#required' => TRUE,
'#title' => 'Заполняя форму, вы соглашаетесь на обработку персональных данных',
];
/** 3 column */
$form['order']['contact']['name'] = array(
'#title' => t('Your name'),
'#type' => 'textfield',
);
$form['order']['contact']['phone'] = array(
'#title' => t('Telephone'),
'#type' => 'textfield',
);
$form['order']['contact']['email'] = array(
'#title' => t('E-mail'),
'#type' => 'textfield',
);
_show_order_get_features();
_show_order_shows();
$form['actionw']['actions']['#type'] = 'actions';
$form['actionw']['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Order show'),
);
if ($showId) {
$form['order']['base']['show']['#default_value'] = $showId;
$form['order']['base']['duration']['#options'] = _show_order_duration_select($form_state, $showId);
}
return $form;
}
function show_order_form_submit($form, &$form_state) {
dpm($form_state['build_info']['calc_data'], 'c-data');
dpm($form_state);
}
function _show_order_load_show($id) {
$show = entity_load('node', [$id]);
return $show;
}
function _show_order_get_features() {
$entityTypeId = 'additional_experience';
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node', '=')
->propertyCondition('type', $entityTypeId, '=');
$result = $query->execute();
$featuresIds = array_keys($result['node']);
$features = entity_load('node', $featuresIds);
// dpm($features);
}
function _show_order_shows(){
$entityTypeId = 'show';
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node', '=')
->propertyCondition('type', $entityTypeId, '=');
$result = $query->execute();
$showIds = array_keys($result['node']);
$shows = entity_load('node', $showIds);
// dpm($shows);
return $shows;
}
function _show_order_show_select($showItems) {
$options = [];
foreach ($showItems as $showItem) {
$options[$showItem->nid] = $showItem->title . ' [' . $showItem->nid . ']';
}
return $options;
}
function _show_order_js_settings($showItems) {
$settings = [];
foreach ($showItems as $showItem) {
$settings['show_data'][$showItem->nid] = _show_order_js_show_data($showItem);
}
return $settings;
}
function _show_order_js_show_data($node) {
$fieldPrices = 'field_show_price_duration_base';
$fieldChildren1 = 'field_show_price_1';
$fieldChildren2 = 'field_show_price_2';
$wrap = entity_metadata_wrapper('node', $node);
$durationPrice = $wrap->$fieldPrices->value();
$priceChildren = [
'to15' => 0,
'15-30' => $wrap->$fieldChildren1->value(),
'over30' => $wrap->$fieldChildren2->value(),
];
$showData = [
'id' => $node->nid,
'label' => $node->title,
'price_children' => $priceChildren,
];
foreach ($durationPrice as $item) {
$duration = $item['first'];
$price = $item['second'];
$showData['base_price'][$duration] = $price;
}
return $showData;
}
function show_order_on_select_show_callback($form, &$form_state) {
// todo: всю форму и рассчитать базовые значения.
$form_state['rebuild'] = TRUE;
// unset($form_state['values']['base']['duration']);
// $form_state['values']['base']['duration'] = -1;
$form['order']['base']['duration']['#options'] = _show_order_duration_select($form_state);
//$form['order']['base']['duration']['#empty_option'] = t('-Select show-');
//$form['order']['base']['duration']['#empty_value'] = -1;
//$form['order']['base']['duration']['#default_value'] = -1;
dpm($form_state);
return $form;
}
function show_order_on_select_duration_callback($form, &$form_state){
$commands = array();
$showId = $form_state['values']['base']['show'];
$durationId = $form_state['values']['base']['duration'];
//$commands[] = ajax_command_alert("Тралала " . "-" . $durationId);
$commands[] = show_order_ajax_command_calc($showId, $durationId);
return array('#type' => 'ajax', '#commands' => $commands);
}
function _show_order_duration_select(&$form_state, $showId = NULL) {
if (is_null($showId)) {
$showId = $form_state['values']['base']['show'];
}
$options = [];
$durationList = $form_state['build_info']['args']['setting']['show_data'][$showId]['base_price'];
foreach ($durationList as $duration => $price) {
$options[$duration] = $duration . 'мин. (' . $price .')';
}
return $options;
}
function show_order_ajax_command_update($options) {
return array(
'command' => 'duration_select_update',
'options' => $options,
);
}
function show_order_ajax_command_calc($amount, $duration) {
return array(
'command' => 'show_order_calc',
'amount' => $amount,
'duration' => $duration,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment