brrocks28
2/23/2017 - 9:18 AM

Custom Status handler view handelr

Custom Status handler view handelr




/**
 * Implements hook_menu().
 */
function finstat_menu() {
  $items = array();

  $items['change-notification-status'] = array(
    'page callback' => '__change_notification_status',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  
  return $items;
}



/**
 * Ajax callback to Update Notification Moderation Status.
 */
function __change_notification_status(){
  $msg = array();
  if (isset($_POST['status']) && $_POST['status'] >= 0 && isset($_POST['id']) && $_POST['id'] >= 1) {
    $current_status = htmlentities($_POST['status']);
    $id = htmlentities($_POST['id']);
    $status = '';
    if ($current_status == 0) {
      $status_value = 1; //APPROVED
      update_notification_moderation_status($id, $status_value);
      $msg['type'] = 1;
      $msg['status'] = '<button id="s_1_'.$id.'" type="button" data-toggle="tooltip" title="" class="btn btn-xs btn-success" onclick="__change_notification_status(this.id);">Approve</button>';
    }
    if ($current_status == 1) {
      $status_value = 2; //SEND
      update_notification_moderation_status($id, $status_value);
      $msg['type'] = 2;
      $msg['status'] =  l('Send','send-notification/'.$id, array('attributes' => array('class' => 'btn btn-xs btn-success','id' => 'home')));  
    }    
  }
  else {
    $msg['error'] = 'error';
  }
  echo json_encode($msg);
}

/*
 * Update Notification Moderation Status.
 */
function update_notification_moderation_status($id,$status_value){
   $query = db_update('notification')
          ->fields(array(
            'moderation_status' => $status_value,
          ))
          ->condition('id', $id, '=')
          ->execute();
}



/*
 * implements hook_views_api()
 */

function notification_views_api() {
  return array(
    'api' => 3.0,
    'path' => drupal_get_path('module', 'notification') . '/includes',
  );
}

/**
 * Change Notification moderation status. AJAX.
 * @param {type} id
 * @returns {undefined}
 */
function __change_notification_status(id){
    var arr = id.split('_');
    var current_status = arr[1];
    var id = arr[2];
    if(!isNaN(status) && status >= 0 && !isNaN(id) && id >= 1){
        jQuery.ajax({
            type:'POST',
            data:{'status':current_status,'id':id},
            dataType:'json',
            url:Drupal.settings.basePath+'change-notification-status',
            async:false,
            success:function(msg){
                if(msg.type == 1){
                    jQuery('#s_0_'+id).replaceWith(jQuery(msg.status));
                }
                if(msg.type == 2){
                    jQuery('#s_1_'+id).replaceWith(jQuery(msg.status));
                }               
            }
        });
    }else{
        alert('Error occured.');
    }    
}

/*
 * hook_views_data()
 */

function notification_views_data() {
  $data = array();
  $data['notification']['table']['group'] = t('Device Notification View');
  $data['notification']['table']['base'] = array(
    'field' => 'id',
    'title' => t('Device Notification'),
  );
  $data['notification']['id'] = array(
    'title' => t('id'),
    'help' => t('id'),
    'field' => array(
      'handler' => 'views_handler_field',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_numeric',
    ),
  );
  $data['notification']['notification_text'] = array(
    'title' => t('notification_text'),
    'help' => t('notification_text'),
    'field' => array(
      'handler' => 'views_handler_field',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );
  $data['notification']['type'] = array(
    'title' => t('type'),
    'help' => t('type'),
    'field' => array(
      'handler' => 'views_handler_field',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_numeric',
    ),
  );
  $data['notification']['device_id'] = array(
    'title' => t('device_id'),
    'help' => t('device_id'),
    'field' => array(
      'handler' => 'views_handler_field',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );
  $data['notification']['read_timestamp'] = array(
    'title' => t('read_timestamp'),
    'help' => t('read_timestamp'),
    'field' => array(
      'handler' => 'views_handler_field_date',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'date_views_filter_handler_simple',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );
  
  $data['notification']['show_flag'] = array(
    'title' => t('show_flag'),
    'help' => t('show_flag'),
    'field' => array(
    'handler' => 'views_handler_field',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_numeric',
    ),
  );
  
  $data['notification']['created'] = array(
    'title' => t('created'),
    'help' => t('created'),
    'field' => array(
      'handler' => 'views_handler_field_date',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'date_views_filter_handler_simple',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );
  $data['notification']['created_by'] = array(
    'title' => t('created_by'),
    'help' => t('created_by'),
    'field' => array(
      'handler' => 'views_handler_field',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_numeric',
    ),
    'relationship' => array(
      'base' => 'users',
      'base field' => 'uid',
      'title' => t('created_by - users'),
      'label' => t('created_by - users'),
      'handler' => 'views_handler_relationship',
    ),
  );
  //status handler
  $data['notification']['moderation_status'] = array(
    'title' => t('moderation_status'),
    'help' => t('moderation_status'),
    'field' => array(
      'handler' => 'notification_custom_handler',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_numeric',
    ),
  );  
  return $data;
}









/**
 * Custom notifiction field handler
 */
class notification_custom_handler extends views_handler_field_numeric{
  function render($values) {
    $status = '';
    if(!empty($values) && property_exists($values, 'notification_moderation_status')){
      if($values->notification_moderation_status == 0){
        $status = '<button id="s_0_'.$values->id.'" type="button" data-toggle="tooltip" title="" class="btn btn-xs btn-success" onclick="__change_notification_status(this.id);">Test Template</button>';
      }else if ($values->notification_moderation_status == 1){
        $status = '<button id="s_1_'.$values->id.'" type="button" data-toggle="tooltip" title="" class="btn btn-xs btn-success" onclick="__change_notification_status(this.id);">Approve</button>';
      }else if ($values->notification_moderation_status == 2){
        $status = l('Send','send-notification/'.$values->id, array('attributes' => array('class' => 'btn btn-xs btn-success')));
      }else{
        $status= "";
      }
    }
    return $status;
  }
}