
Minimal data required title ( text ) abstract (text ) type_id (int) or type (string type name) track_id (int) or track ( string track name) Optional fields social_summary ( text ) attendees_expected_learnt ( text ) * provided only if event is presentation type location (id or name) level (text possible values [N/A, Beginner, Intermediate, Advanced]) * provided only if event is presentation type allow_feedback (int possible values [0,1]) to_record (int possible values [0,1]) tags ( repetitive group: values | delimited) sponsors ( repetitive group : names | delimited ) speakers ( repetitive group: emails | delimited ) start_date (mandatory if is_published is 1) Y-m-d H:i:s format ( summit time zone ) end_date (mandatory if is_published is 1) Y-m-d H:i:s format ( summit time zone ) is_published (int possible values [0,1]) , if this field is set 1 start_date and end_date are mandatories selection_plan (string selection plan name) * provided only if event is presentation type attendees_expected_learnt ( text , only presentations) problem_addressed ( text , only presentations) added endpoint POST /api/v1/summits/{summit_id}/events/csv multipart/form-data * Expected params file send_speaker_email Change-Id: I245d4e1a22e2c5e3c4120c559d0ecb4a5a021c78 Signed-off-by: smarcet <smarcet@gmail.com>
147 lines
4.1 KiB
PHP
147 lines
4.1 KiB
PHP
<?php namespace App\Services\Utils;
|
|
/**
|
|
* Copyright 2019 OpenStack Foundation
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
**/
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
use Iterator;
|
|
/**
|
|
* Class CSVReader
|
|
* @package App\Services\Utils
|
|
*/
|
|
final class CSVReader implements Iterator {
|
|
|
|
private $position = 0;
|
|
/**
|
|
* @var array
|
|
*/
|
|
private $lines = [];
|
|
/**
|
|
* @var array
|
|
*/
|
|
private $header = [];
|
|
|
|
/**
|
|
* CSVReader constructor.
|
|
* @param array $header
|
|
* @param array $lines
|
|
*/
|
|
private function __construct(array $header, array $lines)
|
|
{
|
|
$this->header = $header;
|
|
$this->lines = $lines;
|
|
}
|
|
|
|
/**
|
|
* @param string $content
|
|
* @return array
|
|
*/
|
|
public static function buildFrom(string $content):CSVReader
|
|
{
|
|
Log::debug(sprintf("CSVReader::buildFrom content %s", $content));
|
|
$data = str_getcsv($content,"\n" );
|
|
Log::debug(sprintf("CSVReader::buildFrom data %s", json_encode($data)));
|
|
$idx = 0;
|
|
$header = [];
|
|
$lines = [];
|
|
foreach($data as $row)
|
|
{
|
|
$row = str_getcsv($row, ",");
|
|
Log::debug(sprintf("CSVReader::buildFrom row %s", json_encode($row)));
|
|
++$idx;
|
|
if($idx === 1) {
|
|
|
|
foreach($row as $idx => $val){
|
|
// check the encoding of the header values
|
|
if(mb_detect_encoding($val) == 'UTF-8')
|
|
$val = iconv('utf-8', 'ascii//TRANSLIT', $val);
|
|
$header[] = $val;
|
|
}
|
|
continue;
|
|
}
|
|
$line = [];
|
|
if(count($row) != count($header)) continue;
|
|
for($i = 0; $i < count($header); $i++){
|
|
$line[$header[$i]] = trim($row[$i]);
|
|
}
|
|
$lines[] = $line;
|
|
|
|
} //parse the items in rows
|
|
return new CSVReader($header, $lines);
|
|
}
|
|
|
|
/**
|
|
* @param string $colName
|
|
* @return bool
|
|
*/
|
|
public function hasColumn(string $colName):bool {
|
|
if(mb_detect_encoding($colName) == 'UTF-8')
|
|
$colName = iconv('utf-8', 'ascii//TRANSLIT', $colName);
|
|
return in_array(trim($colName), $this->header);
|
|
}
|
|
|
|
/**
|
|
* Return the current element
|
|
* @link https://php.net/manual/en/iterator.current.php
|
|
* @return mixed Can return any type.
|
|
* @since 5.0.0
|
|
*/
|
|
public function current()
|
|
{
|
|
return $this->lines[$this->position];
|
|
}
|
|
|
|
/**
|
|
* Move forward to next element
|
|
* @link https://php.net/manual/en/iterator.next.php
|
|
* @return void Any returned value is ignored.
|
|
* @since 5.0.0
|
|
*/
|
|
public function next()
|
|
{
|
|
++$this->position;
|
|
}
|
|
|
|
/**
|
|
* Return the key of the current element
|
|
* @link https://php.net/manual/en/iterator.key.php
|
|
* @return mixed scalar on success, or null on failure.
|
|
* @since 5.0.0
|
|
*/
|
|
public function key()
|
|
{
|
|
return $this->position;
|
|
}
|
|
|
|
/**
|
|
* Checks if current position is valid
|
|
* @link https://php.net/manual/en/iterator.valid.php
|
|
* @return boolean The return value will be casted to boolean and then evaluated.
|
|
* Returns true on success or false on failure.
|
|
* @since 5.0.0
|
|
*/
|
|
public function valid()
|
|
{
|
|
return isset($this->lines[$this->position]);
|
|
}
|
|
|
|
/**
|
|
* Rewind the Iterator to the first element
|
|
* @link https://php.net/manual/en/iterator.rewind.php
|
|
* @return void Any returned value is ignored.
|
|
* @since 5.0.0
|
|
*/
|
|
public function rewind()
|
|
{
|
|
$this->position = 0;
|
|
}
|
|
} |