= 0");
while ($row = $stH->fetch(PDO::FETCH_ASSOC)) {
$catIDs[] = $row['ID'];
}
//Processing
$errors = 0;
$nofDates = count($sDate);
for ($i = 0; $i < $nofDates; $i++) {
$error = 0;
if (($IsDate = DDtoID($sDate[$i],$dFormat)) === false) { $error++; }
if (($IeDate = DDtoID($eDate[$i],$dFormat)) === false) { $error++; }
if (($IsTime = DTtoIT($sTime[$i],$tFormat)) === false) { $error++; }
if (($IeTime = DTtoIT($eTime[$i],$tFormat)) === false) { $error++; }
if (!$error) {
if ($eDate[$i]) {
if ($IsDate == $IeDate) { $eDate[$i] = ''; }
elseif ($IeDate < $IsDate) {
list($sDate[$i],$eDate[$i]) = array($eDate[$i],$sDate[$i]); //swap start and end date
} elseif ($IeDate > $IsDate and !$IsTime and !$IeTime) { //all day
$eDate[$i] = IDtoDD(date("Y-m-d",strtotime($IeDate.' 12:00:00') - 86400),$dFormat); //-1 day
}
}
if (!$sTime[$i] and $eTime[$i]) { $sTime[$i] = $eTime[$i]; }
if ($sTime[$i] == $eTime[$i]) { $eTime[$i] = ''; }
if ($eTime[$i]) {
if ($IeTime < $IsTime) {
list($sTime[$i],$eTime[$i]) = array($eTime[$i],$sTime[$i]); //swap start and end time
}
}
}
$errors += $error;
if (!$title[$i]) { $errors++; } //title empty
if (!in_array($catID[$i], $catIDs)) { $catID[$i] = 0; } //reset non-existing category IDs
}
return $errors;
}
/* main-functions */
function instructions() {
global $ax;
$stH = dbQuery("SELECT `ID`,`name` FROM `categories` WHERE `status` >= 0 ORDER BY `ID`");
echo "\n";
}
function uploadFile() {
global $ax, $delimiter, $birthdayID, $dFormat, $tFormat, $lcI;
echo "
\n";
}
function processUpload() {
global $ax, $tFormat;
$fileName = $_FILES['fileName']['tmp_name'];
if (!$fileName) { return $ax['iex_no_file_name']; } //csv file missing
$csvArray = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
unlink($fileName); //release file
$lineNr = 0;
//process events from CSV array
foreach ($csvArray as $csvEvent) {
$csvEvent = strip_tags(html_entity_decode($csvEvent),'
'); //remove unwanted html tags
$csvFields = preg_split("%(? $dum) { $csvFields[$key] = trim($csvFields[$key],' "\''); } //remove blanks & quotes
$lineNr++;
if ((!empty($csvFields[2]) and !ctype_digit($csvFields[2])) and $lineNr = 1) { continue; } //flush header line
if (empty($csvFields[0])) { continue; } //flush events without title
if (empty($csvFields[3]) or count($csvFields) < 4) { //fields empty or too few fields
return "{$ax['iex_csv_file_error_on_line']}: {$lineNr}";
}
array_push($csvFields,'','','',''); //pad to ensure 8 fields
list($title, $venue, $cat, $sDate, $eDate, $sTime, $eTime, $desc) = $csvFields;
$_POST['title'][] = $title;
$_POST['venue'][] = $venue;
$_POST['catID'][] = $cat;
$_POST['sDate'][] = $sDate;
$_POST['eDate'][] = $eDate;
$_POST['sTime'][] = preg_replace('~(\d\d?:\d\d):\d\d~','$1',$sTime); //remove seconds
$_POST['eTime'][] = preg_replace('~(\d\d?:\d\d):\d\d~','$1',$eTime); //remove seconds
$_POST['descr'][] = $desc;
$_POST['birthday'][] = 0;
$_POST['ignore'][] = 0;
}
return ''; //no error
}
function displayEvents($errors) {
global $ax, $delimiter, $birthdayID, $dFormat, $tFormat, $lcI;
echo "{$ax['iex_number_of_errors']}: {$errors} ({$ax['iex_bgnd_highlighted']})
{$ax['iex_verify_event_list']} \"{$ax['iex_add_events']}\"
{$ax['iex_select_ignore_birthday']}
\n";
//display event list
echo "\n";
}
function addEvents() {
global $usr, $ax, $birthdayID, $dFormat, $tFormat;
$msg = '';
$nofEvents = count($_POST['title']);
$added = $dropped = 0;
for ($i = 0; $i < $nofEvents; $i++) {
if (empty($_POST['ignore'][$i])) {
$title = strip_tags($_POST['title'][$i]);
$venue = strip_tags($_POST['venue'][$i]);
$descr = strip_tags($_POST['descr'][$i],'
'); //remove most html tags
$descr = preg_replace("~ +~"," ", $descr); //trim spaces
$descr = str_replace(array("\r\n","\n\r","\n","\r"),"
", $descr); //replace newline by
$descr = addUrlImgEmlTags($descr,'x--'); //add URL link tags
$sDate = DDtoID($_POST['sDate'][$i],$dFormat);
$eDate = ($_POST['eDate'][$i]) ? DDtoID($_POST['eDate'][$i],$dFormat) : "9999-00-00";
$sTime = DTtoIT($_POST['sTime'][$i],$tFormat);
$eTime = ($_POST['eTime'][$i]) ? DTtoIT($_POST['eTime'][$i],$tFormat) : "99:00:00";
if (empty($sTime)) { //all day
$sTime = '00:00';
$eTime = '23:59';
}
$catID = ($_POST['catID'][$i]) ? $_POST['catID'][$i] : 1; //no cat
$rType = $rInterval = $rPeriod = $rMonth = 0;
if (!empty($_POST['birthday'][$i]) or $catID == $birthdayID) { //birthday
$catID = $birthdayID;
$rType = 1;
$rInterval = 1;
$rPeriod = 4;
$eDate = "9999-00-00";
}
// test if event present in db
$q = "SELECT `title` FROM `events` WHERE `status` >= 0 AND `title` = ? AND `sDate` = ? AND `eDate` = ? AND `sTime` = ? AND `eTime` = ?";
$stH = stPrep($q);
stExec($stH,array($title,$sDate,$eDate,$sTime,$eTime));
$row = $stH->fetch(PDO::FETCH_ASSOC);
$stH = null;
//if not present add event to db
if (empty($row)) { //not present
$q = "INSERT INTO `events` (`title`,`venue`,`text1`,`catID`,`userID`,`sDate`,`eDate`,`sTime`,`eTime`,`rType`,`rInterval`,`rPeriod`,`rMonth`,`aDateTime`,`mDateTime`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
$stH = stPrep($q);
stExec($stH,array($title,$venue,$descr,$catID,$usr['ID'],$sDate,$eDate,$sTime,$eTime,$rType,$rInterval,$rPeriod,$rMonth,date("Y-m-d H:i"),date("Y-m-d H:i")));
$added++;
} else {
$dropped++;
}
}
}
if (!$msg) $msg = "{$added} {$ax['iex_events_added']}".($dropped > 0 ? " / {$dropped} {$ax['iex_events_dropped']}" : '');
return $msg;
}
//control logic
$msg = ''; $errors = 0; //init
if ($usr['privs'] == 9) { //admin
if (isset($_POST['uploadFile'])) {
$msg = processUpload();
}
if ((isset($_POST['uploadFile']) and !$msg) or isset($_POST['addEvents'])) {
$errors = processEvtFields($_POST['sDate'],$_POST['eDate'],$_POST['sTime'],$_POST['eTime'],$_POST['title'],$_POST['catID']);
}
if (isset($_POST['addEvents']) and !$errors) {
$msg = addEvents(); //add events to calendar
}
echo "{$msg}
\n";
echo "\n";
} else {
echo "{$ax['no_way']}
\n";
}
?>