ecshop二次开发--电子票

前台效果展示:

ecshop二次开发--电子票_第1张图片





















2.

ecshop二次开发--电子票_第2张图片













3.

ecshop二次开发--电子票_第3张图片























后台展示效果:

ecshop二次开发--电子票_第4张图片

















代码实现:

一、             添加菜单项:路径admin\includes\inc_menu.php

$modules['18_ticket_manage']['01_ticket_list']  = 'ticket.php?act=list';

$modules['18_ticket_manage']['02_ticket_add']  = 'ticket.php?act=add'; 

配置菜单语言

打开 /languages/zh_cn/admin/common.php文件,添加语言

//菜单—添加

$_LANG['18_ticket_manage'] = '电子票管理';

//电子票管理

$_LANG['01_ticket_list'] = '电子票列表';

$_LANG['02_ticket_add'] = '添加电子票';

二、上传图片路径:在upload\data\创建ticket_pic文件夹,图片上传路径

三:数据库:

建立ecs_ticket表

ecshop二次开发--电子票_第5张图片















建立ecs_ticket_cart

ecshop二次开发--电子票_第6张图片














、添加电子票(upload\admin\templates\ticket_add.htm)

1、创建添加电子票模板ticket_add.htm(自己按照ticket表更改布局);
(复制品牌模板upload\admin\templates\brand_info.htm);

以下是修改过后的ticket_add.html




    SHOP 管理中心 - 品牌管理 
    
    
    
    
    


电子票列表 SHOP 管理中心 - 添加电子票

电影名称 *
点击此处查看提示信息电影海报
开场时间
禁止入场时间
座位数量
票价
影片描述


四、创建upload\admin\ticket.php (复制品牌控制器upload\admin\brand.php)

以下是修改好的ticket.php

