/** * Saves a full marker * @param OMarker $marker * @return OMarker The marker * @throws \Exception */ public function setFullMarker($marker) { $page = new Page(); // Store marker specific fields $msettings = new OMarker(); $dbls = array('lat', 'lng'); $ints = array('layer', 'uselayercolor', 'enabled', 'pageId', 'markerId', 'color', 'iconsize'); $strings = array('icon', 'label', 'street', 'number', 'zip', 'city'); if (!$marker->markerId) { $nm = $this->setMarker($marker->lat, $marker->lng, $marker->layer); $marker->markerId = $nm->markerId; } if (method_exists($this->_hook, 'preSetMarker')) { $marker = $this->_hook->preSetMarker($marker); } $p = $page->setPage($marker, false, false); if ($marker->pageId == 0) { $marker->pageId = $p->pageId; } BrightUtils::forceDouble($marker, $dbls); BrightUtils::forceInt($marker, $ints); BrightUtils::escape($marker, $strings); if ($marker->uselayercolor === null || $marker->uselayercolor === false || $marker->uselayercolor === '') { $marker->uselayercolor = 0; } if ($marker->enabled === null || $marker->enabled === false || $marker->enabled === '') { $marker->enabled = 0; } $ss = Connection::getInstance()->escape_string(BrightUtils::createSearchString($marker)); $sql = "UPDATE `gm_markers` \n\t\t\t\tSET `lat`= {$marker->lat}, \n\t\t\t\t`lng`= {$marker->lng}, \n\t\t\t\t`layer`= {$marker->layer}, \n\t\t\t\t`uselayercolor`= {$marker->uselayercolor}, \n\t\t\t\t`enabled`= {$marker->enabled}, \n\t\t\t\t`pageId`= {$marker->pageId}, \n\t\t\t\t`color`= {$marker->color}, \n\t\t\t\t`iconsize`= {$marker->iconsize}, \n\t\t\t\t`icon`='{$marker->icon}', \n\t\t\t\t`label`='{$marker->label}', \n\t\t\t\t`search`='{$ss}',\n\t\t\t\t`street`='{$marker->street}', \n\t\t\t\t`number`='{$marker->number}', \n\t\t\t\t`zip`='{$marker->zip}', \n\t\t\t\t`city`='{$marker->city}' \n\t\t\t\tWHERE `markerId`={$marker->markerId}"; $this->_conn->updateRow($sql); if (method_exists($this->_hook, 'postSetMarker')) { $this->_hook->postSetMarker($marker); } $cache = new Cache(); $cache->deleteCacheByPrefix('marker'); return $this->getMarker($marker->markerId, true, false); }
/** * Creates or updates a calendar event * @param OCalendarEvent $event event The event to create or update * @param boolean $executeHook * @param boolean $updateContent When false, oContent is left untouched * @throws \Exception * @return bool */ public function setEvent(OCalendarEvent $event, $executeHook = true, $updateContent = true) { $ch = null; if (!isset($event->dates) || count($event->dates) == 0) { throw $this->throwException(EventException::NOT_ENOUGH_DATES); } $c = new Cache(); $c->deleteCacheByLabel("calendaritem_{$event->calendarId}"); $c->deleteCacheByPrefix("eventsByIdRange_"); $c->deleteCacheByPrefix('calendar_getEventsByRange'); // Execute hook if present if (class_exists('CalendarHook') && $executeHook) { $ch = new \CalendarHook(); if (method_exists($ch, 'preSetEvent')) { $event = $ch->preSetEvent($event); } } // Set modification values $aid = isset($_SESSION['administratorId']) ? $_SESSION['administratorId'] : 0; $event->createdby = $aid; $event->modifiedby = $aid; $event->label = $this->generateLabel($event->label, $event->calendarId); BrightUtils::forceInt($event, array('calendarId', 'itemType', 'createdby', 'locationId', 'modifiedby')); BrightUtils::escape($event, array('recur', 'label')); $event->enabled = $event->enabled === 1 || $event->enabled === true || $event->enabled === 'true' ? 1 : 0; $until = (double) $event->until; $event->until = date(BrightUtils::$SQLDateTime, $until); $sql = "INSERT INTO calendarnew (`calendarId`, `locationId`, `itemType`, `label`, `recur`, `until`, `enabled`, `deleted`, `creationdate`, `modificationdate`, `createdby`, `modifiedby`) VALUES (\n\t\t\t\t{$event->calendarId},\n\t\t\t\t{$event->locationId},\n\t\t\t\t{$event->itemType},\n\t\t\t\t'{$event->label}',\n\t\t\t\t'{$event->recur}',\n\t\t\t\t'{$event->until}',\n\t\t\t\t{$event->enabled},\n\t\t\t\tNULL, NOW(), NOW(),\n\t\t\t\t{$event->createdby},\n\t\t\t\t{$event->modifiedby})\n\t\t\t\tON DUPLICATE KEY UPDATE\n\t\t\t\titemType = VALUES(`itemType`),\n\t\t\t\tlocationId = VALUES(`locationId`),\n\t\t\t\tlabel = VALUES(`label`),\n\t\t\t\trecur = VALUES(`recur`),\n\t\t\t\tenabled = VALUES(`enabled`),\n\t\t\t\tuntil = VALUES(`until`),\n\t\t\t\tmodificationdate = NOW(),\n\t\t\t\tmodifiedby = VALUES(`modifiedby`),\n\t\t\t\t`calendarId`=LAST_INSERT_ID(`calendarId`)"; $event->calendarId = $this->_conn->insertRow($sql); $sql = 'UPDATE `calendardates` SET `deleted`=1 WHERE `calendarId`=' . $event->calendarId; $this->_conn->updateRow($sql); $sql = 'INSERT INTO `calendardates` (`calendarId`,`starttime`, `endtime`,`allday`,`deleted`,`noend`) VALUES '; $sqla = array(); // Store also for calendarevents $evdates = array(); foreach ($event->dates as &$date) { $date = (object) $date; if ($date->endtime < $date->starttime) { // Quick fix, just add 1 day to the endtime // We could (or should) throw an exception here, since it's not a valid range... $date->endtime = strtotime('+1 day', $date->endtime); } $starttime = date(BrightUtils::$SQLDateTime, $date->starttime); $endtime = date(BrightUtils::$SQLDateTime, $date->endtime); $date->allday = $date->allday == true ? 1 : 0; $date->noend = $date->noend == true ? 1 : 0; $sqla[] = "({$event->calendarId}, '{$starttime}','{$endtime}', {$date->allday},0, {$date->noend})"; $evdates[] = "({$event->calendarId}, '{$starttime}','{$endtime}', {$date->allday}, 0, {$date->noend})"; } $sql .= implode(",\r\n", $sqla); $sql .= ' ON DUPLICATE KEY UPDATE `allday`=VALUES(`allday`), `noend`=VALUES(`noend`), `deleted`=0'; $this->_conn->insertRow($sql); $sql = "DELETE FROM calendardates WHERE deleted=1"; $this->_conn->insertRow($sql); // Delete stored dates // $sql = 'DELETE FROM calendarevents WHERE eventId=' . (int) $event -> calendarId; // $this -> _conn -> deleteRow($sql); $sql = 'UPDATE calendareventsnew SET `deleted`=1 WHERE `calendarId`=' . (int) $event->calendarId; $this->_conn->updateRow($sql); $sql = 'INSERT INTO `calendareventsnew` (`calendarId`, `starttime`, `endtime`, `allday`, `deleted`,`noend`) VALUES '; $sqla = array(); $earr = array(); if ($event->recur && $event->recur != '') { $recur = $event->recur; // Recurring event, process it and add if needed $recarr = explode(';', $recur); $freq = ''; $interval = 0; // If recur has trailing ;, pop last item if ($recarr[count($recarr) - 1] == '') { array_pop($recarr); } foreach ($recarr as $recitem) { $recitemarr = explode('=', $recitem); $key = $recitemarr[0]; $val = $recitemarr[1]; $dayarr = null; $monthrepeat = 'dom'; switch ($key) { case 'FREQ': // Frequency, valid values are: DAILY, WEEKLY, MONTHLY, YEARLY $freq = $val; break; case 'INTERVAL': $interval = (int) $val; break; case 'BYDAY': // Difference between monthly and weekly switch ($freq) { case 'WEEKLY': $days = explode(',', $val); $dayarr = array(); // Find out which days are checked foreach ($days as $day) { switch ($day) { case 'SU': $dayarr[0] = 1; break; case 'MO': $dayarr[1] = 1; break; case 'TU': $dayarr[2] = 1; break; case 'WE': $dayarr[3] = 1; break; case 'TH': $dayarr[4] = 1; break; case 'FR': $dayarr[5] = 1; break; case 'SA': $dayarr[6] = 1; break; } } break; case 'MONTHLY': $monthrepeat = 'dow'; break; } break; case 'BYMONTHDAY': // Difference between monthly and yearly /** * @todo implement Is more implementation really needed, or is this switch just useless an could it be * replaced with an if statement. */ switch ($freq) { case 'MONTHLY': $monthrepeat = 'dom'; break; } break; } } // Add dates, if event recurs, calculate all dates $sqla[] = $evdates[0]; $evstart = $event->dates[0]->starttime; $evend = $event->dates[0]->endtime; $startenddiff = $evend - $evstart; switch ($freq) { case 'DAILY': // Easy! while ($evstart < $until) { $evstart += 86400 * $interval; $evend += 86400 * $interval; $ev = new \stdClass(); $ev->starttime = $evstart; $ev->endtime = $evend; $ev->calendarId = $event->calendarId; $earr[] = $ev; } break; case 'WEEKLY': // Get timestamp of the first day of the week $fdow = date('w', $evstart); $edow = date('w', $evend); $startweek = mktime(date('H', $evstart), date('i', $evstart), date('s', $evstart), date('n', $evstart), date('j', $evstart) - $fdow, date('Y', $evstart)); $ddow = $edow - $fdow; if ($ddow < 0) { $ddow += 7; } $first = true; while ($startweek < $until) { // On the first week, skip the sunday, because, // if the sunday is checked, it is already added before $dow = $first ? $fdow + 1 : 0; $first = false; while ($dow < 7) { if (array_key_exists($dow, $dayarr)) { $ev = new \stdClass(); $ev->starttime = mktime(date('H', $evstart), date('i', $evstart), date('s', $evstart), date('n', $startweek), date('j', $startweek) + $dow, date('Y', $startweek)); // $edow = $dow + $ddow; // if($edow < 0) // $edow +=7; $ev->endtime = mktime(date('H', $evend), date('i', $evend), date('s', $evend), date('n', $startweek), date('j', $startweek) + $dow + $ddow, date('Y', $startweek)); $ev->calendarId = $event->calendarId; if ($ev->starttime < $until) { $earr[] = $ev; } } $dow++; } $startweek += $interval * 604800; } // Now add 1 * $interval weeks and start with the first available day of dayarr break; case 'MONTHLY': if ($monthrepeat == 'dom') { // Day Of Month while ($evstart < $until) { $evstart = mktime(date('H', $evstart), date('i', $evstart), date('s', $evstart), date('n', $evstart) + $interval, date('j', $evstart), date('Y', $evstart)); $evend = mktime(date('H', $evend), date('i', $evend), date('s', $evend), date('n', $evend) + $interval, date('j', $evend), date('Y', $evend)); $ev = new \stdClass(); $ev->starttime = $evstart; $ev->endtime = $evend; $ev->calendarId = $event->calendarId; $earr[] = $ev; } } else { // Day of Week // Get the day of the week (sun - sat) $dow = date('w', $evstart); // Calculate how often that day has occured in the month (eg. the 2nd monday) $nd = ceil(date('j', $evstart) / 7); $mon = date('n', $evstart); while ($evstart < $until) { // Add the interval of months $a = $evstart = strtotime("+{$interval} month", $evstart); // Check the 'new' day of the week $newdow = date('w', $evstart); $delta = $dow - $newdow; if ($delta < 0) { $delta += 7; } // And correct it to the old dow $evstart += $delta * 86400; // We've accidently moved to the next month, correct date // by removing 1 week; if (date('m', $evstart) > date('m', $a)) { $evstart -= 7 * 86400; } // Check how often that day has occured $newnd = ceil(date('j', $evstart) / 7); // And correct it to the original occurence while ($newnd < $nd) { $evstart += 604800; $newnd++; } while ($newnd > $nd) { $evstart -= 604800; $newnd--; } $evend = $evstart + $startenddiff; $ev = new \stdClass(); $ev->starttime = $evstart; $ev->endtime = $evend; $ev->calendarId = $event->calendarId; $earr[] = $ev; } } break; case 'YEARLY': while ($evstart < $until) { $evstart = mktime(date('H', $evstart), date('i', $evstart), date('s', $evstart), date('n', $evstart), date('j', $evstart), date('Y', $evstart) + $interval); $evend = mktime(date('H', $evend), date('i', $evend), date('s', $evend), date('n', $evend), date('j', $evend), date('Y', $evend) + $interval); $ev = new \stdClass(); $ev->starttime = $evstart; $ev->endtime = $evend; $ev->calendarId = $event->calendarId; $earr[] = $ev; } break; } $ad = $event->dates[0]->allday; $ne = $event->dates[0]->noend; foreach ($earr as $ev) { if ($ev->endtime < $ev->starttime) { // Quick fix, just add 1 day to the endtime $ev->endtime = strtotime('+1 day', $ev->endtime); } $sqla[] = "({$event->calendarId},\n\t\t\t\t\t\t\t'" . date(BrightUtils::$SQLDateTime, $ev->starttime) . "',\n\t\t\t\t\t\t\t'" . date(BrightUtils::$SQLDateTime, $ev->endtime) . "',\n\t\t\t\t\t\t\t{$ad},\n\t\t\t\t\t\t\t0,\n\t\t\t\t\t\t\t{$ne})"; } } else { $sqla = $evdates; } // Add dates to db $sql .= join(',', $sqla); $sql .= ' ON DUPLICATE KEY UPDATE starttime=VALUES(starttime), endtime=VALUES(endtime), `deleted`=0, allday=VALUES(`allday`), noend=VALUES(`noend`)'; $this->_conn->insertRow($sql); $sql = 'DELETE FROM calendareventsnew WHERE `deleted`=1 AND `calendarId`=' . (int) $event->calendarId; $this->_conn->deleteRow($sql); if ($updateContent) { $this->setContent($event, 'calendarcontent'); $search = BrightUtils::createSearchString($event); if ((int) $event->locationId > 0) { $search .= $this->conn->getField("SELECT search FROM gm_markers WHERE pageId={$event->locationId}"); } $search = Connection::getInstance()->escape_string($search); $sql = "INSERT INTO calendarindex (calendarId, search) VALUES ({$event->calendarId}, '{$search}') ON DUPLICATE KEY UPDATE search='{$search}' "; $this->_conn->insertRow($sql); } if (isset($ch) && method_exists($ch, 'postSetEvent')) { $ch->postSetEvent($event); } return true; }