Friday, October 25, 2019

Crud Module in Dtrupal 7

curd.info
name = "curd"
description = "Provided the coupon and that delete from my mysql database all the expired coupons."
core = "7.x"
curd.module

function curd_help($path, $arg){
switch ($path) {
case "admin/help#curd":
   return t("Display link to nodes created on this date");
break;
}
}

function curd_permission() {
  
  return array(
    'submit curd form' => array(
      'title' => t('curd form submit'),
      'description' => t('perform submission for current post'),
    ),
    'access curd submission' => array(
    'title' => t('access curd submission'),
    'description' => t('access submission for curd')
    ),
    );
}
/**
* Implements hook_menu.
*/
function curd_menu(){

$items = array();

$items['code_registration'] = array(
'title' => t('Registration Form For Coupon'),
        'page callback' => 'drupal_get_form',
        'page arguments' => array('curd_form'),
         //'page arguments' => array('curd_custom_content_block_form'),
        'access arguments' =>  array('administer users'),
);
 
$items['curd-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' =>'curd_final_result_view',
        'access callback' => TRUE,
    );
    
    $items['edit/%'] = array(
   
        'title' => 'Update The Final Result',
        'page callback' =>'drupal_get_form',
        'page arguments' => array('curd_confirm'),
        'access arguments' =>  array('administer users'),
    );

    $items['coupon_delete/%'] = array(
   
        'title' => 'Delete The Final Result',
         'page callback' => 'drupal_get_form',
     'page arguments' => array('coupon_delete_object'),
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
        'access arguments' =>  array('administer users'),
    );
     $items['coupon_delete/%'] = array(
   
        'title' => 'Delete The Final Result',
         'page callback' => 'drupal_get_form',
     'page arguments' => array('coupon_delete_object'),
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
        'access arguments' =>  array('administer users'),
    );
     $items['ajaxform_autocomplete_callback'] = array(
'page callback' => 'ajaxform_autocomplete_callback',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
);

    return $items;
}

function coupon_delete_object($form, &$form_state) {
$query = db_select('curd', 'c')
    ->fields('c')
    ->condition('curd_id',arg(1),'=');
    $query = $query->execute()->fetchAssoc();
 // print_r($query);
  if ( isset( $form_state['storage'] ) && $form_state['storage']['ask_confirm'] ) {
    $question = t( "Are you sure you want to delete this precious object?" );
    $path = 'final-result-view';
    $description = t( "This cannot be undone." );
    $yes = t( "Confirm" );
    $no = t( "Cancel" );
    return confirm_form( $form, $question, $path, $description, $yes, $no);
  }
  else {
    $form['curd_id'] = array(
'#type' => 'hidden',
'#value' => $query['curd_id'],
);

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

$form['curd'] = array(
    '#type' => 'textfield', 
    '#title' => 'curd',
'#required' => TRUE,
'#value' => $query['coupon_name'],
'#attributes'=>array(
),
    );
    $form['update'] = array(
      '#type' => 'submit',
      '#value' => t( "Update" ),
    );
    $form['delete'] = array(
      '#type' => 'submit',
      '#value' => t( "Delete" ),
    );
  }
  return $form;
}
function coupon_delete_object_submit($form,&$form_state) {
  if ( $form_state['clicked_button']['#value'] == 'Delete' ) {
    $form_state['rebuild'] = TRUE;
    $form_state['storage']['ask_confirm'] = TRUE;
  }
  else if ( isset( $form_state['values']['confirm'] ) && $form_state['values']['confirm'] ) {
  $rs = db_delete('curd')
    ->condition('curd_id', arg(1),'=')
    ->execute();
     $msg = t( "The object information has been updated." );
         drupal_set_message( $msg );
          drupal_goto("final-result-view");
  
  }
  else if ( $form_state['clicked_button']['#value'] == 'Update' ) {
   
        $id = $form_state['input']['curd_id'];
    $expire = $form_state['input']['expire'];
    $curd = $form_state['input']['curd'];
    $data = array('expire' => $expire,'curd' => $curd,);
    $num_updated = db_update('curd')->fields(array('expire_date' => $expire,'coupon_name' => $curd))
    ->condition('curd_id', $id, '=')
    ->execute();
     $msg = t( "The object information has been updated." );
         drupal_set_message( $msg );
          drupal_goto("final-result-view");
    // print_r($num_updated);
    // die();
  }
}
function curd_init(){

if(!(db_table_exists('curd'))) 
{
$achvmnt_list_schema = array(
'description' => 'This is for coupon code listing',
     'fields' => array(
           'curd_id' => array(
               'type' => 'serial', 
               'unsigned' => TRUE,
               'not null' => TRUE 
           ),
            
            'coupon_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
           ), 
           'images' => array(
               'type' => 'varchar',
               'length' => 100,
               'description' => 'this is for custome image',
               'not null' => TRUE
           ), 

        ),
        
        'primary key' => array('curd_id')
    );
db_create_table('curd', $achvmnt_list_schema);}
}

