Drupal 中 寫VBO操作模塊

內容就不說別的什麼了,直接貼下代碼:

<?php

/**
 * @file
 * Defines "fabric_base_vbo" 
 *
 * @TODO: Split admin functions into their own file.
 */
/* function fabric_base_vbo_action_info(&$form, $form_state, $form_id) {
  定義一個VBO操作列表  
} */
//新定義一個VBO 批量設置出倉庫操作
function fabric_base_vbo_action_info(){
	return array(
		'fabric_base_vbo_action'=>array(
			'type'=>'entity',
			'label'=>t('批量出倉'),
			'behavior'=>array('change_property'),
			'configurable'=>FALSE,
			'vbo_configurable'=>TRUE,
			'triggers'=>array('any'),
			'permissions'=>array('fabric base vbo','storage vbo'),
		),
		
	);
}

 //定義一個VBO configuration form 的操作方法
 function fabric_base_vbo_action_views_bulk_operations_form($options){
	  $form=array();
	  $form['vbo']['select']= array(
		'#type' => 'select',
		'#title' => t('批量操作選項'),
		'#options'=>array(
			'AllSelect'=>t('餘量出倉'),
			'Something'=>t('部分出倉'),
			),
		'#default_value'=>!empty($options['vbo']['select'])?$options['vbo']['select']:'',
		
		);
		$form['vbo']['num']=array(			
			'#type'=>'textfield',
			'#title'=>t('數量'),
			'#default_value'=>0,
			'#size'=>60,
			
		);
	 return $form;
 }
 function fabric_base_vbo_action_form($settings,&$form_state){	
	 //dpm($form_state);
	 // watchdog('views','Execute action from',array(),WATCHDOG_DEBUG);
	 $form=array();	 
	 $form['vbo']['select']= array(
		'#type' => 'select',
		'#title' => t('批量操作選項'),
		'#options'=>array(
			'AllSelect'=>t('餘量出倉'),
			'Something'=>t('部分出倉'),
			),
		'#default_value'=>isset($settings['settings']['vbo']['select'])?$settings['settings']['vbo']['select']:'',
		
		);
		$form['vbo']['num']=array(			
			'#type'=>'textfield',
			'#title'=>t('數量'),
			'#default_value'=>0,
			'#size'=>60,			
		);
	 return $form;
 }
 function fabric_base_vbo_action_submit($form,$form_state){	 	
      $return =array();
	   switch ($form_state['values']['select'])
	  {
		  case 'AllSelect':			
			$return['select']=$form_state['values']['select'];		
		  break;
		  case 'Something':
			$return['select']=$form_state['values']['select'];
			$return['num']=$form_state['values']['num'];			
		  break;
		  default:break;
	  } 
		 return $return;
 }
 
 function fabric_base_vbo_action(&$entity,$context){	

	 if (!isset($context['node']->nid)){
		 return;
	 }
	 $node=node_load($context['node']->nid);
	 
	 if(!isset($node->field_warehousestorageio['und'][0]['value'])){
		 $message=t('Can not Storage,Fabric @nid have not setting',array(
						'@nid'=>$context['node']->nid,						
						));
		 return drupal_set_message($message);
		}
	 if($node->field_warehousestorageio['und'][0]['value']<=0){
				$message=t('Can not Storage,Fabric @nid have 0 unit!',array(
						'@nid'=>$context['node']->nid,						
						));
				return drupal_set_message($message);
			}
	// dpm($node);
	  switch ($context['select'])
	  {
		  case 'AllSelect':					
			$node->field_warehousestoio['und'][0]['first']=-(int)$node->field_warehousestorageio['und'][0]['value'];
			$node->field_warehousestoio['und'][0]['second']=time();
			$node->field_warehousestorageio['und'][0]['value']=0;
			node_save($node);
			$message=t('Fabric @nid was update @num unit',array(
						'@nid'=>$context['node']->nid,
						'@num'=>$node->field_warehousestoio['und'][0]['first'],
						));
			drupal_set_message($message);
		  break;
		  case 'Something':
			if (!isset($context['num']) ||  !(int)$context['num']){
				$message=t('Setting Error!Check the Num.');
				return drupal_set_message($message);
			}
			if($node->field_warehousestorageio['und'][0]['value']+$context['num']>=0){
				dpm(1);
				$node->field_warehousestoio['und'][0]['first']=$context['num'];
				$node->field_warehousestoio['und'][0]['second']=time();
				$node->field_warehousestorageio['und'][0]['value']+=$context['num'];
				$storagenum=$context['num'];
			}else{
				dpm(0);
				$node->field_warehousestoio['und'][0]['first']=$node->field_warehousestorageio['und'][0]['value'];
				$node->field_warehousestoio['und'][0]['second']=time();
				$storagenum=$node->field_warehousestorageio['und'][0]['value'];
				$node->field_warehousestorageio['und'][0]['value']=0;				
			}
			node_save($node);			
	
				$message=t('Fabric @nid was update @num unit',array(
						'@nid'=>$context['node']->nid,
						'@num'=>$storagenum,
						));
				drupal_set_message($message);			
			
		  break;
		  default:break;
	  }
	 
 }   


發佈了99 篇原創文章 · 獲贊 15 · 訪問量 38萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章