table("ticket"), $db, 'ticket_id', 'file_name');
/*------------------------------------------------------ */
//-- 电子票列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
    $smarty->assign('ur_here',      '电子票列表');
    $smarty->assign('action_link',  array('text' =>$_LANG['02_ticket_add'] , 'href' => 'ticket.php?act=add'));
    $smarty->assign('full_page',    1);

    //查询电子票信息(改动的部分)
    $sql = "select * from ". $GLOBALS['ecs']->table('ticket');
    $res = $GLOBALS['db']->query($sql); //$res资源类型
    $list = array();
    while($row = $GLOBALS['db']->fetchRow($res))
    {
        $row['run_time'] = local_date('Y-m-d H:i',$row['run_time']);
        $row['ban_time'] = local_date('Y-m-d H:i',$row['ban_time']);

        $list[] = $row;
    }
    $smarty->assign('ticket_list',$list);
    assign_query_info();
    $smarty->display('ticket_list.htm');
}
/*------------------------------------------------------ */
//-- 添加电子票
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'add')
{
    /* 权限判断 */
    admin_priv('goods_manage');
    $smarty->assign('ur_here',    '电子票添加');
    $smarty->assign('action_link', array('text' => $_LANG['01_ticket_list'], 'href' => 'ticket.php?act=list'));
    $smarty->assign('form_action', 'insert');
    $smarty->display('ticket_add.htm');
}
elseif ($_REQUEST['act'] == 'insert')
{
    /*检查电影名称是否重复*/
    admin_priv('ticket_manage');
    $is_only = $exc->is_only('film_name', $_POST['film_name']);
    if (!$is_only)
    {
        sys_msg(sprintf('已存在', stripslashes($_POST['film_name'])), 1);
    }
    //时间处理
    $run_time = local_strtotime($_POST['start_time']);
    $ban_time = local_strtotime($_POST['end_time']);
    /*处理图片*/
    $img_name = basename($image->upload_image($_FILES['film_pic'],'ticket_pic'));

    /*插入数据*/
    $sql = "INSERT INTO ".$ecs->table('ticket')."(film_name, film_pic, run_time, ban_time,ticket_num,ticket_price,film_desc) ".
        "VALUES ('$_POST[film_name]', '$img_name', '$run_time', '$ban_time', '$_POST[ticket_num]', '$_POST[ticket_price]', '$_POST[film_desc]')";
    $db->query($sql);
    admin_log($_POST['film_name'],'add','ticket');
    /* 清除缓存 */
    clear_cache_files();
    $link[0]['text'] ='继续添加';
    $link[0]['href'] = 'ticket.php?act=add';
    $link[1]['text'] = '返回列表';
    $link[1]['href'] = 'ticket.php?act=list';
    sys_msg('添加成功', 0, $link);
}
/*------------------------------------------------------ */
//-- 编辑品牌
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit')
{
    /* 权限判断 */
    admin_priv('brand_manage');
    $sql = "SELECT brand_id, brand_name, site_url, brand_logo, brand_desc, brand_logo, is_show, sort_order ".
        "FROM " .$ecs->table('brand'). " WHERE brand_id='$_REQUEST[id]'";
    $brand = $db->GetRow($sql);
    $smarty->assign('ur_here',     $_LANG['brand_edit']);
    $smarty->assign('action_link', array('text' => $_LANG['06_goods_brand_list'], 'href' => 'brand.php?act=list&' . list_link_postfix()));
    $smarty->assign('brand',       $brand);
    $smarty->assign('form_action', 'updata');
    assign_query_info();
    $smarty->display('brand_info.htm');
}
elseif ($_REQUEST['act'] == 'updata')
{
    admin_priv('brand_manage');
    if ($_POST['brand_name'] != $_POST['old_brandname'])
    {
        /*检查品牌名是否相同*/
        $is_only = $exc->is_only('brand_name', $_POST['brand_name'], $_POST['id']);
        if (!$is_only)
        {
            sys_msg(sprintf($_LANG['brandname_exist'], stripslashes($_POST['brand_name'])), 1);
        }
    }
    /*对描述处理*/
    if (!empty($_POST['brand_desc']))
    {
        $_POST['brand_desc'] = $_POST['brand_desc'];
    }
    $is_show = isset($_REQUEST['is_show']) ? intval($_REQUEST['is_show']) : 0;
    /*处理URL*/
    $site_url = sanitize_url( $_POST['site_url'] );
    /* 处理图片 */
    $img_name = basename($image->upload_image($_FILES['brand_logo'],'brandlogo'));
    $param = "brand_name = '$_POST[brand_name]',  site_url='$site_url', brand_desc='$_POST[brand_desc]', is_show='$is_show', sort_order='$_POST[sort_order]' ";
    if (!empty($img_name))
    {
        //有图片上传
        $param .= " ,brand_logo = '$img_name' ";
    }

    if ($exc->edit($param,  $_POST['id']))
    {
        /* 清除缓存 */
        clear_cache_files();
        admin_log($_POST['brand_name'], 'edit', 'brand');
        $link[0]['text'] = $_LANG['back_list'];
        $link[0]['href'] = 'brand.php?act=list&' . list_link_postfix();
        $note = vsprintf($_LANG['brandedit_succed'], $_POST['brand_name']);
        sys_msg($note, 0, $link);
    }
    else
    {
        die($db->error());
    }
}

/*------------------------------------------------------ */
//-- 编辑品牌名称
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_brand_name')
{
    check_authz_json('brand_manage');

    $id     = intval($_POST['id']);
    $name   = json_str_iconv(trim($_POST['val']));

    /* 检查名称是否重复 */
    if ($exc->num("brand_name",$name, $id) != 0)
    {
        make_json_error(sprintf($_LANG['brandname_exist'], $name));
    }
    else
    {
        if ($exc->edit("brand_name = '$name'", $id))
        {
            admin_log($name,'edit','brand');
            make_json_result(stripslashes($name));
        }
        else
        {
            make_json_result(sprintf($_LANG['brandedit_fail'], $name));
        }
    }
}

