示例#1
0
 protected function setSave($quality = 100)
 {
     $output = new Output($this->actions->output, $this->background, $this->layers);
     $this->save = $output->save($quality);
 }
 /**
  * Import data from excel template
  * @param type $filePath
  * @param type $fields
  * @param Output $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['satker_code'] == 'Kode Satker' and $fieldValidate['activity_code'] == 'Kode Kegiatan' and $fieldValidate['code'] == 'Kode Output' and $fieldValidate['name'] == 'Uraian Output') {
             $validated = TRUE;
         }
         //end of validation
         if ($validated == TRUE) {
             $highestRow = $worksheet->getHighestRow();
             // e.g. 10
             $isSuccess = FALSE;
             for ($row = $startingRow; $row <= $highestRow; ++$row) {
                 $attributes = array();
                 //Read data
                 for ($col = 0; $col < count($fields); ++$col) {
                     $val = $worksheet->getCellByColumnAndRow($fields[$col]['col'], $row)->getValue();
                     $attributes[$fields[$col]['name']] = $val;
                 }
                 //Eof read data
                 $activitycode = trim($attributes['satker_code'], " \t\n\r\v") . '.' . trim($attributes['activity_code'], " \t\n\r\v");
                 $code = $activitycode . '.' . trim($attributes['code'], " \t\n\r\v");
                 $recorded = Output::model()->find(array('condition' => "code='{$code}'"));
                 if ($attributes['satker_code'] != NULL and $attributes['activity_code'] != NULL and $attributes['code'] != NULL and $attributes['name'] != NULL) {
                     if ($recorded) {
                         //Doing update if data with sae code exist
                         $recorded->attributes = $attributes;
                         $satkerCode = isset($attributes['satker_code']) ? trim($attributes['satker_code'], " \t\n\r\v") : NULL;
                         $activityCode = $activitycode;
                         $outputCode = $code;
                         $recorded->satker_code = $satkerCode;
                         $recorded->activity_code = $satkerCode . '.' . $activityCode;
                         $recorded->code = $satkerCode . '.' . $activityCode . '.' . $outputCode;
                         if ($recorded->update()) {
                             $isSuccess = TRUE;
                         }
                         //Eof update data
                     } else {
                         //Doing create data if no recorded data with same code
                         $model = new Output();
                         $model->attributes = $attributes;
                         $satkerCode = isset($attributes['satker_code']) ? trim($attributes['satker_code'], " \t\n\r\v") : NULL;
                         $activityCode = isset($attributes['activity_code']) ? trim($attributes['activity_code'], " \t\n\r\v") : NULL;
                         $outputCode = isset($attributes['code']) ? trim($attributes['code'], " \t\n\r\v") : NULL;
                         $model->satker_code = $satkerCode;
                         $model->activity_code = $satkerCode . '.' . $activityCode;
                         $model->code = $satkerCode . '.' . $activityCode . '.' . $outputCode;
                         if ($model->save()) {
                             $isSuccess = TRUE;
                         }
                         //                        Eof create data
                     }
                 }
             }
             if ($isSuccess) {
                 unlink($filePath);
                 Yii::app()->user->setFlash('success', "File berhasil diimport ke dalam master");
                 $this->redirect(array('index'));
             } else {
                 unlink($filePath);
                 Yii::app()->user->setFlash('error', "Mohon isikan data secara lengkap");
             }
         } else {
             unlink($filePath);
             Yii::app()->user->setFlash('error', "Pastikan file yang Anda upload sudah benar!");
             $this->redirect(array('import'));
         }
     }
 }