<?php

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

/**
 * Function to display donate form.
 */
function donate_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'];
      $donarpan = $dr['donarpan'];
      $address = $dr['address'];
      $comment = $dr['comment'];
      $donationfor = $dr['donationfor'];
      $amountdonated = $dr['amountdonated'];
      $amountdonatedon = $dr['amountdonatedon'];
      $paymode = $dr['paymode'];
      $paymentcollectionstatus = $dr['paymentcollectionstatus'];
      if($dr['paymentcollectiondate']!=0){
            $paymentcollectiondate = date("Y-m-d",$dr['paymentcollectiondate']);
      }else{
            $paymentcollectiondate='';
      }
      $paymentcollectionremark = $dr['paymentcollectionremark'];
      $receiptstatus = $dr['receiptstatus'];
      $displayonwebsite = $dr['displayonwebsite'];
      
      if($paymentcollectionstatus==1){
          $disabled = TRUE;
      }else{
          $disabled=FALSE;
      }
       
  }
  $form['donarname'] = array(
    '#title' => t('Name'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#disabled' => TRUE, 
    '#default_value' => $donarname,  
  );
  
  $form['fathername'] = array(
    '#title' => t('Father Name'),
    '#type' => 'textfield',
    '#disabled' => TRUE, 
    '#default_value' => $fathername, 
  );
  
  $form['donaremail'] = array(
    '#title' => t('Email'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#disabled' => TRUE, 
    '#default_value' => $donaremail, 
  );
  
  $form['donarmobileno'] = array(
    '#title' => t('Mobile No'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#disabled' => TRUE,
    '#default_value' => $donarmobileno, 
  );
 
  $form['donarpan'] = array(
    '#title' => t('PAN CARD'),
    '#type' => 'textfield',
    '#disabled' => TRUE, 
    '#default_value' => $donarpan, 
    '#description' => t('Mandatory if required 80g certificate'),
  );
  
  $form['address'] = array(
    '#title' => t('Address'),
    '#type' => 'textarea',
    '#disabled' => TRUE,
    '#default_value' => $address, 
  );
  
  $form['comment'] = array(
    '#title' => t('Comment'),
    '#type' => 'textarea',
    '#disabled' => TRUE, 
    '#default_value' => $comment, 
  );
  
  $doation_options = array();
  $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", 
                             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);
  }
  $doation_options["Sponsor Sarv Jvar Har Churna"] = "Sponsor Sarv Jvar Har Churna";
  
  if(count($doation_options) > 0){
      $form['donationfor'] = array(
        '#type' => 'radios',
        '#title' => t('Donate'),     
        '#options' => $doation_options,
        '#required' => TRUE,
        '#default_value' => $donationfor,
        '#disabled' => TRUE, 
      );
  }else{
      $form['donationfor'] = array(
        '#type' => 'hidden',
        '#title' => t('Donation Made For'), 
        '#default_value' => $donationfor, 
        '#disabled' => TRUE, 
      );
  }
  
  $form['amountdonated'] = array(
    '#title' => t('Enter Amount (in Rs.)'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#disabled' => TRUE, 
    '#default_value' => $amountdonated, 
  ); 
  $form['amountdonatedon'] = array(
    '#title' => t('Amount Donated On Date'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#disabled' => TRUE, 
    '#default_value' => format_date($amountdonatedon), 
  ); 
  
   
  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 $paymoder){
      $paymodeval = $paymoder['payment_mode'];      
      $paymode_options[$paymodeval] = t($paymodeval);
  }
  $form['paymode'] = array(
    '#type' => 'radios',
    '#title' => t('Payment Mode'),     
    '#options' => $paymode_options,
    '#required' => TRUE,
    '#disabled' => TRUE, 
    '#default_value' => $paymode, 
  );
   
  $paymentcollectionstatus_options = array('0' => t('Payment Not Collected'), '1' => t('Payment Collected'), '2' => t('Fake Donation Payment'));
  $form['paymentcollectionstatus'] = array(
    '#type' => 'radios',
    '#title' => t('Payment Collection Status'),     
    '#options' => $paymentcollectionstatus_options,
    '#required' => TRUE,  
    '#default_value' => $paymentcollectionstatus, 
    '#disabled' => $disabled,
  );
  $form['paymentcollectiondate'] = array(
    '#type' => 'date_popup', //you can find a list of available types in the form api
    '#title' => 'Payment Collection Date (Y-M-D)',
    '#default_value' => $paymentcollectiondate,
    '#required' => TRUE, //make this field required 		
    '#date_format' => 'Y-m-d',
    '#date_year_range' => '-90:+1', 
    //'#datepicker_options' => array('minDate' => 0),
    '#size' => '40',
    '#disabled' => $disabled,
  );
  $form['paymentcollectionremark'] = array(
    '#title' => t('Payment Collection Remark'),
    '#type' => 'textarea',
    '#default_value' => $paymentcollectionremark, 
    '#disabled' => $disabled,
  );
  
  
  /*$receiptstatus_options = array('0' => t('Receipt Not Sent'), '1' => t('Send Receipt'));
  $form['receiptstatus'] = array(
    '#type' => 'radios',
    '#title' => t('Send Receipt'),     
    '#options' => $receiptstatus_options,
    '#required' => TRUE, 
    '#default_value' => $receiptstatus, 
  );*/
  
  $displayonwebsite_options = array('0' => t('No, dont display on website'), '1' => t('Yes, display on website'));
  $form['displayonwebsite'] = array(
    '#type' => 'radios',
    '#title' => t('Display On Website'),     
    '#options' => $displayonwebsite_options,
    '#required' => TRUE,
    '#default_value' => $displayonwebsite,  
  );
   
  $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Edit'),
  );
  
  return $form;
}
  
/**
 * Implements hook_form_validate().
 */
function donate_edit_form_validate($form, &$form_state) {   
    if ($form_state['values']['op'] == t('Edit')) {         

    }
}

/**
 * Implements hook_form_submit().
 */
function donate_edit_form_submit($form, &$form_state) {  
    
    global $user;
    $amountdonatedon = time();
    $id = arg(1);
    
    $ip_address_of_user = $_SERVER['REMOTE_ADDR'];
    // Save the submitted entry.
     
    $paymentcollectiondate = strtotime($form_state['values']['paymentcollectiondate']);
    
    $query_updated = db_update('donate')  
                             ->fields(array('paymentcollectionstatus' => $form_state['values']['paymentcollectionstatus'],
                                            'paymentcollectiondate' => $paymentcollectiondate,
                                            'paymentcollectiondoneby' => $user->uid,
                                            'paymentcollectionremark' => $form_state['values']['paymentcollectionremark'],
                                            'displayonwebsite' => $form_state['values']['displayonwebsite']))
                             ->condition('id', $id, '=')
                             ->execute();
    
          
        
    //////////////// send mail
    /*$donaremail = $form_state['values']['donaremail'];
    $donarname  = $form_state['values']['donarname'];
    $amount     = $form_state['values']['amountdonated'];
    $to = $donaremail;
    $sub = "Thanks for your donation";

    $message = "Dear ".$donarname."<br><br>Thanks for donating Rs. ".$amount.". We have received the amount you donated. 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($to,$sub,$message,$headers);*/
    
    drupal_set_message(t("Donation detail edited successfully."));
        
     
}

 