Wednesday, May 12, 2021

Coupon Code in Module Drupal 7

coupon_code.module
<?php
function coupon_code_help($path, $arg){
switch ($path) {
case "admin/help#coupon_code":
return t("Display link to nodes created on this date");
break;
}
}

function coupon_code_permission() {
  
    return array(
'submit coupon_code form' => array(
  'title' => t('coupon_code form submit'),
  'description' => t('perform submission for current post'),
),
'access coupon_code submission' => array(
'title' => t('access coupon_code submission'),
'description' => t('access submission for coupon_code')
),
);
}

function coupon_code_menu(){

$items = array();

$items['code_registration'] = array(
'title' => t('Registration Form For Coupon'),
        'page callback' => 'drupal_get_form',
        'page arguments' => array('coupon_code_form'),
        'access arguments' =>  array('administer users'),
);
 
$items['coupon_code-table-view'] = array(
        'title' => 'Coupon Table',
        'page callback' =>'coupon_table_view',
        'access callback' => TRUE, 
    );

$items['final-result-view'] = array(
        'title' => 'Coupon Code Final Result',
        'page callback' =>'coupon_code_final_result_view',
        'access callback' => TRUE,
    );
    
    $items['edit/%'] = array(
   
        'title' => 'Update The Final Result',
        'page callback' =>'drupal_get_form',
        'page arguments' => array('coupon_code_confirm'),
        'access arguments' =>  array('administer users'),
    );

    $items['coupon_delete/%'] = array(
   
        'title' => 'Delete The Final Result',
        'page callback' =>'coupon_register_delete',
        'page arguments' => array(1),
        'access arguments' =>  array('administer users'),
    );

    return $items;
}
function coupon_code_init(){

if(!(db_table_exists('coupon_code'))) 
{
$achvmnt_list_schema = array(
'description' => 'Table for city,state and travel fee listing',
     'fields' => array(
           'coupon_id' => array(
               'type' => 'serial', 
               'unsigned' => TRUE,
               'not null' => TRUE 
           ),
            
            'coupon_code_name' => array(
               'type' => 'varchar',
               'length' => 100,
               'not null' => TRUE
           ),
           
           'expire_date' => array(
               'type' => 'varchar',
               'length' => 100,
               'not null' => TRUE
           ),
           
           'created' => array(
               'type' => 'int',
               'description' => 'timestamp',
               'not null' => TRUE
           ),
        ),
        
        'primary key' => array('coupon_id')
    );
db_create_table('coupon_code', $achvmnt_list_schema);}
}



function coupon_code_form(){
// drupal_add_js('https://code.jquery.com/ui/1.12.1/jquery-ui.js', 'external');
// drupal_add_css('https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css', 'external');

$form = array();
$form['expire'] = array(
  '#type' => 'date_popup',
  // '#title' => 'Date',
  '#date_format' => 'd-m-Y',
  '#default_value' => date('d-m-Y'),
);

$form['coupon_code'] = array(
    '#type' => 'textfield', 
    '#title' => 'Coupon_Code',
'#required' => TRUE,
'#attributes'=>array(
    'placeholder'=>t('Add The Coupon Code'),
),
    );

$form['submit_button'] = array(
    '#type' => 'submit',
    '#value' => t('submit'),
);

    return $form;
}    

function coupon_code_form_submit($form, &$form_state){
   
   //echo "<pre>"; 
  // print_r($form_state);
// die;
        //drupal_set_message('hello lakhan');
   
    $record = db_insert('coupon_code')->fields(
    array(
    'coupon_code_name' => $form_state['values']['coupon_code'],
        'expire_date' => $form_state['values']['expire'],
            'created' => time()
    )
    )->execute();
    
    drupal_set_message(t('Data saved in the db successfully!'));
     
}

