development/coursemanagement-bundle/src/Utils/OperationCM.php line 59

Open in your IDE?
  1. <?php
  2. namespace CityMountain\CourseManagementBundle\Utils;
  3. use CityMountain\ToolBundle\Utils\OperationTool;
  4. use Contao\Versions;
  5. use Contao\Email;
  6. use Contao\BackendUser;
  7. use CityMountain\CourseManagementBundle\Utils\hookReplaceInsertTags;
  8. class OperationCM
  9. {    
  10.     public static function getDefaultUnitFromUserToEdit()
  11.     {
  12.         $objRights OperationTool::getUserRights(BackendUser::getInstance()->id,1,array("AND fkModuleAction='MOD09EDITUNIT'"));
  13.         //dump($objRights);
  14.         //if (array_key_exists('mod09',$objRights['module']) && array_key_exists('MOD09EDITUNIT',$objRights['module']['mod09']) && count($objRights['module']['mod09']['MOD09EDITUNIT'])>0)
  15.         if (count($objRights['entry'])>0)
  16.         {
  17.             return current($objRights['entry'])['fkUnit'];
  18.         }else{
  19.             return 0;
  20.         }
  21.     }    
  22.     
  23.     public static function setIndexCalendar($idEve,
  24.                                      $fkUnit,$fkUnitRightEvent,
  25.                                      $fkEventSector1,$fkEventSector2)
  26.     {
  27.         \Database::getInstance()->prepare("DELETE FROM tl_zEventIndex WHERE pid=?")->execute($idEve);
  28.         if($fkUnit)
  29.         {
  30.             \Database::getInstance()->prepare("INSERT INTO tl_zEventIndex SET pid=?,fkUnit=?,tstamp=?")
  31.                                ->execute($idEve,$fkUnit,time());
  32.         }
  33.         
  34.         foreach(deserialize($fkUnitRightEvent) as $arrRight){
  35.             if($arrRight['f1'])\Database::getInstance()->prepare("INSERT INTO tl_zEventIndex SET pid=?,fkUnit=?,tstamp=?")->execute($idEve,$arrRight['f1'],time());
  36.         }
  37.         
  38.         if($fkEventSector1){
  39.             foreach(deserialize($fkEventSector1) as $sector){
  40.                 if($sector<>'')\Database::getInstance()->prepare("INSERT INTO tl_zEventIndex SET pid=?,fkEventSector1=?,tstamp=?")
  41.                     ->execute($idEve,$sector,time());
  42.             }
  43.         }
  44.         
  45.         if($fkEventSector2){
  46.             foreach(deserialize($fkEventSector2) as $sector){
  47.                 if($sector<>'')\Database::getInstance()->prepare("INSERT INTO tl_zEventIndex SET pid=?,fkEventSector2=?,tstamp=?")
  48.                     ->execute($idEve,$sector,time());
  49.             }
  50.         }
  51.     }
  52.     
  53.     public static function replaceInsertTags($insertTag,$useCache=true)
  54.     {            
  55.         $objInsertTags = new ReplaceInsertTagsListener();
  56.         return $objInsertTags->hookReplaceInsertTags($insertTag,$useCache,'',array(),array(),array(),0,0);
  57.         //$insertTag,$useCache=true,$cachedValue = '',$flags=array(),$tags=array(),$cache=array(),$_rit=0,$_cnt=0
  58.     }
  59.     
  60.     public static function getEvent($idSelect,$getObject=false)
  61.     {
  62.         return OperationTool::getGenericEntry($idSelect,'tl_calendar_events',$getObject);
  63.     }
  64.     
  65.     public static function getUnitName($idUnit,$getWithLink=false)
  66.     {
  67.         $objUnit \Database::getInstance()->prepare("SELECT uName,uUrl FROM tl_zUnit WHERE id = ?")->limit(1)->execute($idUnit);
  68.         if($getWithLink)return '<a href="'.$objUnit->uUrl.'" target="_NEW">'.$objUnit->uName.'</a>';
  69.         return $objUnit->uName;
  70.     }
  71.     
  72.     public static function getBookings($idEvent=0,$getData=true)
  73.     {
  74.         $arrRes = array();
  75.         
  76.         if($idEvent=='')$idEvent=0;
  77.         
  78.         $objBooking \Database::getInstance()->prepare("SELECT * 
  79.                                                            FROM tl_zEventBooking 
  80.                                                           WHERE pid=?
  81.                                                        ORDER BY insertTstamp")->execute($idEvent);
  82.         while($objBooking->next())
  83.         {
  84.             if($getData){
  85.                 $arrRes[] = $objBooking->row();
  86.             }else{
  87.                 $arrRes[$objBooking->id] = $objBooking->lastname.' '.$objBooking->firstname;
  88.             }
  89.             
  90.         }
  91.         return $arrRes;
  92.     }
  93.     
  94.     public static function getBookingAccessTime()
  95.     {
  96.         if(BackendUser::getInstance()->isAdmin){
  97.             return \Config::get('eventBookingAccessTimeAdmin');
  98.         }else{
  99.             return \Config::get('eventBookingAccessTime');
  100.         }
  101.     }
  102.     
  103.     public static function checkEventContact($idEvent)
  104.     {
  105.         $objResult \Database::getInstance()->prepare("SELECT cmContact FROM tl_calendar_events WHERE id=? ")->limit(1)->execute($idEvent);
  106.         $contact deserialize($objResult->cmContact);
  107.         if($contact[0]['cmName'])return true;
  108.         return false;
  109.     }
  110.     
  111.     public static function importBookingViaDC($dc)
  112.     {
  113.         return static::importBooking($dc->id);
  114.     }
  115.     
  116.     public static function importBooking($idBooking)
  117.     {    
  118.         \System::loadLanguageFile('tl_zEventBooking');
  119.         
  120.         if (\Input::post('FORM_SUBMIT') == 'importBooking')
  121.         {
  122.             if($_FILES['source']['tmp_name']<>'' 
  123.                     && ($_FILES['source']['type']=='application/vnd.ms-excel' || $_FILES['source']['type']=='text/csv')
  124.                        ){
  125.                 // Create temporary file
  126.                 $tmpName 'system/tmp/importBooking_' date('Y-m-d_Hi') . '.csv';
  127.                 $objFiles \Files::getInstance();
  128.                 $objFiles->move_uploaded_file($_FILES['source']['tmp_name'],$tmpName);
  129.                 //$this->objFile = new \File($tmpName);
  130.                 
  131.                 static::doImportBooking($idBooking,$tmpName);
  132.                 
  133.             }else{
  134.                 \Contao\Message::addError('Fehler - keine Datei oder kein CSV Format');
  135.             }            
  136.         }    
  137.         
  138.         
  139.         //Formular
  140.         $objTemplate = new \BackendTemplate('be_modOperationtImportBooking');        
  141.         $objTemplate->buttonBack \Contao\Environment::get('base').'contao?do=calendar&table=tl_calendar_events&id=2';
  142.         $objTemplate->title $GLOBALS['TL_LANG']['tl_zEventBooking']['importBookingValue'][0];
  143.         $objTemplate->action \Environment::get('request');
  144.         
  145.         return $objTemplate->parse();
  146.     }
  147.     
  148.     public static function doImportBooking($idBooking,$fileName)
  149.     {
  150.         $objFile = new \File($fileName);
  151.         if($objFile->path=='')return;
  152.         
  153.         
  154.         $neededFields = array('gender','lastname','firstname','street','postal','city','reachedBy','mobile','email','comment','dateOfBirth','homeTown','ahvNr');
  155.         
  156.         $fieldDispatch = array();
  157.         $row 1;
  158.         if(($handle $objFile->handle) !== FALSE) {
  159.             
  160.             $dataFault false;
  161.             $importArray = array();
  162.             while(($data fgetcsv($handle10000";")) !== FALSE) {
  163.                 //dump($data);
  164.                 if($row == 1){
  165.                     for($i=0;$i<count($data);$i++) 
  166.                     {
  167.                         $data[$i] = $data[$i];
  168.                     }
  169.                     $missingField false;
  170.                     for($i=0;$i<count($neededFields);$i++) {
  171.                         if(!in_array($neededFields[$i], $data)){
  172.                             $missingField true;
  173.                             $missingFieldArr[]=    $neededFields[$i];
  174.                         }
  175.                         for($p=0;$p<count($data);$p++) {
  176.                             if($neededFields[$i] == $data[$p]) $fieldDispatch[$neededFields[$i]] = $p;
  177.                         }
  178.                     }
  179.                     
  180.                     if($missingField == true)
  181.                     {
  182.                         \Contao\Message::addError("Es sind nicht alle Felder vorhanden. ".implode(",",$missingFieldArr));
  183.                         return;
  184.                     }
  185.                 }
  186.                 else{
  187.                     $executionsAry = array();
  188.                     for($i=0;$i<count($neededFields);$i++) {
  189.                         
  190.                         if($neededFields[$i] == "gender"){
  191.                             $executionsAry["gender"] = utf8_encode($data[$fieldDispatch[$neededFields[$i]]]);
  192.                             if($executionsAry["gender"]=='male'||$executionsAry["gender"]=='female'||$executionsAry["gender"]=='other'){
  193.                                 
  194.                             }else{
  195.                                 \Contao\Message::addError('Geschlecht in Zeile '.$row.' ist falsch');
  196.                                 $dataFault true;
  197.                                 break;
  198.                             }
  199.                         }elseif($neededFields[$i] == "reachedBy"){
  200.                             $executionsAry["reachedBy"] = utf8_encode($data[$fieldDispatch[$neededFields[$i]]]);
  201.                             if($executionsAry["reachedBy"]=='email'||$executionsAry["reachedBy"]=='mobile'){
  202.                                 
  203.                             }else{
  204.                                 \Contao\Message::addError('Erreichbar per in Zeile '.$row.' ist falsch');
  205.                                 $dataFault true;
  206.                                 break;
  207.                             }
  208.                         }/*elseif($neededFields[$i] == "persnr"){
  209.                             $executionsAry["zPersonalNummer"] = utf8_encode($data[$fieldDispatch[$neededFields[$i]]]);
  210.                             $executionsAry["username"] = utf8_encode($data[$fieldDispatch[$neededFields[$i]]]);
  211.                             $executionsAry["password"] = 'K;Wgk2FArCp!';
  212.                         }*/elseif($neededFields[$i] == "dateOfBirth" && $data[$fieldDispatch[$neededFields[$i]]]<>''){
  213.                             $arrTmp explode('.',$data[$fieldDispatch[$neededFields[$i]]]);                            
  214.                             $executionsAry["dateOfBirth"] = mktime(0,0,0,$arrTmp[1],$arrTmp[0],$arrTmp[2]);
  215.                         }else
  216.                         {
  217.                             $executionsAry[$neededFields[$i]] = utf8_encode($data[$fieldDispatch[$neededFields[$i]]]);
  218.                         }
  219.                     }
  220.                     
  221.                     
  222.                     $executionsAry["pid"] = $idBooking;
  223.                     $executionsAry["insertTstamp"] = time();
  224.                     $executionsAry["tstamp"] = time();
  225.                     
  226.                     $importArray[] = $executionsAry;
  227.                 }
  228.                 $row++;
  229.             }
  230.             fclose($handle);
  231.             
  232.             //Wenn Fehler kein Import
  233.             if($dataFault){
  234.                 \Contao\Message::addError('Daten Fehler - kein Import');
  235.             }else{
  236.                 
  237.                 $insertCount 0;
  238.                 foreach($importArray as $arrImport){
  239.                     $executionsAryKeys     array_keys($arrImport);
  240.                     $sets implode(" = ?, "$executionsAryKeys)." = ?";
  241.                     //dump($sets);
  242.                     //dump($arrImport);
  243.                     //Import
  244.                     \Database::getInstance()->prepare("INSERT INTO tl_zEventBooking SET ".$sets)->execute($arrImport);                    
  245.                     $insertCount++;
  246.                 }
  247.                 
  248.                 \Contao\Message::addInfo($insertCount " Einträge importiert");    
  249.             } 
  250.             
  251.                     
  252.         }
  253.         
  254.     }
  255.     
  256. }