/** * Update article channel */ public function actionUpdate($id) { $channelId = new \MongoId($id); $channel = ArticleChannel::findByPk($channelId); if (empty($channel)) { throw new BadRequestHttpException('Incorrect channel id'); } if ($channel->isDefault) { throw new BadRequestHttpException(Yii::t('microSite', 'default_channel_protected')); } $params = $this->getParams(); //check the field to make sure the type and name is exists if (empty($params['name'])) { throw new BadRequestHttpException(Yii::t('microSite', 'channel_name_missing')); } if (!empty($params['fields'])) { foreach ($params['fields'] as $field) { if (empty($field['type']) || empty($field['name'])) { throw new BadRequestHttpException(Yii::t('microSite', 'custom_field_missing')); } } } $channel->load($params, ''); if ($channel->save() === false && $channel->hasErrors()) { LogUtil::error(['message' => 'save article channel error', 'errors' => $channel->getErrors()], 'microsite'); throw new ServerErrorHttpException("Save article channel failed for unknown reasons"); } //update the articles for the channel $articles = Article::findByChannel($channelId); foreach ($articles as $article) { $oldFields = $article->fields; $newFields = []; foreach ($channel->fields as $field) { $field['content'] = ''; foreach ($oldFields as $oldField) { if ($field['id'] === $oldField['id']) { $field['name'] = $oldField['name']; if ($field['type'] === $oldField['type']) { $field['content'] = empty($oldField['content']) ? '' : $oldField['content']; } } } $newFields[] = $field; } $article->fields = $newFields; if (!$article->save()) { LogUtil::error(['message' => 'save article error', 'errors' => $article->getErrors()], 'microsite'); throw new ServerErrorHttpException('Save article failed for unknown reasons'); } } return $channel; }