/**
  * 录入
  *
  */
 public function actionCreate()
 {
     parent::_acl('province_create');
     $model = new Province();
     if (isset($_POST['Province'])) {
         $model->attributes = $_POST['Province'];
         if ($model->save()) {
             AdminLogger::_create(array('catalog' => 'create', 'intro' => '录入省份,ID:' . $model->id));
             $this->redirect(array('index'));
         }
     }
     $this->render('create', array('model' => $model));
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     IsAuth::Admin();
     $model = new Province();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Province'])) {
         $model->attributes = $_POST['Province'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->province_id));
         }
     }
     $this->render('create', array('model' => $model));
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Province();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Province'])) {
         $model->attributes = $_POST['Province'];
         if ($model->save()) {
             Yii::app()->user->setFlash('success', "Provinsi sudah ditambah");
             $this->redirect(array('admin', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
Exemple #4
0
 function updatePosition()
 {
     $positionList = $this->input->post('positionList');
     $idList = $this->input->post('idList');
     $locations = new Province();
     for ($i = 0; $i < count($idList); $i++) {
         $locations->where("id", $idList[$i]);
         $locations->get();
         $locations->position = $positionList[$i];
         $locations->save();
         $locations->clear();
     }
     redirect("admin/locations/listAll/");
 }
 public function actionUpload()
 {
     parent::actionUpload();
     $folder = $_SERVER['DOCUMENT_ROOT'] . Yii::app()->request->baseUrl . '/upload/';
     // folder for uploaded files
     $file = $folder . basename($_FILES['uploadfile']['name']);
     if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
         $row = 0;
         if (($handle = fopen($file, "r")) !== FALSE) {
             while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                 if ($row > 0) {
                     $model = Province::model()->findByPk((int) $data[0]);
                     if ($model === null) {
                         $model = new Province();
                     }
                     $model->provinceid = (int) $data[0];
                     $country = Country::model()->findbyattributes(array('countryname' => $data[1]));
                     if ($country !== null) {
                         $model->countryid = $country->countryid;
                     }
                     $model->provincename = $data[2];
                     $model->recordstatus = (int) $data[3];
                     try {
                         if (!$model->save()) {
                             $this->messages = $this->messages . Catalogsys::model()->getcatalog(' upload error at ' . $data[0]);
                         }
                     } catch (Exception $e) {
                         $this->messages = $this->messages . $e->getMessage();
                     }
                 }
                 $row++;
             }
         } else {
             $this->messages = $this->messages . ' memory or harddisk full';
         }
         fclose($handle);
     } else {
         $this->messages = $this->messages . ' check your directory permission';
     }
     if ($this->messages == '') {
         $this->messages = 'success';
     }
     echo $this->messages;
 }
 public function getRows($pq)
 {
     $rows = $pq->find("table[bgcolor='#ffccff'] tr[bgcolor='#ccffff']");
     $n = count($rows);
     echo 'Jumlah propinsi: ' . $n . " \n";
     $count = 0;
     foreach ($rows as $value) {
         $count++;
         $row = pq($value);
         $kolom_provinsi = $row->find('td:eq(1) > a');
         $nama_provinsi = $kolom_provinsi->text();
         $link_provinsi = str_replace(' ', '%20', self::NomorNetKodePosBaseUrl . $kolom_provinsi->attr('href')) . '&perhal=1000';
         echo sprintf("Memproses propinsi %d dari %d\n", $count, $n);
         echo sprintf("Propinsi: %s Link: %s\n", $nama_provinsi, $link_provinsi);
         $province = Province::model()->findByAttributes(array('name' => $nama_provinsi));
         if (!$province instanceof Province) {
             $province = new Province();
             $province->name = $nama_provinsi;
             if (!$province->save()) {
                 throw new CException('Cannot save province');
             }
         }
         $countdistrict = 0;
         $pqDist = phpQuery::newDocumentHtml($this->getContents($link_provinsi));
         $rowsDist = $pqDist->find("table[bgcolor='#ffccff'] tr[bgcolor='#ccffff']");
         $nDist = count($rowsDist);
         $countDist = 0;
         foreach ($rowsDist as $valueDist) {
             $countDist++;
             $rowDist = pq($valueDist);
             switch ($rowDist->find('td:eq(2)')->text()) {
                 case 'Kota':
                     $tipeDist = 'kota';
                     break;
                 default:
                 case 'Kab.':
                     $tipeDist = 'kabupaten';
                     break;
             }
             $kolomDist = $rowDist->find('td:eq(3) > a');
             $namaDist = $kolomDist->text();
             $linkDist = str_replace(' ', '%20', self::NomorNetKodePosBaseUrl . $kolomDist->attr('href')) . '&perhal=1000';
             echo sprintf("Memproses distrik " . $province->name . " %d dari %d\n", $countDist, $nDist);
             echo sprintf("Distrik: %s Link: %s\n", $namaDist, $linkDist);
             $distrik = District::model()->findByAttributes(array('name' => $namaDist, 'province_id' => $province->id, 'type' => $tipeDist));
             if (!$distrik instanceof District) {
                 $distrik = new District();
                 $distrik->name = $namaDist;
                 $distrik->type = $tipeDist;
                 $distrik->province_id = $province->id;
                 if (!$distrik->save()) {
                     throw new CException('Cannot save district');
                 }
             }
             $pqZone = phpQuery::newDocumentHtml($this->getContents($linkDist));
             $rowsZone = $pqZone->find("table[bgcolor='#ffccff'] tr[bgcolor='#ccffff']");
             $nZone = count($rowsZone);
             $countZone = 0;
             foreach ($rowsZone as $valueZone) {
                 $countZone++;
                 $rowZone = pq($valueZone);
                 $kolomZone = $rowZone->find('td:eq(4) > a');
                 $namaZone = $kolomZone->text();
                 $linkZone = str_replace(' ', '%20', self::NomorNetKodePosBaseUrl . $kolomZone->attr('href')) . '&perhal=1000';
                 echo sprintf("Memproses zone  %d dari %d\n", $countZone, $nZone);
                 echo sprintf("zone: %s Link: %s\n", $namaZone, $linkZone);
                 $new_zone = false;
                 $zone = Zone::model()->findByAttributes(array('name' => $namaZone, 'district_id' => $distrik->id));
                 if (!$zone instanceof Zone) {
                     $zone = new Zone();
                     $zone->name = $namaZone;
                     $zone->active = 1;
                     $zone->district_id = $distrik->id;
                     if (!$zone->save()) {
                         throw new CException('Cannot save Zone');
                     }
                     $new_zone = true;
                 }
                 echo 'sukses saving zone' . "\n";
                 $countArea = 0;
                 $pqArea = phpQuery::newDocumentHtml($this->getContents($linkZone));
                 $rowsArea = $pqArea->find("table[bgcolor='#ffccff'] tr[bgcolor='#ccffff']");
                 $nArea = count($rowsArea);
                 // Let's speed up things a bit
                 $trans = Yii::app()->db->beginTransaction();
                 foreach ($rowsArea as $valueArea) {
                     $countArea++;
                     $rowArea = pq($valueArea);
                     $kolomArea = $rowArea->find('td:eq(2) > a');
                     $kolomKodePos = $rowArea->find('td:eq(1)');
                     $namaArea = $kolomArea->text();
                     $kodePos = $kolomKodePos->text();
                     $linkKodePos = str_replace(' ', '%20', self::NomorNetKodePosBaseUrl . $kolomArea->attr('href')) . '&perhal=1000';
                     $area = $new_zone ? null : Area::model()->findByAttributes(array('name' => $namaArea, 'zone_id' => $zone->id));
                     if (!$area instanceof Area) {
                         $area = new Area();
                         $area->name = $namaArea;
                         $area->postcode = $kodePos;
                         $area->zone_id = $zone->id;
                         if (!$area->save()) {
                             throw new CException('Cannot save area');
                         }
                     }
                     echo 'Sukses Saving Code Post' . "/n kode pos adalah " . $area->postcode . "\n";
                 }
                 $trans->commit();
             }
         }
     }
 }
 /**
  * Import data from excel template
  * @param type $filePath
  * @param type $fields
  * @param Province $model
  * @param type $startingRow
  */
 public function importExcelToMysql($filePath, $fields = array(), $model = null, $startingRow = 2)
 {
     $objPHPExcel = PHPExcel_IOFactory::load($filePath);
     foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
         //field validation
         $fieldValidate = array();
         $validated = FALSE;
         for ($col = 0; $col < count($fields); ++$col) {
             $val = $worksheet->getCellByColumnAndRow($fields[$col]['col'], 1)->getValue();
             $fieldValidate[$fields[$col]['name']] = $val;
         }
         if ($fieldValidate['code'] == 'Kode Provinsi' and $fieldValidate['name'] == 'Nama Provinsi') {
             $validated = TRUE;
         }
         //end of validation
         if ($validated == TRUE) {
             $highestRow = $worksheet->getHighestRow();
             // e.g. 10
             $isSuccess = false;
             for ($row = $startingRow; $row <= $highestRow; ++$row) {
                 $attributes = array();
                 for ($col = 0; $col < count($fields); ++$col) {
                     $val = $worksheet->getCellByColumnAndRow($fields[$col]['col'], $row)->getValue();
                     $attributes[$fields[$col]['name']] = $val;
                 }
                 if ($attributes['code'] != NULL and $attributes['name'] != NULL) {
                     $code = $attributes['code'];
                     $recorded = Province::model()->find(array('condition' => "code='{$code}'"));
                     if ($recorded) {
                         $recorded->attributes = $attributes;
                         if ($recorded->update()) {
                             $isSuccess = TRUE;
                         }
                     } else {
                         $model = new Province();
                         $model->attributes = $attributes;
                         if ($model->save()) {
                             $isSuccess = TRUE;
                         }
                     }
                 }
             }
             if ($isSuccess) {
                 unlink($filePath);
                 Yii::app()->user->setFlash('success', "Data berhasil diimport.");
                 $this->redirect(array('index'));
             } else {
                 unlink($filePath);
                 Yii::app()->user->setFlash('error', "Mohon masukkan data secara lengkap.");
             }
         } else {
             unlink($filePath);
             Yii::app()->user->setFlash('error', "Pastikan file yang Anda upload sudah benar!");
             $this->redirect(array('import'));
         }
     }
 }