/** * 广告位添加 * @date: 2016年1月7日 * @author: chenxiaolin */ public function advertisementAddAction() { $this->setLeftNav('advertisementAdd'); $req = $this->request; $response = new ResponseResult(); $response->callback = $req->getPost('callback', null, 'parent.setFormResult'); $response->callbackJavascriptTag = true; if (!$req->isPost()) { $this->view->setVar('openCity', $this->openCityList()); return; } else { $city = $req->getPost('city'); $title = $req->getPost('advertisement_title'); $position = $req->getPost('position'); $type = $req->getPost('type'); $intro = $req->getPost('advertisement_intro'); $out_link = $req->getPost('out_link'); //判断空数据 if ($type <= 0 || empty($title) || $position <= 0 || $city < 0) { $response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '参数异常!'); return $response; } //判断图文介绍还是外链地址 if ($type == 1 && empty($intro)) { $response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '请填写图文介绍!'); return $response; } if ($type == 2 && empty($out_link)) { $response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '请填写外链地址!'); return $response; } //判断外链地址是否合法 if ($type == 2 && !Util::CheckUrl($out_link)) { $response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '外链地址不合法!'); return $response; } //全国广告位与城市广告位互斥 if (in_array(0, $city)) { //全国广告 $cityAdNum = Advertisement::count("city_id not in ( 0 ) and position = {$position}"); //查询此广告位是否已存在数据 if ($cityAdNum > 0) { $response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '此广告位已被城市占用!'); return $response; } //该全国广告位是否已存在数据 $ad = Advertisement::findFirst("city_id = 0 and position = {$position}"); if ($ad) { $response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '此广告位已有数据!'); return $response; } } else { //城市广告 //查询全国此广告位是否已存在数据 $countryAd = Advertisement::findFirst("city_id = 0 and position = {$position}"); if ($countryAd) { $response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '此广告位已被全国占用!'); return $response; } //该城市广告位是否已存在数据 foreach ($city as $k => $v) { $ad = Advertisement::findFirst("city_id = {$v} and position = {$position}"); //某城市该广告位存在数据 if ($ad) { $citys = SysOpenCity::findFirst("city_id = {$v}"); $city_name = $citys->city_name; $response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, $city_name . "此广告位已有数据!"); return $response; } } } //保存封面 $uploadFile = new UploadPic(); $uploadFile->request = $req; $cover = $uploadFile->advertisementPics(); if ($cover == false || !is_array($cover) || count($cover) == 0 || $cover[0] == false) { if ($_FILES['advertisement_cover']['error'] == 1) { $response->sendError(ResponseResultStatus::UPLOAD_FILE_ERROR, '文件大小超过了1M!'); return $response; } else { $response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '广告封面必须上传!'); return $response; } } $cover = $cover[0]; foreach ($city as $k => $v) { $adv = new Advertisement(); $adv->advertisement_title = $title; $adv->advertisement_cover = $cover; $adv->position = $position; $adv->type = $type; if ($type == 1) { $adv->advertisement_intro = $intro; $adv->out_link = ''; } if ($type == 2) { $adv->out_link = $out_link; $adv->advertisement_intro = ''; } $adv->addtime = date('Y-m-d H:i:m'); $adv->city_id = $v; $status = $adv->save(); if ($status === false) { $this->databaseErrorLog($adv); $response->sendError(ResponseResultStatus::DATABASE_ERROR, '保存数据异常!'); } else { $response->sendResult($status); } } return $response; } }