protected function fakeClientType() { $fakeClientType = new ClientType(); $fakeClientType->client_type_name = "Fake Client Type"; $fakeClientType->save(); return $fakeClientType; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = ClientType::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id]); $query->andFilterWhere(['like', 'type', $this->type]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = ClientType::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'active' => $this->active]); $query->andFilterWhere(['like', 'type', $this->type]); return $dataProvider; }
public function upload() { if ($this->validate()) { $isValidFormat = true; $isValidEncoding = true; if (($handle = fopen($this->csvFile->tempName, "r")) !== FALSE) { if (($firstLine = fgetcsv($handle)) !== FALSE) { if (count($firstLine) >= 2) { if (mb_detect_encoding($firstLine[0]) !== 'ASCII') { $isValidEncoding = false; $firstLine = $this->convert_to_ASCII($firstLine); } } else { $isValidFormat = false; } if ($isValidFormat) { if ($firstLine[0] !== 'client_id' || $firstLine[1] !== 'full_name' || $firstLine[2] !== 'client_type_id' || $firstLine[3] !== 'discipline_id' || $firstLine[4] !== 'active') { $isValidFormat = false; } } } if ($isValidFormat) { $success = true; $row = 1; while (($data = fgetcsv($handle)) !== FALSE) { if (!$isValidEncoding) { $data = $this->convert_to_ASCII($data); } $row++; $model = new Client(); $model->client_id = $data[0]; $model->full_name = $data[1]; $model->client_type_id = $data[2]; $model->discipline_id = $data[3]; $model->active = $data[4]; if (!$model->save()) { $idClient = Client::find()->where(['client_id' => $model->client_id])->exists(); $typeClient = ClientType::find()->where(['id' => $model->client_type_id])->exists(); $idDiscipline = Discipline::find()->where(['id' => $model->discipline_id])->exists(); $activo = Client::find()->where(['active' => $model->active])->exists(); if ($idClient == true) { Yii::$app->session->addFlash("error-upload", "Error en línea: [" . $row . "] ----- El cliente con id: [" . $model->client_id . "] ya se encuentra registrado."); } else { if ($typeClient == false || $idDiscipline == false || $activo == false) { if ($typeClient == false) { $typeClient = "[client_type_id] No Válido. "; } else { $typeClient = ""; } if ($idDiscipline == false) { $idDiscipline = "[discipline_id] No Válido. "; } else { $idDiscipline = ""; } if ($activo == false) { $activo = "[active] No Válido."; } else { $activo = ""; } Yii::$app->session->addFlash("error-upload", "Error en línea: [" . $row . "] ----- " . $typeClient . "" . $idDiscipline . "" . $activo . ""); } } } } if (!feof($handle)) { $success = false; Yii::$app->session->setFlash('error', Yii::t('app', 'Unexpected error.')); } } else { Yii::$app->session->setFlash('error', Yii::t('app', 'El formato del archivo No es válido. El formato válido es: ') . "<div class=\"panel panel-default\">\n\t\t\t\t\t\t\t<div class=\"panel-body\">\n\t\t\t\t\t\t\t\t<p>client_id,full_name,client_type_id,discipline_id,active</p>\n\t\t\t\t\t\t\t\t<p>98341245,Pedro Salazar Dos,1,5,1</p>\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t</div>"); } fclose($handle); } return $isValidFormat && $success; } else { return false; } }
/** * @return \yii\db\ActiveQuery */ public function getClientType() { return $this->hasOne(ClientType::className(), ['id' => 'client_type_id']); }
/** * Finds the ClientType model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return ClientType the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = ClientType::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
?> <?php echo $form->field($model, 'client_id')->textInput(['maxlength' => 30]); ?> <?php echo $form->field($model, 'first_name')->textInput(['maxlength' => 175]); ?> <?php echo $form->field($model, 'last_name')->textInput(['maxlength' => 175]); ?> <?php echo $form->field($model, 'client_type_id')->radioList(ArrayHelper::map(ClientType::find()->all(), 'id', 'type')); ?> <?php echo $form->field($model, 'discipline_id')->dropDownList(ArrayHelper::map(Discipline::find()->all(), 'id', 'name', 'short_name'), ['prompt' => Yii::t('app', 'You could select a discipline')]); ?> <?php echo $form->field($model, 'active')->checkbox([], false); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?>