Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function calculator($form, &$form_state) {
- $form = array();
- $form['#attributes']['class'] = 'calc__form-order';
- $form['#prefix'] = '<div id="calculator"><div class="calc__wrap"><h3 class="calc__title">Калькулятор аренды</h3>';
- $form['#suffix'] = '</div></div>';
- $form['#attached']['css'][] = drupal_get_path('theme', 'clean_theme') . '/css/calculator.css';
- $form['#attached']['js'][] = drupal_get_path('theme', 'clean_theme') . '/js/ion.js';
- $form['#attached']['js'][] = drupal_get_path('theme', 'clean_theme') . '/js/calculator.js';
- $price = '609000';
- $form['price'] = array(
- '#prefix' => '<div class="calc__form-order-block"><span class="calc__form-order-subtitle">Стоимость техники</span><div class="clearfix">',
- '#suffix' => '<span class="valute">Руб.</span></div></div>',
- '#type' => 'textfield',
- '#required' => TRUE,
- '#size' => 10,
- '#default_value' => $price,
- '#weight' => 2,
- );
- if(isset($form_state['input']['price'])){
- $current_price = preg_replace("/[^0-9]/", "",$form_state['input']['price']);
- } else {
- $current_price = $price;
- }
- $percent = !empty($form_state['input']) ? $form_state['input']['slider'] : '20';
- $payment = $current_price / 100 * $percent;
- if(!empty($form_state['input'])){
- $payment = $form_state['input']['payment'];
- }
- if($payment <= $current_price / 5){
- $payment = $current_price/5;
- }
- if($payment >= $current_price/2){
- $payment = $current_price/5;
- }
- if(!empty($form_state['input'])){
- $temp_price = $payment;
- $temp_percent = $temp_price/$current_price*100;
- if($percent != $temp_percent){
- $percent = $temp_percent;
- }
- }
- $slider = '<div class="procent-slider" data-current="'.$percent.'">
- <div class="clearfix">
- <div class="left">От 20%</div>
- <div class="right">До 50%</div>
- </div>
- <div class="procent-slider__step clearfix">
- <span></span>
- <span></span>
- <span></span>
- <span></span>
- </div>
- <div id="procent-slider"></div>
- </div>';
- $form['payment'] = array(
- '#prefix' => '<div class="calc__form-order-block"><span class="calc__form-order-subtitle">Первоначальный взнос</span><div class="calc__form-order-text">От 20% до 50% от стоимости техники</div><div class="clearfix">',
- '#suffix' => '</div>'.$slider.'</div>',
- '#type' => 'textfield',
- '#required' => TRUE,
- '#size' => 3,
- '#value' => $payment,
- '#weight' => 3,
- );
- $form['slider'] = array(
- '#type' => 'hidden',
- '#value' => !empty($form_state['input']) ? $form_state['input']['slider'] : '20',
- '#attributes' => array('class' => array('hidden_slider_value')),
- );
- $form['duration'] = array(
- '#prefix' => '<div class="calc__form-order-block no_pad_bottom"><span class="calc__form-order-subtitle">Срок аренды</span><div class="calc__form-order-lease clearfix">',
- '#suffix' => '</div></div>',
- '#type' => 'radios',
- '#default_value' => 12,
- '#options' => array(6 => t('6 месяцев'), 12 => t('1 год')),
- '#weight' => 4,
- );
- $form['contacts'] = array(
- '#weight' => 7,
- '#prefix' => '<div class="contacts" id="contacts"><a class="send-btn" href="#"><span>Отправить заявку</span></a><div class="calc__form-order-wrap">
- <div class="calc__form-order_content">
- <h3 class="calc__form-order-title">Оформление заявки</h3>',
- '#suffix' => '</div></div>',
- );
- $form['contacts']['name'] = array(
- '#prefix' => '<div class="form-row">',
- '#suffix' => '</div>',
- '#type' => 'textfield',
- '#title' => 'Имя',
- '#attributes' => array('placeholder' => t('Ваше имя')),
- '#hidden' => TRUE
- );
- $form['contacts']['phone'] = array(
- '#prefix' => '<div class="form-row">',
- '#suffix' => '</div>',
- '#type' => 'textfield',
- '#title' => 'Телефон',
- '#attributes' => array('placeholder' => t('+7-xxx-xx-xx'))
- );
- $form['contacts']['submit'] = array(
- '#prefix' => '<div class="clearfix"><div class="btn-holder">',
- '#suffix' => '</div></div>',
- '#type' => 'submit',
- '#value' => 'Отправить заявку',
- '#ajax' => array(
- 'callback' => 'send_contacts_form',
- 'wrapper' => 'contacts',
- 'progress' => array('type' => 'none'),
- ),
- );
- $form['submit'] = array(
- '#prefix' => '<div class="form-row clearfix">',
- '#suffix' => '</div>',
- '#type' => 'submit',
- '#name' => 'calculate',
- '#value' => t('Рассчитать'),
- '#weight' => 5,
- '#ajax' => array(
- 'callback' => 'calculator_callback',
- 'wrapper' => 'calculator',
- 'method' => 'replace',
- 'effect' =>'fade',
- 'progress' => array('type' => 'none'),
- ),
- );
- return $form;
- }
- /**
- * AJAX callback
- */
- function calculator_callback($form, &$form_state) {
- $price = preg_replace("/[^0-9]/", "",$form_state['input']['price']);
- $payment = preg_replace("/[^0-9]/", "",$form_state['input']['payment']);
- $duration = preg_replace("/[^0-9]/", "",$form_state['input']['duration']);
- $slider = preg_replace("/[^0-9]/", "",$form_state['input']['slider']);
- $response = '';
- if(!is_numeric($price) || !is_numeric($payment)){
- $response = '<p>Укажите верные данные.</p>';
- } elseif($payment > $price/2 || $payment < $price / 5){
- $temp = $payment/$price*100;
- $form['payment']['#value'] = $payment;
- if($payment > $price/2){
- $form['payment']['#value'] = $price/2;
- }
- if($payment < $price / 5){
- $form['payment']['#value'] = $price/5;
- }
- $response = '<p class="message">Первоначальный взнос может быть только в диопазоне 20%-50% от стоимости техники </p>';
- } else {
- $form['payment']['#value'] = $payment;
- $form['slider']['#value'] = $slider;
- $percent = $payment/$price;
- $monthly = $price * 0.05;
- if($duration == '6'){
- $tobuy = ($price*.65)-($payment-($price*.2));
- } else {
- $tobuy = ($price*.5)-($payment-($price*.2));
- }
- $_SESSION['gl_pno_calculator']['global'] = $form_state['input'];
- $_SESSION['gl_pno_calculator']['monthly'] = $monthly;
- $_SESSION['gl_pno_calculator']['tobuy'] = $tobuy;
- $response = '<div class="calc__form-order-block resume">
- <div class="calc__form-order-calculation">
- <div class="item item-price clearfix"> <span>Ежемесячный платёж:</span> <strong><em class="calculation-price">'.number_format($monthly, 0, ',', ' ').'</em> Руб.</strong> </div>
- <div class="item item-price clearfix"> <span>Выкупная стоимость:</span> <strong><em class="calculation-price">'.number_format($tobuy, 0, ',', ' ').'</em> Руб.</strong> </div>
- <div class="item clearfix"> <span>Срок аренды:</span> <strong>'.$duration.' месяцев</strong> </div>
- <div class="print"> <a href="/calculator/print" target="_blankmPDF">Распечатать расчёт</a> </div>
- </div>
- </div>';
- $form_state['rebuild'] = TRUE;
- }
- $form['response'] = array(
- '#weight' => 6,
- '#markup' => $response,
- );
- return $form;
- //return $out;
- }
- function send_contacts_form($form, $form_state) {
- $values = $form_state['input'];
- if($values['name'] == '' || $values['phone'] == ''){
- return '<div class="calc__form-order-wrap active"><div class="calc__form-order_success">
- <h3 class="calc__form-order-title">Неверно указанные данны!</h3>
- <p>Пожалуйста, укажите имя и телефон!</p>
- </div></div>';
- }
- $price = $values['price'];
- $payment = $values['payment'];
- $duration = $values['duration'];
- $name = $values['name'];
- $phone = $values['phone'];
- $calc = $_SESSION['gl_pno_calculator'];
- $body = '<p><strong>Заявка на аренду техники</strong><br>
- Стоимость техники: '.$price.'<br>
- Первоначальный взнос: '.$payment.'<br>
- Срок аренды: '.$duration.'</p>
- <p><strong>Результаты расчетов</strong><br>
- Ежемесячный платеж: '.number_format($calc['monthly'], 0, ',', ' ').' Руб<br>
- Выкупная стоимость: '.number_format($calc['tobuy'], 0, ',', ' ').' Руб.</p>
- <p><strong>Контакты пользователя</strong><br>
- Имя: '.$name.'<br>
- Телефон: '.$phone.'</p>';
- $message = array(
- 'to' => 'info@gl-pno.ru, ilienko@gl-pno.ru, bushin@gl-pno.ru, dgastudio@gmail.com',
- 'subject' => 'Заявка на аренду техники (ждет звонка в течении 15 минут).',
- 'body' => $body,
- 'headers' => array(
- 'From' => variable_get('site_mail', 'me@localhost.com'),
- 'MIME-Version' => '1.0',
- 'Content-Type' => 'text/html;charset=utf-8',),
- );
- $system = drupal_mail_system('fotoblot_custom', 'offer');
- if ($system->mail($message)) {
- return '<div class="calc__form-order-wrap active"><div class="calc__form-order_success">
- <h3 class="calc__form-order-title">Заявка успешно отправлена</h3>
- <img alt="" src="/sites/all/themes/clean_theme/images/img-success.png">
- <p>Ваша заявка успешно отправлена, в ближайшее время наш менеджер свяжется с вами.</p>
- </div></div>';
- }
- else {
- return 'There was an error sending your email';
- }
- }
- function calculator_print(){
- $values = $_SESSION['gl_pno_calculator']['data'];
- $html = '<p>проверка<p>'.$values['price'].'<p>';
- module_load_include('php', 'custom', 'mpdf/mpdf');
- $mpdf = new mPDF('utf-8', 'A4', '8', '', 10, 10, 7, 7, 10, 10);
- /*
- $stylesheet = file_get_contents($dir.'/css/css.css');
- $mpdf->WriteHTML($stylesheet,1); // The parameter 1 tells that this is css/style only and no body/html/text
- */
- $mpdf->list_indent_first_level = 0;
- $mpdf->WriteHTML($html, 2);
- $mpdf->Output();
- }
- /**
- * Implementation of hook_mail().
- */
- function custom_mail($key, &$message, $params){
- // Set the mail content type to html to send an html e-mail (optional).
- $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
- // Grab the subject and body from params and add it to the message.
- $message['subject'] = $params['subject'];
- $message['body'] = $params['body'];
- if($key == 'custom_html') {
- $headers = array(
- 'MIME-Version' => '1.0',
- 'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
- 'Content-Transfer-Encoding' => '8Bit',
- 'X-Mailer' => 'Drupal'
- );
- foreach ($headers as $key => $value) {
- $message['headers'][$key] = $value;
- }
- $message['subject'] = $params['subject'];
- $message['body'] = array();
- $message['body'] = $params['body'];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement