public function init() { parent::init(); if ($this->pageId) { if ($this->addFieldId) { $this->_field = PageFieldRecord::findOne(['page_id' => $this->pageId, 'additional_field_id' => $this->addFieldId]); } else { throw new InvalidParamException(Yii::t('front', 'No required parameter given') . ' - addFieldId'); } } else { throw new InvalidParamException(Yii::t('front', 'No required parameter given') . ' - pageId'); } }
/** * @return \yii\db\ActiveQuery */ public function getPageFields() { return $this->hasMany(PageFieldRecord::className(), ['page_id' => 'id']); }
/** * Updates an existing Page model. * If update is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = new PageForm($id); if ($model->load(Yii::$app->request->post()) && $model->validate()) { $model->savePage(false); $additionalFieldIds = Yii::$app->request->post('AdditionalFieldId'); $pageFields = PageFieldRecord::findAll(['page_id' => $model->item_id]); foreach ($pageFields as $pageField) { if (isset($additionalFieldIds[$pageField->additional_field_id])) { $pageField->content = $additionalFieldIds[$pageField->additional_field_id]; $pageField->save(); unset($additionalFieldIds[$pageField->additional_field_id]); } else { $pageField->delete(); } } if ($additionalFieldIds) { foreach ($additionalFieldIds as $key => $content) { $pageField = new PageFieldRecord(); $pageField->page_id = $model->item_id; $pageField->additional_field_id = $key; $pageField->content = $content; $pageField->save(); } } $session = Yii::$app->session; $session->setFlash('info', Yii::t('back', 'Page successfully updated!')); return $this->redirect(['index']); } else { return $this->render('update', compact('model')); } }
/** * Renders additional fields for actual page */ public function renderAdditionalFields() { $output = ''; if ($this->item_id) { $pageFields = PageFieldRecord::findAll(['page_id' => $this->item_id]); foreach ($pageFields as $pageField) { $output .= '<div class="form-inline"><div class="form-group">'; $output .= '<label for="AdditionalFieldId[' . $pageField->additional_field_id . ']">' . $pageField->additionalField->label . '</label><br />'; $output .= Html::textInput('AdditionalFieldId[' . $pageField->additional_field_id . ']', $pageField->content, ['class' => 'form-control']); $output .= ' ' . Html::a('<span class="glyphicon glyphicon-remove"></span>', '#!', ['class' => 'form-control btn btn-danger remove-field-btn']); $output .= '</div></div>'; } } echo $output; }