Example #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params, $emp_id = null)
 {
     if ($emp_id == null) {
         $query = CellNumbers::find();
     } else {
         $query = CellNumbers::find()->where(['employee_id' => $emp_id]);
     }
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['status' => SORT_ASC]]]);
     $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->joinWith('employee');
     $query->andFilterWhere(['cell_numbers.id' => $this->id, 'status' => $this->status]);
     $query->andFilterWhere(['like', 'cell_number', $this->cell_number])->andFilterWhere(['like', 'employees.snp', $this->employee_id]);
     return $dataProvider;
 }
 /**
  * Обрабатываем загруженный файл
  */
 public function actionReadFile()
 {
     $filename = 'in/cellnumbers.txt';
     //$fsz = filesize($filename);
     $readfile = fopen($filename, 'r');
     $err = '';
     //инвертируем статус. номера, не прошедшие обработку, останутся с отрицательным статусом
     Yii::$app->db->createCommand('UPDATE cell_numbers SET status = status*(-1)')->execute();
     while ($str = fgets($readfile, 1024)) {
         //$items = explode(chr(9), $str);
         $items = explode(";", $str);
         if (count($items) != 3) {
             Yii::$app->db->createCommand('UPDATE cell_numbers SET status = status*(-1)')->execute();
             //возвращаем статусы
             Yii::$app->session->setFlash('error', 'Файл не соответствует формату');
             break;
         }
         if (stristr($str, 'Абонент') > '') {
             continue;
         }
         $str_cell = str_replace('-', '', $items[2]);
         $cell = CellNumbers::findOne(['cell_number' => $str_cell]);
         $emp = Employees::findOne(['snp' => $items[0]]);
         if (isset($cell)) {
             if (!isset($emp)) {
                 continue;
             }
             if ($cell->employee_id != $emp->id) {
                 $cell_count = CellNumbers::find()->where(['employee_id' => $emp->id])->count();
                 $cell->employee_id = $emp->id;
                 $cell->status = $cell_count + 1;
                 $cell->save();
             } else {
                 $cell->status = $cell->status * -1;
                 if ($cell->validate()) {
                     $cell->save();
                 } else {
                     //если не прошли валидацию - собираем ошибки
                     foreach ($cell->errors as $key => $error) {
                         $err .= '<b>' . $key . '</b> ' . implode('   ', $error) . '<br>';
                     }
                     Yii::$app->session->setFlash('error', $err);
                 }
             }
         } else {
             $cell = new CellNumbers();
             if ($emp) {
                 $cell_count = CellNumbers::find()->where(['employee_id' => $emp->id])->count();
                 $cell->status = $cell_count + 1;
                 $cell->employee_id = $emp->id;
             }
             $cell->cell_number = $str_cell;
             $cell->save();
         }
     }
     //номерам, не попавшим в обработку (статус < 0), удаляем владельцев и сбрасываем статус на 1
     Yii::$app->db->createCommand('UPDATE cell_numbers SET employee_id = NULL, status = 1 WHERE status < 0')->execute();
 }
Example #3
0
 /**
  * @return array|\yii\db\ActiveRecord[]
  */
 public static function arrayCells()
 {
     return CellNumbers::find()->select('cell_number as value, cell_number as label')->where("cell_number > '' AND status = 1")->orderBy('cell_number')->asArray()->all();
 }