Exemple #1
0
 /**
  * @param \ApiParam $params
  * @param \User $user
  * @return \Ad
  * @throws \Exception
  */
 public static function buildAd(\ApiParam $params, \User $user)
 {
     if ($params->adId) {
         $ad = self::loadAndCheck($params->adId, $user, false, $params->udid);
         $ad->set('modifyIp', \Ip::remote_addr());
     } else {
         $ad = new \Ad();
         $ad->userIp = \Ip::remote_addr();
         $ad->status = \Ad::STATUS_ACTIVE;
         $ad->userId = $user->id;
         //City 编辑信息不能更换city
         $city = \City::loadByName($params->cityEnglishName);
         if (!isset(\Page::$context['city'])) {
             \Page::$context['city'] = $city;
         }
         if (!$city) {
             throw new \Exception('invalid city', 504);
         }
         $ad->areaCityLevelId = $city->oid;
         $ad->set('totalReport', 0);
         $ad->set('MasterCheckDate', 0);
     }
     //Category
     if (!$params->adId) {
         $ad->categoryEnglishName = $params->categoryEnglishName;
     } else {
         $ad->categoryEnglishName = $ad->categoryEnglishName;
     }
     try {
         $category = \Category::loadByName($ad->categoryEnglishName);
     } catch (\Exception $e) {
         throw new \Exception('该类目不存在', '407');
     }
     if ($category->level != \Category::LEVEL_SECOND) {
         throw new \Exception('信息只能发布到二级类目', '406');
     }
     //联系方式
     $params->contact = $params->contact ?: $params->mobile;
     //@todo 如果没有写发布人的,加上发布人属性,保留到2012年9月2日 by zhaojun
     if (!$params->faburen) {
         $ad->setPosterType(null);
         if ($ad->get('faburen')) {
             $params->faburen = $ad->get('faburen');
         } else {
             $params->faburen = 'm33660';
             //dirty fix, but it works(liuweili@baixing.com)
         }
     }
     $metas = $category->metas();
     if ($params->wanted) {
         switch ($category->parentEnglishName) {
             case 'gongzuo':
             case 'jianzhi':
                 $metas = \Category::loadByName('qiuzhijianli')->metas();
                 break;
             case 'ershou':
                 $metas = \Category::loadByName('qiumai')->metas();
                 break;
             case 'cheliang':
                 $metas = \Category::loadByName('cheliangqiugou')->metas();
                 break;
             default:
                 $metas = \Category::loadByName('qiufang')->metas();
                 break;
         }
     }
     foreach ($metas as $meta) {
         $data = $meta->dataValue($params);
         $dataWithoutUnit = $params->{$meta->name};
         if (strpos($meta->style, 'required') !== false && !array_filter($data, function ($a) {
             return $a !== null && $a !== '';
         })) {
             if (!array_key_exists('地区', $data) && $params->api_key == 'api_mobile_android') {
                 \InstantCounter::count('fabubitian');
                 throw new \Exception('属性' . $meta->displayName . '是必填项,请填写', '422');
             }
         }
         if ($meta->maxlength && mb_strlen($dataWithoutUnit) > $meta->maxlength) {
             throw new \Exception($meta->displayName . '不能超过' . $meta->maxlength . '个字', '423');
         }
         foreach ($data as $key => $value) {
             $ad->set($key, is_array($value) ? implode(',', $value) : (string) $value);
         }
     }
     if (!$ad->title) {
         throw new \Exception('属性标题是必填项,请填写', '422');
     }
     //Images
     if (is_string($params->images) && strlen($params->images)) {
         $params->image = explode(',', $params->images);
     }
     $images = self::validImage($params->image);
     $images = array_slice($images, 0, $user->imageLimit(\City::loadByName($params->cityEnglishName)));
     if ($ad->categoryEnglishName != 'ktvjiuba') {
         $ad->set('images', implode(" ", $images));
     } else {
         $ad->set('images', null);
     }
     $ad->set('wanted', intval($params->wanted));
     $contacts = $ad->contacts();
     $ad->set('contacts', implode(' ', $contacts));
     $fingers = $ad->fingers();
     $ad->set('fingers', implode(' ', $fingers));
     $urls = $ad->urls();
     $ad->set('urls', implode(' ', $urls));
     $ad->set('fabuSessionId', intval($params->fabuSessionId));
     return $ad;
 }