init(); // 顺风城市编码 //kernel::single('logisticsmanager_citycode_sf')->install(); $this->insertTemplate(); $this->_addExtendWaybillTmpl(); } function xml_to_array( $xml ) { $reg = "/<(\\w+)[^>]*?>([\\x00-\\xFF]*?)<\\/\\1>/"; if(preg_match_all($reg, $xml, $matches)) { $count = count($matches[0]); $arr = array(); for($i = 0; $i < $count; $i++) { $key = $matches[1][$i]; $val = $this->xml_to_array( $matches[2][$i] ); // 递归 if(array_key_exists($key, $arr)) { if(is_array($arr[$key])) { if(!array_key_exists(0,$arr[$key])) { $arr[$key] = array($arr[$key]); } }else{ $arr[$key] = array($arr[$key]); } $arr[$key][] = $val; }else{ $arr[$key] = $val; } } return $arr; }else{ return $xml; } } function unescape($str){ $ret = ''; $len = strlen($str); for ($i = 0; $i < $len; $i++){ if ($str[$i] == '%' && $str[$i+1] == 'u'){ $val = hexdec(substr($str, $i+2, 4)); if ($val < 0x7f) { $ret .= chr($val); } else if($val < 0x800) { $ret .= chr(0xc0|($val>>6)).chr(0x80|($val&0x3f)); } else { $ret .= chr(0xe0|($val>>12)).chr(0x80|(($val>>6)&0x3f)).chr(0x80|($val&0x3f)); } $i += 5; } else if ($str[$i] == '%') { $ret .= urldecode(substr($str, $i, 3)); $i += 2; } else { $ret .= $str[$i]; } } return $ret; } /** * 插入模板 * * @return void * @author **/ private function insertTemplate() { $templateObj = app::get("logisticsmanager")->model("express_template"); $tmpls = app::get("ome")->model("print_tmpl")->getList(); if ($tmpls) { foreach ($tmpls as $key => $val) { $tmplData = $prt_tmpl_data = array(); $newTmplStr = $imgUrl = ""; $width = $height = 0; // 转换背景,计算宽、高 $imgUrl = kernel::single('base_storager')->getUrl($val['file_id'],"file"); if ($imgUrl) { list($widthImg, $heightImg) = getimagesize($imgUrl); $width = intval($widthImg*25.4/96); $height = intval($heightImg*25.4/96); } else { $width = intval($val['prt_tmpl_width']); $height = intval($val['prt_tmpl_height']); $imgUrl = 'NONE'; } $newTmplStr .= "paper:".$width.",".$height.",".$imgUrl.";\n"; //解析老模板数据 $prt_tmpl_data = $this->xml_to_array(urldecode($val['prt_tmpl_data'])); foreach($prt_tmpl_data['printer']['item'] as $k => $v) { //判断标签类型 if ($v['ucode'] == 'text') { $newTmplStr .= "report_label:"; $ucode = ''; } else { $newTmplStr .= "report_field:"; $ucode = $v['ucode']; } //解码标签名称和字体 $v['name'] = $this->unescape($v['name']); $v['font'] = $this->unescape($v['font']); //转换样式信息 $font = ($v['font']=='undefined') ? '宋体' : $this->unescape($v['font']); $fontsize = intval($v['fontsize']*72*10/96); switch($v['align']) { case 'center' : $align = 1; break; case 'right' : $align = 2; break; default: $align = 0; break; } //计算标签位置 $position = array(); $position = explode(':',$v['position']); $left = number_format($position[0]/96,6); $top = number_format($position[1]/96,6); $right = number_format(($left+$position[2]/96),6); $bottom = number_format(($top+$position[3]/96),6); //生成模板数据 $newTmplStr .= $left.",".$top.",".$right.",".$bottom.",".$v['name'].",".$ucode.",0,".$font.",".$fontsize.",".$v['border'].",".$v['italic'].",0,0,0,".$align.",0;\n"; unset($v); } $newTmplData = array(); $newTmplData['template_id'] = $val['prt_tmpl_id']; $newTmplData['template_name'] = $val['prt_tmpl_title']; $newTmplData['template_type'] = 'normal'; $newTmplData['status'] = $val['shortcut']; $newTmplData['template_width'] = $width; $newTmplData['template_height'] = $height; $newTmplData['file_id'] = $val['file_id']; $newTmplData['template_data'] = $newTmplStr; $templateObj->insert($newTmplData); unset($val); } } $sql = <<exec($sql); } //初始化电子面单模板 private function _addExtendWaybillTmpl(){ $dlytpl_dir = ROOT_DIR."/app/logisticsmanager/initial/tpl/"; if($handle = opendir($dlytpl_dir)){ while(false !== ($dtp = readdir($handle))){ $path_parts = pathinfo($dtp); if($path_parts['extension'] == 'dtp'){ $file['tmp_name'] = $dlytpl_dir.$dtp; $file['name'] = $dtp; $result = kernel::single('logisticsmanager_print_tmpl')->upload_tmpl($file); } } closedir($handle); } } }