function coupon_table_view(){

//drupal_set_message("hi lakhan");

$data = db_select('coupon_code' , 'code')
            ->fields('code' , array('coupon_id','coupon_code_name','expire_date','created'));
       
$results = $data->execute()->fetchAll();         
         
         // echo"<pre>";
        // print_r($results);

    $header=array(t('Id'),t('Coupon Code'),t('Expire Date'),t('date'));

$rows=array();

foreach($results as $result){
$rows[]=array(
$result->coupon_id,
$result->coupon_code_name,
$result->expire_date,
date('Y-d-m', $result->created),

        );
}

return theme('table',array('header'=>$header,'rows'=>$rows));
}

function coupon_code_final_result_view(){
    //drupal_set_message("hi lakhan");
    
    $data = db_select('coupon_code' , 'code')
        ->fields('code' , array('coupon_id','coupon_code_name','expire_date','created'));
       
    $results = $data->execute()->fetchAll();

    $per_page = 4;
    $current_page ='';



        // echo"<pre>";
        // print_r($results);

    $date = date("Y-m-d");
    $res = "Coupon not expire";
    $res1= "Coupon expire";

    $rows=array();

foreach($results as $result){

$a = $result->expire_date >= $date;

$r = strtotime($result->expire_date);
$r2 = strtotime($date);

$diff = $r - $r2;

$x1 = abs(floor($diff / (60 * 60 * 24))) + 1 ."  - days";

      // print_r($r2);
// die();
          
            if($a){

     $a = $res ;
}
else{
$a = $res1;
}
 
$rows[]=array(
$result->coupon_id,
$code = $result->coupon_code_name,


// date('Y-d-m', $result->created),

$a,
$x1,
$expire = $result->expire_date,
l('Edit','edit/'.$result->coupon_id),
    l('Delete','coupon_delete/'.$result->coupon_id),

        );

         // echo"<pre>";
        // print_r($date);

    $current_page = pager_default_initialize(count($rows), $per_page);
  $chunks = array_chunk($rows, $per_page, TRUE); 
}




$header=array(t('Id'),t('Coupon Code'),t('Coupon Expire OR Not'),t('Day To Expired'), t('Date'),t('Edit'),t('Delete'));

             // echo"<pre>";
        // print_r($header);

    $output = theme('table', array('header' => $header, 'rows' => $chunks[$current_page]));
    $output .= theme('pager', array('quantity',count($rows)));
    return $output;
}
function coupon_register_delete($id){
    // drupal_set_message("$id");
db_delete('coupon_code')
    ->condition('coupon_id', $id)
    ->execute();
    drupal_set_message("Deleted Successfully Complete");
    drupal_goto('final-result-view');
}

function coupon_code_confirm($form, &$form_state){

// drupal_set_message($nid);
    $query = db_select('coupon_code', 'c')
    ->fields('c')
    ->condition('coupon_id',arg(1),'=');
    $query = $query->execute()->fetchAssoc();

    $form = array();

    $form['coupon_id'] = array(
'#type' => 'hidden',
'#value' => $query['coupon_id'],
);

   $form['expire'] = array(
  '#type' => 'textfield',
  // '#title' => 'Date',
  '#date_format' => 'd-m-Y',
  '#value' => $query['expire_date'],
);

$form['coupon_code'] = array(
    '#type' => 'textfield', 
    '#title' => 'Coupon_Code',
'#required' => TRUE,
'#value' => $query['coupon_code_name'],
'#attributes'=>array(
),
    );

$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('submit'),
);    
    return $form;
}

function coupon_code_confirm_submit($form, &$form_state){

// echo"<pre>";
//  echo $id = $form_state['input']['coupon_id'];
// print_r($form_state);die;

$id = $form_state['input']['coupon_id'];
    $expire = $form_state['input']['expire'];
    $coupon_code = $form_state['input']['coupon_code'];
    $data = array('expire' => $expire,'coupon_code' => $coupon_code,);
    $num_updated = db_update('coupon_code')->fields(array('expire_date' => $expire,'coupon_code_name' => $coupon_code))
    ->condition('coupon_id', $id, '=')
    ->execute();
    drupal_set_message('Updated successfully');
    drupal_goto('final-result-view');         
}

?>

No comments:

Post a Comment

If you have any problem please let me know.