<?php

/**
 * 下载
 */

namespace Module\Download;

use Pyramid\Component\HttpFoundation\Response;
use Pyramid\Component\HttpFoundation\RedirectResponse;
use Exception;

entity_register('download', array(
    'primaryKey' => 'id',
    'baseTable'  => 'download',
));

config()->set('download_category', array(
    '0' => '说明资料下载',
    '1' => '收费标准下载',
));

class Download {
    
    /**
     * 列表
     * @access download
     * @route /admin/download
     * @route /admin/download/list
     */
    public static function listdownload($request) {
        $keywords = $request->getParameter('keywords');
        $page = $request->getParameter('page');
        $query = db_select('download', 'd')->extend('Pager')->size(50)->page($page);
        $query->fields('d')->condition('status',1)->orderBy('timeline', 'DESC')->orderBy('id','DESC');
        $data  = $query->execute()->fetchAll();
        $pager = $query->fetchPager();
        return new Response(render('/admin/download/download.list.html', array(
            'pager' => $pager,
            'data'  => $data,
        )));
    }
    
    /**
     * 添加
     * @access download_add
     * @route /admin/download/add
     */
    public static function downloadAdd($request) {
        if ($request->post->getParameters()) {
            $download = (object) $request->post->getParameters();
            $download->created = $download->updated = time();
            try {
                entity_insert('download', $download);
                return new RedirectResponse($request->getUriForPath('/admin/download'), 1, '添加成功');
            } catch (Exception $e) {
                return new RedirectResponse($request->getUriForPath('/admin/download'), 4, $e->getMessage());
            }
        }
        return new Response(render('/admin/download/download.add.html', array(
            'products' => db_query("select id,name from {product} where status=1")->fetchAllKeyed(),
        )));
    }
    
    /**
     * 修改
     * @access download_edit
     * @route /admin/download/edit/{id}
     */
    public static function downloadEdit($request) {
        $id = $request->route->getParameter('id');
        if (!$id) {
            return new RedirectResponse($request->getUriForPath('/admin/download'), 4, '无此记录');
        }
        if (!$download = entity_object('download', $id)) {
            return new RedirectResponse($request->getUriForPath('/admin/download'), 4, '无此记录');
        }
        if ($request->post->getParameters()) {
            $download = (object) $request->post->getParameters();
            $download->id = $id;
            $download->updated = time();
            try {
                entity_update('download', $download);
                return new RedirectResponse($request->getUriForPath('/admin/download'), 1, '更新成功');
            } catch (Exception $e) {
                return new RedirectResponse($request->getUriForPath('/admin/download'), 4, $e->getMessage());
            }
        }
        return new Response(render('/admin/download/download.edit.html', array(
            'data' => $download,
            'products' => db_query("select id,name from {product} where status=1")->fetchAllKeyed(),
        )));
    }

    /**
     * 删除
     * @access download_delete
     * @route /admin/download/delete/{id}
     */
    public static function downloadDelete($request) {
        validateRequestToken($request, 600);
        $id = $request->route->getParameter('id');
        if (!$id) {
            return new RedirectResponse($request->getUriForPath('/admin/download'), 4, '无此记录');
        }
        if (!$download = entity_object('download', $id)) {
            return new RedirectResponse($request->getUriForPath('/admin/download'), 4, '无此记录');
        }
        $storeUpdate = (object) array('id'=>$download->id,'status'=>0);
        entity_update('download', $storeUpdate);            
        return new RedirectResponse($request->getUriForPath('/admin/download'), 1, '操作成功');
    }

}


permission_register(array(
    'download' => array(
        'bundle'        => '资料下载',
        'title'         => '下载管理',
        'description'   => '下载一览',
    ),
    'download_add' => array(
        'bundle'        => '资料下载',
        'title'         => '添加下载',
        'description'   => '',
    ),
    'download_edit' => array(
        'bundle'        => '资料下载',
        'title'         => '修改下载',
        'description'   => '',
    ),
    'download_delete' => array(
        'bundle'        => '资料下载',
        'title'         => '删除下载',
        'description'   => '',
    ),
));
