public function actionUpdate($id) { $model = $this->loadModel($id); //$this->performAjaxValidation($model); if (isset($_POST[$this->modelName])) { $model->attributes = $_POST[$this->modelName]; $model->scenario = $model->type; if (in_array($model->type, array_flip(Advert::getAvailableTypes()))) { $isFile = $isHtml = false; if ($model->type == 'file') { $isFile = true; $oldFile = $model->file_path; } if ($model->type == 'html') { $isHtml = true; } if ($model->validate()) { if ($isFile) { $activeLangs = Lang::getActiveLangs(); if ($activeLangs && is_array($activeLangs)) { foreach ($activeLangs as $key => $val) { $model->setAttribute('js_' . $key, ''); $model->setAttribute('html_' . $key, ''); } } $model->file = CUploadedFile::getInstance($model, 'file_path'); if ($model->file) { $model->file_path = md5(uniqid()) . '.' . $model->file->extensionName; } } else { $activeLangs = Lang::getActiveLangs(); if ($activeLangs && is_array($activeLangs)) { foreach ($activeLangs as $key => $val) { if ($isHtml) { $model->setAttribute('js_' . $key, ''); } else { # js $model->setAttribute('html_' . $key, ''); } } } $model->file_path = $model->url = $model->alt_text = ''; } if ($model->save(false)) { if ($model->file) { $model->file->saveAs(Yii::getPathOfAlias('webroot') . '/uploads/rkl/' . $model->file_path); @unlink(Yii::getPathOfAlias('webroot') . '/uploads/rkl/' . $oldFile); } $this->redirect(array('admin')); } } } } $this->render('update', array('model' => $model)); }
<div class="form"> <?php $types = Advert::getAvailableTypes(); $positions = Advert::getAvailablePositions(); $areas = Advert::getAvailableAreas(); $typesJs = CJavaScript::encode(array_keys($types)); Yii::app()->clientScript->registerScript('typesJs', "var typesJs = " . $typesJs . ";", CClientScript::POS_END); $form = $this->beginWidget('CustomForm', array('id' => $this->modelName . '-form', 'enableAjaxValidation' => false, 'htmlOptions' => array('enctype' => 'multipart/form-data'))); ?> <p class="note"><?php echo Yii::t('common', 'Fields with <span class="required">*</span> are required.'); ?> </p> <?php echo $form->errorSummary($model); ?> <div class="rowold"> <?php echo $form->labelEx($model, 'type'); ?> <?php echo $form->dropDownList($model, 'type', $types, array('onchange' => 'changeType(this.value)', 'class' => 'width150', 'id' => 'type')); ?> <?php echo $form->error($model, 'type'); ?> </div>