public function actionRemovesda()
 {
     $connection = Yii::$app->db;
     if (Yii::$app->request->post()) {
         $value = \Yii::$app->request->post();
         $user = User::findOne(['id' => $value['userid']]);
         if ($user) {
             $congress = $value['congress'];
             $sdaid = $value['sdaid'];
             $model = SdaSchedules::deleteAll('congress_id = :congress_id AND sda_id = :sda_id AND attendee_id = :attendee_id', [':congress_id' => $congress, ':sda_id' => $sdaid, ':attendee_id' => $user->attendee_id]);
             if ($model) {
                 echo 'SDA deleted successfully';
             }
         } else {
             echo "User is not exist in Contect List";
         }
         die;
     }
 }
 /**
  * Finds the SdaSchedules model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return SdaSchedules the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = SdaSchedules::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getSdaSchedules()
 {
     return $this->hasMany(SdaSchedules::className(), ['attendee_id' => 'id']);
 }
 /**
  * Import SDA in CSV format
  * @return mixed
  */
 public function actionExcel()
 {
     $model = new Sda();
     if ($model->load(Yii::$app->request->post())) {
         $row = 1;
         $congid = $_POST['Sda']['id'];
         $model->fileName = UploadedFile::getInstance($model, 'fileName');
         $time = time();
         $model->fileName->saveAs('csv/' . $time . '.' . $model->fileName->extension);
         $model->fileName = 'csv/' . $time . '.' . $model->fileName->extension;
         $requiredHeaders = array('Title', 'Abstract #', 'Priority', 'Abstract', 'Assigned to', 'Date', 'Product', 'Session Start Time', 'Session End Time', 'Presentation Start Time', 'Presentation End Time', 'Presenter', 'SDA type', 'Location', 'Category', 'Session id', 'Session #', 'Authors', 'Company', 'Team', 'Link to Poster', 'Poster Board Number', 'Presentation Number');
         $f = fopen($model->fileName, 'r');
         $firstLine = fgets($f);
         //get first line of csv file
         fclose($f);
         $foundHeaders = array_map('trim', str_getcsv(trim($firstLine), ',', '"'));
         //parse to array
         $result = array_diff_assoc($foundHeaders, $requiredHeaders);
         $csverrormessage = implode(",", $result);
         if ($foundHeaders !== $requiredHeaders) {
             \Yii::$app->getSession()->setFlash('error', 'CSV is in incorrect for ' . $csverrormessage . ' Column');
         } else {
             $handle = fopen($model->fileName, "r");
             while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                 $row++;
                 // Getting Product id
                 $product = \backend\models\Products::find()->where(['name' => $data[6]])->one();
                 $product = isset($product['id']) ? $product['id'] : '';
                 // Getting Priority  ID
                 $priority_name = SdaPriority::find()->where(['priority_name' => $data[2]])->one();
                 $priority_name = isset($priority_name['id']) ? $priority_name['id'] : '3';
                 // Getting Sda Type  ID
                 $sdatype = SdaTypes::find()->where(['sda_name' => $data[12]])->one();
                 $sdatype = isset($sdatype['id']) ? $sdatype['id'] : '9';
                 $cmp = CompanyDetails::find()->where(['name' => $data[18]])->one();
                 $cmp = isset($cmp['id']) ? $cmp['id'] : '2';
                 if ($row == 2) {
                     //ignoring
                 } else {
                     $title = Yii::$app->db->quoteValue($data[0]);
                     $abstracthash = Yii::$app->db->quoteValue($data[1]);
                     $abstract = Yii::$app->db->quoteValue($data[3]);
                     $amgenatten = explode(',', $data[4]);
                     $date = Yii::$app->db->quoteValue(date("Y-m-d H:i:s", strtotime($data[5])));
                     $prod = Yii::$app->db->quoteValue($product);
                     if (!empty($data[7])) {
                         $sessiostart = Yii::$app->db->quoteValue(date("H:i:s", strtotime($data[7])));
                     } else {
                         $sessiostart = "null";
                     }
                     if (!empty($data[8])) {
                         $sessionend = Yii::$app->db->quoteValue(date("H:i:s", strtotime($data[8])));
                     } else {
                         $sessionend = "null";
                     }
                     if (!empty($data[9])) {
                         $presentationstart = Yii::$app->db->quoteValue(date("H:i:s", strtotime($data[9])));
                     } else {
                         $presentationstart = "null";
                     }
                     if (!empty($data[10])) {
                         $presentationend = Yii::$app->db->quoteValue(date("H:i:s", strtotime($data[10])));
                     } else {
                         $presentationend = "null";
                     }
                     $presenter = Yii::$app->db->quoteValue($data[11]);
                     $location = Yii::$app->db->quoteValue($data[13]);
                     $category = Yii::$app->db->quoteValue($data[14]);
                     $session = Yii::$app->db->quoteValue($data[15]);
                     $sessionhash = Yii::$app->db->quoteValue($data[16]);
                     $authors = Yii::$app->db->quoteValue($data[17]);
                     $team = Yii::$app->db->quoteValue($data[19]);
                     $linktoposter = Yii::$app->db->quoteValue($data[20]);
                     $posterboard = Yii::$app->db->quoteValue($data[21]);
                     $presentationno = Yii::$app->db->quoteValue($data[22]);
                     if ($title) {
                         $sql = "insert into sda(\n                                          congress_id,\n                                          title,\n                                          abstract_id,\n                                          priority,\n                                          abstract,\n                                          date,\n                                          session_start_time,\n                                          session_end_time,\n                                          presentation_start_time,\n                                          presentation_end_time,\n                                          presenter,\n                                          sda_type_id,\n                                          product,\n                                          location,\n                                          category,\n                                          session_id,\n                                          session,\n                                          authors,\n                                          company_id,\n                                          team,\n                                          link_to_poster,\n                                          poster_board_no,\n                                          presentation_no\n                                         )" . " values (" . "'{$congid}'," . "{$title}," . "{$abstracthash}," . "'{$priority_name}'," . "{$abstract}," . "{$date}," . "{$sessiostart}," . "{$sessionend}," . "{$presentationstart}," . "{$presentationend}," . "{$presenter}," . "'{$sdatype}'," . "'{$product}'," . "{$location}," . "{$category}," . "{$session}," . "{$sessionhash}," . "{$authors}," . "'{$cmp}'," . "{$team}," . "{$linktoposter}," . "{$posterboard}," . "{$presentationno})";
                         //save the data
                         Yii::$app->db->createCommand($sql)->execute();
                         $sdaId = Yii::$app->db->getLastInsertID();
                         if (count($amgenatten) > 0) {
                             foreach ($amgenatten as $value) {
                                 $value = trim($value);
                                 $amgenattendee = explode(' ', $value);
                                 if (!empty($amgenattendee[0]) && !empty($amgenattendee[1])) {
                                     $attendeeexits = \backend\models\AmgenContactList::find()->where(['first_name' => $amgenattendee[0], 'last_name' => $amgenattendee[1]])->one();
                                     if ($attendeeexits) {
                                         $modelSdaSchedule = new \backend\models\SdaSchedules();
                                         $modelSdaSchedule->sda_id = $sdaId;
                                         $modelSdaSchedule->congress_id = $congid;
                                         $modelSdaSchedule->attendee_id = $attendeeexits->id;
                                         $modelSdaSchedule->assigned = 0;
                                         $modelSdaSchedule->save(false);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             fclose($handle);
             Yii::$app->getSession()->setFlash('success', 'All SDA is Uploaded Successfully');
         }
     }
     return $this->render('excel', ['model' => $model]);
 }
 /**
  * Updates an existing Sda model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $modelsSdaSchedules = $model->sdaSchedules;
     if ($model->load(Yii::$app->request->post())) {
         $oldIDs = ArrayHelper::map($modelsSdaSchedules, 'id', 'id');
         $modelsSdaSchedules = Model::createMultiple(SdaSchedules::classname(), $modelsSdaSchedules);
         Model::loadMultiple($modelsSdaSchedules, Yii::$app->request->post());
         $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsSdaSchedules, 'id', 'id')));
         // ajax validation
         if (Yii::$app->request->isAjax) {
             Yii::$app->response->format = Response::FORMAT_JSON;
             return ArrayHelper::merge(ActiveForm::validateMultiple($modelsSdaSchedules), ActiveForm::validate($model));
         }
         $model->date = \DateTime::createFromFormat('m-d-Y', $model->date)->format('Y-m-d');
         // validate all models
         $valid = $model->validate();
         if ($valid || !$valid) {
             //change condition for update without being validate
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $model->save(false)) {
                     if (!empty($deletedIDs)) {
                         SdaSchedules::deleteAll(['id' => $deletedIDs]);
                     }
                     foreach ($modelsSdaSchedules as $modelSdaSchedule) {
                         if (!empty($modelSdaSchedule->attendee_id)) {
                             if (empty($modelSdaSchedule->congress_id)) {
                                 $modelSdaSchedule->sda_id = $model->id;
                                 $modelSdaSchedule->congress_id = $model->congress_id;
                                 $modelSdaSchedule->assigned = 0;
                                 $modelSdaSchedule->isNewRecord = true;
                             }
                             $flag = $modelSdaSchedule->save(false);
                             if (!$flag) {
                                 $transaction->rollBack();
                                 break;
                             }
                         }
                     }
                 }
                 if ($flag) {
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $model->id]);
                 }
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         }
     } else {
         $congressDetail = CongressDetails::findOne($model->congress_id);
         $congressStartDate = \DateTime::createFromFormat('Y-m-d', $congressDetail->start_date)->format('m-d-Y');
         $congressEndDate = \DateTime::createFromFormat('Y-m-d', $congressDetail->end_date)->format('m-d-Y');
         return $this->render('update', ['model' => $model, 'modelsSdaSchedules' => empty($modelsSdaSchedules) ? [new SdaSchedules()] : $modelsSdaSchedules, 'congressStartDate' => $congressStartDate, 'congressEndDate' => $congressEndDate]);
     }
 }