diff --git a/include/controllers/activity_controller.php b/include/controllers/activity_controller.php
index a93e4a45..ca0eed0b 100755
--- a/include/controllers/activity_controller.php
+++ b/include/controllers/activity_controller.php
@@ -223,27 +223,24 @@ public function importActivities()
if ($this->page->request->isPost()) {
$post = $this->page->request->post;
- /*
- Nothing in $post...
- foreach ($post as $key => $value) {
- echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."
";
- }
- exit;
- */
-
- if (empty($post->importactivities)) { // skal jeg bruge file eller importactivities ?
- $this->errorMessage('Ingen Excel fil valgt.');
+ $filename = $_FILES['file']['tmp_name'];
+ $originalName = $_FILES['file']['name'];
+ $indexOfXls = strpos($originalName, ".xls");
+ if($indexOfXls === false)
+ {
+ $this->errorMessage('Ingen Excel (.xls) fil valgt.');
$this->hardRedirect($this->url('aktiviteterhome'));
- }
+ }
else {
try {
- if ($this->model->importActivities()) {
+ if ($this->model->importActivities($filename)) {
$this->successMessage('Aktiviteter blev importeret.');
$this->log("Aktiviter blev importeret af {$this->model->getLoggedInUser()->user}", 'Aktivitet', $this->model->getLoggedInUser());
$this->hardRedirect($this->url('aktiviteterhome'));
} else
{
$this->errorMessage('Kunne ikke importere aktiviteter.');
+ $this->log("Aktiviter kunne ikke importeres af {$this->model->getLoggedInUser()->user}", 'Aktivitet', $this->model->getLoggedInUser());
$this->hardRedirect($this->url('aktiviteterhome'));
}
} catch (Exception $e) {
diff --git a/include/entities/aktiviteter.php b/include/entities/aktiviteter.php
index 299451c7..d2a116e0 100755
--- a/include/entities/aktiviteter.php
+++ b/include/entities/aktiviteter.php
@@ -350,4 +350,97 @@ protected function setAgeRequirement($type, $age)
return $this;
}
+
+ public function importActivity($array)
+ {
+ // Required values
+ if($array['A'] == null) $this->updated = date('Y-m-d H:i:s');
+ else $this->updated = date('Y-m-d H:i:s', $array['A']);
+
+ if($array['B'] == null) $this->navn = "Navn";
+ else $this->navn = $array['B'];
+
+ if($array['C'] == null) $this->title_en = "Name";
+ else $this->title_en = $array['C'];
+
+ if($array['D'] == null) $this->author = "Forfatter";
+ else $this->author = $array['D'];
+
+ $this->type = 'rolle';
+ $typeText = $array['E'];
+ if($typeText == null);
+ else if(stripos($typeText, 'bræt') !== false) $this->type = 'braet';
+ else if(stripos($typeText, 'braet') !== false) $this->type = 'braet';
+ else if(stripos($typeText, 'live') !== false) $this->type = 'live';
+ else if(stripos($typeText, 'figur') !== false) $this->type = 'figur';
+ else if(stripos($typeText, 'workshop') !== false) $this->type = 'workshop';
+ else if(stripos($typeText, 'otto') !== false) $this->type = 'ottoviteter';
+ else if(stripos($typeText, 'magic') !== false) $this->type = 'magic';
+ else if(stripos($typeText, 'system') !== false) $this->type = 'system';
+ else if(stripos($typeText, 'junior') !== false) $this->type = 'junior';
+
+ $this->sprog = 'dansk';
+ $sprogText = $array['F'];
+ if($sprogText == null);
+ else if(stripos($sprogText, 'engelsk') !== false)
+ {
+ if(stripos($sprogText, 'dansk') !== false) $this->sprog = 'dansk+engelsk';
+ else $this->sprog = 'engelsk';
+ }
+
+ if($array['G'] == null) $this->min_deltagere_per_hold = 1;
+ else $this->min_deltagere_per_hold = $array['G'];
+
+ if($array['H'] == null) $this->max_deltagere_per_hold = 1;
+ else $this->max_deltagere_per_hold = $array['H'];
+
+ if($array['I'] == null) $this->spilledere_per_hold = 1;
+ else $this->spilledere_per_hold = $array['I'];
+
+ if($array['J'] == null) $this->pris = 0;
+ else $this->pris = $array['J'];
+
+ if($array['M'] == null) $this->varighed_per_afvikling = 1;
+ else $this->varighed_per_afvikling = (float)$array['M'];
+
+ if($array['N'] == null) $this->teaser_dk = "TeaserDK";
+ else $this->teaser_dk = $array['N'];
+
+ if($array['O'] == null) $this->teaser_en = "TeaserEN";
+ else $this->teaser_en = $array['O'];
+
+ $this->replayable = 'nej';
+ $replayableText = $array['P'];
+ if($replayableText == null);
+ else if(stripos($replayableText, 'ja') !== false) $this->replayable = 'ja';
+
+ if($array['Q'] == null) $this->max_signups = 0;
+ else $this->max_signups = $array['Q'];
+
+ if($array['R'] == null) $this->foromtale = "foromtale";
+ else $this->foromtale = $array['R'];
+
+ if($array['S'] == null) $this->description_en = "foromtaleEN";
+ else $this->description_en = $array['S'];
+
+ // Default values:
+ $this->kan_tilmeldes = 'ja';
+ $this->note = NULL;
+ $this->lokale_eksklusiv = 'ja';
+ $this->wp_link = 0;
+ $this->tids_eksklusiv = 'ja';
+
+ // Default values (which are not visible under "Opret Aktivitet")
+ $this->hidden = 'nej';
+ $this->karmatype = 0;
+
+ if (!$this->insert()) { // TO DO: Check for conflicts. If activity with same name already exists, we should probably skip it.
+ return false;
+ }
+
+ if($array['K'] != null) $this->setMinAge($array['K']); // Optional field - can only be set after activity is created
+ if($array['L'] != null) $this->setMaxAge($array['L']); // Optional field - can only be set after activity is created
+
+ return true;
+ }
}
diff --git a/include/models/activity_model.php b/include/models/activity_model.php
index 09f74451..9dfa2c99 100755
--- a/include/models/activity_model.php
+++ b/include/models/activity_model.php
@@ -172,48 +172,50 @@ public function opretAktivitet(RequestVars $post)
return $activity;
}
- public function importActivities()
+ public function importActivities($filename)
{
- $activity = $this->createEntity('Aktiviteter');
- // Required values
- $activity->updated = date('Y-m-d H:i:s');
- $activity->navn = "Test";
- $activity->title_en = "Test";
- $activity->author = "Text";
- $activity->type = 'rolle';
- $activity->sprog = 'dansk';
- $activity->min_deltagere_per_hold = 1;
- $activity->max_deltagere_per_hold = 1;
- $activity->spilledere_per_hold = 1;
- $activity->pris = 0;
- $activity->varighed_per_afvikling = 1;
- $activity->teaser_dk = "TeaserDK";
- $activity->teaser_en = "TeaserEN";
- $activity->replayable = 'nej';
- $activity->max_signups = 0;
- $activity->foromtale = "foromtale"; // Can be null
- $activity->description_en = "DescriptionEN";
+ include_once LIB_FOLDER . 'PHPExcel/Classes/PHPExcel/IOFactory.php';
- // Default values:
- $activity->kan_tilmeldes = 'ja';
- $activity->note = NULL;
- $activity->lokale_eksklusiv = 'ja';
- $activity->wp_link = 0;
- $activity->tids_eksklusiv = 'ja';
+ $objPHPExcel = PHPExcel_IOFactory::load($filename);
+ $objPHPExcel->setActiveSheetIndex(0);
+ $worksheet = $objPHPExcel->getActiveSheet();
+ $highestColumn = $worksheet->getHighestColumn();
+ $highestRow = $worksheet->getHighestRow();
- // Default values (which are not visible under "Opret Aktivitet")
- $activity->hidden = 'nej';
- $activity->karmatype = 0;
+ if($highestColumn < 'S')
+ {
+ $this->log("Aktiviteter kunne ikke importeres - manglende kolonner", 'Aktivitet', $this->model->getLoggedInUser());
+ return false;
+ }
+ else if($worksheet->getCellByColumnAndRow('A',1)->getValue() != "Tidsstempel")
+ {
+ $this->log("Aktiviteter kunne ikke importeres - forkert format", 'Aktivitet', $this->model->getLoggedInUser());
+ return false;
+ }
- if (!$activity->insert()) {
- return false;
- }
+ $result = true;
- // Optional values:
- //$activity->setMinAge(7);
- //$activity->setMaxAge(50);
+ for ($row = 2; $row < $highestRow + 1; $row++) {
+ $array = array();
+ for ($column = 'A'; $column < 'T'; $column++) {
+ $val = $worksheet->getCell($column . $row)->getValue();
+ // echo "Row ".$row." Column ".$column. ": ".$val . "
";
+ $array[$column] = $val;
+ }
+
+ $activity = $this->createEntity('Aktiviteter');
+
+ if(!$activity->importActivity($array)) // TO DO: function importActivity() in this class didn't work
+ {
+ $result = false; // return true if at least one activity was succesfully imported
+ }
+ else
+ {
+ //$this->log("Aktivitet kunne ikke importeres", 'Aktivitet', $this->model->getLoggedInUser()); // TO DO: This log statement crashes the script
+ }
+ }
- return true;
+ return $result;
}
/**
diff --git a/include/templates/activity/importactivities.phtml b/include/templates/activity/importactivities.phtml
index 3c606d1b..09c5bb5a 100644
--- a/include/templates/activity/importactivities.phtml
+++ b/include/templates/activity/importactivities.phtml
@@ -2,13 +2,35 @@