Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.21 KB | None | 0 0
  1. <?php
  2.  
  3. function calculator($form, &$form_state) {
  4. $form = array();
  5. $form['#attributes']['class'] = 'calc__form-order';
  6.  
  7. $form['#prefix'] = '<div id="calculator"><div class="calc__wrap"><h3 class="calc__title">Калькулятор аренды</h3>';
  8. $form['#suffix'] = '</div></div>';
  9.  
  10. $form['#attached']['css'][] = drupal_get_path('theme', 'clean_theme') . '/css/calculator.css';
  11.  
  12. $form['#attached']['js'][] = drupal_get_path('theme', 'clean_theme') . '/js/ion.js';
  13. $form['#attached']['js'][] = drupal_get_path('theme', 'clean_theme') . '/js/calculator.js';
  14.  
  15. $price = '609000';
  16.  
  17. $form['price'] = array(
  18. '#prefix' => '<div class="calc__form-order-block"><span class="calc__form-order-subtitle">Стоимость техники</span><div class="clearfix">',
  19. '#suffix' => '<span class="valute">Руб.</span></div></div>',
  20. '#type' => 'textfield',
  21. '#required' => TRUE,
  22. '#size' => 10,
  23. '#default_value' => $price,
  24. '#weight' => 2,
  25. );
  26. if(isset($form_state['input']['price'])){
  27. $current_price = preg_replace("/[^0-9]/", "",$form_state['input']['price']);
  28. } else {
  29. $current_price = $price;
  30. }
  31. $percent = !empty($form_state['input']) ? $form_state['input']['slider'] : '20';
  32.  
  33. $payment = $current_price / 100 * $percent;
  34. if(!empty($form_state['input'])){
  35. $payment = $form_state['input']['payment'];
  36. }
  37. if($payment <= $current_price / 5){
  38. $payment = $current_price/5;
  39. }
  40. if($payment >= $current_price/2){
  41. $payment = $current_price/5;
  42. }
  43.  
  44. if(!empty($form_state['input'])){
  45. $temp_price = $payment;
  46. $temp_percent = $temp_price/$current_price*100;
  47. if($percent != $temp_percent){
  48. $percent = $temp_percent;
  49. }
  50. }
  51.  
  52.  
  53. $slider = '<div class="procent-slider" data-current="'.$percent.'">
  54. <div class="clearfix">
  55. <div class="left">От 20%</div>
  56. <div class="right">До 50%</div>
  57. </div>
  58. <div class="procent-slider__step clearfix">
  59. <span></span>
  60. <span></span>
  61. <span></span>
  62. <span></span>
  63. </div>
  64. <div id="procent-slider"></div>
  65. </div>';
  66.  
  67. $form['payment'] = array(
  68. '#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">',
  69. '#suffix' => '</div>'.$slider.'</div>',
  70. '#type' => 'textfield',
  71. '#required' => TRUE,
  72. '#size' => 3,
  73. '#value' => $payment,
  74. '#weight' => 3,
  75. );
  76. $form['slider'] = array(
  77. '#type' => 'hidden',
  78. '#value' => !empty($form_state['input']) ? $form_state['input']['slider'] : '20',
  79. '#attributes' => array('class' => array('hidden_slider_value')),
  80.  
  81. );
  82. $form['duration'] = array(
  83. '#prefix' => '<div class="calc__form-order-block no_pad_bottom"><span class="calc__form-order-subtitle">Срок аренды</span><div class="calc__form-order-lease clearfix">',
  84. '#suffix' => '</div></div>',
  85. '#type' => 'radios',
  86. '#default_value' => 12,
  87. '#options' => array(6 => t('6 месяцев'), 12 => t('1 год')),
  88. '#weight' => 4,
  89. );
  90.  
  91. $form['contacts'] = array(
  92. '#weight' => 7,
  93. '#prefix' => '<div class="contacts" id="contacts"><a class="send-btn" href="#"><span>Отправить заявку</span></a><div class="calc__form-order-wrap">
  94. <div class="calc__form-order_content">
  95. <h3 class="calc__form-order-title">Оформление заявки</h3>',
  96. '#suffix' => '</div></div>',
  97. );
  98. $form['contacts']['name'] = array(
  99. '#prefix' => '<div class="form-row">',
  100. '#suffix' => '</div>',
  101. '#type' => 'textfield',
  102. '#title' => 'Имя',
  103. '#attributes' => array('placeholder' => t('Ваше имя')),
  104. '#hidden' => TRUE
  105. );
  106. $form['contacts']['phone'] = array(
  107. '#prefix' => '<div class="form-row">',
  108. '#suffix' => '</div>',
  109. '#type' => 'textfield',
  110. '#title' => 'Телефон',
  111. '#attributes' => array('placeholder' => t('+7-xxx-xx-xx'))
  112. );
  113. $form['contacts']['submit'] = array(
  114. '#prefix' => '<div class="clearfix"><div class="btn-holder">',
  115. '#suffix' => '</div></div>',
  116. '#type' => 'submit',
  117. '#value' => 'Отправить заявку',
  118. '#ajax' => array(
  119. 'callback' => 'send_contacts_form',
  120. 'wrapper' => 'contacts',
  121. 'progress' => array('type' => 'none'),
  122. ),
  123. );
  124.  
  125. $form['submit'] = array(
  126. '#prefix' => '<div class="form-row clearfix">',
  127. '#suffix' => '</div>',
  128. '#type' => 'submit',
  129. '#name' => 'calculate',
  130. '#value' => t('Рассчитать'),
  131. '#weight' => 5,
  132. '#ajax' => array(
  133. 'callback' => 'calculator_callback',
  134. 'wrapper' => 'calculator',
  135. 'method' => 'replace',
  136. 'effect' =>'fade',
  137. 'progress' => array('type' => 'none'),
  138. ),
  139. );
  140.  
  141. return $form;
  142. }
  143.  
  144. /**
  145. * AJAX callback
  146. */
  147. function calculator_callback($form, &$form_state) {
  148.  
  149. $price = preg_replace("/[^0-9]/", "",$form_state['input']['price']);
  150. $payment = preg_replace("/[^0-9]/", "",$form_state['input']['payment']);
  151. $duration = preg_replace("/[^0-9]/", "",$form_state['input']['duration']);
  152. $slider = preg_replace("/[^0-9]/", "",$form_state['input']['slider']);
  153.  
  154. $response = '';
  155. if(!is_numeric($price) || !is_numeric($payment)){
  156. $response = '<p>Укажите верные данные.</p>';
  157. } elseif($payment > $price/2 || $payment < $price / 5){
  158.  
  159. $temp = $payment/$price*100;
  160. $form['payment']['#value'] = $payment;
  161.  
  162. if($payment > $price/2){
  163. $form['payment']['#value'] = $price/2;
  164. }
  165. if($payment < $price / 5){
  166. $form['payment']['#value'] = $price/5;
  167. }
  168. $response = '<p class="message">Первоначальный взнос может быть только в диопазоне 20%-50% от стоимости техники </p>';
  169. } else {
  170. $form['payment']['#value'] = $payment;
  171. $form['slider']['#value'] = $slider;
  172. $percent = $payment/$price;
  173. $monthly = $price * 0.05;
  174. if($duration == '6'){
  175. $tobuy = ($price*.65)-($payment-($price*.2));
  176. } else {
  177. $tobuy = ($price*.5)-($payment-($price*.2));
  178. }
  179. $_SESSION['gl_pno_calculator']['global'] = $form_state['input'];
  180. $_SESSION['gl_pno_calculator']['monthly'] = $monthly;
  181. $_SESSION['gl_pno_calculator']['tobuy'] = $tobuy;
  182.  
  183. $response = '<div class="calc__form-order-block resume">
  184. <div class="calc__form-order-calculation">
  185. <div class="item item-price clearfix"> <span>Ежемесячный платёж:</span> <strong><em class="calculation-price">'.number_format($monthly, 0, ',', ' ').'</em> Руб.</strong> </div>
  186. <div class="item item-price clearfix"> <span>Выкупная стоимость:</span> <strong><em class="calculation-price">'.number_format($tobuy, 0, ',', ' ').'</em> Руб.</strong> </div>
  187. <div class="item clearfix"> <span>Срок аренды:</span> <strong>'.$duration.' месяцев</strong> </div>
  188. <div class="print"> <a href="/calculator/print" target="_blankmPDF">Распечатать расчёт</a> </div>
  189. </div>
  190. </div>';
  191. $form_state['rebuild'] = TRUE;
  192.  
  193. }
  194. $form['response'] = array(
  195. '#weight' => 6,
  196. '#markup' => $response,
  197. );
  198.  
  199. return $form;
  200.  
  201. //return $out;
  202. }
  203.  
  204.  
  205. function send_contacts_form($form, $form_state) {
  206. $values = $form_state['input'];
  207.  
  208. if($values['name'] == '' || $values['phone'] == ''){
  209. return '<div class="calc__form-order-wrap active"><div class="calc__form-order_success">
  210. <h3 class="calc__form-order-title">Неверно указанные данны!</h3>
  211. <p>Пожалуйста, укажите имя и телефон!</p>
  212. </div></div>';
  213. }
  214.  
  215. $price = $values['price'];
  216. $payment = $values['payment'];
  217. $duration = $values['duration'];
  218. $name = $values['name'];
  219. $phone = $values['phone'];
  220.  
  221.  
  222.  
  223. $calc = $_SESSION['gl_pno_calculator'];
  224.  
  225. $body = '<p><strong>Заявка на аренду техники</strong><br>
  226. Стоимость техники: '.$price.'<br>
  227. Первоначальный взнос: '.$payment.'<br>
  228. Срок аренды: '.$duration.'</p>
  229. <p><strong>Результаты расчетов</strong><br>
  230. Ежемесячный платеж: '.number_format($calc['monthly'], 0, ',', ' ').' Руб<br>
  231. Выкупная стоимость: '.number_format($calc['tobuy'], 0, ',', ' ').' Руб.</p>
  232. <p><strong>Контакты пользователя</strong><br>
  233. Имя: '.$name.'<br>
  234. Телефон: '.$phone.'</p>';
  235.  
  236. $message = array(
  237. 'to' => 'info@gl-pno.ru, ilienko@gl-pno.ru, bushin@gl-pno.ru, dgastudio@gmail.com',
  238. 'subject' => 'Заявка на аренду техники (ждет звонка в течении 15 минут).',
  239. 'body' => $body,
  240. 'headers' => array(
  241. 'From' => variable_get('site_mail', 'me@localhost.com'),
  242. 'MIME-Version' => '1.0',
  243. 'Content-Type' => 'text/html;charset=utf-8',),
  244. );
  245. $system = drupal_mail_system('fotoblot_custom', 'offer');
  246. if ($system->mail($message)) {
  247. return '<div class="calc__form-order-wrap active"><div class="calc__form-order_success">
  248. <h3 class="calc__form-order-title">Заявка успешно отправлена</h3>
  249. <img alt="" src="/sites/all/themes/clean_theme/images/img-success.png">
  250. <p>Ваша заявка успешно отправлена, в ближайшее время наш менеджер свяжется с вами.</p>
  251. </div></div>';
  252. }
  253. else {
  254. return 'There was an error sending your email';
  255. }
  256.  
  257. }
  258.  
  259.  
  260. function calculator_print(){
  261. $values = $_SESSION['gl_pno_calculator']['data'];
  262. $html = '<p>проверка<p>'.$values['price'].'<p>';
  263.  
  264. module_load_include('php', 'custom', 'mpdf/mpdf');
  265.  
  266. $mpdf = new mPDF('utf-8', 'A4', '8', '', 10, 10, 7, 7, 10, 10);
  267. /*
  268. $stylesheet = file_get_contents($dir.'/css/css.css');
  269. $mpdf->WriteHTML($stylesheet,1); // The parameter 1 tells that this is css/style only and no body/html/text
  270. */
  271. $mpdf->list_indent_first_level = 0;
  272. $mpdf->WriteHTML($html, 2);
  273. $mpdf->Output();
  274. }
  275.  
  276. /**
  277. * Implementation of hook_mail().
  278. */
  279. function custom_mail($key, &$message, $params){
  280.  
  281. // Set the mail content type to html to send an html e-mail (optional).
  282. $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
  283.  
  284. // Grab the subject and body from params and add it to the message.
  285. $message['subject'] = $params['subject'];
  286. $message['body'] = $params['body'];
  287.  
  288. if($key == 'custom_html') {
  289. $headers = array(
  290. 'MIME-Version' => '1.0',
  291. 'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
  292. 'Content-Transfer-Encoding' => '8Bit',
  293. 'X-Mailer' => 'Drupal'
  294. );
  295. foreach ($headers as $key => $value) {
  296. $message['headers'][$key] = $value;
  297. }
  298. $message['subject'] = $params['subject'];
  299. $message['body'] = array();
  300. $message['body'] = $params['body'];
  301. }
  302.  
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement