/**
  * Finds the Center model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Center the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Center::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Exemple #2
0
use common\components\helpers\Show;
use yii\helpers\Html;
use yii\grid\GridView;
$this->title = 'Danh sách trạm';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="station-index">

    <h4><?php 
echo Html::encode($this->title);
?>
</h4>

    <?php 
echo GridView::widget(['dataProvider' => $dataProvider, 'columns' => [['class' => 'yii\\grid\\SerialColumn', 'options' => ['width' => '6%']], 'code', 'name', ['attribute' => 'center_id', 'format' => 'text', 'value' => function ($model) {
    $center = Center::findOne($model->center_id);
    if ($center) {
        return $center->name;
    }
    return null;
}], ['attribute' => 'area_id', 'format' => 'text', 'value' => function ($model) {
    $area = Area::findOne($model->area_id);
    if ($area) {
        return $area->name;
    }
    return null;
}], ['attribute' => 'status', 'format' => 'html', 'value' => function ($model) {
    if ($model->status == Station::STATUS_CONNECTED) {
        $html = Show::decorateString($model->getStatus($model->status), 'good');
    }
    if ($model->status == Station::STATUS_LOST) {
 protected function findModel($id)
 {
     // session
     $role = new Role();
     if (!$role->isAdministrator) {
         $position = $role->getPosition();
         $stationIds = Station::getByRole($position, Yii::$app->user->id);
         if (!empty($stationIds) && !in_array($id, $stationIds)) {
             $this->permissionDeny();
         }
     }
     if (($model = Station::findOne($id)) !== null) {
         // find area & center
         $model->area = Area::findOne($model->area_id);
         $model->center = Center::findOne($model->center_id);
         // get equipment
         $equipments = EquipmentStatus::findAll(['station_id' => $id]);
         if (!empty($equipments)) {
             foreach ($equipments as $equipment) {
                 $model->equipment[] = $equipment->equipment_id;
             }
         }
         // get power equipment
         $powerEquipments = PowerStatus::findAll(['station_id' => $id]);
         if (!empty($powerEquipments)) {
             foreach ($powerEquipments as $equipment) {
                 $model->power_equipment[] = $equipment->item_id;
             }
         }
         // get dc
         $dcEquips = DcEquipmentStatus::findByStation($id);
         if (!empty($dcEquips)) {
             foreach ($dcEquips as $dcEquip) {
                 $model->dc_equip_ids[] = $dcEquip['equipment_id'];
                 $model->dc_equip_status[] = ['equipment_id' => $dcEquip['equipment_id'], 'name' => $dcEquip['name'], 'amperage' => $dcEquip['amperage'], 'voltage' => $dcEquip['voltage'], 'unit_voltage' => $dcEquip['unit_voltage'], 'temperature' => $dcEquip['temperature'], 'unit_amperage' => $dcEquip['unit_amperage'], 'status' => $dcEquip['status']];
             }
         }
         // get sensor status
         $query = new Query();
         $sensorStatus = $query->select('sst.*, s.type, s.unit_type, s.name')->from('sensor_status sst')->leftJoin('sensor s', 's.id = sst.sensor_id')->where(['sst.station_id' => $id])->andWhere(['s.active' => Sensor::STATUS_ACTIVE])->all();
         if (!empty($sensorStatus)) {
             foreach ($sensorStatus as $sst) {
                 $model->sensor_status[] = ['type' => $sst['type'], 'sensor_id' => $sst['sensor_id'], 'name' => $sst['name'], 'unit' => $sst['unit_type'], 'value' => $sst['value'], 'status' => $sst['status']];
             }
         }
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }