Created
September 23, 2018 10:38
-
-
Save kartagis/34e3b3789417db9af26c1a590e632572 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\mitacan\Form; | |
use Drupal\Core\Form\FormBase; | |
use Drupal\Core\Form\FormStateInterface; | |
class MitacanUploadForm extends FormBase { | |
public function buildForm(array $form, FormStateInterface $form_state) { | |
$form['csv_upload'] = [ | |
'#type' => 'managed_file', | |
'#title' => 'CSV dosyasını yükleyin', | |
'#upload_validators' => [ | |
'file_validate_extensions' => ['csv'], | |
'file_validate_size' => [25600000], | |
] | |
]; | |
$form['submit'] = [ | |
'#type' => 'submit', | |
'#value' => 'Yükle' | |
]; | |
return $form; | |
} | |
public function getFormId() { | |
return 'mitacan.upload'; | |
} | |
public function submitForm(array &$form, FormStateInterface $form_state) { | |
$fids = $form_state->getValue('csv_upload', []); | |
$files = \Drupal::entityTypeManager()->getStorage('file')->loadMultiple($fids); | |
foreach ($files as $file) { | |
$destination = $file->toArray()['uri'][0]['value']; | |
$file = fopen($destination, "r"); | |
while (!feof($file)) { | |
$b2bresim = fgetcsv($file); | |
\Drupal::database()->insert('mitacan') | |
->fields(array( | |
'images' => $b2bresim[0], | |
'stokkodu' => $b2bresim[1], | |
'stokadi' => $b2bresim[2], | |
'stokkodugrup1' => $b2bresim[3], | |
'stokadigrup1' => $b2bresim[4], | |
'stokkodugrup2' => $b2bresim[5], | |
'stokadigrup2' => $b2bresim[6], | |
'stokkodugrup3' => $b2bresim[7], | |
'stokadigrup3' => $b2bresim[8], | |
'stokkodugrup4' => $b2bresim[9], | |
'stokadigrup4' => $b2bresim[10], | |
'listeno' => $b2bresim[11], | |
'listeadi' => $b2bresim[12], | |
'logolar' => $b2bresim[13] | |
)) | |
->execute(); | |
} | |
fclose($file); | |
drupal_set_message('CSV data added to the database'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment