/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new BaptismRecord();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['BaptismRecord'])) {
         $model->attributes = $_POST['BaptismRecord'];
         if ($model->save()) {
             if (isset($model->member_id) and $model->member_id) {
                 if (!isset($model->member->baptism_dt)) {
                     $member = $model->member;
                     if (!$member->saveAttributes(array('baptism_dt' => $model->baptism_dt))) {
                         Yii::trace(sprintf("Error saving member %s baptism_dt: %s", $member->id, implode(", ", $member->getErrors('baptism_dt'))), 'application.controllers.BaptismRecord');
                     }
                 }
             }
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
 public function actionIndex($file)
 {
     $rfile = preg_replace('/\\.csv$/', '-rej.csv', $file);
     if (($fh = fopen($file, 'r')) !== FALSE) {
         $proc = false;
         $rej = 0;
         $num = 0;
         while (($data = fgetcsv($fh, 1000)) !== FALSE) {
             if ($proc) {
                 ++$num;
                 try {
                     $rec = new BaptismRecord();
                     $rec->name = $data[0];
                     $dob = FormatHelper::dateConv($data[1]);
                     $rec->dob = $dob;
                     $rec->baptism_dt = FormatHelper::dateConv($data[2]);
                     $rec->baptism_place = $data[3];
                     if (preg_match('/^m/i', $data[4])) {
                         $rec->sex = 1;
                     } else {
                         $rec->sex = 2;
                     }
                     $rec->residence = $data[5];
                     $rec->mother_tongue = $data[6];
                     $rec->fathers_name = $data[7];
                     $rec->mothers_name = $data[8];
                     $rec->godfathers_name = $data[9];
                     $rec->godmothers_name = $data[10];
                     $rec->minister = $data[11];
                     try {
                         $rec->confirmation_dt = FormatHelper::dateConv($data[12]);
                     } catch (Exception $e) {
                         echo "Exception rec #{$num}: " . $e->getMessage() . " - Confirmation date. Set to empty\n";
                     }
                     try {
                         $rec->marriage_dt = FormatHelper::dateConv($data[13]);
                     } catch (Exception $e) {
                         echo "Exception rec #{$num}: " . $e->getMessage() . " - Marriage date. Set to empty\n";
                     }
                     $rec->remarks = $data[14];
                     if (!$rec->save()) {
                         throw new Exception("Unable to save record");
                     }
                 } catch (Exception $e) {
                     if (1 == ++$rej) {
                         if (($rej_fh = fopen($rfile, 'w')) === FALSE) {
                             echo "File {$rfile} cannot be opened in write mode. Do you have write permissions?";
                             echo "Printing rejected records to STDOUT";
                             $rej_fh = STDOUT;
                         }
                         fputcsv($rej_fh, $hdr);
                     }
                     echo "Caught exception Record #{$num}: " . $e->getMessage() . ". Saved to rejects file\n";
                     fputcsv($rej_fh, $data);
                 }
             } else {
                 $proc = true;
                 $hdr = $data;
             }
         }
     }
     fclose($fh);
     echo "Import complete. Total records: {$num}, success: " . ($num - $rej);
     if (isset($rej_fh)) {
         fclose($rej_fh);
         echo ", rejected: {$rej}.\nRejects saved to: {$rfile}";
     }
     echo ".\n";
 }