<?php

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

/**
 * Function to display donate form.
 */
function donate_mail_receipt_edit_form($form, $form_state) {
  $form = array();
  global $user;
  $edit_id = arg(1);
  $donate_query = db_query("SELECT * FROM {donate} where id = :id", array(':id'=> $edit_id), array('fetch' => PDO::FETCH_ASSOC));
  foreach($donate_query as $dr){
      $donarname = $dr['donarname'];
      $fathername = $dr['fathername'];
      $donaremail = $dr['donaremail'];
      $donarmobileno = $dr['donarmobileno'];
      $address = $dr['address'];
      $comment = $dr['comment'];
      $amountdonated = $dr['amountdonated'];
      $amountdonatedon = $dr['amountdonatedon'];
      $paymode = $dr['paymode'];
      $paymentcollectionstatus = $dr['paymentcollectionstatus'];
      $receiptstatus = $dr['receiptstatus'];
      $displayonwebsite = $dr['displayonwebsite'];
  }
  $donarreceiptsubject = "Receipt of donation payment received";
  $donarreceipt = "<div><table border='0'>
                    <tr><td colspan='2'><h2>Donation Receipt</h2></td></tr>
                     
                    <tr><td width='20%'><b>Date: </b>".format_date($amountdonatedon)."</td><td align='right' width='10%'><b>Receipt No:</b> ".$edit_id."</td></tr>
                    <tr><td colspan='2'><b>Donated by: </b>".ucfirst($donarname)."</td></tr>
                    <tr><td colspan='2'><b>Address: </b>".ucfirst($address)."</td></tr>
                    <tr><td colspan='2'><b>Amount Donated:</b> Rs.".$amountdonated."</td></tr>
                    <tr><td colspan='2'><b>Amount Received By: </b>IASE Deemed to be University</td></tr>
                    <tr><td colspan='2'><b>Description:</b> </td></tr>
                     
                    <tr><td colspan='2'><b>Authorized signature:</b> </td></tr>
                  </table></div>
                  <br><div align='center'>Thankyou for your generosity. We apprecaiate your support.<br><br>IASE Deemed to be University, a unit of GVM<br> Sardarshahr, Rajasthan, India</div>";
  
  $form['donarname'] = array(
    '#title' => t('Name'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#disabled' => TRUE, 
    '#default_value' => $donarname,  
  );   
  
  $form['donaremail'] = array(
    '#title' => t('Email'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#disabled' => TRUE, 
    '#default_value' => $donaremail, 
  );
  
  $form['receiptsubject'] = array(
    '#title' => t('Receipt Subject'),
    '#type' => 'textfield',
    '#required' => TRUE,  
    '#disabled' => TRUE, 
    '#default_value' => $donarreceiptsubject, 
  );
  $form['receiptval'] = array(
     
    '#markup' => $donarreceipt, 
  );  
  $form['receipt'] = array(
    '#title' => t('Receipt'),
    '#type' => 'hidden',
    '#required' => TRUE, 
    '#disabled' => TRUE, 
    '#default_value' => $donarreceipt, 
  );   
  if($paymentcollectionstatus==1){
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Send Receipt Mail'),
    );
  }
  
  return $form;
}
  
/**
 * Implements hook_form_validate().
 */
function donate_mail_receipt_edit_validate($form, &$form_state) {   
    if ($form_state['values']['op'] == t('Send Receipt Mail')) {         

    }
}

/**
 * Implements hook_form_submit().
 */
function donate_mail_receipt_edit_submit($form, &$form_state) {  
    
    global $user;
    
    $mailsent = time();    
    $ip_address_of_user = $_SERVER['REMOTE_ADDR'];
    // Save the submitted entry.
     
    //////////////// send mail
    $donaremail = $form_state['values']['donaremail'];
    $donarname  = $form_state['values']['donarname'];     
     
    $subject = $form_state['values']['receiptsubject'];
    $receipt = $form_state['values']['receipt'];
    
    //$mailaccount = "info@iaseuniversityonline.org";
    $mailaccount = "societygvm@gmail.com";
    
    $entry = array(        
                'mail_sent_to_name'  => $donarname,
                'mail_sent_to_email' => $donaremail,
                'mail_subject'       => $subject,
                'mail_message'       => $receipt,
                'mail_sent_date'     => $mailsent,
                'mail_sent_by_user'  => $user->uid,
                'mail_type'          => '1',
                'ip_address'         => $ip_address_of_user,
            );    
    //////////////////// code to send mail 
    $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,$subject,$receipt,$headers);
    ///////////////////
    if($mail){
        $tid = db_insert('donate_sent_mail')
               ->fields($entry)
               ->execute();
        
        drupal_set_message(t("Receipt Mail sent successfully to @email.", array('@email' => $donaremail)));  
    }else{
        drupal_set_message(t("Receipt Mail not sent successfully to @email.", array('@email' => $donaremail)));  
    }    
     
}

 