<?php
namespace CityMountain\CourseManagementBundle\Utils;
use CityMountain\ToolBundle\Utils\OperationTool;
use Contao\Versions;
use Contao\Email;
use Contao\BackendUser;
use CityMountain\CourseManagementBundle\Utils\hookReplaceInsertTags;
class OperationCM
{
public static function getDefaultUnitFromUserToEdit()
{
$objRights = OperationTool::getUserRights(BackendUser::getInstance()->id,1,array("AND fkModuleAction='MOD09EDITUNIT'"));
//dump($objRights);
//if (array_key_exists('mod09',$objRights['module']) && array_key_exists('MOD09EDITUNIT',$objRights['module']['mod09']) && count($objRights['module']['mod09']['MOD09EDITUNIT'])>0)
if (count($objRights['entry'])>0)
{
return current($objRights['entry'])['fkUnit'];
}else{
return 0;
}
}
public static function setIndexCalendar($idEve,
$fkUnit,$fkUnitRightEvent,
$fkEventSector1,$fkEventSector2)
{
\Database::getInstance()->prepare("DELETE FROM tl_zEventIndex WHERE pid=?")->execute($idEve);
if($fkUnit)
{
\Database::getInstance()->prepare("INSERT INTO tl_zEventIndex SET pid=?,fkUnit=?,tstamp=?")
->execute($idEve,$fkUnit,time());
}
foreach(deserialize($fkUnitRightEvent) as $arrRight){
if($arrRight['f1'])\Database::getInstance()->prepare("INSERT INTO tl_zEventIndex SET pid=?,fkUnit=?,tstamp=?")->execute($idEve,$arrRight['f1'],time());
}
if($fkEventSector1){
foreach(deserialize($fkEventSector1) as $sector){
if($sector<>'')\Database::getInstance()->prepare("INSERT INTO tl_zEventIndex SET pid=?,fkEventSector1=?,tstamp=?")
->execute($idEve,$sector,time());
}
}
if($fkEventSector2){
foreach(deserialize($fkEventSector2) as $sector){
if($sector<>'')\Database::getInstance()->prepare("INSERT INTO tl_zEventIndex SET pid=?,fkEventSector2=?,tstamp=?")
->execute($idEve,$sector,time());
}
}
}
public static function replaceInsertTags($insertTag,$useCache=true)
{
$objInsertTags = new ReplaceInsertTagsListener();
return $objInsertTags->hookReplaceInsertTags($insertTag,$useCache,'',array(),array(),array(),0,0);
//$insertTag,$useCache=true,$cachedValue = '',$flags=array(),$tags=array(),$cache=array(),$_rit=0,$_cnt=0
}
public static function getEvent($idSelect,$getObject=false)
{
return OperationTool::getGenericEntry($idSelect,'tl_calendar_events',$getObject);
}
public static function getUnitName($idUnit,$getWithLink=false)
{
$objUnit = \Database::getInstance()->prepare("SELECT uName,uUrl FROM tl_zUnit WHERE id = ?")->limit(1)->execute($idUnit);
if($getWithLink)return '<a href="'.$objUnit->uUrl.'" target="_NEW">'.$objUnit->uName.'</a>';
return $objUnit->uName;
}
public static function getBookings($idEvent=0,$getData=true)
{
$arrRes = array();
if($idEvent=='')$idEvent=0;
$objBooking = \Database::getInstance()->prepare("SELECT *
FROM tl_zEventBooking
WHERE pid=?
ORDER BY insertTstamp")->execute($idEvent);
while($objBooking->next())
{
if($getData){
$arrRes[] = $objBooking->row();
}else{
$arrRes[$objBooking->id] = $objBooking->lastname.' '.$objBooking->firstname;
}
}
return $arrRes;
}
public static function getBookingAccessTime()
{
if(BackendUser::getInstance()->isAdmin){
return \Config::get('eventBookingAccessTimeAdmin');
}else{
return \Config::get('eventBookingAccessTime');
}
}
public static function checkEventContact($idEvent)
{
$objResult = \Database::getInstance()->prepare("SELECT cmContact FROM tl_calendar_events WHERE id=? ")->limit(1)->execute($idEvent);
$contact = deserialize($objResult->cmContact);
if($contact[0]['cmName'])return true;
return false;
}
public static function importBookingViaDC($dc)
{
return static::importBooking($dc->id);
}
public static function importBooking($idBooking)
{
\System::loadLanguageFile('tl_zEventBooking');
if (\Input::post('FORM_SUBMIT') == 'importBooking')
{
if($_FILES['source']['tmp_name']<>''
&& ($_FILES['source']['type']=='application/vnd.ms-excel' || $_FILES['source']['type']=='text/csv')
){
// Create temporary file
$tmpName = 'system/tmp/importBooking_' . date('Y-m-d_Hi') . '.csv';
$objFiles = \Files::getInstance();
$objFiles->move_uploaded_file($_FILES['source']['tmp_name'],$tmpName);
//$this->objFile = new \File($tmpName);
static::doImportBooking($idBooking,$tmpName);
}else{
\Contao\Message::addError('Fehler - keine Datei oder kein CSV Format');
}
}
//Formular
$objTemplate = new \BackendTemplate('be_modOperationtImportBooking');
$objTemplate->buttonBack = \Contao\Environment::get('base').'contao?do=calendar&table=tl_calendar_events&id=2';
$objTemplate->title = $GLOBALS['TL_LANG']['tl_zEventBooking']['importBookingValue'][0];
$objTemplate->action = \Environment::get('request');
return $objTemplate->parse();
}
public static function doImportBooking($idBooking,$fileName)
{
$objFile = new \File($fileName);
if($objFile->path=='')return;
$neededFields = array('gender','lastname','firstname','street','postal','city','reachedBy','mobile','email','comment','dateOfBirth','homeTown','ahvNr');
$fieldDispatch = array();
$row = 1;
if(($handle = $objFile->handle) !== FALSE) {
$dataFault = false;
$importArray = array();
while(($data = fgetcsv($handle, 10000, ";")) !== FALSE) {
//dump($data);
if($row == 1){
for($i=0;$i<count($data);$i++)
{
$data[$i] = $data[$i];
}
$missingField = false;
for($i=0;$i<count($neededFields);$i++) {
if(!in_array($neededFields[$i], $data)){
$missingField = true;
$missingFieldArr[]= $neededFields[$i];
}
for($p=0;$p<count($data);$p++) {
if($neededFields[$i] == $data[$p]) $fieldDispatch[$neededFields[$i]] = $p;
}
}
if($missingField == true)
{
\Contao\Message::addError("Es sind nicht alle Felder vorhanden. ".implode(",",$missingFieldArr));
return;
}
}
else{
$executionsAry = array();
for($i=0;$i<count($neededFields);$i++) {
if($neededFields[$i] == "gender"){
$executionsAry["gender"] = utf8_encode($data[$fieldDispatch[$neededFields[$i]]]);
if($executionsAry["gender"]=='male'||$executionsAry["gender"]=='female'||$executionsAry["gender"]=='other'){
}else{
\Contao\Message::addError('Geschlecht in Zeile '.$row.' ist falsch');
$dataFault = true;
break;
}
}elseif($neededFields[$i] == "reachedBy"){
$executionsAry["reachedBy"] = utf8_encode($data[$fieldDispatch[$neededFields[$i]]]);
if($executionsAry["reachedBy"]=='email'||$executionsAry["reachedBy"]=='mobile'){
}else{
\Contao\Message::addError('Erreichbar per in Zeile '.$row.' ist falsch');
$dataFault = true;
break;
}
}/*elseif($neededFields[$i] == "persnr"){
$executionsAry["zPersonalNummer"] = utf8_encode($data[$fieldDispatch[$neededFields[$i]]]);
$executionsAry["username"] = utf8_encode($data[$fieldDispatch[$neededFields[$i]]]);
$executionsAry["password"] = 'K;Wgk2FArCp!';
}*/elseif($neededFields[$i] == "dateOfBirth" && $data[$fieldDispatch[$neededFields[$i]]]<>''){
$arrTmp = explode('.',$data[$fieldDispatch[$neededFields[$i]]]);
$executionsAry["dateOfBirth"] = mktime(0,0,0,$arrTmp[1],$arrTmp[0],$arrTmp[2]);
}else
{
$executionsAry[$neededFields[$i]] = utf8_encode($data[$fieldDispatch[$neededFields[$i]]]);
}
}
$executionsAry["pid"] = $idBooking;
$executionsAry["insertTstamp"] = time();
$executionsAry["tstamp"] = time();
$importArray[] = $executionsAry;
}
$row++;
}
fclose($handle);
//Wenn Fehler kein Import
if($dataFault){
\Contao\Message::addError('Daten Fehler - kein Import');
}else{
$insertCount = 0;
foreach($importArray as $arrImport){
$executionsAryKeys = array_keys($arrImport);
$sets = implode(" = ?, ", $executionsAryKeys)." = ?";
//dump($sets);
//dump($arrImport);
//Import
\Database::getInstance()->prepare("INSERT INTO tl_zEventBooking SET ".$sets)->execute($arrImport);
$insertCount++;
}
\Contao\Message::addInfo($insertCount . " Einträge importiert");
}
}
}
}