/**
* Create Form.
*/
function curd_form(){

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

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

$form['upload_item'] = array(
'#type' => 'file',
'#title' => 'Upload File'
    );
if(isset($_SESSION['uploaded_link'])) {
$form['uploaded_link'] = array(
'#type' => 'markup',
'#markup' => '' . $_SESSION['uploaded_link'] . ''
);

unset($_SESSION['uploaded_link']);
            }
            $form['article_search'] = array(
'#type' => 'textfield',
'#title' => 'Search Article',
'#maxlength' => 128,
'#autocomplete_path' => 'ajaxform_autocomplete_callback',
);


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


    return $form;
}    

function curd_custom_image_submit($form, &$form_state) {
    if (isset($form_state['values']['custom_image'])) {
    $file = file_load($form_state['values']['custom_image']);

    $file->status = FILE_STATUS_PERMANENT;

    file_save($file);
  }
}


function curd_form_submit($form, &$form_state){
   
//   echo "
"; 

//  print_r($form_state);
// die;
        //drupal_set_message('hello lakhan');
$validators = array(
'file_validate_size' => array("2097152"),
'file_validate_extensions' => array("jpg png doc docx pdf gif jpeg")
);

$location = 'public://';

$uploadCheck = file_save_upload('upload_item', $validators, $location);

if($uploadCheck) {
$link = file_create_url($uploadCheck->uri);
$_SESSION['uploaded_link'] = $link;  
    $record = db_insert('curd')->fields(
    array(
    'coupon_name' => $form_state['values']['coupon'],
        'expire_date' => $form_state['values']['expire'],
            'created' => time(),
            'images' => $link,
            // 'images' => $form_state['values']['custom_image'],
           
    )
    )->execute();
}  
    drupal_set_message(t('Data saved in the db successfully!'));
     
}

function coupon_table_view(){

//drupal_set_message("hi lakhan");

$data = db_select('curd' , 'code')
            ->fields('code' , array('curd_id','coupon_name','expire_date','created','images'));
       
$results = $data->execute()->fetchAll();         
         
         // echo"
";

        // print_r($results);

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

$rows=array();
foreach($results as $result){
$rows[]=array(
$result->curd_id,
$result->coupon_name,
$result->expire_date,
date('Y-d-m', $result->created),
''
        );
}
return theme('table',array('header'=>$header,'rows'=>$rows));
}

function curd_final_result_view(){
    //drupal_set_message("hi lakhan");
    
    $data = db_select('curd' , 'code')
        ->fields('code' , array('curd_id','coupon_name','expire_date','created'));
       
    $results = $data->execute()->fetchAll();

    $per_page = 4;
    $current_page ='';


        // echo"
";

        // 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->curd_id,
$code = $result->coupon_name,


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

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

        );

         // echo"
";

        // 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"
";

        // 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");
$rs = db_delete('curd')
    ->condition('curd_id', $id)
    ->execute();
    // print_r($rs);
    // die();
    drupal_set_message("Deleted Successfully Complete");
    drupal_goto('final-result-view');
}

function curd_confirm($form, &$form_state){

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

    $form = array();

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

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

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

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

function curd_confirm_submit($form, &$form_state){

// echo"
";

//  echo $id = $form_state['input']['curd_id'];
// print_r($form_state);die;

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

function ajaxform_autocomplete_callback($text) {
//we can get the titile of our cuatome table
$results = array();
  $query = db_select('curd', 'c');
  $query
      ->condition('c.coupon_name', '%' . db_like($text) . '%', 'LIKE')
      ->fields('c', array('coupon_name'))
      ->orderBy('coupon_name', 'ASC');
  $colors = $query->execute();
 
  foreach ($colors as $row) {
    $results[$row->coupon_name] = check_plain($row->coupon_name);
  }
   drupal_json_output($results);
}

?>

No comments:

Post a Comment

If you have any problem please let me know.