<?php

/**
 * @file
 * Provide Donate Form Data.
 */

/**
 * Function to display donate form.
 */
function donate_form($form, $form_state) {
  $form = array();
  global $user;
  
  $form['donarname'] = array(
    '#title' => t('Name'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  
  $form['fathername'] = array(
    '#title' => t('Father Name'),
    '#type' => 'textfield',
     
  );
  
  $form['donaremail'] = array(
    '#title' => t('Email'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  
  $form['donarmobileno'] = array(
    '#title' => t('Mobile No'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  
  $form['donarpan'] = array(
    '#title' => t('PAN CARD'),
    '#type' => 'textfield',
    '#description' => t('Mandatory if required 80g certificate'),
  );
  
  $form['address'] = array(
    '#title' => t('Address'),
    '#type' => 'textarea',
     
  );
  
  $form['comment'] = array(
    '#title' => t('Comment'),
    '#type' => 'textarea',
    
  );
  
  $doation_options = array();
  $doation_options["Sponsor Sarv Jvar Har Churna"] = "Sponsor Sarv Jvar Har Churna <a href='https://donategvm.iaseuniversityonline.org/donate/sites/default/files/Brochure.pdf' target='_blank'>Download Brochure</a>";
  //$doation_query = db_query("SELECT name,tid FROM {taxonomy_term_data} where vid = :vid", array(':vid'=> '2'), array('fetch' => PDO::FETCH_ASSOC));
  $doation_query = db_query("SELECT t.name FROM donate_taxonomy_term_data t , donate_field_data_field_show_on_donation_form s where t.vid = :vid and t.tid=s.entity_id and s.field_show_on_donation_form_value=:show ORDER BY t.weight ASC", 
                             array(':vid'=> '2',':show'=> 'Yes'), array('fetch' => PDO::FETCH_ASSOC));
  foreach($doation_query as $doationfor_val){
      $donation_forvalue = $doationfor_val['name'];
      $doation_options[$donation_forvalue] = t($donation_forvalue);
  }
   
  
  if(count($doation_options) > 0){
      $form['donationfor'] = array(
        '#type' => 'radios',
        '#title' => t('Donate'),     
        '#options' => $doation_options,
        '#required' => TRUE,
        '#attributes' => array(             
         ),
        '#id' => 'donate_id', 
        '#prefix' => "<div class='donate-sponsor-div-radio-form'>",
        '#suffix' => "</div>",  
      );
  }else{
      $form['donationfor'] = array(
        '#type' => 'hidden',
        '#title' => t('Donate'), 
      );
  }
  
  //////////////////////////////////////////////////////////////////
   
    
    $form['#attached']['js'] = array(
        drupal_get_path('module', 'donate') . '/donate.js',
    );

  
    $states_options = donate_states();
    $form['in_states'] = array(
        '#type' => 'select',
        '#title' => t('State'),     
        '#options' => $states_options, 
        '#prefix' => "<div class='donate-sarvjhc' style='display:none;'>",
        '#suffix' => "</div>",

    );

    $form['description_churna'] = array(
        '#title' => t('Description'),
        '#type' => 'textarea', 
        '#prefix' => "<div class='donate-sarvjhc' style='display:none;'>",
        '#suffix' => "</div>",
    );

    $form['no_of_pouches'] = array(
        '#title' => t('No of Pouches'),
        '#type' => 'textfield',
        '#description' => t('Enter a numeric value for no of packets to donate'),
        '#prefix' => "<div class='donate-sarvjhc-no-pouch' style='display:none;'>",
        '#suffix' => "</div>", 
         
    );   
   
   
  
  /////////////////////////////////////////////////////////////////
   
  $form['amountdonated'] = array(
    '#title' => t('Enter Amount (in Rs.)'),
    '#type' => 'textfield',
    '#prefix' => "<div class='donate-amount-value' id='donate-amount-value-id'>",
    '#suffix' => "</div>",  
    '#required' => TRUE,
     
  ); 
   
  if($user->uid==1 || $user->uid==9){
      $paymode_query = db_query("SELECT payment_mode FROM {donate_payment_modes} where status_for_admin = :status_for_admin", array(':status_for_admin'=> '1'), array('fetch' => PDO::FETCH_ASSOC));
  }else{
      $paymode_query = db_query("SELECT payment_mode FROM {donate_payment_modes} where status_for_users = :status_for_users", array(':status_for_users'=> '1'), array('fetch' => PDO::FETCH_ASSOC));
  }
  $paymode_options = array();
  foreach($paymode_query as $paymode){
      $paymodeval = $paymode['payment_mode'];
      $paymode_options[$paymodeval] = t($paymodeval);
  }
  $form['paymode'] = array(
    '#type' => 'radios',
    '#title' => t('Payment Mode'),     
    '#options' => $paymode_options,
    '#required' => TRUE,
  );
  
  $form['termscond'] = array(
    '#type' => 'checkbox',
    '#title' => t('Yes, I accept the <a href="https://donategvm.iaseuniversityonline.org/donate/terms-conditions"  target="_blank">Terms and Conditions</a>'),  
    '#options' => array('1'=>'1'),
    '#required' => TRUE,
    '#default_value' => '1', 
  );
   
  $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Donate'),
  );
  
  return $form;
}
  
/**
 * Implements hook_form_validate().
 */
function donate_form_validate($form, &$form_state) {   
    if ($form_state['values']['op'] == t('Donate')) {
        $email = $form_state['values']['donaremail'];
        if (!valid_email_address($email)) {
            form_set_error('[donaremail]', t('The email address appears to be invalid.'));
        }
        $mobileno = $form_state['values']['donarmobileno'];
        if(!empty($mobileno)){
            if (!(is_numeric($mobileno))) {       
                form_set_error('donarmobileno', t('The mobile no appears to be invalid.'));
            }
        }
        $noofpouch = $form_state['values']['no_of_pouches'];
        if(!empty($noofpouch)){
            if (!(is_numeric($noofpouch))) {       
                form_set_error('no_of_pouches', t('Enter numeric value.'));
            }
        }
        $amount_donated = $form_state['values']['amountdonated'];
        if(!empty($amount_donated)){
            if (!(is_numeric($amount_donated))) {       
                form_set_error('amountdonated', t('The entered amount appears to be invalid.'));
            }
        }

    }
}

 

/**
 * Implements hook_form_submit().
 */
function donate_form_submit($form, &$form_state) {   //print_r($form_state['values']); exit;
    
    global $user;
    $amountdonatedon = time();
    
    $receiptstatus = 0;
    $mailstatus    = 0;
    
    $ip_address_of_user = $_SERVER['REMOTE_ADDR'];
    // Save the submitted entry.
    $formnoval = mt_rand(). mt_rand();
    $formno=substr(hash('sha256', $formnoval . microtime()), 0, 10);
    
    if($form_state['values']['paymode']=="Online"){
        $trackid=substr(hash('sha256', mt_rand() . microtime()), 0, 20);
        $online_process_status = "Request Sent";
        $_SESSION['donate_tr']=$trackid; 
        $paymentstatus = 0;
        $paymentdetail = "";
    }else{
        $trackid='';
        $online_process_status='';
        $paymentstatus = 1;
        $paymentdetail = $form_state['values']['paymode']." payment done";
    }
    if($form_state['values']['donationfor']=="Sponsor Sarv Jvar Har Churna"){
        $instate = $form_state['values']['in_states'];
        $descchurn = $form_state['values']['description_churna'];
        $noof_pouch = $form_state['values']['no_of_pouches'];
    }else{
        $instate = '';
        $descchurn = '';
        $noof_pouch = '';
    }
    $entry = array(
        'formno'          => strtoupper($formno),
        'donarname'       => $form_state['values']['donarname'],
        'fathername'      => $form_state['values']['fathername'],
        'donaremail'      => $form_state['values']['donaremail'],
        'donarmobileno'   => $form_state['values']['donarmobileno'],
        'donarpan'        => $form_state['values']['donarpan'],
        'address'         => $form_state['values']['address'],
        'comment'         => $form_state['values']['comment'],
        'donationfor'     => $form_state['values']['donationfor'],
        'amountdonated'   => $form_state['values']['amountdonated'],
        'paymode'         => $form_state['values']['paymode'],
        'amountdonatedon' => $amountdonatedon,
        'paymentdetail'   => $paymentdetail,
        'paymentstatus'   => $paymentstatus,
        'receiptstatus'   => $receiptstatus,
        'mailstatus'      => $mailstatus,  
        'online_trackid'  => $trackid,
        'online_process_status' => $online_process_status,
        'ip_address    '  => $ip_address_of_user,
        'in_states'       => $instate,
        'description_churna' => $descchurn,
        'no_of_pouches'      => $noof_pouch,
    );
     
     
    
    $id = db_insert('donate')
          ->fields($entry)
          ->execute();
    
    if($id){        
        if($form_state['values']['paymode']=="Online"){
            $chk=md5($id);
            
            $query_updated = db_update('donate')  
                             ->fields(array('online_hashcheck' => $chk))
                             ->condition('id', $id, '=')
                             ->execute();
            
            $getterr=base64_encode("$chk");
            $_SESSION['donate_h'] = $getterr;  
            $_SESSION['donate_hash'] = $chk;  	
            drupal_goto("donateform/donateformnext", array('query' => array('h'=>$getterr)));             
        }else{
            //////////////// send mail
            $donaremail = $form_state['values']['donaremail'];
            $donarname  = $form_state['values']['donarname'];
            $amount     = $form_state['values']['amountdonated'];             
            $sub = "Thanks for your donation";

            $message = "Dear ".$donarname."<br><br>Thanks for donating Rs. ".$amount.". Your efforts can enlighten the life of someone.<br><br>Thanks & Regards";
            //$mailaccount = "info@iaseuniversityonline.org";
            $mailaccount = "societygvm@gmail.com";

            $New_Line = "\r\n";
            $headers = "MIME-Version: 1.0" .$New_Line;
            $headers .= "Content-type: text/html; charset=iso-8859-1" .$New_Line;
            $headers .= "Content-Transfer-Encode: 7bit " .$New_Line;
            $headers .= "From: $mailaccount ".$New_Line;
            'Reply-To:  '.$mailaccount  . "\r\n" .
            'X-Mailer: PHP/' . phpversion();                 	
            $mail = mail($donaremail,$sub,$message,$headers);
            drupal_set_message(t("Thankyou for donating Rs.@entry. Your effors can enlighten the life of someone.", array('@entry' => $form_state['values']['amountdonated'])));
        }
    }else{
        drupal_set_message(t("Oops, something went wrong and your donation is not made successfully. Try again later."));
    }
    
}

function donate_states(){
    $states = array('Anywhere'=>'Anywhere',
                    'Andhra Pradesh'=>'Andhra Pradesh',
                    'Arunachal Pradesh'=>'Arunachal Pradesh',
                    'Assam'=>'Assam',
                    'Bihar'=>'Bihar',
                    'Chhattisgarh'=>'Chhattisgarh',
                    'Goa'=>'Goa',
                    'Gujarat'=>'Gujarat',
                    'Haryana'=>'Haryana',
                    'Himachal Pradesh'=>'Himachal Pradesh',
                    'Jharkhand'=>'Jharkhand',
                    'Karnataka'=>'Karnataka',
                    'Kerala'=>'Kerala',
                    'Madhya Pradesh'=>'Madhya Pradesh',
                    'Maharashtra'=>'Maharashtra',        
                    'Manipur'=>'Manipur',
                    'Meghalaya'=>'Meghalaya',
                    'Mizoram'=>'Mizoram',
                    'Nagaland'=>'Nagaland',
                    'Odisha'=>'Odisha',
                    'Punjab'=>'Punjab',
                    'Rajasthan'=>'Rajasthan',
                    'Sikkim'=>'Sikkim',
                    'Tamil Nadu'=>'Tamil Nadu',
                    'Telangana'=>'Telangana',
                    'Tripura'=>'Tripura',        
                    'Uttar Pradesh'=>'Uttar Pradesh',
                    'Uttarakhand'=>'Uttarakhand',
                    'West Bengal'=>'West Bengal',
                    'Andaman and Nicobar Islands'=>'Andaman and Nicobar Islands',
                    'Chandigarh'=>'Chandigarh',
                    'Dadra and Nagar Haveli and Daman and Diu'=>'Dadra and Nagar Haveli and Daman and Diu',
                    'Delhi'=>'Delhi',
                    'Jammu and Kashmir'=>'Jammu and Kashmir',
                    'Ladakh'=>'Ladakh',
                    'Lakshadweep'=>'Lakshadweep',
                    'Puducherry'=>'Puducherry',
            );
    return $states;
}

 