Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 21.99 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  * Mortgage Calculator module
  6.  */
  7.  
  8. /**
  9.  * Implements hook_menu().
  10.  */
  11. function mortgage_calculator_menu() {
  12.  
  13.   $items['mortgage-calculator'] = array(
  14.     'title' => 'Mortgage calculator',
  15.     'page callback' => 'mortgage_calculator_page',
  16.     'access callback' => TRUE,
  17.     'type' => MENU_CALLBACK,
  18.   );
  19.  
  20.   return $items;
  21. }
  22.  
  23. /**
  24.  * Implements hook_block_info().
  25.  */
  26. function mortgage_calculator_block_info() {
  27.  
  28.   $blocks['mortgage_calculator_1'] = array(
  29.     'info' => t('Mortgage Calculator'),
  30.     'cache' => DRUPAL_CACHE_PER_PAGE,
  31.     'weight' => -11,
  32.     'status' => BLOCK_CUSTOM_ENABLED,
  33.     'region' => 'content',
  34.     'visibility' => BLOCK_VISIBILITY_LISTED,
  35.     'pages' => 'mortgage-calculator',
  36.   );
  37.   $blocks['mortgage_calculator_js_1'] = array(
  38.     'info' => t('Mortgage Calculator JS'),
  39.     'cache' => DRUPAL_CACHE_PER_PAGE,
  40.   );
  41.  
  42.   return $blocks;
  43. }
  44.  
  45. /**
  46.  * Implements hook_block_view().
  47.  */
  48. function mortgage_calculator_block_view($delta = '') {
  49.   $block = array();
  50.  
  51.   switch ($delta) {
  52.  
  53.     case 'mortgage_calculator_1':
  54.       $block['subject'] = t('Mortgage Calculator');
  55.       $form = drupal_get_form('mortgage_calculator_form');
  56.  
  57.       $block['content'] = $form;
  58.       break;
  59.  
  60.     case 'mortgage_calculator_js_1':
  61.       $block['subject'] = t('Mortgage Calculator');
  62.       $form = drupal_get_form('mortgage_calculator_js_form');
  63.       $block['content'] = $form;
  64.       break;
  65.   }
  66.   return $block;
  67. }
  68.  
  69. /**
  70.  * Implements hook_block_configure().
  71.  */
  72. function mortgage_calculator_block_configure($delta = '') {
  73.  
  74.   // todo: add supporting entity tokens https://api.drupal.org/api/examples/token_example!token_example.module/function/token_example_example_form/7
  75.  
  76.   $form = array();
  77.   if ($delta == 'mortgage_calculator_1') {
  78.     $form['mortgage_calculator_payment_frequency_list'] = array(
  79.       '#type' => 'checkboxes',
  80.       '#title' => t('Payment Frequency'),
  81.       '#default_value' => variable_get('mortgage_calculator_payment_frequency_list', array('monthly', 'yearly')),
  82.       '#options' => _mortgage_calculator_payment_frequency(),
  83.     );
  84.  
  85.     $form['mc_defaults'] = array(
  86.       '#title' => t('Default values'),
  87.       '#type' => 'fieldset',
  88.       '#collapsible' => FALSE,
  89.       '#collapsed' => FALSE,
  90.     );
  91.     $form['mc_defaults']['mortgage_calculator_config_loan_amount'] = array(
  92.       '#type' => 'textfield',
  93.       '#title' => t('Mortgage'),
  94.       '#default_value' => variable_get('mortgage_calculator_config_loan_amount', ''),
  95.       '#size' => 50,
  96.     );
  97.     $form['mc_defaults']['mortgage_calculator_config_mortgage_rate'] = array(
  98.       '#type' => 'textfield',
  99.       '#title' => t('Interest Rate'),
  100.       '#default_value' => variable_get('mortgage_calculator_config_mortgage_rate', ''),
  101.       '#size' => 50,
  102.     );
  103.     $form['mc_defaults']['mortgage_calculator_config_years_to_pay'] = array(
  104.       '#type' => 'textfield',
  105.       '#title' => t('Amortization Years'),
  106.       '#default_value' => variable_get('mortgage_calculator_config_years_to_pay', ''),
  107.       '#size' => 50,
  108.     );
  109.     $form['mc_defaults']['mortgage_calculator_config_term'] = array(
  110.       '#type' => 'textfield',
  111.       '#title' => t('Term Years'),
  112.       '#default_value' => variable_get('mortgage_calculator_config_term', ''),
  113.       '#size' => 50,
  114.     );
  115.     $form['mc_defaults']['mortgage_calculator_compounding'] = array(
  116.       '#type' => 'textfield',
  117.       '#title' => t('Compounding'),
  118.       '#default_value' => variable_get('mortgage_calculator_config_compounding', ''),
  119.       '#size' => 50,
  120.     );
  121.   }
  122.   elseif ($delta == 'mortgage_calculator_js_1') {
  123.     $form['mortgage_calculator_js_payment_frequency_list'] = array(
  124.       '#type' => 'checkboxes',
  125.       '#title' => t('Payment Frequency'),
  126.       '#default_value' => variable_get('mortgage_calculator_js_payment_frequency_list', array('monthly')),
  127.       '#options' => _mortgage_calculator_payment_frequency(),
  128.     );
  129.     $form['mc_defaults'] = array(
  130.       '#title' => t('Default values'),
  131.       '#type' => 'fieldset',
  132.       '#collapsible' => FALSE,
  133.       '#collapsed' => FALSE,
  134.     );
  135.     $form['mc_defaults']['mortgage_calculator_js_config_loan_amount_2'] = array(
  136.       '#type' => 'textfield',
  137.       '#title' => t('Mortgage'),
  138.       '#size' => 50,
  139.       '#default_value' => variable_get('mortgage_calculator_js_config_loan_amount_2', ''),
  140.     );
  141.     $form['mc_defaults']['mortgage_calculator_js_config_mortgage_rate_2'] = array(
  142.       '#type' => 'textfield',
  143.       '#title' => t('Interest Rate'),
  144.       '#size' => 50,
  145.       '#default_value' => variable_get('mortgage_calculator_js_config_mortgage_rate_2', ''),
  146.     );
  147.     $form['mc_defaults']['mortgage_calculator_js_config_years_to_pay_2'] = array(
  148.       '#type' => 'textfield',
  149.       '#title' => t('Amortization Years'),
  150.       '#size' => 50,
  151.       '#default_value' => variable_get('mortgage_calculator_js_config_years_to_pay_2', ''),
  152.     );
  153.     $form['mc_defaults']['mortgage_calculator_js_config_term_2'] = array(
  154.       '#type' => 'textfield',
  155.       '#title' => t('Term Years'),
  156.       '#size' => 50,
  157.       '#default_value' => variable_get('mortgage_calculator_js_config_term_2', ''),
  158.     );
  159.     $form['mc_defaults']['mortgage_calculator_js_config_compounding_2'] = array(
  160.       '#type' => 'textfield',
  161.       '#title' => t('Compounding'),
  162.       '#size' => 50,
  163.       '#default_value' => variable_get('mortgage_calculator_js_config_compounding_2', ''),
  164.     );
  165.   }
  166.  
  167.   if (module_exists('token')) {
  168.     $form['mc_defaults']['content'] = array(
  169.       '#token_types' => array('node'),
  170.       '#theme' => 'token_tree',
  171.       '#global_types' => FALSE,
  172.       '#click_insert' => TRUE,
  173.     );
  174.   }
  175.   else {
  176.     $form['mc_defaults']['content'] = array(
  177.       '#markup' => t('For viewing the available tokens, please enable the <a href="@mod_url">Token module</a>.', array('@mod_url' => url('https://drupal.org/project/token'))),
  178.     );
  179.   }
  180.  
  181.   return $form;
  182. }
  183.  
  184. /**
  185.  * Implements hook_block_save().
  186.  */
  187. function mortgage_calculator_block_save($delta = '', $edit = array()) {
  188.   if ($delta == 'mortgage_calculator_1') {
  189.     variable_set('mortgage_calculator_payment_frequency_list', $edit['mortgage_calculator_payment_frequency_list']);
  190.     variable_set('mortgage_calculator_config_loan_amount', $edit['mortgage_calculator_config_loan_amount']);
  191.     variable_set('mortgage_calculator_config_mortgage_rate', $edit['mortgage_calculator_config_mortgage_rate']);
  192.     variable_set('mortgage_calculator_config_years_to_pay', $edit['mortgage_calculator_config_years_to_pay']);
  193.     variable_set('mortgage_calculator_config_term', $edit['mortgage_calculator_config_term']);
  194.     variable_set('mortgage_calculator_config_compounding', $edit['mortgage_calculator_config_compounding']);
  195.   }
  196.   elseif ($delta == 'mortgage_calculator_js_1') {
  197.     variable_set('mortgage_calculator_js_payment_frequency_list', $edit['mortgage_calculator_js_payment_frequency_list']);
  198.     variable_set('mortgage_calculator_js_config_loan_amount_2', $edit['mortgage_calculator_js_config_loan_amount_2']);
  199.     variable_set('mortgage_calculator_js_config_mortgage_rate_2', $edit['mortgage_calculator_js_config_mortgage_rate_2']);
  200.     variable_set('mortgage_calculator_js_config_years_to_pay_2', $edit['mortgage_calculator_js_config_years_to_pay_2']);
  201.     variable_set('mortgage_calculator_js_config_term_2', $edit['mortgage_calculator_js_config_term_2']);
  202.     variable_set('mortgage_calculator_js_config_compounding_2', $edit['mortgage_calculator_js_config_compounding_2']);
  203.   }
  204. }
  205.  
  206. /**
  207.  * Helper function for getting a payment frequency array by an index.
  208.  *
  209.  * @param array $types
  210.  *   an array of selected types
  211.  *
  212.  * @return array
  213.  *   a payment frequency array
  214.  */
  215. function _mortgage_calculator_payment_frequency($types = array()) {
  216.  
  217.   $payments = array(
  218.     'monthly' => t('Monthly'),
  219.     'yearly' => t('Yearly'),
  220.     'semi-monthly' => t('Semi-monthly'),
  221.     'bi-weekly' => t('Bi-weekly'),
  222.     'weekly' => t('Weekly'),
  223.     'accelerated_bi-weekly' => t('Accelerated Bi-weekly'),
  224.     'accelerated_weekly' => t('Accelerated Weekly'),
  225.   );
  226.  
  227.   if (empty($types)) {
  228.     return $payments;
  229.   }
  230.   else {
  231.     return array_intersect_key($payments, array_flip($types));
  232.   }
  233. }
  234.  
  235.  
  236. /**
  237.  * Form for a mortgage calculator.
  238.  */
  239. function mortgage_calculator_form($form_state) {
  240.   $node = menu_get_object();
  241.  
  242.   $loan_amount = isset($_SESSION['mortgage_calculator_loan_amount']) ? $_SESSION['mortgage_calculator_loan_amount'] : 0;
  243.   $mortgage_rate = isset($_SESSION['mortgage_calculator_mortgage_rate']) ? $_SESSION['mortgage_calculator_mortgage_rate'] : 0;
  244.   $years_to_pay = isset($_SESSION['mortgage_calculator_years_to_pay']) ? $_SESSION['mortgage_calculator_years_to_pay'] : 0;
  245.   $desired_display = isset($_SESSION['mortgage_calculator_desired_display']) ? $_SESSION['mortgage_calculator_desired_display'] : 'monthly';
  246.  
  247.   $form['mortgage_calculator_loan_amount'] = array(
  248.     '#type' => 'textfield',
  249.     '#title' => t('Mortgage'),
  250.     '#default_value' => variable_get('mortgage_calculator_config_loan_amount', '') ? token_replace(variable_get('mortgage_calculator_config_loan_amount', '0'), array('node' => $node)) : $loan_amount,
  251.     '#size' => 10,
  252.     '#maxlength' => 64,
  253.   );
  254.   $form['mortgage_calculator_mortgage_rate'] = array(
  255.     '#type' => 'textfield',
  256.     '#title' => t('Interest Rate'),
  257.     '#default_value' => variable_get('mortgage_calculator_config_mortgage_rate', '') ? token_replace(variable_get('mortgage_calculator_config_mortgage_rate', '0'), array('node' => $node)) : $mortgage_rate,
  258.     '#size' => 10,
  259.     '#maxlength' => 64,
  260.   );
  261.   $form['mortgage_calculator_years_to_pay'] = array(
  262.     '#type' => 'textfield',
  263.     '#title' => t('Amortization Years'),
  264.     '#default_value' => variable_get('mortgage_calculator_config_years_to_pay', '') ? token_replace(variable_get('mortgage_calculator_config_years_to_pay', '0'), array('node' => $node)) : $years_to_pay,
  265.     '#size' => 10,
  266.     '#maxlength' => 64,
  267.   );
  268.   $form['mortgage_calculator_term'] = array(
  269.     '#type' => 'textfield',
  270.     '#title' => t('Term Years'),
  271.     '#default_value' => variable_get('mortgage_calculator_config_term', '') ? token_replace(variable_get('mortgage_calculator_config_term', '0'), array('node' => $node)) : $term,
  272.     '#size' => 10,
  273.     '#maxlength' => 64,
  274.   );
  275.   $form['mortgage_calculator_compounding'] = array(
  276.     '#type' => 'textfield',
  277.     '#title' => t('Compounding'),
  278.     '#default_value' => variable_get('mortgage_calculator_config_compounding', '') ? token_replace(variable_get('mortgage_calculator_config_compounding', '0'), array('node' => $node)) : $compounding,
  279.     '#size' => 10,
  280.     '#maxlength' => 64,
  281.   );
  282.   $form['mortgage_calculator_desired_display'] = array(
  283.     '#type' => 'select',
  284.     '#title' => t('Payment Schedule'),
  285.     '#default_value' => $desired_display,
  286.     '#options' => _mortgage_calculator_payment_frequency(variable_get('mortgage_calculator_payment_frequency_list', array(
  287.       'monthly',
  288.       'yearly',
  289.     ))),
  290.     '#description' => t(''),
  291.   );
  292.   $form['submit'] = array(
  293.     '#type' => 'submit',
  294.     '#value' => t('Calculate'),
  295.   );
  296.   return $form;
  297. }
  298.  
  299. /**
  300.  * Form for a mortgage calculator - validation function.
  301.  */
  302. function mortgage_calculator_form_validate($form, &$form_state) {
  303.   if ($form_state['values']['mortgage_calculator_loan_amount'] == '' || $form_state['values']['mortgage_calculator_loan_amount'] <= 0) {
  304.     form_set_error('mortgage_calculator_loan_amount', t('Please enter a value of loan amount.'));
  305.   }
  306.   if ($form_state['values']['mortgage_calculator_mortgage_rate'] == '' || $form_state['values']['mortgage_calculator_mortgage_rate'] <= 0) {
  307.     form_set_error('mortgage_calculator_mortgage_rate', t('Please enter a value of mortgage rate.'));
  308.   }
  309.   if ($form_state['values']['mortgage_calculator_years_to_pay'] == '' || $form_state['values']['mortgage_calculator_years_to_pay'] <= 0) {
  310.     form_set_error('mortgage_calculator_years_to_pay', t('Please enter a value of years to pay.'));
  311.   }
  312.  
  313. }
  314.  
  315. /**
  316.  * Form for a mortgage calculator - submit function.
  317.  */
  318. function mortgage_calculator_form_submit($form, &$form_state) {
  319.   foreach ($form_state['values'] as $key => $value) {
  320.     $_SESSION[$key] = $value;
  321.   }
  322.   $form_state['redirect'] = 'mortgage-calculator';
  323. }
  324.  
  325. /**
  326.  * Mortgage calculator page.
  327.  */
  328. function mortgage_calculator_page() {
  329.   $output = '';
  330.  
  331.   if (isset($_SESSION['mortgage_calculator_loan_amount']) & isset($_SESSION['mortgage_calculator_mortgage_rate']) & isset($_SESSION['mortgage_calculator_years_to_pay']) &  isset($_SESSION['mortgage_calculator_desired_display'])) {
  332.     $theme_values = array(
  333.       'loan_amount' => $_SESSION['mortgage_calculator_loan_amount'],
  334.       'mortgage_rate' => $_SESSION['mortgage_calculator_mortgage_rate'],
  335.       'years_to_pay' => $_SESSION['mortgage_calculator_years_to_pay'],
  336.       'term' => $_SESSION['mortgage_calculator_term'],
  337.       'compounding' => $_SESSION['mortgage_calculator_compounding'],
  338.       'desired_display' => $_SESSION['mortgage_calculator_desired_display'],
  339.     );
  340.  
  341.     $output = theme('mortgage_calculator', $theme_values);
  342.   }
  343.  
  344.   return $output;
  345. }
  346.  
  347. /**
  348.  * Implements hook_theme().
  349.  */
  350. function mortgage_calculator_theme($existing, $type, $theme, $path) {
  351.   return array(
  352.     'mortgage_calculator' => array(
  353.       'variables' => array(
  354.         'loan_amount' => NULL,
  355.         'mortgage_rate' => NULL,
  356.         'years_to_pay' => NULL,
  357.         'term' => NULL,
  358.         'compounding' => NULL,
  359.         'desired_display' => NULL,
  360.       ),
  361.       'template' => 'mortgage-calculator',
  362.     ),
  363.   );
  364. }
  365.  
  366.  
  367. /**
  368.  * Preprocess function.
  369.  */
  370. function template_preprocess_mortgage_calculator(&$variables) {
  371.   $variables += mortgage_calculator_calculation($variables['loan_amount'], $variables['mortgage_rate'], $variables['years_to_pay'], $variables['term'], $variables['compounding'],  $variables['desired_display']);
  372.  
  373.   $header = array(
  374.     t('Month'),
  375.     t('Beginning Balance'),
  376.     t('Payment Amount'),
  377.     t('Principle'),
  378.     t('Interest'),
  379.     t('Ending Balance'),
  380.   );
  381.  
  382.   if ($variables['desired_display'] == 'monthly') {
  383.     $header[0] = t('Month');
  384.   }
  385.   elseif ($variables['desired_display'] == 'yearly') {
  386.     $header[0] = t('Year');
  387.   }
  388.   elseif ($variables['desired_display'] == 'semi-monthly') {
  389.     $header[0] = t('Semi-Month');
  390.   }
  391.   elseif ($variables['desired_display'] == 'bi-weekly') {
  392.     $header[0] = t('Two Week');
  393.   }
  394.   elseif ($variables['desired_display'] == 'weekly') {
  395.     $header[0] = t('Week');
  396.   }
  397.   elseif ($variables['desired_display'] == 'accelerated_bi-weekly') {
  398.     $header[0] = t('Two Week');
  399.   }
  400.   elseif ($variables['desired_display'] == 'accelerated_weekly') {
  401.     $header[0] = t('Week');
  402.   }
  403.  
  404.   $variables['header'] = $header;
  405. }
  406.  
  407. /**
  408.  * Function for a mortgage calculation.
  409.  *
  410.  * @param int $loan_amount
  411.  *   a loan amount
  412.  * @param int $mortgage_rate
  413.  *   a mortgage rate
  414.  * @param int $years_to_pay
  415.  *   years to pay
  416.  * @param int $term
  417.  *   term
  418.  * @param int $compounding
  419.  *   compounding
  420.  * @param array $desired_display
  421.  *   possible values 'monthly', 'yearly', 'semi-monthly', 'bi-weekly', 'weekly',
  422.  *   'accelerated_bi-weekly', 'accelerated_weekly'
  423.  *
  424.  * @return array
  425.  *   where a key 'rows' contains array of rows with $desired_display mortgage calculations
  426.  */
  427. function mortgage_calculator_calculation($loan_amount, $mortgage_rate, $years_to_pay, $term,$compounding, $desired_display) {
  428.   if ($desired_display == 'monthly') {
  429.     //$rate_per = ($mortgage_rate / 100) / 12;
  430.     $number_of_payments = $years_to_pay * 12;
  431.   }
  432.   elseif ($desired_display == 'yearly') {
  433.     //$rate_per = $mortgage_rate / 100;
  434.     $number_of_payments = $years_to_pay;
  435.   }
  436.   elseif ($desired_display == 'semi-monthly') {
  437.    // $rate_per = ($mortgage_rate / 100) / 24;
  438.     $number_of_payments = $years_to_pay * 24;
  439.   }
  440.   elseif ($desired_display == 'bi-weekly') {
  441.    // $rate_per = ($mortgage_rate / 100) / 26;
  442.     $number_of_payments = $years_to_pay * 26;
  443.   }
  444.   elseif ($desired_display == 'weekly') {
  445.    // $rate_per = ($mortgage_rate / 100) / 52;
  446.     $number_of_payments = $years_to_pay * 52;
  447.   }
  448.   elseif ($desired_display == 'accelerated_bi-weekly') {
  449.     //$rate_per = ($mortgage_rate / 100) / 24;
  450.     $number_of_payments = $years_to_pay * 26;
  451.   }
  452.   elseif ($desired_display == 'accelerated_weekly') {
  453.    // $rate_per = ($mortgage_rate / 100) / 48;
  454.     $number_of_payments = $years_to_pay * 52;
  455.   }
  456.    $interestPer = $mortgage_rate/100;
  457.     if ($compounding == "Semi-annually"){
  458.       $rate_per = pow((1 + $interestPer/2), (1/6)) - 1;
  459.     } else if ($compounding == "Monthly"){
  460.         $rate_per = pow((1 + $interestPer/12), (1/6)) - 1;
  461.     }
  462.     $payment = (($loan_amount*$rate_per) * pow((1+$rate_per),$number_of_payments)) / (pow((1+$rate_per),$number_of_payments) - 1);
  463.  
  464. /* ($loan_amount * pow(1 + $rate_per, $number_of_payments) * $rate_per) / (pow(1 + $rate_per, $number_of_payments) - 1); */
  465.  
  466.  
  467.   $beginning_balance = $loan_amount;
  468.   for ($i = 1; $i <= $number_of_payments; $i++) {
  469.     $interest = $rate_per * $beginning_balance;
  470.     $principlePaid = $payment - $interest;
  471.     $rows[] = array(
  472.       $i,
  473.       round($beginning_balance, 2),
  474.       round($payment, 2),
  475.       round($principlePaid, 2),
  476.       round($interest, 2),
  477.       abs(round($beginning_balance - ($payment - $interest), 2)),
  478.     );
  479.     $beginning_balance -= $payment - $interest;
  480.   }
  481.  
  482.   return array(
  483.     'rows' => $rows,
  484.     'row' => array(
  485.       'number_of_payments' => $number_of_payments,
  486.       'payment' => $payment,
  487.     )
  488.   );
  489. }
  490.  
  491. /**
  492.  * Form for a mortgage calculator JS.
  493.  */
  494. function mortgage_calculator_js_form($form_state) {
  495.   $node = menu_get_object();
  496.  
  497.   $form['loan_amount_2'] = array(
  498.     '#type' => 'textfield',
  499.     '#title' => t('Mortgage'),
  500.     '#size' => 10,
  501.     '#maxlength' => 64,
  502.     '#default_value' => token_replace(variable_get('mortgage_calculator_js_config_loan_amount_2', ''), array('node' => $node)),
  503.   );
  504.   $form['mortgage_rate_2'] = array(
  505.     '#type' => 'textfield',
  506.     '#title' => t('Interest Rate'),
  507.     '#size' => 10,
  508.     '#maxlength' => 64,
  509.     '#default_value' => token_replace(variable_get('mortgage_calculator_js_config_mortgage_rate_2', ''), array('node' => $node)),
  510.   );
  511.   $form['years_to_pay_2'] = array(
  512.     '#type' => 'textfield',
  513.     '#title' => t('Amortization Years'),
  514.     '#size' => 10,
  515.     '#maxlength' => 64,
  516.     '#default_value' => token_replace(variable_get('mortgage_calculator_js_config_years_to_pay_2', ''), array('node' => $node)),
  517.   );
  518.   $form['term_2'] = array(
  519.     '#type' => 'textfield',
  520.     '#title' => t('Term Years'),
  521.     '#size' => 10,
  522.     '#maxlength' => 64,
  523.     '#default_value' => token_replace(variable_get('mortgage_calculator_js_config_term_2', ''), array('node' => $node)),
  524.   );
  525.   $form['compounding_2'] = array(
  526.     '#type' => 'textfield',
  527.     '#title' => t('Compounding'),
  528.     '#size' => 10,
  529.     '#maxlength' => 64,
  530.     '#default_value' => token_replace(variable_get('mortgage_calculator_js_config_compounding_2', ''), array('node' => $node)),
  531.     '#attributes' => array('readonly' => array('readonly')),
  532.   );
  533.  
  534.   $payment_frequencies = variable_get('mortgage_calculator_js_payment_frequency_list', array('monthly'));
  535.   if (!(count($payment_frequencies) == 1 && $payment_frequencies[0] == 'monthly')) {
  536.     $form['payment_frequency_2'] = array(
  537.       '#type' => 'select',
  538.       '#title' => t('Payment Schedule'),
  539.       '#default_value' => variable_get('payment_frequency_2', 'monthly'),
  540.       '#options' => _mortgage_calculator_payment_frequency($payment_frequencies),
  541.     );
  542.   }
  543.  
  544.   $form['#executes_submit_callback'] = FALSE;
  545.   $form['calculate_2'] = array(
  546.     '#type' => 'button',
  547.     '#value' => t('View Table'),
  548.   );
  549.  
  550.   $form['result_2'] = array(
  551.     '#type' => 'textfield',
  552.     '#title' => check_plain((count($payment_frequencies) == 1 && $payment_frequencies[0] == 'monthly') ? t('Monthly Payment') : t('Payment Amount')),
  553.     '#size' => 10,
  554.     '#maxlength' => 64,
  555.     '#attributes' => array('readonly' => array('readonly')),
  556.   );
  557.  
  558.   $form['#attached']['js'][] = drupal_get_path('module', 'mortgage_calculator') . '/mortgage_calculator.js';
  559.  
  560.   return $form;
  561. }
  562.  
  563. /**
  564.  * Implements hook_filter_info().
  565.  */
  566. function mortgage_calculator_filter_info() {
  567.   $filters['mortgage_calculator_filter'] = array(
  568.     'title' => t('Embed Mortgage Calculator JS'),
  569.     'description' => t('in format [mortgage_calculator_js:PRICE;RATE;YEARS] for JS version'),
  570.     'process callback' => '_mortgage_calculator_filter',
  571.     'tips callback' => '_mortgage_calculator_filter_tips',
  572.     'cache' => FALSE,
  573.   );
  574.   return $filters;
  575. }
  576.  
  577. /**
  578.  * Helper function for the mortgage_calculator_filter_info() hook.
  579.  */
  580. function _mortgage_calculator_filter($text, $filter, $format, $langcode, $cache, $cache_id) {
  581.   if (preg_match_all('/\[mortgage_calculator_js\:(.+)\]/isU', $text, $matches_code)) {
  582.     // For preventing infinite loop.
  583.     static $mortgage_calculator_js;
  584.     if (!$mortgage_calculator_js) {
  585.       $mortgage_calculator_js = TRUE;
  586.  
  587.       $code = $matches_code[0][0];
  588.  
  589.       $values = explode(';', $matches_code[1][0]);
  590.  
  591.       variable_set('loan_amount_2', $values[0]);
  592.       variable_set('mortgage_rate_2', $values[1]);
  593.       variable_set('years_to_pay_2', $values[2]);
  594.       variable_set('term', $values[3]);
  595.       variable_set('compounding_2', $values[4]);
  596.  
  597.       $form = drupal_get_form('mortgage_calculator_js_form');
  598.       $calculator = drupal_render($form);
  599.  
  600.       variable_set('loan_amount_2', '');
  601.       variable_set('mortgage_rate_2', '');
  602.       variable_set('years_to_pay_2', '');
  603.       variable_set('term', '');
  604.       variable_set('compounding_2', '');
  605.  
  606.       $text = str_replace($code, $calculator, $text);
  607.     }
  608.   }
  609.  
  610.   return $text;
  611. }
  612.  
  613. /**
  614.  * Helper function for the mortgage_calculator_filter_info() hook.
  615.  */
  616. function _mortgage_calculator_filter_tips($filter, $format, $long = FALSE) {
  617.   return t('Mortgage Calculator JS can be added inline by [mortgage_calculator_js:PRICE;RATE;YEARS;COMPOUNDING]. For example [mortgage_calculator_js:10000;4.5;10]. More in README.txt');
  618. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement