Created
February 5, 2018 20:04
-
-
Save rivaadara111/4996e1a91648aa6ad94986f324edc675 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function gs_ads_menu() { | |
$items = array(); | |
$items['admin/bookings/takeovers'] = array( | |
'type' => MENU_CALLBACK, | |
'description' => 'A form for booking ad takeovers.', | |
'page callback' => 'gs_ads_custom_page_display', | |
'access callback' => 'user_access', | |
'access arguments' => array('eck add booking takeover entities'), | |
'file' => 'includes/gs_ads.form.inc' | |
); | |
return $items; | |
} | |
function gs_ads_custom_page_display() { | |
drupal_set_title('Takeovers'); | |
$output = render(drupal_get_form('gs_ads_takeover_form')); | |
$output .= render(views_embed_view('ad_booking_admin','takeovers')); | |
return $output; | |
} | |
/** | |
* Implements hook_preprocess_html(). | |
*/ | |
function gs_ads_preprocess_html(&$vars) { | |
$current_path = current_path(); | |
$go_adfree = FALSE; | |
// Add conditional stylesheets for admin pages on admin theme. | |
if ($current_path == "admin/bookings/takeovers") { | |
$theme_path = drupal_get_path('theme', 'straight2014'); | |
drupal_add_css(drupal_get_path('module', 'gs_ads') . '/css/gs_ads_admin.css', array('weight' => CSS_THEME)); | |
drupal_add_js(drupal_get_path('module', 'gs_ads') . '/js/gs_ads_admin.js', array('weight' => 1)); | |
} | |
if (path_is_admin($current_path) && path_to_theme() == 'themes/seven') { | |
return; | |
} | |
} | |
/****** | |
* form.inc starts here | |
*******/ | |
<?php | |
function gs_ads_takeover_form($form, &$form_state) { | |
$section_options = array(); | |
$section_options['/home'] = t('Home'); | |
$section_options['/news'] = t('News'); | |
$section_options['/music'] = t('Music'); | |
$section_options['/arts'] = t('Arts'); | |
$section_options['/life'] = t('Life'); | |
$section_options['/movies'] = t('Movies'); | |
$section_options['/food'] = t('Food'); | |
$section_options['/cannabis'] = t('Cannabis'); | |
$section_options['/contests'] = t('Contests'); | |
$form['section'] = array( | |
'#title' => 'Section', | |
'#type' => 'select', | |
'#required' => TRUE, | |
'#options' => $section_options, | |
'#empty_option' => '' | |
); | |
$form['tier'] = array( | |
'#title' => 'Tier', | |
'#type' => 'select', | |
'#required' => TRUE, | |
'#options' => array(7 => 7), | |
'#default_value' => 7 | |
); | |
$url = request_uri(); | |
if(strpos($url, '?') !== false){ | |
$parts = parse_url($url); | |
parse_str($parts['query'], $query); | |
$tomorrow = date('Y-m-d', $query['start']); | |
$day_after_tomorrow = date('Y-m-d', $query['end']); | |
} | |
else { | |
$date = date('Y-m-d'); | |
$tomorrow = date('Y-m-d',strtotime($date . "+1 days")); | |
$day_after_tomorrow = date('Y-m-d',strtotime($date . "+2 days")); | |
} | |
$form['start_date'] = array( | |
'#title' => 'Starts', | |
'#type' => 'date_popup', | |
'#date_format' => 'Y/m/d H:i', | |
'#default_value' => $tomorrow, | |
'#date_label_position' => 'within', | |
'#date_year_range' => '-0:+3', | |
'#timepicker' => 'timepicker', | |
'#datepicker_options' => array( | |
'minDate' => 0, | |
), | |
'#timepicker_options' => array( | |
'defaultTime' => '00:00', | |
), | |
'#required' => TRUE | |
); | |
$form['end_date'] = array( | |
'#title' => 'Ends', | |
'#type' => 'date_popup', | |
'#date_format' => 'Y/m/d H:i', | |
'#default_value' => $day_after_tomorrow, | |
'#date_label_position' => 'within', | |
'#date_year_range' => '-0:+3', | |
'#timepicker' => 'timepicker', | |
'#datepicker_options' => array( | |
'minDate' => 1, | |
), | |
'#timepicker_options' => array( | |
'defaultTime' => '00:00', | |
), | |
'#required' => TRUE | |
); | |
$form['note'] = array( | |
'#title' => t("Note"), | |
'#type' => 'textfield', | |
'#size' => 15, | |
'#default_value' => $u_supplied, | |
'#required' => TRUE | |
); | |
$form['submit'] = array( | |
'#type' => 'submit', | |
'#value' => t('Add'), | |
'#attributes' => array('class' => array('button button-red form-actions')), | |
'#submit' => array('gs_ads_takeover_form_submit') | |
); | |
return $form; | |
} | |
function gs_ads_takeover_form_submit($form, &$form_state) { | |
$entity = entity_create('booking', array('type' =>'takeover')); | |
$entity->path = $form_state['values']['section']; | |
$entity->tier = $form_state['values']['tier']; | |
$start_date = strtotime($form_state['values']['start_date']); | |
$entity->start = $start_date; | |
$end_date = strtotime($form_state['values']['end_date']); | |
$entity->end = $end_date; | |
$entity->note = $form_state['values']['note']; | |
$entity->save(); | |
if($entity->id){ | |
drupal_set_message('Your takeover for ' . $form_state['values']['note'] . ' from ' . date('D M d, Y',$start_date) . ' to ' . date('D M d, Y',$end_date) . ' has been booked in section ' . $form_state['values']['section'] . '.','status'); | |
$form_state['redirect'] = array( | |
'/admin/bookings/takeovers', | |
array( | |
'query' => array( | |
'start' => $start_date, | |
'end' => $end_date | |
), | |
), | |
); | |
} | |
else{ | |
drupal_set_message('There was an error saving a new takeover. Please try again.'. 'error'); | |
} | |
} | |
/**** | |
*automagically set end date/time to be 24h from start time | |
*****/ | |
<script> | |
(function ($) { | |
Drupal.behaviors.form_modif = { | |
attach: function (context, settings) { | |
$('input#edit-start-date-datepicker-popup-0').change(function(){ | |
var start_date = $('input#edit-start-date-datepicker-popup-0').val(), | |
d = new Date(start_date), | |
n = d.toLocaleDateString(), | |
day = 86400, | |
end_date = (new Date(n).getTime() / 1000) + day, | |
d2 = new Date(end_date * 1000), | |
yyyy = (d2.getFullYear()), | |
mm = (('0' + (d2.getMonth() + 1)).slice(-2)), | |
dd = (('0' + (d2.getDate())).slice(-2)), | |
date = yyyy + '/' + mm + '/' + dd; | |
$('input#edit-end-date-datepicker-popup-0').val(date); | |
}); | |
} | |
}; | |
}(jQuery)); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment