diff --git a/homepage/participo/lib/participoLib/participo.php b/homepage/participo/lib/participoLib/participo.php index 74958d4..1f5181a 100644 --- a/homepage/participo/lib/participoLib/participo.php +++ b/homepage/participo/lib/participoLib/participo.php @@ -506,7 +506,10 @@ function logLoginsToJsonFile($userName, $fileName = 'lastLogins.json') if (!array_key_exists('lastLogins', $lastLogins[$userName])) { $lastLogins[$userName]['lastLogins'] = []; } - $lastLogins[$userName]['lastLogins'] = array_merge([date('Y-m-d H:i:s')], $lastLogins[$userName]['lastLogins']); + $usersLastLogins = $lastLogins[$userName]['lastLogins']; + $usersLastLogins = array_merge([date('Y-m-d H:i:s')], $usersLastLogins); + $usersLastLogins = array_slice($usersLastLogins, 0, 10); + $lastLogins[$userName]['lastLogins'] = $usersLastLogins; file_put_contents($fileName, json_encode($lastLogins)); } catch (Exception $e) { // silently ignore errors diff --git a/homepage/participo/lib/participoLib/ride.php b/homepage/participo/lib/participoLib/ride.php new file mode 100644 index 0000000..46b9f3f --- /dev/null +++ b/homepage/participo/lib/participoLib/ride.php @@ -0,0 +1,147 @@ +name = $name; + $this->value = $value; + $this->type = $type; + } + static public function collect(array $row){ + $names = []; + foreach($row as $entry){ + if(isset($entry->name)){ + $names[] = $entry->name; + } + } + return $names; + } + public function value(){return $this->value;} + public function setValue($value){$this->value = $value;} + private string $name = null; + private $value = null; + private int $data_type = null; +} + +class Ride{ + /** Constructor + * + * - filters/sanitizes all inputs + * - sets all members + * + * @param mixed $id setValue for the id + * @param mixed $eventId setValue for the eventId + * @param mixed $driverId setValue for the driverId + * @param mixed $seats setValue for the seats + * @param string $passengerIds string of comma separated values + */ + public function __construct($id, $eventId, $driverId, $seats, string $passengerIds){ + $this->id = new PdoCellValue('id', filterId($id), PDO::PARAM_INT); + $this->eventId = new PdoCellValue('eventId', filterId($eventId), PDO::PARAM_INT); + $this->driverId = new PdoCellValue('fahrerId', filterId($driverId), PDO::PARAM_INT); + $this->seats= new PdoCellValue('plaetze', filterCount($seats), PDO::PARAM_INT); + $this->passengerIds = new PdoCellValue('mitfahrer', filterCsv($passengerIds, filterId), PDO::PARAM_STRING); + } + + /** parse and sanitize a csvFormatted string */ + function filterCsv(string $list, $callback, string $separator=',', bool $dontTrim = false){ + $list = explode($separator, $list); + foreach($list as &$element){ + $element = $callback($element); + if(!$dontTrim){ + $element = trim($element); + } + } + return $list; + } + +//// +// private functions +//// + +// DbInterface + private static $dbName = 'cwsvjudo'; + private static $dbTableName = 'wkParticipo_Fahrten'; + private static $dbFullTableNameString = '`'.$dbName.'`.`'.$dbTableName.'`'; + + /** Slice a list of arrays 'horizontal' through a specific key + * + * - iterate through a list of arrays and collect the values under a specific key + * - if the key doesn't exist nothing (not even null) will be collected + * + * @param array $array source array + * @param mixed $key key to collect + * @return array list of array entries + */ + function sliceArrayByKey(array $array, $key){ + $sliced = []; + foreach($array as $entry){ + if(isset($entry[$key])){ + $sliced[] = $entry[$key]; + } + } + return $sliced; + } + + private static function dbSelect(){} + private static function dbInsert(array $values){ + $names = []; + $binds = []; + $params = []; + foreach($values as $value){ + $names[] = $value->name; + $binds[] = ':'.$value->name; + $params[':'.$value->name] = [ + 'value' => $value->name + , 'data_type' => $value->type + ]; + } + + $query = 'INSERT INTO '.$dbFullTableNameString.' ('.implode(',', $names).') values ('.implode(',', $binds).');'; + + // @todo remove ignoreErrors again + // @todo Inserting-Starter-Statement returns false on execution: Why? It seems to succeed! + $response = dbConnector::query($query, $params, ['ignoreErrors'=>true]); + return dbConnector::getLastInsertId(); + } + private static function dbDelete(){} + private static function dbUpdate(){} + + private + +//// +// private variables +//// + + /** Unique Identifier for the Ride + * + * @var int > 0 + */ + private $id=null; + /** Id of the event for the ride + * + * @var int > 0 + */ + private $eventId = null; + /** Id of the user who offered the drive + * + * @var int > 0 + */ + private $driverId = null; + /** number of seats the drive offers + * + * @var int > 0 + */ + private $seats = null; + /** List of Ids of the users this ride chauffeurs + * + * @var array(int > 0) + */ + private $passengerIds = null; + + private $event = null; + private $driver = null; + private $passengers = null; +} \ No newline at end of file