public function actionImport()
 {
     $total_records = 0;
     $inserted_records = 0;
     $msg = "";
     $model = new Business();
     $model->setScenario('importFile');
     //$file = CUploadedFile::getInstance($model,'csv_file');
     if (isset($_POST['Business'])) {
         //                          echo $_POST['Business']['import_option']; die();
         if (!empty($_FILES['Business']['tmp_name']['import_file'])) {
             $file = CUploadedFile::getInstance($model, 'import_file');
             $fp = fopen($file->tempName, 'r');
             $row = 1;
             if ($fp) {
                 $line = fgetcsv($fp, 1000, ";");
                 $first_time = true;
                 $c = 1;
                 if ($_POST['Business']['import_option'] == "Insert") {
                     do {
                         if ($first_time == true) {
                             $first_time = false;
                             $headerLine = $line;
                             continue;
                         }
                         //$data = explode(",",$line);
                         //$data = $line[0];
                         //   $data = explode(';',$line);
                         $fData = array_combine($headerLine, $line);
                         //echo '<pre>'; var_dump($fData); echo '</pre>'; die();
                         $email_id = $fData['business_email_id1'];
                         $modelObj = Business::model()->findByAttributes(array('business_email_id1' => $email_id));
                         if (isset($modelObj->business_email_id1)) {
                             $modelObj = new Business();
                             $modelObj->setIsNewRecord(true);
                             $modelObj->attributes = $fData;
                             $modelObj->logo_photo_name = $fData['logo_photo_name'];
                             $modelObj->cover_photo_name = $fData['cover_photo_name'];
                             //     echo '<pre>'; var_dump($modelObj); echo '</pre>'; die();
                             if ($modelObj->validate()) {
                                 $modelObj->save();
                                 var_dump($model->getErrors());
                             } else {
                                 die(CVarDumper::dump($modelObj->errors, 10, true));
                             }
                             $modelObj->business_id;
                             $inserted_records = $inserted_records + 1;
                         }
                         $total_records = $total_records + 1;
                     } while (($line = fgetcsv($fp, 1000, ";")) != FALSE);
                     $msg = $inserted_records . " records are inserted" . " out of " . $total_records;
                     $this->render('import', array('model' => $model, 'total_records' => $total_records, 'inserted_records' => $inserted_records, 'msg' => $msg));
                 }
                 if ($_POST['Business']['import_option'] == "Update") {
                     do {
                         if ($first_time == true) {
                             $first_time = false;
                             $headerLine = $line;
                             continue;
                         }
                         //$data = explode(",",$line);
                         $data = $line[0];
                         $data = explode(',', $data);
                         //echo '<pre>'; var_dump($headerLine); echo '</pre>'; die();
                         $fData = array_combine($headerLine, $data);
                         $email_id = $fData['business_email_id1'];
                         $modelObj = Business::model()->find('business_email_id1=:business_email_id1', array(':business_email_id1' => $email_id));
                         //  echo '<pre>'; var_dump($modelObj); echo '</pre>'; die();
                         //                                                echo var_dump($modelObj);
                         if ($modelObj) {
                             $modelObj->attributes = $fData;
                             $modelObj->logo_photo_name = $fData['logo_photo_name'];
                             $modelObj->cover_photo_name = $fData['cover_photo_name'];
                             //   echo '<pre>'; var_dump($modelObj); echo '</pre>'; die();
                             $modelObj->save();
                             $modelObj->business_id;
                             $inserted_records = $inserted_records + 1;
                         }
                         $total_records = $total_records + 1;
                     } while (($line = fgetcsv($fp, 1000, ";")) != FALSE);
                     // echo "count:".$inserted_records.'---'.$total_records; die();
                     $msg = $inserted_records . " records are updated" . " out of " . $total_records;
                     $this->render('import', array('model' => $model, 'total_records' => $total_records, 'inserted_records' => $inserted_records, 'msg' => $msg));
                     //$this->redirect('././view');
                 }
             }
             $msg = "Please select insert or update option for import file.";
             $this->render('import', array('model' => $model, 'total_records' => $total_records, 'inserted_records' => $inserted_records, 'msg' => $msg));
         }
         $msg = "Please upload file.";
         $this->render('import', array('model' => $model, 'total_records' => $total_records, 'inserted_records' => $inserted_records, 'msg' => $msg));
     }
     $this->render('import', array('model' => $model, 'total_records' => $total_records, 'inserted_records' => $inserted_records, 'msg' => $msg));
 }
 public function actionSearch()
 {
     $model = new Business();
     // uncomment the following code to enable ajax-based validation
     /*
     if(isset($_POST['ajax']) && $_POST['ajax']==='business-search-form')
     {
     	echo CActiveForm::validate($model);
     	Yii::app()->end();
     }
     */
     if (isset($_POST['Business'])) {
         $model->attributes = $_POST['Business'];
         if ($model->validate()) {
             // form inputs are valid, do something here
             return;
         }
     }
     $this->render('search', array('model' => $model));
 }
 public function actionCsvForm()
 {
     $model = new Business();
     // uncomment the following code to enable ajax-based validation
     /*
     if(isset($_POST['ajax']) && $_POST['ajax']==='business-csvForm-form')
     {
     	echo CActiveForm::validate($model);
     	Yii::app()->end();
     }
     */
     $msg = '';
     $type = '';
     $r = 0;
     if (isset($_POST['Business']) && $_POST['Business']['uploadmode'] == 1) {
         unset($_POST['Business']['uploadmode']);
         //echo '<pre>'; var_dump($_POST['Business']); echo '</pre>';	die();
         $file = CUploadedFile::getInstance($model, 'csv_file');
         if (!empty($file)) {
             $fp = fopen($file->tempName, 'r');
             $row = 1;
             $headerLine = 1;
             $r = 0;
             $dataHead = array();
             try {
                 $flag = false;
                 $duplicate = 0;
                 if ($fp !== FALSE) {
                     while (($data = fgetcsv($fp)) !== FALSE) {
                         $num = count($data);
                         if ($row !== $headerLine) {
                             if (count($dataHead) == count($data)) {
                                 $newData = array_combine($dataHead, $data);
                                 $modelObj = new Business();
                                 $modelObj->setIsNewRecord(true);
                                 $modelObj->attributes = $newData;
                                 $modelObj->logo_photo_name = $newData['logo_photo_name'];
                                 $modelObj->logo_photo_name = $newData['cover_photo_name'];
                                 $result = $modelObj->model()->countByAttributes(array('business_email_id1' => $newData['business_email_id1']));
                                 if (!$result) {
                                     if ($modelObj->validate()) {
                                         if ($modelObj->save()) {
                                             $flag = true;
                                             $r++;
                                         }
                                     } else {
                                         die(CVarDumper::dump($modelObj->errors, 10, true));
                                     }
                                 } else {
                                     $duplicate++;
                                     $flag = true;
                                 }
                             }
                         } else {
                             $dataHead = $data;
                         }
                         $row++;
                         $type = 'new';
                     }
                 }
                 if ($flag) {
                     $msg .= $r . '  row(s) inserted successfully';
                     Yii::app()->user->setFlash('success', $msg);
                     //$this->render('csvForm',array('model'=>$model));
                     //$this->render('index', array('model' => $model,'msg'=>$r,'duplicaterow'=>$duplicate));
                 }
             } catch (Exception $error) {
                 throw new CHttpException(404, 'File can\'t upload. Please check your file');
             }
         }
         // end if of file is not empty
     }
     /*
      * Update record by csv
      */
     if (isset($_POST['Business']) && isset($_POST['Business']['uploadmode']) && $_POST['Business']['uploadmode'] == 2) {
         $r = $this->_update($_POST['Business']);
         $msg .= $r . ' row(s) updated successfully';
         Yii::app()->user->setFlash('success', $msg);
     }
     $this->render('csvForm', array('model' => $model));
 }