Example #1
0
 private function _saveSingleCookbook($data, $user, $accountId)
 {
     $data['type'] = [];
     if (isset($data['category']) && sizeof($data['category']) > 0) {
         array_unique($data['category']);
         $data['type'] = array_merge($data['type'], $data['category']);
     }
     if (isset($data['subCategory']) && sizeof($data['subCategory']) > 0) {
         array_unique($data['subCategory']);
         $data['type'] = array_merge($data['type'], $data['subCategory']);
     }
     if (isset($data['cuisineType']) && sizeof($data['cuisineType']) > 0) {
         array_unique($data['cuisineType']);
         $data['type'] = array_merge($data['type'], $data['cuisineType']);
     }
     if (sizeof($data['type'] > 0)) {
         array_unique($data['type']);
     }
     $cookbook = new Cookbook();
     $now = time();
     $cookbook->attributes = $data;
     $cookbook->operator = $user->name;
     $cookbook->createdDate = $now;
     $cookbook->updatedDate = $now;
     $cookbook->startDate = $now;
     $cookbook->endDate = mktime(0, 0, 0, 12, 31, 2016);
     $cookbook->activeSortTime = $cookbook->createdDate;
     $cookbook->inactiveSortTime = $cookbook->createdDate;
     $cookbook->sample = [];
     $cookbook->isSampleOpen = self::SAMPLE_CLOSE;
     $cookbook->active = self::ACTIVE_OFF;
     $cookbook->accountId = $accountId;
     $cookbook->hasImportImg = false;
     $cookbook->save();
     CookingType::saveByCookbook($cookbook, $cookbook->accountId, $user);
     return ['cookbookId' => $cookbook->_id, 'name' => $cookbook->title, 'image' => $cookbook->image];
 }
 public function actionSave()
 {
     $accountId = $this->getAccountId();
     $request = Yii::$app->request;
     $adminId = $request->post("id");
     $admin = User::findOne($adminId);
     if ($admin == null) {
         return ['code' => 1209, 'msg' => 'not login'];
     }
     $cookbookId = $request->post('cookbookId', null);
     $title = $request->post('title', '');
     $image = $request->post('image', '');
     $content = $request->post('content', '');
     $ingredient = $request->post('ingredient', []);
     $startDate = $request->post('startDate', '');
     $endDate = $request->post('endDate', '');
     $shareUrl = $request->post('shareUrl', '');
     $isSampleOpen = $request->post('isSampleOpen', 'N');
     $sample = $request->post('sample', []);
     $active = $request->post('active', 'Y');
     $type = $request->post('type', []);
     $video = $request->post('video', '');
     $restaurantName = $request->post('restaurantName', '');
     $cookName = $request->post('cookName', '');
     $tip = $request->post('tip', '');
     $creativeExperience = $request->post('creativeExperience', '');
     $deliciousSecret = $request->post('deliciousSecret', '');
     $cookbook = null;
     if ($cookbookId != null) {
         $cookbook = Cookbook::findOne($cookbookId);
     }
     if ($cookbook == null) {
         $cookbook = new Cookbook();
         $cookbook->createdDate = time();
     }
     //Sort time
     $activeSortTime = $cookbook->activeSortTime;
     $inactiveSortTime = $cookbook->inactiveSortTime;
     if ($activeSortTime == null) {
         $activeSortTime = $cookbook->createdDate;
         $inactiveSortTime = $cookbook->createdDate;
     }
     if ($active == 'Y' && $startDate / 1000 < time()) {
         if ($cookbook->startDate < time() && $cookbook->active == 'Y') {
             $inactiveSortTime = $endDate / 1000;
         } else {
             $activeSortTime = time();
             $inactiveSortTime = $endDate / 1000;
         }
     } else {
         if ($cookbook->startDate < time() && $cookbook->active == 'Y') {
             $inactiveSortTime = time();
             $activeSortTime = $startDate / 1000;
         } else {
             $activeSortTime = $startDate / 1000;
         }
     }
     $cookbook->activeSortTime = $activeSortTime;
     $cookbook->inactiveSortTime = $inactiveSortTime;
     //lock samples
     for ($i = 0; $i < count($cookbook->sample); $i++) {
         $result = Sample::unlockSample($cookbook->sample[$i]);
     }
     for ($j = 0; $j < count($sample); $j++) {
         $result = Sample::lockSample($sample[$j]);
         if ($result['code'] == 500) {
             return ['code' => 1204, 'msg' => $result];
         }
     }
     $cookbook->title = $title;
     $cookbook->image = $image;
     $cookbook->content = $content;
     $cookbook->ingredient = $this->clearHashKey($ingredient);
     $cookbook->startDate = strlen($startDate) > 0 ? $startDate / 1000 : null;
     $cookbook->endDate = strlen($endDate) > 0 ? $endDate / 1000 : null;
     $cookbook->isSampleOpen = $isSampleOpen;
     $cookbook->sample = $this->clearHashKey($sample);
     $cookbook->active = $active;
     $cookbook->updatedDate = time();
     $cookbook->operator = $admin['name'];
     $cookbook->type = $type;
     $cookbook->shareUrl = $shareUrl;
     $cookbook->accountId = $accountId;
     $cookbook->video = $video;
     $cookbook->restaurantName = $restaurantName;
     $cookbook->cookName = $cookName;
     $cookbook->hasImportImg = true;
     $cookbook->tip = $tip;
     $cookbook->creativeExperience = $creativeExperience;
     $cookbook->deliciousSecret = $deliciousSecret;
     $cookbook->save();
     Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
     return $cookbook->_id;
 }