elseif($_REQUEST['act'] == 'add_brand')
{
    $brand = empty($_REQUEST['brand']) ? '' : json_str_iconv(trim($_REQUEST['brand']));

    if(brand_exists($brand))
    {
        make_json_error($_LANG['brand_name_exist']);
    }
    else
    {
        $sql = "INSERT INTO " . $ecs->table('brand') . "(brand_name)" .
            "VALUES ( '$brand')";

        $db->query($sql);
        $brand_id = $db->insert_id();

        $arr = array("id"=>$brand_id, "brand"=>$brand);

        make_json_result($arr);
    }
}
/*------------------------------------------------------ */
//-- 编辑排序序号
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_sort_order')
{
    check_authz_json('brand_manage');

    $id     = intval($_POST['id']);
    $order  = intval($_POST['val']);
    $name   = $exc->get_name($id);

    if ($exc->edit("sort_order = '$order'", $id))
    {
        admin_log(addslashes($name),'edit','brand');

        make_json_result($order);
    }
    else
    {
        make_json_error(sprintf($_LANG['brandedit_fail'], $name));
    }
}

/*------------------------------------------------------ */
//-- 切换是否显示
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'toggle_show')
{
    check_authz_json('brand_manage');
    $id     = intval($_POST['id']);
    $val    = intval($_POST['val']);
    $exc->edit("is_show='$val'", $id);
    make_json_result($val);
}

/*------------------------------------------------------ */
//-- 删除品牌
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
    check_authz_json('brand_manage');
    $id = intval($_GET['id']);
    /* 删除该品牌的图标 */
    $sql = "SELECT brand_logo FROM " .$ecs->table('brand'). " WHERE brand_id = '$id'";
    $logo_name = $db->getOne($sql);
    if (!empty($logo_name))
    {
        @unlink(ROOT_PATH . DATA_DIR . '/brandlogo/' .$logo_name);
    }
    $exc->drop($id);
    /* 更新商品的品牌编号 */
    $sql = "UPDATE " .$ecs->table('goods'). " SET brand_id=0 WHERE brand_id='$id'";
    $db->query($sql);
    $url = 'brand.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
    ecs_header("Location: $url\n");
    exit;
}
/*------------------------------------------------------ */
//-- 删除品牌图片
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'drop_logo')
{
    /* 权限判断 */
    admin_priv('brand_manage');
    $brand_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
    /* 取得logo名称 */
    $sql = "SELECT brand_logo FROM " .$ecs->table('brand'). " WHERE brand_id = '$brand_id'";
    $logo_name = $db->getOne($sql);
    if (!empty($logo_name))
    {
        @unlink(ROOT_PATH . DATA_DIR . '/brandlogo/' .$logo_name);
        $sql = "UPDATE " .$ecs->table('brand'). " SET brand_logo = '' WHERE brand_id = '$brand_id'";
        $db->query($sql);
    }
    $link= array(array('text' => $_LANG['brand_edit_lnk'], 'href' => 'brand.php?act=edit&id=' . $brand_id), array('text' => $_LANG['brand_list_lnk'], 'href' => 'brand.php?act=list'));
    sys_msg($_LANG['drop_brand_logo_success'], 0, $link);
}

/*------------------------------------------------------ */
//-- 排序、分页、查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $brand_list = get_brandlist();
    $smarty->assign('brand_list',   $brand_list['brand']);
    $smarty->assign('filter',       $brand_list['filter']);
    $smarty->assign('record_count', $brand_list['record_count']);
    $smarty->assign('page_count',   $brand_list['page_count']);

    make_json_result($smarty->fetch('brand_list.htm'), '',
        array('filter' => $brand_list['filter'], 'page_count' => $brand_list['page_count']));
}
/**
 * 获取品牌列表
 *
 * @access  public
 * @return  array
 */
