Skip to content

Instantly share code, notes, and snippets.

@cyberlex404
Created October 20, 2017 07:36
Show Gist options
  • Select an option

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

Select an option

Save cyberlex404/e095d491ffa95b82facd4be4505264e0 to your computer and use it in GitHub Desktop.
<?php
/**
* Implements hook_form_alter().
*/
function extra_form_alter(&$form, &$form_state, $form_id) {
if ( isset($_GET['show']) && $_GET['show'] != '' ) {
$nid = $_GET['show'];
}
if ($form_id == 'webform_client_form_140') {
$qq = db_select('node', 'n');
$qq->condition('n.type', 'show');
$qq->fields('n', array('title', 'nid'));
$qq->innerJoin('field_data_field_show_price', 'p', 'p.entity_id = n.nid');
$qq->fields('p', array('field_show_price_value'));
$qq->addJoin('LEFT OUTER', 'field_data_field_show_price_1', 'p1', 'p1.entity_id = n.nid');
$qq->fields('p1', array('field_show_price_1_value'));
$qq->addJoin('LEFT OUTER', 'field_data_field_show_price_2', 'p2', 'p2.entity_id = n.nid');
$qq->fields('p2', array('field_show_price_2_value'));
$qq->innerJoin('field_data_field_show_duration', 'd', 'd.entity_id = n.nid');
$qq->fields('d', array('field_show_duration_value'));
$qq->addJoin('LEFT OUTER', 'field_data_field_show_duration_1', 'd1', 'd1.entity_id = n.nid');
$qq->fields('d1', array('field_show_duration_1_value'));
$qq->addJoin('LEFT OUTER', 'field_data_field_show_duration_2', 'd2', 'd2.entity_id = n.nid');
$qq->fields('d2', array('field_show_duration_2_value'));
$res = $qq->execute()->fetchAll();
unset($form['submitted']['nazvanie_shou']['#options']);
foreach ($res as $i => $show) {
if (isset($nid) && $nid == $show->nid) {
$form['submitted']['nazvanie_shou']['#default_value'] = $show->nid;
}
$form['submitted']['nazvanie_shou']['#options'][$show->nid] = $show->title;
$form['submitted']['nazvanie_shou']['#extra'][$show->nid]['title'] = $show->title;
$form['submitted']['nazvanie_shou']['#extra'][$show->nid]['price'] = 0+$show->field_show_price_value;
$form['submitted']['nazvanie_shou']['#extra'][$show->nid]['price1'] = 0+$show->field_show_price_1_value;
$form['submitted']['nazvanie_shou']['#extra'][$show->nid]['price2'] = 0+$show->field_show_price_2_value;
$form['submitted']['nazvanie_shou']['#extra'][$show->nid]['duration'] = 0+$show->field_show_duration_value;
$form['submitted']['nazvanie_shou']['#extra'][$show->nid]['duration1'] = 0+$show->field_show_duration_1_value;
$form['submitted']['nazvanie_shou']['#extra'][$show->nid]['duration2'] = 0+$show->field_show_duration_2_value;
}
$form['submitted']['nazvanie_shou']['#empty_value'] = 0;
$form['submitted']['nazvanie_shou']['#theme'] = 'megashow_nazvanieshow_select';
$qq = db_select('taxonomy_vocabulary', 'v');
$qq->condition('v.machine_name', 'udalennost_ot_mkad');
$qq->addJoin('LEFT OUTER', 'taxonomy_term_data', 't', 't.vid = v.vid');
$qq->orderBy('t.weight', 'ASC');
$qq->fields('t', array('tid', 'name'));
$qq->innerJoin('field_data_field_udalennost_price', 'p', 'p.entity_id = t.tid');
$qq->addField('p', 'field_udalennost_price_value', 'price');
$res = $qq->execute()->fetchAll();
unset($form['submitted']['udalennost_ot_mkad']['#options']);
foreach ($res as $k => $tid) {
if ($tid->price == 0) {
$form['submitted']['udalennost_ot_mkad']['#default_value'] = $tid->tid;
}
$form['submitted']['udalennost_ot_mkad']['#options'][$tid->tid] = $tid->name;
$form['submitted']['udalennost_ot_mkad']['#extra'][$tid->tid]['price'] = $tid->price;
$form['submitted']['udalennost_ot_mkad']['#extra'][$tid->tid]['title'] = $tid->name;
}
$form['submitted']['udalennost_ot_mkad']['#theme'] = 'megashow_udalennost_ot_mkad_select';
$form['submitted']['udalennost_ot_mkad']['#empty_value'] = 0;
$qq = db_select('field_data_field_show_price_duration_base', 'd');
$qq->fields('d', array('entity_id', 'field_show_price_duration_base_first', 'field_show_price_duration_base_second', 'delta'));
$opt = $qq->execute()->fetchAll();
$form['submitted']['duration']['#empty_value'] = 0;
foreach ($opt as $k => $dur) {
$form['submitted']['duration']['#options'][$dur->entity_id.'-'.$dur->delta] = $dur->field_show_price_duration_base_first . 'мин - ' . $dur->field_show_price_duration_base_second . 'р.';
$form['submitted']['duration']['#extra'][$dur->entity_id.'-'.$dur->delta]['nid'] = $dur->entity_id;
$form['submitted']['duration']['#extra'][$dur->entity_id.'-'.$dur->delta]['delta'] = $dur->delta;
$form['submitted']['duration']['#extra'][$dur->entity_id.'-'.$dur->delta]['duration'] = $dur->field_show_price_duration_base_first;
$form['submitted']['duration']['#extra'][$dur->entity_id.'-'.$dur->delta]['price'] = $dur->field_show_price_duration_base_second;
}
$form['submitted']['duration']['#theme'] = 'megashow_duration_select';
$form['#after_build'][] = '_form_alter_after_build';
}
}
function _form_alter_after_build(&$form, &$form_state) {
unset($form['submitted']['udalennost_ot_mkad']['#options'][0]);
return $form;
}
/**
* Implements hook_theme().
*/
function extra_theme($existing, $type, $theme, $path) {
return array(
'megashow_nazvanieshow_select'=>array(
'function'=>'theme_megashow_nazvanieshow_select',
'render element' => 'element',
),
'megashow_udalennost_ot_mkad_select'=>array(
'function'=>'theme_megashow_udalennost_ot_mkad_select',
'render element' => 'element',
),
'megashow_duration_select'=>array(
'function'=>'theme_megashow_duration_select',
'render element' => 'element',
),
);
}
function theme_megashow_duration_select($variables) {
$element = $variables['element'];
element_set_attributes($element, array('id', 'name', 'size'));
_form_set_class($element, array('form-select'));
$html = '<select' . drupal_attributes($element['#attributes']) . '>' . form_select_options($element) . '</select>';
$html .= '<div class="hidden params">';
$html .= '<div class="param" data-n="0" data-p="0" data-d="0" data-p="0" data-val="0"></div>';
foreach ($element['#extra'] as $nid => $dur) {
$data_attr = 'data-n="'.$dur['nid'].'"';
$data_attr .= ' data-p="'.$dur['price'].'"';
$data_attr .= ' data-d="'.$dur['duration'].'"';
$data_attr .= ' data-val="'.$dur['nid'].'-'.$dur['delta'].'"';
$html .= '<div class="param" '.$data_attr.'></div>';
}
$html .= '</div>';
return $html;
}
function theme_megashow_nazvanieshow_select($variables) {
$element = $variables['element'];
element_set_attributes($element, array('id', 'name', 'size'));
_form_set_class($element, array('form-select'));
$html = '<select' . drupal_attributes($element['#attributes']) . '>' . form_select_options($element) . '</select>';
$html .= '<div class="hidden params">';
foreach ($element['#extra'] as $nid => $show) {
$data_attr = 'data-n="'.$nid.'"';
$data_attr .= ' data-p="'.$show['price'].'"';
$data_attr .= ' data-p1="'.$show['price1'].'"';
$data_attr .= ' data-p2="'.$show['price2'].'"';
$data_attr .= ' data-d="'.$show['duration'].'"';
$data_attr .= ' data-d1="'.$show['duration1'].'"';
$data_attr .= ' data-d2="'.$show['duration2'].'"';
$html .= '<div class="param sh'.$nid.'" value="'.$nid.'" '.$data_attr.'>'.$show['title'].'</div>';
}
$html .= '</div>';
return $html;
}
function theme_megashow_udalennost_ot_mkad_select($variables) {
$element = $variables['element'];
element_set_attributes($element, array('id', 'name', 'size'));
_form_set_class($element, array('form-select'));
$html = '<select' . drupal_attributes($element['#attributes']) . '>' . form_select_options($element) . '</select>';
$html .= '<div class="hidden params">';
foreach ($element['#extra'] as $tid => $udalennost) {
$data_attr = ' data-p="'.$udalennost['price'].'"';
$html .= '<div class="param ud'.$tid.'" value="'.$tid.'" '.$data_attr.'>'.$udalennost['title'].'</div>';
}
$html .= '</div>';
return $html;
}
/**
* Implements hook_panels_pane_prerender().
*/
function extra_panels_pane_prerender($pane) {
if (($pane->panel == 'experiments' || $pane->panel == 'bottom') && $pane->did == 4 && $pane->type == "views" && $pane->subtype == 'experiment') {
$cat = variable_get('experiment_category_list');
isset($cat[$pane->configuration['args']]) ? $pane->configuration['args'] = $cat[$pane->configuration['args']] : false;
$tax = taxonomy_term_load($pane->configuration['args']);
$pane->configuration['override_title'] = 1;
isset($tax->name) ? $pane->configuration['override_title_text'] = $tax->name: false;
}
}
/**
* Implements hook_menu().
*/
function extra_menu() {
$items = array();
$items['admin/config/experiment'] = array(
'title' => t('Эксперименты и опыты (категории на главной)'),
'page callback' => 'drupal_get_form',
'page arguments' => array('experiment_category_list'),
'access callback' => 'extra_role_access',
'access arguments' => array(array('administrator', 'redactor')),
'position' => 'right',
'weight' => -4,
);
return $items;
}
function extra_role_access($roles) {
if (user_is_logged_in()) {
global $user;
if (in_array('administrator', $user->roles)) {
return TRUE;
}
foreach ($roles as $role) {
if (in_array($role, $user->roles)) {
return TRUE;
}
}
}
return FALSE;
}
function experiment_category_list() {
$form = array();
$cat = variable_get('experiment_category_list');
$qq = db_select('taxonomy_term_data', 't');
$qq->condition('t.vid', '12');
$qq->fields('t', array('tid', 'name'));
$cat_list = $qq->execute()->fetchAllKeyed();
$form['experiment_category_list'] = array(
'#tree' => TRUE,
);
$form['experiment_category_list'][1] = array(
'#type' => 'select',
'#title' => 'Первая выборка',
'#required' => TRUE,
'#options' => $cat_list,
'#default_value' => isset($cat[1]) ? $cat[1] : 0,
);
$form['experiment_category_list'][2] = array(
'#type' => 'select',
'#title' => 'Вторая выборка',
'#required' => TRUE,
'#options' => $cat_list,
'#default_value' => isset($cat[2]) ? $cat[2] : 0,
);
$form['experiment_category_list'][3] = array(
'#type' => 'select',
'#title' => 'Третья выборка',
'#required' => TRUE,
'#options' => $cat_list,
'#default_value' => isset($cat[3]) ? $cat[3] : 0,
);
$megashow_zakaz_show_ryba = variable_get('megashow_zakaz_show_ryba');
$form['megashow_zakaz_show_ryba'] = array(
'#type' => 'textfield',
'#title' => 'Рыбный текст "При заказе шоу ..."',
'#required' => TRUE,
'#default_value' => isset($megashow_zakaz_show_ryba) ? $megashow_zakaz_show_ryba : '',
);
$megashow_zakazhite_prazdnik = variable_get('megashow_zakazhite_prazdnik');
$form['megashow_zakazhite_prazdnik'] = array(
'#type' => 'textfield',
'#title' => 'Рыбный текст "Закажите праздник уже сейчас ..."',
'#required' => TRUE,
'#default_value' => isset($megashow_zakazhite_prazdnik) ? $megashow_zakazhite_prazdnik : '',
);
return system_settings_form($form);
}
/**
* Implements hook_block_view_alter().
*/
function extra_block_view_alter(&$data, $block) {
if ($block->delta == '18') {
$node = node_load(arg(1));
if (isset($node->field_show_select_text[LANGUAGE_NONE]) && $node->field_show_select_text[LANGUAGE_NONE][0]['value'] != '') {
$text = $node->field_show_select_text[LANGUAGE_NONE][0]['value'];
} else {
$text = variable_get('megashow_zakaz_show_ryba');
}
$data['content'] = zakazat_show_link($text);
}
if ($block->delta == '19') {
$node = node_load(arg(1));
if (isset($node->field_show_zakazhite_text[LANGUAGE_NONE]) && $node->field_show_zakazhite_text[LANGUAGE_NONE][0]['value'] != '') {
$text = $node->field_show_zakazhite_text[LANGUAGE_NONE][0]['value'];
} else {
$text = variable_get('megashow_zakazhite_prazdnik');
}
$data['content'] = zakazat_show_link($text);
}
}
function zakazat_show_link($text) {
$show ='';
if ( arg(1) != '' && is_numeric(arg(1)) && arg(0) == 'node' ) {
$node = node_load(arg(1));
if ($node->type == 'show') {
$show = 'show='.arg(1).'&';
}
}
$html = '<div class="present-order"><p>';
$html .= $text.' &gt;&gt;';
$html .= '<a class="colorbox-node" rel="webform" href="/zakazat-shou-ochen-prosto?'.$show.'width=1100&height=760">Заказать шоу</a>';
$html .= '</p></div>';
return $html;
}
/**
* Implements hook_webform_submission_presave().
*/
function extra_webform_submission_presave($node, &$submission) {
foreach ($submission->data as $k => $value) {
switch ($node->webform['components'][$k]['form_key']) {
case 'nazvanie_shou':
$submission->data[$k][0] = db_select('node', 'n')->condition('n.nid', $value[0])->fields('n', array('title'))->execute()->fetchField();
break;
case 'udalennost_ot_mkad':
$submission->data[$k][0] = db_select('taxonomy_term_data', 't')->condition('t.tid', $value[0])->fields('t', array('name'))->execute()->fetchField();
break;
}
}
}
/**
* Implements hook_views_pre_render().
*/
function extra_views_pre_render(&$view) {
if ($view->name == 'show') {
if (count($view->result) > 0) {
foreach ($view->result as $i => $val) {
if (isset($val->field_field_show_price_duration_base[0]['raw']['second'])) {
$view->result[$i]->field_field_show_price_duration_base[0]['raw']['second'] = number_format($val->field_field_show_price_duration_base[0]['raw']['second'], 0, '', ' ');
}
}
}
}
}
/**
* Implements hook_views_pre_build().
*/
function extra_views_pre_build(&$view) {
if ($view->name == 'show' && $view->vid == '12' && ($view->current_display == 'panel_pane_1' || $view->current_display == 'show_front_2')&& isset($view->args[0])) {
if ( nodequeue_load_queue_by_name('cat_'.$view->args[0]) ) {
if (isset($view->relationship['nodequeue_rel'])) {
unset($view->relationship['nodequeue_rel']->options['names']);
$view->relationship['nodequeue_rel']->options['names']['cat_' . $view->args[0]] = 'cat_' . $view->args[0];
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment