cluesshop.com

Tuesday, 13 May 2014

Add filteration based on filed in drupal 7 full code backend

function giftcard_list_form($form,&$form_state){
   global $base_url;
    drupal_add_css(drupal_get_path('module', 'coupon') . '/coupon.css');
    if (isset($_GET['option'])) {
//    if (isset($form_state['values']['option'])) {
//        $option = $form_state['values']['option'];
        $option = $_GET['option'];
    }

    drupal_set_title('Gift Card', $output = CHECK_PLAIN);
    $html = '<div class="row-fluid"><div class="span12">'
            . '<a style="color:#0088CC;text-decoration:none;" href="' . $base_url . 'giftcard/add">Add Giftcard </a>'
            . '</div></div>';
    $form['#method'] = 'get';
    $form['option'] = array(
        '#type' => 'select',
        '#options'=>drupal_map_assoc(array('All','AVAILABLE','UNAVAILABLE')),
        '#default_value' => $option,
        '#prefix' => $html . '<div class="row-fluid"><div class="span2">',
        '#suffix' => '</div>',
        '#attributes' => array('class' => array('input-medium')),
    );

    $form['btn'] = array(
        '#type' => 'submit',
        '#value' => 'Go',
        '#prefix' => '<div class="span10" style="margin-left: 0px;margin-top:14px;">',
        '#suffix' => '</div></div>',
    );

    $output = '';

    $header = array(
        array('data' => 'Id', 'field' => 'id'),
        array('data' => 'Card ID', 'field' => 'card_id'),
        array('data' => 'Status'),
        array('data' => 'Created'),
    );
    if (isset($_GET ['sort']) && isset($_GET ['order'])) {
        if ($_GET ['sort'] == 'asc')
            $sort = 'ASC';
        else
            $sort = 'DESC';

        switch ($_GET ['order']) {

            case 'Id':
                $order = 'id';
                break;
            case 'Card ID':
                $order = 'card_id';
                break;
            case 'Status':
                $order='flag';
                break;
        }
    } else {
         $sort = 'ASC';
         $order = 'id';
    }

    $query = db_select("giftcard", "c");
    $query->fields('c', array(
        'id',
        'card_id',
        'created',
        'flag',
    ));
   
   
   // $query->condition('type', 'groupon', '=');
//    $or = db_or()->condition('type', 'groupon')->condition('type', 'web')->condition('type', 'livingsocial');
//    $query->condition($or);
    if ($option == 'AVAILABLE') {
        $query->condition('flag', '0', '=');
    } else if ($option == 'UNAVAILABLE') {
        $query->condition('flag', '1', '=');
    }
    $query->orderBy($order, $sort);


    $query = $query->extend('TableSort')->extend('PagerDefault')->limit(5);
    $result = $query->execute()->fetchAll();

    global $base_url;
    $rows = array();
    if (count($result) > 0) {
        for ($i = 0; $i < count($result); $i++) {

            $createddate = $result[$i]->created != '' ? date('Y-m-d', $result[$i]->created) : "";
            // $d=$result[$i]->created!='' ?
            if($result[$i]->flag==0)
            {
                $statusactive='AVAILABLE';
            }
          else {
             $statusactive='UNAVAILABLE';
           }
           // '<a href="wwww.test.com">Edit </a>'
           $rows[] = array(
                $result[$i]->id,
                $result[$i]->card_id,
                $statusactive,
                $createddate,
               // '<a href="' . $base_url . '/giftcard/' . $result[$i]->id . '/edit">Edit |</a>
               // <a href="' . $base_url . '/giftcard/' . $result[$i]->id . '/delete">Delete</a>'
            );
        }
    }

    $output .= '<div class="">';
    $output .= '<div class="">';

    $output .= '</div>';
    $output .= theme_table(array(
                'header' => $header,
                'rows' => $rows,
                'attributes' => array(
                    'class' => array(
                        ''
                    )
                ),
                'sticky' => true,
                'caption' => '',
                'colgroups' => array(),
                'empty' => t("No Records!")
            )) . theme('pager');

    $output .= '</div>';


    $form['outputmarkup'] = array(
        '#type' => 'markup',
        '#markup' => $output
    );
    return $form;
}

No comments:

Post a Comment