Exemplo n.º 1
0
 public function actionEdit()
 {
     $this->layout = '//layouts/admin';
     $success = false;
     $id = $_REQUEST['id'];
     $model = Orders::model()->findByPk($id);
     $this->pageTitle = is_object($model) ? "Заказ #" . $model->id : 'Новый заказ';
     $this->breadcrumbs = array('Заказы' => array('/admin/orders'), is_object($model) ? "Заказ #" . $model->id : 'Новый заказ');
     if (isset($_POST['data']) && is_object($model)) {
         $dataArray = $_POST['data'];
         $dataArray['timestamp'] = isset($dataArray['timestamp']) ? strtotime($dataArray['timestamp']) : time();
         $orderItems = json_decode($dataArray['order_items'], true);
         unset($dataArray['order_items']);
         $model->setAttributes($dataArray);
         if ($model->save()) {
             $success = true;
             $oldItems = OrderItems::model()->findAllByAttributes(array("order_id" => $model->id));
             if (is_array($oldItems) && count($oldItems)) {
                 foreach ($oldItems as $item) {
                     $item->delete();
                 }
             }
             if (is_array($orderItems) && count($orderItems)) {
                 foreach ($orderItems as $oItem) {
                     foreach ($oItem as $oId => $amount) {
                         $object = CatalogObjects::model()->findByPk($oId);
                         if (is_object($object) && $amount) {
                             $oItemModel = new OrderItems();
                             $attributes = array();
                             $attributes['order_id'] = $model->id;
                             $attributes['name'] = $object->name;
                             $attributes['product_id'] = $object->id;
                             $attributes['price'] = $object->price;
                             $attributes['amount'] = $amount;
                             $oItemModel->setAttributes($attributes);
                             $oItemModel->save();
                         }
                     }
                 }
             }
         }
     }
     $this->render('edit', array('model' => $model, 'success' => $success, 'errors' => $model->errors));
 }