欢迎各位兄弟 发布技术文章
这里的技术是共享的
<?php function shipingzhongcustomtask_nodeapi(&$node, $op, $a3 = null , $a4 = null){ if($op == 'presave'){ //这是对发布任务的hook if($node->type == 'publish_task'){ //仅仅新增的时候 置剩余件数 等于 件数 if(empty($node->nid)){ $node->field_remaining_num[0]['value'] = $node->field_pub_task_num[0]['value']; } else{ $accept_task_num = count($node->field_accept_task); count($node->field_accept_task)==1 && empty($node->field_accept_task[0]['nid']) && $accept_task_num=0; $node->field_remaining_num[0]['value'] = $node->field_pub_task_num[0]['value']-$accept_task_num; } //在新增 修 修改之前 把 总金额置为 数量 乘以 单价 $node->field_pub_task_money[0]['value'] = floatval($node->field_pub_task_num[0]['value'])* floatval($node->field_pub_task_price[0]['value']); // $tasks = $node->field_accept_task; // //如果 接受的任务不存在的话 就删除这个接受任务的引用吧 // foreach($tasks as $key=>$value){ // $accept_task_node = node_load($value['nid']); // if(empty($accept_task_node)){ // drupal_set_message($value['nid']); // unset($tasks[$key]); // } // } // $node->field_accept_task = $tasks; } } //删除认领任务表时进行的操作 就是把发布任务的外键去掉 if($op == 'delete') { if($node->type == 'accept_task') { $sql = "select nid from {content_field_accept_task} where field_accept_task_nid=%d "; $publish_task_nid = db_result(db_query($sql, $node->nid)); $publish_task_node = node_load($publish_task_nid); foreach($publish_task_node->field_accept_task as $key=>$value){ if($value['nid']==$node->nid){ unset($publish_task_node->field_accept_task[$key]); } } node_save($publish_task_node); } } } function shipingzhongcustomtask_form_alter(&$form, &$form_state, $form_id) { if($form_id=='publish_task_node_form' ) { $form['#after_build'][] = '_set_field_pub_task_money_readonly'; } if($form_id=='accept_task_node_form' ) { //$form['#after_build'][] = '_set_accept_task_node_form'; } } //function _set_accept_task_node_form($form, &$form_state){ // $export = kprint_r($form, TRUE); // $vars - is a variable which you want to print. // print $export; // return $form; //} function _set_field_pub_task_money_readonly($form, &$form_state) { _my_fix_readonly($form['field_pub_task_money']); return $form; } function _my_fix_readonly(&$elements){ foreach (element_children($elements) as $key) { if (isset($elements[$key]) && $elements[$key]) { // Recurse through all children elements. _my_fix_readonly($elements[$key]); } } if (!isset($elements['#attributes'])) { $elements['#attributes'] = array(); } $elements['#attributes']['readonly'] = 'readonly'; $elements['#attributes']['style'] = 'background:#ebebe4;border:1px solid #a9a9a9'; } function _accept_task_num($publish_task_nid){ return drupal_get_form('accept_task_form',$publish_task_nid); } function accept_task_form(&$form_state,$publish_task_nid)//accept_task_form 就是 $form_id { $arg1 = arg(1); $arg2 = arg(2); if($arg1==$publish_task_nid){ $form['accept_task_num'] = array( '#type' =>'textfield',//drupal 规定文本框用textfield来表示 '#size' => 2, '#default_value' => $arg2 ); }else { $form['accept_task_num'] = array( '#type' =>'textfield',//drupal 规定文本框用textfield来表示 '#size' => 2, ); } // 文本框 name是文本框的名称 $form['publish_task_nid'] = array( '#type' =>'hidden',//drupal 规定文本框用textfield来表示 '#default_value' => $publish_task_nid ); //提交按钮 // $form['submit'] = array('#type'=>'submit','#value'=>'领单'); $form['#after_build'][] = '_remove_form_item_div'; return $form; } function _remove_form_item_div($form, &$form_state) { //$form['#theme'] = array('accept_task_form'); if(!empty($form['accept_task_num'])){ $form['accept_task_num']['#theme'] = array('remove_form_item_div_for_field'); } if(!empty($form['field_acce_task_pic'])){ $form['field_acce_task_pic']['#theme'] = array('remove_form_item_div_for_file');; // $form['field_acce_task_pic']['#theme'] = array('content_multiple_values');; } if(!empty($form['fid'])){ //$form['fid']['#theme'] = array('remove_form_item_div_for_upload_file');; } if(!empty($form['field_acce_task_link'])){ $form['field_acce_task_link']['#theme'] = array('remove_form_item_div_for_field'); } if(!empty($form['field_acce_task_status'])){ $form['field_acce_task_status']['#theme'] = array('remove_form_item_div_for_select'); } // // $export = kprint_r($form, TRUE); // $vars - is a variable which you want to print. // print $export; return $form; } function shipingzhongcustomtask_theme() { return array( 'accept_task_form' => array( 'render element' => 'form', ), 'remove_form_item_div_for_field' => array( 'render element' => 'element', ), 'remove_form_item_div_for_select' => array( 'render element' => 'element', ), 'remove_form_item_div_for_file' => array( 'render element' => 'element', ), 'remove_form_item_div_for_upload_file' => array( 'render element' => 'element', ), 'my_accept_task' =>array( 'template' => 'my_accept_task' ), 'pub_task_in_accept_node' => array( 'arguments' => array('node' => NULL), 'template' => 'pub_task_in_accept_node', ), ); } function theme_remove_form_item_div_for_upload_file($element) { return 'aaaa'; } function theme_remove_form_item_div_for_file($element) { _form_set_class($element, array('form-file')); //return theme('form_element', $element, '<input type="file" name="'. $element['#name'] .'"'. ($element['#attributes'] ? ' '. drupal_attributes($element['#attributes']) : '') .' id="'. $element['#id'] .'" size="'. $element['#size'] ."\" />\n"); return _accept_task_num_text_field_form_element($element, '<input type="file" name="'. $element['#name'] .'"'. ($element['#attributes'] ? ' '. drupal_attributes($element['#attributes']) : '') .' id="'. $element['#id'] .'" size="'. $element['#size'] ."\" />\n"); } function theme_remove_form_item_div_for_select($element) { $select = ''; $size = $element['#size'] ? ' size="'. $element['#size'] .'"' : ''; _form_set_class($element, array('form-select')); $multiple = $element['#multiple']; return _accept_task_num_text_field_form_element($element, '<select name="'. $element['#name'] .''. ($multiple ? '[]' : '') .'"'. ($multiple ? ' multiple="multiple" ' : '') . drupal_attributes($element['#attributes']) .' id="'. $element['#id'] .'" '. $size .'>'. form_select_options($element) .'</select>'); } function theme_remove_form_item_div_for_field($element) { $size = empty($element['#size']) ? '' : ' size="'. $element['#size'] .'"'; $maxlength = empty($element['#maxlength']) ? '' : ' maxlength="'. $element['#maxlength'] .'"'; $class = array('form-text'); $extra = ''; $output = ''; if ($element['#autocomplete_path'] && menu_valid_path(array('link_path' => $element['#autocomplete_path']))) { drupal_add_js('misc/autocomplete.js'); $class[] = 'form-autocomplete'; $extra = '<input class="autocomplete" type="hidden" id="'. $element['#id'] .'-autocomplete" value="'. check_url(url($element['#autocomplete_path'], array('absolute' => TRUE))) .'" disabled="disabled" />'; } _form_set_class($element, $class); if (isset($element['#field_prefix'])) { $output .= '<span class="field-prefix">'. $element['#field_prefix'] .'</span> '; } $output .= '<input type="text"'. $maxlength .' name="'. $element['#name'] .'" id="'. $element['#id'] .'"'. $size .' value="'. check_plain($element['#value']) .'"'. drupal_attributes($element['#attributes']) .' />'; if (isset($element['#field_suffix'])) { $output .= ' <span class="field-suffix">'. $element['#field_suffix'] .'</span>'; } return _accept_task_num_text_field_form_element($element, $output) . $extra; } function _accept_task_num_text_field_form_element($element, $value) { // This is also used in the installer, pre-database setup. $t = get_t(); $output = ''; $required = !empty($element['#required']) ? '<span class="form-required" title="'. $t('This field is required.') .'">*</span>' : ''; if (!empty($element['#title'])) { $title = $element['#title']; if (!empty($element['#id'])) { $output .= ' <label for="'. $element['#id'] .'">'. $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n"; } else { $output .= ' <label>'. $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n"; } } $output .= " $value\n"; if (!empty($element['#description'])) { $output .= ' <div class="description">'. $element['#description'] ."</div>\n"; } return $output; } function theme_accept_task_form($form) { $output = ''; // $output .= drupal_render($element['form_id']); // $output .= drupal_render($element['form_build_id']); // $output .= drupal_render($element['form_token']); // $output .= drupal_render($element['accept_task_num']); // $output .= drupal_render($element['submit']); //$output .= drupal_render_children($form); //这个好像 drupal 6 不行 drupal 7 才行 $output .= drupal_render($form); return $output; // Anonymous div to satisfy XHTML compliance. // $action = $element['#action'] ? 'action="' . check_url($element['#action']) . '" ' : ''; // return '<form ' . $action . ' accept-charset="UTF-8" method="' . $element['#method'] . '" id="' . $element['#id'] . '"' . drupal_attributes($element['#attributes']) . ">\n<div>" . $element['#children'] . "\n</div></form>\n"; } //表单函数_validate 验证类似于钩子函数的回调函数 function accept_task_form_validate(&$form, &$form_state) { $publish_task_nid = $form_state['values']['publish_task_nid']; $accept_task_num = intval($form_state['values']['accept_task_num']); if(empty($accept_task_num) || !is_int($accept_task_num) || $accept_task_num <=0){ form_set_error('','领单件数必须为正整数'); // $form['accept_task_num']['#default_value'] = ''; // $form_state['values']['accept_task_num'] = ''; // form_set_value($form['accept_task_num'], '', $form_state); drupal_goto('list-publish-task/'.$publish_task_nid.'/'.$accept_task_num); exit; } $node_publish_task = node_load($publish_task_nid); /** * 用户提交的所有数据在 $form_state['values'] 之中,相当于 $_POST */ // form_set_error('',$form_values['name']); if($accept_task_num > $node_publish_task->field_remaining_num[0]['value']) { form_set_error('','领单件数是 '.$accept_task_num.' 不能大于剩余件数 '.$node_publish_task->field_remaining_num[0]['value']); // $form['accept_task_num']['#default_value'] = ''; // $form_state['values']['accept_task_num'] = ''; // form_set_value($form['accept_task_num'], '', $form_state); drupal_goto('list-publish-task/'.$publish_task_nid.'/'.$accept_task_num); exit; } // $export = kprint_r($form, TRUE); // $vars - is a variable which you want to print. // print $export; // $export = kprint_r($form_state, TRUE); // $vars - is a variable which you want to print. // print $export; // var_dump($form_state['values']['publish_task_nid']); // var_dump($form_state['values']['accept_task_num']); // var_dump($node_publish_task->field_remaining_num[0]['value']); // var_dump("AAAA");exit; } //表单函数_submit 提交函数 function accept_task_form_submit(&$form, &$form_state) { global $user; $publish_task_nid = $form_state['values']['publish_task_nid']; $node_publish_task = node_load($publish_task_nid); $accept_task_num = intval($form_state['values']['accept_task_num']); for($i=0;$i<$accept_task_num;$i++) { $node = null; $node = (object)$node; //创建空节点对象 $node->uid = $user->uid; // 创建订单者统一设为超级管理员 $node->type = 'accept_task'; //节点类型为“领取任务” $node->title = '接受'.$node_publish_task->title.date('YmdHis').'-'.rand(1,1000); $node->field_acce_task_status[0]['value'] = 'nodo'; node_save($node); // drupal_set_message($node->nid); $node_publish_task->field_accept_task[]=array('nid'=>$node->nid); } $node_publish_task->field_remaining_num[0]['value'] = $node_publish_task->field_remaining_num[0]['value'] - $accept_task_num; node_save($node_publish_task); /** * 用户提交的所有数据在 $form_state['values'] 之中,相当于 $_POST */ drupal_set_message('领单成功!'); drupal_goto('my_accept_task/'.$publish_task_nid); } //定义权限 仅仅指明什么权限对这个模块可用 function shipingzhongcustomtask_perm(){ return array('create custom accept task','modify custom accept task','delete custom accept task'); } function modify_accept_task_by_my_pub() { global $user; $publish_task_nid = db_result(db_query("SELECT nid FROM `content_field_accept_task` where field_accept_task_nid='%d'", intval($_POST['nid']))); $publish_task_node = node_load($publish_task_nid); //超级管理员 或 这个认领任务 属于的 发布任务的 uid 等于 当前用户的uid if($user->uid==1 || $user->uid==$publish_task_node->uid){ return true; } return false; } function _accept_task($role) { global $user; if((is_array($user->roles) && in_array($role, $user->roles))|| $user->uid==1) { return true; } return false; } function shipingzhongcustomtask_menu() { $items = array(); $items['my_accept_task/%node'] = array( 'type' => MENU_CALLBACK, 'title'=>'我领取的任务', 'page callback'=>'shipingzhongcustomtask_my_accept_tasks_page', 'page arguments' => array(1), //'page arguments' => array(1,2), //它的优先级 大于 url传来的参数我 //如果要默认值 就放在回调函数的形参上 //array(1,2)表示只取url前两个参数 array(1)表示只取url第一个参数 'access arguments'=> array('create custom accept task') ); $items['modify_accept_task'] = array( 'type' => MENU_CALLBACK, 'title'=>'修改的任务', 'page callback'=>'shipingzhongcustomtask_modify_accept_task_page', 'page arguments' => array(1), //'page arguments' => array(1,2), //它的优先级 大于 url传来的参数我 //如果要默认值 就放在回调函数的形参上 //array(1,2)表示只取url前两个参数 array(1)表示只取url第一个参数 'access arguments'=> array('modify custom accept task') ); $items['delete_accept_task'] = array( 'type' => MENU_CALLBACK, 'title'=>'删除的任务', 'page callback'=>'shipingzhongcustomtask_delete_accept_task_page', 'page arguments' => array(1,2), //'page arguments' => array(1,2), //它的优先级 大于 url传来的参数我 //如果要默认值 就放在回调函数的形参上 //array(1,2)表示只取url前两个参数 array(1)表示只取url第一个参数 'access arguments'=> array('delete custom accept task') ); $items['complete_redo_accept_task'] = array( 'type' => MENU_CALLBACK, 'title'=>'完成或重做的任务', 'page callback'=>'shipingzhongcustomtask_complete_redo_accept_task', //'page arguments' => array(1,2), //它的优先级 大于 url传来的参数我 //如果要默认值 就放在回调函数的形参上 //array(1,2)表示只取url前两个参数 array(1)表示只取url第一个参数 'access callback' => 'modify_accept_task_by_my_pub', ); $items['add_field_pub_task_type'] = array( 'type' => MENU_CALLBACK, 'title'=>'增加任务的类型', 'page callback'=>'shipingzhongcustomtask_add_field_pub_task_type', //'page arguments' => array(1,2), //它的优先级 大于 url传来的参数我 //如果要默认值 就放在回调函数的形参上 //array(1,2)表示只取url前两个参数 array(1)表示只取url第一个参数 'access arguments' => array('edit field_pub_task_type'), ); $items['chakantuorfile'] = array( 'type' => MENU_CALLBACK, 'title'=>'查看图或者文件', 'page callback'=>'shipingzhongcustomtask_chakantuorfile', //'page arguments' => array(1,2), //它的优先级 大于 url传来的参数我 //如果要默认值 就放在回调函数的形参上 //array(1,2)表示只取url前两个参数 array(1)表示只取url第一个参数 'access arguments' => array('认领任务角色'), 'access callback' => '_accept_task', ); return $items; } function shipingzhongcustomtask_chakantuorfile($fid){ $file = field_file_load($fid); //session_cache_limiter("public"); // for IE 好像 file_transfer 里面已处理了 if($file) { //下面是下载功能 // $arr_headers = array( // 'Content-type: application/octet-stream', // 'Accept-Ranges: bytes', // 'Content-Length: '.filesize(file_create_path($file['filepath'])), // 'Content-disposition: attachment; filename='.$file['filename'].';' // ); //下面是浏览器在线直接打开 $type = file_get_mimetype($file['filename']); $arr_headers = array( 'Pragma: public', 'Expires: 0', 'Cache-Control: must-revalidate, post-check=0, pre-check=0', 'Cache-Control: public', 'Content-Type: '.$type, 'Content-Disposition: inline; filename=' . $file['filename'] . ';', 'Content-Transfer-Encoding: binary', ); //var_dump($_SERVER['DOCUMENT_ROOT'] . base_path() . file_create_path($file['filepath']));exit; file_transfer(file_create_path($file['filepath']), $arr_headers); } else { return drupal_access_denied(); } } function _custom_sort_status_order($a,$b) { if($a->field_acce_task_status[0]['value'] == $b->field_acce_task_status[0]['value']){ return 0; } $content_field = content_fields('field_acce_task_status'); $allowed_values = content_allowed_values($content_field); $allowed_value_keys = array_keys($allowed_values); $index_a = array_search($a->field_acce_task_status[0]['value'],$allowed_value_keys); $index_b = array_search($b->field_acce_task_status[0]['value'],$allowed_value_keys); return ($index_a<$index_b)?-1:1; } //按未做 重做 已做 完成 进行排序 redo nodo havedo completedo function _sort_status_order($arr) { uasort($arr,"_custom_sort_status_order"); return $arr; } function shipingzhongcustomtask_my_accept_tasks_page($node_publish_task=null) { global $user; if(empty($node_publish_task)){ return ''; } $node_publish_task = node_build_content($node_publish_task); $tasks = $node_publish_task->field_accept_task; //如果 接受的任务不存在的话 就删除这个接受任务的引用吧 $my_tasks = array(); foreach($tasks as $key=>$value){ $accept_task_node = node_load($value['nid']); if($accept_task_node->uid == $user->uid ){ $my_tasks[$accept_task_node->nid] = $accept_task_node; } } $my_tasks = _sort_status_order($my_tasks); $output_string = ''; //$content = field_view_field('node', $node_publish_task, 'field_pub_task_type'); // $export = kprint_r($node_publish_task,TRUE); // print $export; // $node_publish_task = node_build_content($node_publish_task); // $export = kprint_r($node_publish_task,TRUE); // print $export; //$node_publish_task = node_view($node_publish_task, 'full'); $output_string .= theme('pub_task_in_accept_node', $node_publish_task); foreach($my_tasks as $key=>$accept_task_node){ $output_string .= drupal_get_form('accept_task_do_form', $accept_task_node); } $output_string .= '<br />'; $num = 0; foreach($my_tasks as $key=>$accept_task_node){ if($accept_task_node->field_acce_task_status[0]['value']=='completedo'){ $num = $num+1; } } $total_money = $num*$node_publish_task->field_pub_task_price[0]['value']; $output_string .='我认领了<span class="hongse">'.count($my_tasks).'</span>件, 我完成了<span class="hongse">'.$num.'</span>件, 我得到的人民币是<span class="hongse">'.$total_money.'</span>元,删除后可能值不对,(如出现错误)可以点'.' <a href="" class="hongse">刷新</a>'; //$output_string .= theme('my_accept_task'); return $output_string; } function accept_task_do_form(&$form_state,$accept_task_node)//accept_task_form 就是 $form_id { $content_field = content_fields('field_acce_task_status'); $allowed_values = content_allowed_values($content_field); // $element = content_fields('field_image', 'accept_task_node_form'); // $form['#field_info']['field_image'] = $element; // $element = (array) content_field_form($form, $form_state, $element); // $form['field_image'] = $element['field_image']; $form['nid'] = array( '#type' =>'hidden',// '#value'=>$accept_task_node->nid, ); // $form['field_acce_task_pic'] = array( // '#type' =>'file',// // '#title'=>'图像', // // ); // $form['field_acce_task_pic'] = array( // '#type' => 'image_upload_element', // '#title'=>'图像', //// /'#default_value' => $accept_task_node->field_acce_task_pic, // {files} object // ); //好像 字段名字 非要有 fid 或 fid1 fid2 fid3 fid4 等等 $field_acce_task_pic_obj = db_fetch_object(db_query("SELECT * FROM {files} WHERE fid = %d", $accept_task_node->field_acce_task_pic[0]['fid'])); $field_acce_task_file_obj = db_fetch_object(db_query("SELECT * FROM {files} WHERE fid = %d", $accept_task_node->field_acce_task_file[0]['fid'])); if(!empty($accept_task_node->field_acce_task_pic[0])) { $form['fid'] = array( '#type' => 'image_upload_element', '#title' => '图像', '#required' => FALSE, '#default_value' => $field_acce_task_pic_obj,//这是一个对象 不是数组 '#file_validators' => array('file_validate_size' => array(1048576)), '#image_preview_size' => '150x150', ); }else{ $form['fid']= array( '#type' => 'image_upload_element', '#title' => '图像', '#required' => FALSE, '#default_value' => null, '#file_validators' => array('file_validate_size' => array(1048576)), '#image_preview_size' => '150x150', ); } if(!empty($accept_task_node->field_acce_task_file[0])) { $form['fid1'] = array( '#type' => 'upload_element', '#title' => '文件', '#required' => FALSE, '#default_value' =>$field_acce_task_file_obj,//这是一个对象 不是数组 '#file_validators' => array('file_validate_size' => array(1048576)), ); }else{ $form['fid1']= array( '#type' => 'upload_element', '#title' => '文件', '#required' => FALSE, '#default_value' => null, '#file_validators' => array('file_validate_size' => array(1048576)), ); } // 文本框 name是文本框的名称 $form['field_acce_task_link'] = array( '#type' =>'textfield',//drupal 规定文本框用textfield来表示 '#title'=>'链接', '#default_value' => $accept_task_node->field_acce_task_link[0]['url'], '#size'=>40 ); //四种状态 redo nodo havedo completedo if($accept_task_node->field_acce_task_status[0]['value'] =='nodo' || $accept_task_node->field_acce_task_status[0]['value'] =='havedo'){ unset($allowed_values['redo']); unset($allowed_values['completedo']); }else if($accept_task_node->field_acce_task_status[0]['value'] =='redo'){ unset($allowed_values['completedo']); }else if($accept_task_node->field_acce_task_status[0]['value'] =='completedo'){ unset($allowed_values['redo']); unset($allowed_values['nodo']); unset($allowed_values['havedo']); } $form['field_acce_task_status'] = array( '#type' =>'select',//drupal 规定文本框用textfield来表示 '#default_value' => $accept_task_node->field_acce_task_status[0]['value'], '#options' => $allowed_values , '#title'=>'状态', ); $form['linkfidbefor'] = array( '#type' => 'markup', '#value' => '<div class="clr"></div>', ); $form['msg']= array( '#prefix'=>'<div id="user-dig-msg'.$accept_task_node->nid.'" class="user-dig-msg">', '#type'=>'item', '#suffix'=>'</div>', ); $form['#attributes']['enctype'] = 'multipart/form-data'; $form['#attributes']['class'] = 'accept-task-do-form clearfix'; //提交按钮 ////提交按钮 // 好像如果使用 ahah ajax 提交 这里不能为 submit $form['submit1'] = array( '#type'=>'submit', //记住 这里切切要是 submit 如果是button的话 就会无法 执行 form_submit 也无法执行 form_validate '#value'=>'确定', //这里 #submit 和下面的 $form['#submit'][] 好像作用是一样的 //反正好像不能使用原来的,好像去掉也可以 //'#submit'=>array('accept_task_do_form_submit_ahah'), '#ahah' => array( //'event' => 'click', // This is the "magical path". Note that the parameter is an array of // the parents of the form item of the wrapper div! 'path' => 'modify_accept_task', 'wrapper' => 'user-dig-msg'.$accept_task_node->nid, ), ); //这个 validate 好像去掉也可以 //$form['#validate'][] = 'accept_task_do_form_validate_ahah'; //$form['#submit'][] = 'accept_task_do_form_submit_ahah'; $form['delete'] = array( '#type'=>'button', //记住 这里切切要是 submit 如果是button的话 就会无法 执行 form_submit 也无法执行 form_validate '#value'=>'删除', //这里 #submit 和下面的 $form['#submit'][] 好像作用是一样的 //反正好像不能使用原来的,好像去掉也可以 //'#submit'=>array('accept_task_do_form_submit_ahah'), '#ahah' => array( 'event' => 'click', // This is the "magical path". Note that the parameter is an array of // the parents of the form item of the wrapper div! 'path' => 'delete_accept_task', 'wrapper' => 'user-dig-msg'.$accept_task_node->nid, ), ); $form['#after_build'][] = '_remove_form_item_div'; // var_dump($form); // $export = kprint_r($form,TRUE); // print drupal_to_js(array('data' => $export)); // $export = kprint_r($form, TRUE); // $vars - is a variable which you want to print. // print $export; return $form; } function shipingzhongcustomtask_image($image = FALSE, $alt = FALSE, $title = FALSE, $copy = FALSE) { if (is_object($image)) { return theme('imagecache', 'upload_element_preview', $image->filepath, $alt, $title) . '<br/><i>' . check_plain($copy) . '</li>'; } return '--'; } function _check_url($url){ if(!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/% =~_|]/i",$url)){ return false; } return true; } function accept_task_do_form_validate($form, &$form_state) { if(!empty($form_state['values']['field_acce_task_link'])){ if(!_check_url($form_state['values']['field_acce_task_link'])){ form_set_error('field_acce_task_link', '链接网址格式不对!'); } } } function accept_task_do_form_validate_ahah($form, &$form_state) { //form_set_error('field_acce_task_link', '链接网址格式不对_validate_ahah!'); accept_task_do_form_validate($form, $form_state); } function accept_task_do_form_submit($form, &$form_state) { global $user; $nid = $form_state['values']['nid']; $fid = $form_state['values']['fid']->fid; $fid1 = $form_state['values']['fid1']->fid; $field_acce_task_link = $form_state['values']['field_acce_task_link']; $field_acce_task_status = $form_state['values']['field_acce_task_status']; $node = node_load($nid); if(!empty($fid)){ $fid_obj = $form_state['values']['fid']; // upload_element_save($fid_obj, date('Y').'/'.date('m').'/'.date('d').'/'.$user->uid.'/', FILE_EXISTS_RENAME,'imagecache_preset'); // upload_element_save($fid_obj, 'dest/directory', FILE_EXISTS_RENAME); //好像不能创建二层目录 // var_dump($fid_obj); // var_dump('upload_element/'.date('Y-m-d').'-'.$user->uid); // var_dump(FILE_EXISTS_RENAME);exit; upload_element_save($fid_obj, 'upload_element/'.date('Y-m-d').'-'.$user->uid, FILE_EXISTS_RENAME); $node->field_acce_task_pic[0]['fid'] = $fid; } if(!empty($fid1)){ //这里使用 drupal自带的 upload 模块 好像效果不理想,自己自定义的一个文件这段吧 // $fid1_obj = $form_state['values']['fid1']; // //好像不能创建二层目录 // upload_element_save($fid1_obj, 'upload_element/'.date('Y-m-d').'-'.$user->uid, FILE_EXISTS_RENAME); // //$save_file_obj = db_fetch_object(db_query("SELECT * FROM {files} WHERE fid = %d", $fid1)); // $fid1_obj->source='fid1'; // file_set_status($fid1_obj, FILE_STATUS_PERMANENT); // $fid1_obj->destination =$fid1_obj->filepath; // $node->files = array($fid1=>$fid1_obj); // //删除原来的 // db_query("DELETE FROM {upload} WHERE nid = %d",$node->nid); // db_query("INSERT INTO {upload} (fid, nid, vid, description, list, weight) VALUES (%d, %d, %d, '%s', %d, %d)", // $fid1,$node->nid,$node->vid,$fid1_obj->filename,1,0); $fid1_obj = $form_state['values']['fid1']; // upload_element_save($fid_obj, date('Y').'/'.date('m').'/'.date('d').'/'.$user->uid.'/', FILE_EXISTS_RENAME,'imagecache_preset'); // upload_element_save($fid_obj, 'dest/directory', FILE_EXISTS_RENAME); //好像不能创建二层目录 upload_element_save($fid1_obj, 'upload_element/'.date('Y-m-d').'-'.$user->uid, FILE_EXISTS_RENAME); $node->field_acce_task_file[0]['fid'] = $fid1; } if(!empty($field_acce_task_link)){ $node->field_acce_task_link[0]['url'] = $field_acce_task_link; } if(!empty($field_acce_task_status)){ $node->field_acce_task_status[0]['value'] = $field_acce_task_status; } node_save($node); $content_field = content_fields('field_acce_task_status'); $allowed_values = content_allowed_values($content_field); drupal_set_message('任务状态更改为 '.$allowed_values[$field_acce_task_status]); } //function accept_task_do_form_submit_ahah($form, &$form_state) { // //form_set_error('field_acce_task_link', '链接网址格式不对_submit_ahah!'); // accept_task_do_form_submit($form, $form_state); //} function shipingzhongcustomtask_modify_accept_task_page() { $form_state = array('storage' => NULL, 'submitted' => FALSE); $form_build_id = $_POST['form_build_id']; $form = form_get_cache($form_build_id, $form_state); $args = $form['#parameters']; $form_id = array_shift($args); $form_state['post'] = $form['#post'] = $_POST; $form['#programmed'] = $form['#redirect'] = FALSE; // Process the form with drupal_process_form(), which calls the submit handlers that put whatever was worthy of keeping in the $form_state drupal_process_form($form_id, $form, $form_state); //进行上面的表单验单 提交操作的时候,好像用户的 $user->cache 这个时间戳就变了 就变成当前时间了 //同时上面的表单构建的时间 (是要比较$user->cache 时间和 $cache->created 时间的) //所以为了另一个ajax提交 我们人为的修改下 $user->cache global $user; //用户缓存时间 减去 7200吧 (两个小时之内吧) 如果不这么做的话 第二个 ajax 表单 及第二次ajax 之后 表单就无法提交 $user->cache = $user->cache-7200; if ($error = form_get_errors()){ $output = implode($error); // since there were errors, drupal_process_form has done its job. No need to rebuild $form. // Session messages would get displayed at the next regular request, but we're in AHAH here, so that won't happen. Make them go away. unset($_SESSION['messages']); } else { $content_field = content_fields('field_acce_task_status'); $allowed_values = content_allowed_values($content_field); $field_acce_task_status = $form_state['values']['field_acce_task_status']; $message = drupal_get_messages('status'); $output = '状态已更改为 <span class="hongse">'.$allowed_values[$field_acce_task_status].'</span>, 可以点击图像或文件<span class="hongse">更新</span>,看到实时效果'; // you might want more than one status message // $output = '状态已更改为 '.$allowed_values[$field_acce_task_status].''; // you might want more than one status message // everything has been submitted in drupal_process_form, so $form has to be rebuilt // var_dump($form_id); // var_dump($form_state); // var_dump($args); // var_dump($form_build_id); $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id); unset($_SESSION['messages']); // same as above } //好像这里不能用 一用的话 好像数据出错 //drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION); drupal_json(array('status' => TRUE, 'data' => $output)); exit; } function shipingzhongcustomtask_delete_accept_task_page() { $master_nid = db_result(db_query("SELECT nid FROM `content_field_accept_task` where field_accept_task_nid='%d'", intval($_POST['nid']))); $master_node = node_load($master_nid); $master_node->field_remaining_num[0]['value'] = intval($master_node->field_remaining_num[0]['value'])+1; //让主 发布任务node 保存 node_save($master_node); //删除任务 node_delete(intval($_POST['nid'])); //好像这里不能用 一用的话 好像数据出错 //drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION); //进行上面的 删除操作的时候,好像用户的 $user->cache 这个时间戳同样会变了 就变成当前时间了 //同时上面的表单构建的时间 (是要比较$user->cache 时间和 $cache->created 时间的) //所以为了另一个ajax提交 我们人为的修改下 $user->cache global $user; //用户缓存时间 减去 7200吧 (两个小时之内吧) 如果不这么做的话 第二个 ajax 表单 及第二次ajax 之后 表单就无法提交 $user->cache = $user->cache-7200; drupal_json(array('status' => TRUE, 'data' =>'已删除这个任务')); } function shipingzhongcustomtask_add_field_pub_task_type() { // $master_nid = db_result(db_query("SELECT nid FROM `content_field_accept_task` where field_accept_task_nid='%d'", intval($_POST['nid']))); // $master_node = node_load($master_nid); // $master_node->field_remaining_num[0]['value'] = intval($master_node->field_remaining_num[0]['value'])+1; // //让主 发布任务node 保存 // node_save($master_node); // //删除任务 // node_delete(intval($_POST['nid'])); //好像这里不能用 一用的话 好像数据出错 //drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION); drupal_json(array('status' => TRUE, 'data' =>'成功添加类型')); } function _show_all_files($arr_files) { if(empty($arr_files)){ return ''; } $file_str = ''; foreach($arr_files as $key=>$file) { $file_str .= '<br />'; $file_str .= '<a href="/'.$file->filepath.'" target="_blank" >'.$file->filename.'</a>'; } return $file_str; } //完成任务form function complete_task_form(&$form_state,$accept_task_nid)//accept_task_form 就是 $form_id { $form['nid'] = array( '#type' =>'hidden',//drupal 规定文本框用textfield来表示 '#default_value' => $accept_task_nid ); $form['status'] = array( '#type' =>'hidden',//drupal 规定文本框用textfield来表示 '#default_value' => 'completedo' ); $form['msg']= array( '#prefix'=>'<div id="user-dig-msg-completedo'.$accept_task_nid.'" class="user-dig-msg">', '#type'=>'item', '#suffix'=>'</div>', ); //提交按钮 ////提交按钮 // 好像如果使用 ahah ajax 提交 这里不能为 submit $form['submit1'] = array( '#type'=>'button', //记住 这里切切要是 submit 如果是button的话 就会无法 执行 form_submit 也无法执行 form_validate '#value'=>'确定', //这里 #submit 和下面的 $form['#submit'][] 好像作用是一样的 //反正好像不能使用原来的,好像去掉也可以 //'#submit'=>array('accept_task_do_form_submit_ahah'), '#ahah' => array( //'event' => 'click', // This is the "magical path". Note that the parameter is an array of // the parents of the form item of the wrapper div! 'path' => 'complete_redo_accept_task', 'wrapper' => 'user-dig-msg-completedo'.$accept_task_nid, ), ); return $form; } //重做任务form function redo_task_form(&$form_state,$accept_task_nid)//accept_task_form 就是 $form_id { $form['nid'] = array( '#type' =>'hidden',//drupal 规定文本框用textfield来表示 '#default_value' => $accept_task_nid ); $form['status'] = array( '#type' =>'hidden',//drupal 规定文本框用textfield来表示 '#default_value' => 'redo' ); $form['msg']= array( '#prefix'=>'<div id="user-dig-msg-redo'.$accept_task_nid.'" class="user-dig-msg">', '#type'=>'item', '#suffix'=>'</div>', ); //提交按钮 ////提交按钮 // 好像如果使用 ahah ajax 提交 这里不能为 submit $form['submit1'] = array( '#type'=>'button', //记住 这里切切要是 submit 如果是button的话 就会无法 执行 form_submit 也无法执行 form_validate '#value'=>'确定', //这里 #submit 和下面的 $form['#submit'][] 好像作用是一样的 //反正好像不能使用原来的,好像去掉也可以 //'#submit'=>array('accept_task_do_form_submit_ahah'), '#ahah' => array( //'event' => 'click', // This is the "magical path". Note that the parameter is an array of // the parents of the form item of the wrapper div! 'path' => 'complete_redo_accept_task', 'wrapper' => 'user-dig-msg-redo'.$accept_task_nid, ), ); return $form; } function shipingzhongcustomtask_complete_redo_accept_task($status='') { //这是接受的任务 node $node = node_load($_POST['nid']); $node->field_acce_task_status[0]['value'] = $_POST['status']; node_save($node); $content_field = content_fields('field_acce_task_status'); $allowed_values = content_allowed_values($content_field); $output = '状态已更改为 '.$allowed_values[$_POST['status']].""; // you might want more than one status message //好像这里不能用 一用的话 好像数据出错 //drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION); drupal_json(array('status' => TRUE, 'data' => $output)); } function _get_cntype_by_entype($entype) { $content_field = content_fields('field_pub_task_type'); $allowed_values = content_allowed_values($content_field); return $allowed_values[$entype]; } function add_pub_task_type_form() { // $str = "hi钓鱼岛是中国的"; // preg_match_all("/./u", $str, $arr); // print_r($arr[0]); // 文本框 name是文本框的名称 $form['add_pub_task_type'] = array( '#type' =>'textfield',//drupal 规定文本框用textfield来表示 '#title'=>'增加任务类型',//标题 显示在文本框前面 ); $form['submit'] = array( '#type'=>'submit', '#value'=>'增加' ); //提交按钮 // return $form; } //表单函数_validate 验证类似于钩子函数的回调函数 function add_pub_task_type_form_validate(&$form, &$form_state) { $add_pub_task_type = $form_state['values']['add_pub_task_type']; if(empty(trim($add_pub_task_type))) { form_set_error('add_pub_task_type','添加的任务类型不能为空'); } $content_field = content_fields('field_pub_task_type'); $allowed_values = content_allowed_values($content_field); if(in_array($add_pub_task_type,$allowed_values)){ form_set_error('add_pub_task_type','已存在这个任务类型'); } } function _get_pub_task_key($en_strs,$allowed_values) { if(array_key_exists($en_strs,$allowed_values)){ $en_strs=$en_strs.'1'; _get_pub_task_key($en_strs,$allowed_values); } return $en_strs; } //表单函数_submit 提交函数 function add_pub_task_type_form_submit(&$form, &$form_state) { $content_field = content_fields('field_pub_task_type'); $allowed_values = content_allowed_values($content_field); $add_pub_task_type = $form_state['values']['add_pub_task_type']; preg_match_all("/./u", $add_pub_task_type, $arr); include_once("Pinyin.class.php"); $pinyin = new Pinyin(); $en_arr = array(); foreach($arr[0] as $key=>$value){ $en_str=@$pinyin->strtopin($value); if(empty($en_str)){ $en_str = 'xxxx'; } $en_arr[] = trim($en_str); } $en_strs = implode('',$en_arr); $en_strs = _get_pub_task_key($en_strs,$allowed_values); $allowed_values[$en_strs] = $add_pub_task_type; $one_en_cn_arr = array(); foreach($allowed_values as $key=>$value){ $one_en_cn_arr[] = $key.'|'.$value; } $one_en_cn_str = implode(' ',$one_en_cn_arr); $query = "SELECT global_settings FROM {content_node_field} where field_name='field_pub_task_type'"; $global_settings = db_result(db_query($query)); $global_settings = unserialize($global_settings); $global_settings['allowed_values'] = $one_en_cn_str; $global_settings = serialize($global_settings); db_query("update {content_node_field} set global_settings='%s' where field_name='field_pub_task_type'", $global_settings); db_query("DELETE FROM {cache_content}"); drupal_set_message('增加的任务类型为'.$add_pub_task_type); }