function get_brandlist()
{
    $result = get_filter();
    if ($result === false)
    {
        /* 分页大小 */
        $filter = array();

        /* 记录总数以及页数 */
        if (isset($_POST['brand_name']))
        {
            $sql = "SELECT COUNT(*) FROM ".$GLOBALS['ecs']->table('brand') .' WHERE brand_name = \''.$_POST['brand_name'].'\'';
        }
        else
        {
            $sql = "SELECT COUNT(*) FROM ".$GLOBALS['ecs']->table('brand');
        }

        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        $filter = page_and_size($filter);

        /* 查询记录 */
        if (isset($_POST['brand_name']))
        {
            if(strtoupper(EC_CHARSET) == 'GBK')
            {
                $keyword = iconv("UTF-8", "gb2312", $_POST['brand_name']);
            }
            else
            {
                $keyword = $_POST['brand_name'];
            }
            $sql = "SELECT * FROM ".$GLOBALS['ecs']->table('brand')." WHERE brand_name like '%{$keyword}%' ORDER BY sort_order ASC";
        }
        else
        {
            $sql = "SELECT * FROM ".$GLOBALS['ecs']->table('brand')." ORDER BY sort_order ASC";
        }

        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($rows = $GLOBALS['db']->fetchRow($res))
    {
        $brand_logo = empty($rows['brand_logo']) ? '' :
            '.$GLOBALS[';
        $site_url   = empty($rows['site_url']) ? 'N/A' : ''.$rows['site_url'].'';

        $rows['brand_logo'] = $brand_logo;
        $rows['site_url']   = $site_url;

        $arr[] = $rows;
    }

    return array('brand' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

?>


以下代码是ticket_list.html用来显示电子票



{if $full_page}
{include file="pageheader.htm"}
{insert_scripts files="../js/utils.js,listtable.js"}

{include file="brand_search.htm"}
{/if} {foreach from=$ticket_list item=ticket} {/foreach}
电影名称 开场时间 禁止时间 总票数 票价 操作
{$ticket.film_name} {$brand.brand_name|escape:html} {$ticket.run_time} {$ticket.ban_time} {$ticket.ticket_num} {$ticket.ticket_price} 编辑 | 删除
{include file="page.htm"}
{if $full_page}
{include file="pagefooter.htm"} {/if}

到达这里之后,后台添加也就完成了下面是前台的展示效果代码:

一、添加导航栏 :电子票
         打开后台---系统设置—自定义导航栏--添加导航

ecshop二次开发--电子票_第7张图片


















二、在upload下建立一个控制器ticket.php

以下代码是ticket.php

caching = true;
}
/*------------------------------------------------------ */
//-- INPUT
/*------------------------------------------------------ */
/* 获得请求的分类 ID */
if (!empty($_REQUEST['id']))
{
    $ticket_id = intval($_REQUEST['id']);
         //echo $ticket_id;die;
}
if (empty($ticket_id))
{  
         assign_template();
         $position = assign_ur_here('', $_LANG['all_ticket']);
         $smarty->assign('page_title',      $position['title']);    // 页面标题
         $smarty->assign('ur_here',         $position['ur_here']);  // 当前位置
         $smarty->assign('categories',      get_categories_tree()); // 分类树
         $smarty->assign('helps',           get_shop_help());       // 网店帮助
         $smarty->assign('top_goods',       get_top10());           // 销售排行
         $sql ="select * from " . $GLOBALS['ecs']->table('ticket');
         $res = $GLOBALS['db']->query($sql);
         $ticket_list = $GLOBALS['db']->getAll($sql);
         $smarty->assign('ticket_list', $ticket_list);
    $smarty->display('ticket_list.dwt');

    exit();

}
$ticket_info = get_ticket_info($ticket_id); // 电子票列表
$seats = str_replace('排', '_', $ticket_info['unavailable']);
$arr = explode(',', str_replace('座', '', $seats));
$ticket_info['unavailable'] = json_encode($arr, true);
 #$sql="select * from ecs_ticket where ticket_id = '$ticket_id'";
 #$arr=$GLOBALS['db']->getAll($sql);
 #print_r($arr);
//print_r($ticket_info);die;
if (empty($ticket_info))
{
         ecs_header("Location: ./\n");
         exit;
}
$smarty->assign('data_dir',    DATA_DIR);
// 赋值固定内容
assign_template();
$position = assign_ur_here($cate, $ticket_info['ticket_name']);
$smarty->assign('page_title',     $position['title']);   // 页面标题
$smarty->assign('ur_here',        $position['ur_here']); // 当前位置
//print_r($position);die;
$smarty->assign('categories',     get_categories_tree());        // 分类树
$smarty->assign('helps',          get_shop_help());              // 网店帮助
$smarty->assign('top_goods',      get_top10());                  // 销售排行
$smarty->assign('show_marketprice', $_CFG['show_marketprice']);
$smarty->assign('ticket', $ticket_info);
$smarty->display('ticket_info.dwt');
/*------------------------------------------------------ */
//-- PRIVATE FUNCTION
/*------------------------------------------------------ */
/**
 * 获得指定电子票的详细信息
 *
 * @access  private
 * @param   integer $id
 * @return  void
 */
function get_ticket_info($id)
{
    $sql = 'SELECT * FROM ' . $GLOBALS['ecs']->table('ticket') . " WHERE ticket_id = '$id'";    
    $row = $GLOBALS['db']->getRow($sql);    
         $row['run_time'] = local_date('Y-m-d H:i', $row['run_time']);
         $row['ban_time'] = local_date('Y-m-d H:i', $row['ban_time']);
         return $row;
}
?>

三、电子票列表模板

1、新建upload\themes\default\ticket_list.dwt(自己布局)

以下是ticket_list.dwt








{$page_title}






{* 包含脚本文件 *}
{insert_scripts files='common.js'}




{$ticket_data.ticket_pic|escape:html}
余票:{$ticket_data.ticket_num}张|价格:{$ticket_data.ticket_price}元|购买


创建电子票详情ticket_info.dwt(自己布局);
(复制 goods.dwt模板upload\themes\default\goods.dwt);

以下是ticket_info.dwt







	

{$page_title}











	
	
	

{$lang.ticket_info}

{$ticket.film_name}

{$lang.film_start_c}:{$ticket.run_time}

{$lang.film_end_c}:{$ticket.ban_time}

{$lang.film_act_er}:{$ticket.film_desc}

{$lang.film_price_c}:{$ticket.ticket_price}{$lang.film_unit} {$lang.film_remain}:{$surplus_num}{$lang.film_spread}

{$lang.film_check_seat}

{$lang.film_pm_1}

{$lang.film_check_seat}:

    {$lang.film_checked_seat}:

    {$lang.film_all}:0{$lang.film_unit}

    {insert_scripts files='js/seat/jquery-1.8.3.min.js,seat/jquery.seat-charts.min.js'}

    3、找到seat插件,整体文件夹放到upload/js中;

    ecshop二次开发--电子票_第8张图片










    生成订单upload\flow.php

    把以下代码放到if ($_REQUEST['step'] == 'add_to_cart')前面

    /* 购买电子票
    * */
    if ($_REQUEST['step'] == 'ticket')
    {   
                  include_once('includes/cls_json.php');
                  $json  = new JSON;     
                  $_POST['goods'] = strip_tags(urldecode($_POST['goods']));
                  $_POST['goods'] = json_str_iconv($_POST['goods']);
                  $goods = $json->decode($_POST['goods']);
                  //print_r($goods);die;
           /*
         * 检查用户是否已经登录
         * 如果用户已经登录了则检查是否有默认的收货地址
         * 如果没有登录则跳转到登录和注册页面
         */
        if (empty($_SESSION['direct_shopping']) && $_SESSION['user_id'] == 0)
        {
                  $result['error'] = 1;
                  $result['message'] = "请先登录";
            die($json->encode($result));
        }
           /* 检查:商品数量是否合法 */
           if (empty($goods->number))
           {
                  $result['error']   = 2;
                  $result['message'] = '还没有选座';         
                   echo  $json->encode($result);die;
           }
           /* 检查:商品数量是否合法*/
    	   /*
           if ($goods->number > 1)
           {
                  $result['error']   = 5;
                  $result['message'] = '系统正在升级,只能选一张票';            
                  echo  $json->encode($result);die;
           } 
    	   */
           $spec = explode(',', trim(str_replace('座', '座,', $goods->spec), ',')); // 座位转换成数组格式     
           // 随机生成票号
           $string = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';
           $uid = $_SESSION['user_id'];    
           for($j = 0; $j < count($spec); $j++){
                  $str = '';
                  for ($i=0; $i < 10; $i++) { 
                         $str.= $string[rand(0,strlen($string)-1)];
                  }                    
                  $data[] = $uid.$str;
           }     
           
        /* 取得商品信息 */
        $ticket_info = "SELECT * FROM " .$GLOBALS['ecs']->table('ticket'). " WHERE ticket_id = '".$goods->goods_id ."'";
        $ticket = $GLOBALS['db']->getRow($ticket_info);
        //此处出现问题bug未解决
           foreach($spec as $key => $val)
           {          
                  /* 初始化要插入购物车的基本件数据 */
                  $parent[] = array(
                         'user_id'        => $_SESSION['user_id'],
                         'ticket_order'   => $data[$key],
                         'ticket_id'      => $goods->goods_id,
                         'film_name'      => $ticket['film_name'],
                         'ticket_price'   => $ticket['ticket_price'],
                         'seat'           =>  $val
                  );     
           }
           foreach($spec as $sp) // 查询其他人是否已购买
           {
                  $seat = "SELECT seat FROM " .$GLOBALS['ecs']->table('ticket_cart'). " WHERE seat = '".$sp."' AND ticket_id = '" . $goods->goods_id ."'";  
                  $one = $GLOBALS['db']->getOne($seat);         
                  if($one) 
                  {
                         $result['error'] = 4;
                         $result['message'] = '座位已被预定';                
                  }
                  else
                  {
                         //购物车没有此物品,则插入
                         foreach($parent as $p)
                         {
    						$GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('ticket_cart'), $p, 'INSERT'); 
    						$sql_select="select unavailable from ".$GLOBALS['ecs']->table('ticket')."where ticket_id='$p[ticket_id]'";
    						$str=$GLOBALS['db']->getOne($sql_select);
    						//$seat接过来的值 如果数据库为空则添加数据 否则 就拼接原数据+,+新数据
    						if(empty($str)){
    						   $seat=$p['seat'];
    						}else{
    							$seat=$str.",".$p['seat'];
    						}
    						$sql_update="update".$GLOBALS['ecs']->table('ticket')."set unavailable='$seat' where ticket_id='$p[ticket_id]'";
    						$GLOBALS['db']->getAll($sql_update);	
                                       $result['error'] = 3;
                                       $result['message'] = "购买成功";
                         }                     
                  }
           } 
           echo  $json->encode($result);die;
    }
    elseif($_REQUEST['step'] == 'ticket_done')
    {
                  $sql = " select * from ". $GLOBALS['ecs']->table('ticket_cart').
                  " where user_id = ".$_SESSION['user_id'];          
                  $carts = $GLOBALS['db']->getAll($sql);
                  $smarty->assign('carts', $carts);                          
                  $num = count($carts); // 总票数
                  $smarty->assign('num', $num);     
                  $price = '';
                  foreach ($carts as $key => $val)
                  {
                         $price    +=  $val['ticket_price'];
                  }
                  $smarty->assign('total_price', $price);            
    }


    6、找到themes\default\flow.dwt(生成订单列表)

    (搜索)把以下代码放到其前面

    
    
             
    {$lang.remember_order_number}:
    票号 电影名称 座位号
    {$cart.ticket_order} {$cart.film_name} {$cart.seat}
    总票数:{$num} 总金额:{$total_price} 元

    ok,一切完成。


    你可能感兴趣的:(ecshop二次开发)