function _training_register() { $invalid = _check_training_data(); $result = array(); if (count($invalid) > 0) { _json_die("以下字段输入错误:" . implode(",", $invalid)); } $salon = get_post(trim($_POST['salon_id'])); //前面已经验证过salon_id的有效性 $salon_meta = get_post_meta($salon->ID); if (!$salon_meta['opening'][0]) { //沙龙已经关闭报名 _json_die('已经关闭报名'); } $max_audience = (int) $salon_meta['max_audience'][0]; $post_title = $salon->ID . '-' . $salon->post_title; //检查姓名和电话是否存在 $post = _get_register_post(trim($_POST['salon_id']), trim($_POST['name']), trim($_POST['mobile']), POST_TYPE_TRAINING_REGISTER); if ($post) { _json_die('您的姓名和电话已经存在,请不要重复报名!'); } //插入post $post_id = wp_insert_post(array('post_type' => POST_TYPE_TRAINING_REGISTER, 'post_status' => 'pending', 'post_title' => $post_title, 'post_content' => '')); if ($post_id) { $success = true; //插入meta if (!add_post_meta($post_id, 'salon_id', array($_POST['salon_id']))) { $success = false; } else { $keys = array('area', 'city', 'classtype', 'classseq', 'name', 'bodhi_name', 'gender', 'mobile', 'idcode', 'email', 'position', 'other_position', 'food', 'lodge'); foreach ($keys as $k) { if (!empty($_POST[$k]) && !add_post_meta($post_id, $k, $_POST[$k])) { $success = false; break; } } } if (!$success) { wp_delete_post($post_id, true); _json_die('插入meta数据错误'); } wp_update_post(array('ID' => $post_id, 'post_title' => $post_title . '(' . $_POST['name'] . ')')); $ret = array('id' => $post_id, 'success' => true); if ($max_audience > 0 && _get_salon_register_count($salon->ID) > $max_audience) { $ret['message'] = '报名人数已满,您的信息已经被保存,请等待短信或邮件通知!'; } else { $ret['message'] = '您的信息已经被保存,请留意您的短信或邮件通知!'; } _json_die($ret); } else { _json_die('wp_insert_post 失败'); } }
function ajax_salon_export_all() { require 'PHPExcel/PHPExcel.php'; $ret = array('success' => false); $post_id = (int) trim($_REQUEST['id']); if (!($post = get_post($post_id))) { $ret['message'] = '参数错误!'; _json_die($ret); } $salon_meta = get_post_meta($post_id); $registers = _get_salon_registers($post_id); //create excel $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); $sheet = $objPHPExcel->getActiveSheet(); $sheet->setTitle($post->post_title); $sheet->mergeCells('A1:E1'); $sheet->setCellValue('A1', $post->post_title); $sheet->mergeCells('A2:C2'); $sheet->setCellValue('A2', '活动地址:'); $sheet->mergeCells('D2:E2'); $sheet->setCellValue('D2', '导出日期:' . date("Y-m-d")); $sheet->getRowDimension(1)->setRowHeight(35); $sheet->getRowDimension(2)->setRowHeight(28); $sheet->getRowDimension(3)->setRowHeight(30); $sheet->setCellValue('A3', '序号')->setCellValue('B3', '姓 名')->SetCellValue('C3', '法 名')->SetCellValue('D3', '性别')->SetCellValue('E3', '手 机'); $max_rows = 3 + count($registers); $sheet->getStyle("A1:I" . $max_rows)->getFont()->setName('宋体'); $sheet->getStyle("A1:I" . $max_rows)->getAlignment()->setWrapText(true); $sheet->getStyle("A1:I" . $max_rows)->getFont()->setSize(11); $sheet->getStyle("A1")->getFont()->setSize(16); $sheet->getStyle("A2:I2")->getFont()->setSize(12); $sheet->getColumnDimension('A')->setWidth(10); $sheet->getColumnDimension('B')->setWidth(16); $sheet->getColumnDimension('C')->setWidth(14); $sheet->getColumnDimension('D')->setWidth(10); $sheet->getColumnDimension('E')->setWidth(16); $sheet->getStyle("A1:I" . $max_rows)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $sheet->getStyle("A1:I" . $max_rows)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); $sheet->getStyle("A2")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $sheet->getStyle("D2")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); $sheet->getStyle("A3:E" . $max_rows)->applyFromArray(array('borders' => array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN)))); $i = 4; foreach ($registers as $r) { $meta = get_post_meta($r->ID); $sheet->setCellValue('A' . $i, $i - 3)->setCellValue('B' . $i, $meta['name'][0])->setCellValue('C' . $i, $meta['bodhi_name'][0])->setCellValue('D' . $i, $meta['gender'][0])->setCellValue('E' . $i, $meta['mobile'][0]); $sheet->getRowDimension($i)->setRowHeight(20); $i = $i + 1; } header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="salon.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); return $objWriter->save('php://output'); }