public function actionCreate() { Yii::app()->clientScript->registerPackage('items-edit'); if (!isset($_GET['id']) || NULL == ($model = Items::model()->findByAttributes(array('user_id' => Yii::app()->user->id, 'id' => (int) $_GET['id'])))) { $model = new Items(); } if (isset($_POST['Items'])) { $model->attributes = $_POST['Items']; $fn = Files::model()->uploadFile('Items[image]'); if ($fn) { $model->image = $fn['filename']; } elseif (isset($_POST['Items']['image_src'])) { $model->image = $_POST['Items']['image_src']; } $new = $model->isNewRecord; if ($model->save()) { $cats = isset($_POST['categories']) ? $_POST['categories'] : array(); if (!$new && empty($cats)) { ItemsCategory::model()->deleteAllByAttributes(array('item_id' => $model->id)); } else { if (!$new) { $dbcats = ItemsCategory::model()->findAll('item_id=:iid', array('iid' => $model->id)); $ids = array(); foreach ($dbcats as $i => $dbc) { if (false !== ($k = array_search($dbc->category_id, $cats))) { unset($cats[$k]); unset($dbc[$i]); } else { $ids[] = $dbc->category_id; } } //deleteing corresponding categories if (!empty($ids)) { $ids = implode(",", $ids); ItemsCategory::model()->deleteAll('item_id=:iid AND category_id IN (:cid)', array('iid' => $model->id, 'cid' => $ids)); } } if (!empty($cats)) { foreach ($cats as $cat) { $c = new ItemsCategory(); $c->attributes = array('category_id' => $cat, 'item_id' => $model->id); $c->save(); } } } Yii::app()->user->setFlash('ItemSaved', '1'); $this->refresh(); } } $this->render('_form', array('model' => $model)); }
echo Yii::t('site', 'Создание товара'); ?> </h1> <div class="form"> <?php if (Yii::app()->user->getFlash('ItemSaved') > 0) { ?> <div class="success"> Сохранено! </div> <?php } $form = $this->beginWidget('CActiveForm', array('id' => 'office-form', 'htmlOptions' => array('enctype' => 'multipart/form-data'), 'enableAjaxValidation' => false)); $icats = array(); if (!$model->isNewRecord) { $dbs = ItemsCategory::model()->findAllByAttributes(array('item_id' => $model->id)); $icats = array_map(create_function('$item', 'return $item->category_id;'), $dbs); } $cats = Category::model()->findAll(); ?> <?php echo $form->errorSummary($model); ?> <?php echo $model->isNewRecord ? '' : $form->hiddenField($model, 'id'); ?> <div class="field"> <?php echo $form->labelEx($model, 'image'); ?> <?php