/** * @param int|string|null $id * @return \yii\web\Response|string * @throws NotFoundHttpException * @throws \yii\web\ServerErrorHttpException */ public function actionEdit($id = null) { if (null === $id) { throw new NotFoundHttpException(); } /** @var Customer $model */ if (null === ($model = Customer::findOne(['id' => $id]))) { throw new NotFoundHttpException(); } if (true === \Yii::$app->request->isPost && $model->load(\Yii::$app->request->post())) { if ($model->saveModelWithProperties(\Yii::$app->request->post())) { return $this->refresh(); } } return $this->render('edit', ['model' => $model]); }
/** * @param null $customer * @return string|\yii\web\Response * @throws \yii\web\ServerErrorHttpException */ public function actionCreate($customer = null) { $customer = intval($customer) > 0 ? Customer::findOne(['id' => intval($customer)]) : null; $contragent = Contragent::createEmptyContragent(null === $customer ? Customer::createEmptyCustomer() : $customer); if (true === \Yii::$app->request->isPost) { $data = \Yii::$app->request->post(); if ($contragent->load($data) && $contragent->save()) { if (!empty($contragent->getPropertyGroup())) { $contragent->getPropertyGroup()->appendToObjectModel($contragent); $data[$contragent->getAbstractModel()->formName()] = isset($data['ContragentNew']) ? $data['ContragentNew'] : []; } $contragent->saveModelWithProperties($data); $contragent->refresh(); $deliveryInformation = DeliveryInformation::createNewDeliveryInformation($contragent, false); return $this->redirect(Url::toRoute(['edit', 'id' => $contragent->id])); } } return $this->render('create', ['model' => $contragent]); }
/** * @return array */ public function actionAjaxCustomer($template = null) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $result = ['more' => false, 'results' => []]; $search = \Yii::$app->request->get('search', []); $user_id = isset($search['user']) && User::USER_GUEST !== intval($search['user']) ? intval($search['user']) : User::USER_GUEST; if (!empty($search['term'])) { $query = Customer::find()->select('id, first_name, middle_name, last_name, email, phone')->where(['user_id' => $user_id])->andWhere('first_name LIKE :term1 OR middle_name LIKE :term2 OR last_name LIKE :term3 OR email LIKE :term4 OR phone LIKE :term5', [':term1' => '%' . trim($search['term']) . '%', ':term2' => '%' . trim($search['term']) . '%', ':term3' => '%' . trim($search['term']) . '%', ':term4' => '%' . trim($search['term']) . '%', ':term5' => '%' . trim($search['term']) . '%'])->asArray(); $result['results'] = array_values($query->all()); } if (!empty($result['results']) && 'simple' === $template) { $result['cards'] = array_reduce($result['results'], function ($result, $item) { /** @var array $item */ $result[$item['id']] = \app\modules\shop\widgets\Customer::widget(['viewFile' => 'customer/backend_list', 'model' => Customer::findOne(['id' => $item['id']])]); return $result; }, []); } return $result; }