<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class SortAction extends AuthorAction
{
    public function run()
    {
        $chapter = $this->getController()->getChapter();
        
    	$drafts = $this->db()->select(array('id'))->from(Draft::table())->where(array('chapterid'=>$chapter['chapterid']))->find();
    	$ids = array();
    	foreach ($drafts as $draft)
    	{
    		$ids[] = $draft['id'];
    	}
    
    	foreach ($_POST as $key => $value)
    	{
    		if (false !== strpos($key, 'sort'))
    		{
    			$id = array_pop(explode('_', $key));
    			if (!empty($ids) && in_array($id, $ids))
    			{
    				$sql = "UPDATE `".Draft::table()."` SET `sort` = '$value' WHERE id='$id'";
    				$this->db()->query($sql);
    			}
    		}
    	}
    	
    	$message[] = '排序成功';
    	$message[] = '返回'.ilink('续传列表', $this->getController()->url('system',array('s'=>'author','c'=>'draft','cid'=>$chapter['chapterid']))).'.'.ilink('章节列表',$this->getController()->url('system',array('s'=>'author','c'=>'chapter','bid'=>$chapter['bookid']))).'.'.ilink('作者中心', $this->getController()->url('system',array('s'=>'author')));
    	$this->showMessage($message);
    }
}