實現功能,操作項可以單獨刪除,批量項可以批量刪除,批量顯示,隱藏(改廣告的數據庫的status_is的值);
1.視圖關鍵點:
復制代碼 代碼如下:
<td ><input type="checkbox" name="id[]" value="<?php echo $row->id?>">
<?php echo $row->id?></td>
復制代碼 代碼如下:
<div class="fixsel">
<input type="checkbox" name="chkall" id="chkall" onclick="checkAll(this.form, 'id')" />
<label for="chkall">全選</label>
<select name="command">
<option>選擇操作</option>
<option value="adDelete">刪除</option>
<option value="adVerify">顯示</option>
<option value="adUnVerify">隱藏</option>
</select>
<input id="submit_maskall" class="button confirmSubmit" type="submit" value="提交" name="maskall" />
</div>
復制代碼 代碼如下:
/*單個刪除*/
<a href="<?php echo $this->createUrl('batch',array('command'=>'adDelete', 'id'=>$row->id))?>" class="confirmSubmit">
2.控制器:
復制代碼 代碼如下:
/**
* 批量操作
*
*/
public function actionBatch ()
{
if (XUtils::method() == 'GET') { //單個刪除是get方式
$command = trim($_GET['command']);
$ids = intval($_GET['id']);
} else
if (XUtils::method() == 'POST') {
$command = trim($_POST['command']);
$ids = $_POST['id'];
is_array($ids) && $ids = implode(',', $ids);
} else {
XUtils::message('errorBack', '只支持POST,GET數據');
}
empty($ids) && XUtils::message('error', '未選擇記錄');
switch ($command) {
case 'adDelete':
parent::_acl('ad_delete');
parent::_adminiLogger(array('catalog'=>'delete', 'intro'=>'刪除廣告,ID:'.$ids));
parent::_delete(new Ad(), $ids, array ('ad' ), array ('attach_file' ));
break;
case 'adVerify':
parent::_acl('ad_verify');
parent::_adminiLogger(array('catalog'=>'delete', 'intro'=>'廣告狀態變更為顯示,ID:'.$ids));
parent::_verify(new Ad(), 'verify', $ids, array ('ad' ));
break;
case 'adUnVerify':
parent::_acl('ad_verify');
parent::_adminiLogger(array('catalog'=>'delete', 'intro'=>'廣告狀態變更為隱藏,ID:'.$ids));
parent::_verify(new Ad(), 'unVerify', $ids, array ('ad' ));
break;
default:
throw new CHttpException(404, '錯誤的操作類型:' . $command);
break;
}
}
父類的刪除函數:
復制代碼 代碼如下:
protected function _delete ($model = null, $id = '', $redirect = 'index', $attach = null, $pkField = 'id')
{
if ($attach) { //如果有附件要刪除廣告的圖片
$data = $model->findAll($pkField . ' IN(:id)', array (':id' => $id ));
foreach ((array) $data as $row) {
foreach ((array) $attach as $value) {
if (! empty($row[$value])) {
@unlink($row[$value]);
}
}
}
}
$result = $model->deleteAll(array ('condition' => 'id IN(' . $id . ')' ));
//刷新緩存
self::_refreshCache($model);
$this->redirect($redirect);
}
復制代碼 代碼如下:
protected function _verify ($model = null, $type = 'verify', $id = '', $redirect = 'index', $cdField = 'status_is', $pkField = 'id')
{
$criteria = new CDbCriteria();
$criteria->condition = $pkField . ' IN(' . $id . ')';
$showStatus = $type == 'verify' ? 'Y' : 'N';
$result = $model->updateAll(array ($cdField => $showStatus ), $criteria);
//刷新緩存
self::_refreshCache($model);
$this->redirect($redirect);
}