<?php

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

/**
 * Function to display donate form.
 */
function donate_mail_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'];
  }
  $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['mailsubject'] = array(
    '#title' => t('Subject'),
    '#type' => 'textfield',
    '#required' => TRUE,  
  );
  
  $form['mailmessage'] = array(
    '#title' => t('Mail Message'),
    '#type' => 'text_format',
    '#format' => 'full_html',  
    '#required' => TRUE, 
  );   
   
  $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Send Mail'),
  );
  
  return $form;
}
  
/**
 * Implements hook_form_validate().
 */
function donate_mail_edit_form_validate($form, &$form_state) {   
    if ($form_state['values']['op'] == t('Send Mail')) {         

    }
}

/**
 * Implements hook_form_submit().
 */
function donate_mail_edit_form_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']['mailsubject'];
    $message = $form_state['values']['mailmessage'];
    
    //$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'       => $message,
                '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,$message,$headers);
    ///////////////////
    if($mail){
        $tid = db_insert('donate_sent_mail')
               ->fields($entry)
               ->execute();
        
        drupal_set_message(t("Mail sent successfully to @email.", array('@email' => $donaremail)));  
    }else{
        drupal_set_message(t("Mail not sent successfully to @email.", array('@email' => $donaremail)));  
    }   
     
}

 