Skip to content

Commit

Permalink
v2.0.0-beta.5
Browse files Browse the repository at this point in the history
  • Loading branch information
kjmartens committed Mar 19, 2018
1 parent 6fce67b commit 3187844
Show file tree
Hide file tree
Showing 18 changed files with 263 additions and 52 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Solspace Freeform Changelog

## 2.0.0-beta.5 - 2018-03-19
### Fixed
- Added Live Preview functionality back.
- Added Calendar 1.x to 2.x (Craft 2.x to 3.x) migration path.
- Fixed a bug where the Calendar Event fieldtype was not available.
- Fixed a bug where the Agenda widget would visually allow you to drag and drop events (locked now).
- Fixed a bug where the Agenda widget would not correctly display all day and multi-day events.
- Fixed some deprecation errors with dashboard widgets.
- Fixed a bug where Quick Creating events with title format option was not working correctly.
- Fixed a bug where Events function wasn't correctly including multi-day events.
- Fixed a bug where translations were not being loaded.

## 2.0.0-beta.4 - 2018-03-15
### Fixed
- Added back all Calendar widgets.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "solspace/craft3-calendar",
"description": "The most powerful event management plugin for Craft.",
"version": "2.0.0-beta.4",
"version": "2.0.0-beta.5",
"type": "craft-plugin",
"minimum-stability": "dev",
"authors": [
Expand Down
30 changes: 21 additions & 9 deletions src/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use craft\events\RegisterUrlRulesEvent;
use craft\events\RegisterUserPermissionsEvent;
use craft\services\Dashboard;
use craft\services\Fields;
use craft\services\UserPermissions;
use craft\web\twig\variables\CraftVariable;
use craft\web\UrlManager;
Expand All @@ -17,6 +18,7 @@
use Solspace\Calendar\Controllers\EventsController;
use Solspace\Calendar\Controllers\SettingsController;
use Solspace\Calendar\Controllers\ViewController;
use Solspace\Calendar\FieldTypes\EventFieldType;
use Solspace\Calendar\Models\CalendarModel;
use Solspace\Calendar\Models\CalendarSiteSettingsModel;
use Solspace\Calendar\Models\SettingsModel;
Expand Down Expand Up @@ -98,15 +100,17 @@ public function init()
{
parent::init();

$this->controllerMap = [
'api' => ApiController::class,
'codepack' => CodePackController::class,
'calendars' => CalendarsController::class,
'events-api' => EventsApiController::class,
'events' => EventsController::class,
'settings' => SettingsController::class,
'view' => ViewController::class,
];
if (!\Craft::$app->request->isConsoleRequest) {
$this->controllerMap = [
'api' => ApiController::class,
'codepack' => CodePackController::class,
'calendars' => CalendarsController::class,
'events-api' => EventsApiController::class,
'events' => EventsController::class,
'settings' => SettingsController::class,
'view' => ViewController::class,
];
}

$this->setComponents(
[
Expand Down Expand Up @@ -147,6 +151,14 @@ function (RegisterComponentTypesEvent $event) {
}
);

Event::on(
Fields::class,
Fields::EVENT_REGISTER_FIELD_TYPES,
function (RegisterComponentTypesEvent $event) {
$event->types[] = EventFieldType::class;
}
);

// craft()->on('i18n.onAddLocale', [craft()->calendar_events, 'addLocaleHandler']);
// craft()->on('i18n.onAddLocale', [craft()->calendar_calendars, 'addLocaleHandler']);

Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/EventsApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public function actionCreate(): Response
$event->endDate = $endDate;
$event->allDay = $isAllDay;

if (Calendar::getInstance()->events->saveEvent($event, false)) {
if (Calendar::getInstance()->events->saveEvent($event, false, true)) {
return $this->asJson(['event' => $event]);
}

Expand Down
95 changes: 82 additions & 13 deletions src/Controllers/EventsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Solspace\Calendar\Library\DateHelper;
use Solspace\Calendar\Library\Exceptions\EventException;
use Solspace\Calendar\Library\RecurrenceHelper;
use Solspace\Calendar\Models\ExceptionModel;
use Solspace\Calendar\Models\SelectDateModel;
use Solspace\Calendar\Resources\Bundles\EventEditBundle;
use yii\helpers\FormatConverter;
use yii\web\HttpException;
Expand Down Expand Up @@ -344,7 +346,7 @@ private function renderEditForm(Event $event, string $title): Response
$showPreviewButton = false;
if (!\Craft::$app->getRequest()->isMobileBrowser(true) && $this->getCalendarService()->isEventTemplateValid($calendar, $event->siteId)) {
$this->getView()->registerJs('Craft.LivePreview.init(' . Json::encode([
'fields' => '#title-field, #fields > div > div > .field, #fields > div > .field',
'fields' => '#title-field, #fields .calendar-event-wrapper > .field, #fields > .field > .field',
'extraFields' => '#settings',
'previewUrl' => $event->getUrl(),
'previewAction' => 'calendar/events/preview',
Expand Down Expand Up @@ -453,25 +455,92 @@ private function getEventModel(): Event
*/
private function populateEventModel(Event $event)
{
// Set the entry attributes, defaulting to the existing values for whatever is missing from the post data
$event->slug = \Craft::$app->getRequest()->getBodyParam('slug', $event->slug);
$event->enabled = (bool) \Craft::$app->getRequest()->getBodyParam('enabled', $event->enabled);
$event->enabledForSite = (bool) \Craft::$app->getRequest()->getBodyParam('enabledForSite', $event->enabledForSite);
$event->title = \Craft::$app->getRequest()->getBodyParam('title', $event->title);
$request = \Craft::$app->request;

// Prevent the last entry type's field layout from being used
$event->fieldLayoutId = null;
$eventId = $event->id;
$values = $request->getBodyParam(self::EVENT_FIELD_NAME);
if (null === $values) {
throw new HttpException('No event data posted');
}

$fieldsLocation = \Craft::$app->getRequest()->getParam('fieldsLocation', 'fields');
$event->slug = $request->getBodyParam('slug', $event->slug);
$event->enabled = (bool) $request->getBodyParam('enabled', $event->enabled);
$event->enabledForSite = (bool) $request->getBodyParam('enabledForSite', $event->enabledForSite);
$event->title = $request->getBodyParam('title', $event->title);

$event->fieldLayoutId = null;
$fieldsLocation = $request->getParam('fieldsLocation', 'fields');
$event->setFieldValuesFromRequest($fieldsLocation);

// Author
$authorId = \Craft::$app->getRequest()->getBodyParam('author', ($event->authorId ?: \Craft::$app->getUser()->getIdentity()->id));
if (is_array($authorId)) {
$authorId = $authorId[0] ?? null;
if (\is_array($authorId)) {
$authorId = $authorId[0] ?? null;
$event->authorId = $authorId;
}

$event->authorId = $authorId;
$event->enabled = (bool) $request->post('enabled', $event->enabled);

if (isset($values['calendarId'])) {
$event->calendarId = $values['calendarId'];
}

$isCalendarPublic = Calendar::getInstance()->calendars->isCalendarPublic($event->getCalendar());

$isNewAndPublic = !$event->id && !$isCalendarPublic;
if ($eventId || $isNewAndPublic) {
CalendarPermissionHelper::requireCalendarEditPermissions($event->getCalendar());
}

if (isset($values['startDate'])) {
$event->startDate = new Carbon($values['startDate']['date'] . ' ' . $values['startDate']['time'], DateHelper::UTC);
}

if (isset($values['endDate'])) {
$event->endDate = new Carbon($values['endDate']['date'] . ' ' . $values['endDate']['time'], DateHelper::UTC);
}

if (isset($values['allDay'])) {
$event->allDay = (bool) $values['allDay'];
}

if ($event->allDay && $event->startDate && $event->endDate) {
$event->startDate->setTime(0, 0, 0);
$event->endDate->setTime(23, 59, 59);
}

$startDate = $event->getStartDate();
$endDate = $event->getEndDate();

if ($startDate && $endDate && $startDate->eq($endDate)) {
$endDate = $endDate->addHour();
$event->endDate->setTime(
$endDate->hour,
$endDate->minute,
$endDate->second
);
}

$this->handleRepeatRules($event, $values);

if (isset($values['exceptions'])) {
foreach ($values['exceptions'] as $date) {
$exception = new ExceptionModel();
$exception->eventId = $event->id;
$exception->date = new Carbon($date, DateHelper::UTC);

$event->addException($exception);
}
}

if (isset($values['selectDates'])) {
foreach ($values['selectDates'] as $date) {
$selectDate = new SelectDateModel();
$selectDate->eventId = $event->id;
$selectDate->date = new Carbon($date, DateHelper::UTC);

$event->addSelectDate($selectDate);
}
}
}

/**
Expand Down
60 changes: 49 additions & 11 deletions src/Elements/Db/EventQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Solspace\Commons\Helpers\PermissionHelper;
use yii\db\Connection;

class EventQuery extends ElementQuery
class EventQuery extends ElementQuery implements \Countable
{
const FORMAT_MONTH = 'Ym';
const FORMAT_WEEK = 'YW';
Expand Down Expand Up @@ -279,7 +279,7 @@ public function count($q = '*', $db = null): int
$this->all($db);

if (null === $this->totalCount) {
$this->totalCount = \count($this->events);
$this->totalCount = \count($this->events ?? []);
}

return $this->totalCount;
Expand All @@ -295,7 +295,8 @@ public function all($db = null): array
$configHash = $this->getConfigStateHash();

// Nasty elements index hack
if (\Craft::$app->request->post('context') === 'index') {
$context = \Craft::$app->request->post('context');
if (\in_array($context, ['index', 'modal'], true)) {
$this->loadOccurrences = false;
}

Expand Down Expand Up @@ -430,12 +431,28 @@ public function getEventsByHour(Carbon $date): array
*/
protected function beforePrepare(): bool
{
$table = Event::TABLE_STD;
$calendarTable = CalendarRecord::TABLE;
$table = Event::TABLE_STD;
$calendarTable = CalendarRecord::TABLE;

// join in the products table
$this->joinElementTable($table);
$this->join = [['INNER JOIN', $calendarTable, "$calendarTable.id = $table.calendarId"]];
$hasCalendarsJoined = false;

if (!empty($this->join)) {
foreach ($this->join as $join) {
if ($join[1] === $calendarTable) {
$hasCalendarsJoined = true;
}
}
}

if (!$hasCalendarsJoined) {
if (null === $this->join) {
$this->join = [];
}

$this->join[] = ['INNER JOIN', $calendarTable, "$calendarTable.id = $table.calendarId"];
}

// select the price column
$this->query->select(
Expand Down Expand Up @@ -490,17 +507,34 @@ protected function beforePrepare(): bool

if ($this->rangeStart) {
$rangeStartString = $this->extractDateAsFormattedString($this->rangeStart);

$this->subQuery->andWhere(
"($table.rrule IS NULL AND $table.startDate >= :rangeStart) OR ($table.rrule IS NOT NULL)",
['rangeStart' => $rangeStartString]
"($table.rrule IS NULL AND $table.endDate >= :rangeStart)
OR ($table.rrule IS NOT NULL AND $table.until IS NOT NULL AND $table.until >= :rangeStart)
OR ($table.rrule IS NOT NULL AND $table.until IS NULL)
OR ($table.freq = :freq)",
[
'rangeStart' => $rangeStartString,
'freq' => RecurrenceHelper::SELECT_DATES,
]
);
}

if ($this->rangeEnd) {
$rangeEndString = $this->extractDateAsFormattedString($this->rangeEnd);
$rangeEnd = $this->rangeEnd->copy();

if ($rangeEnd->format('His') === '000000') {
$rangeEnd->setTime(23, 59, 59);
}

$rangeEndString = $this->extractDateAsFormattedString($rangeEnd);

$this->subQuery->andWhere(
"$table.endDate <= :rangeEnd",
['rangeEnd' => $rangeEndString]
"$table.startDate <= :rangeEnd OR $table.freq = :freq",
[
'rangeEnd' => $rangeEndString,
'freq' => RecurrenceHelper::SELECT_DATES,
]
);
}

Expand Down Expand Up @@ -859,6 +893,10 @@ private function orderEvents(array &$events)
$modifier = $this->getSortModifier();
$orderBy = $this->getOrderByField() ?? 'startDate';

if (false !== strpos($orderBy, '.')) {
$orderBy = 'startDate';
}

usort(
$events,
function (Event $eventA, Event $eventB) use ($modifier, $orderBy) {
Expand Down
29 changes: 29 additions & 0 deletions src/Elements/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,19 @@ public function getExceptions(): array
return $this->exceptions;
}

/**
* @param ExceptionModel $exceptionModel
*
* @return $this
*/
public function addException(ExceptionModel $exceptionModel): Event
{
$this->getExceptions();
$this->exceptions[] = $exceptionModel;

return $this;
}

/**
* @return array
*/
Expand Down Expand Up @@ -542,6 +555,22 @@ public function getSelectDatesAsString($format = 'Y-m-d'): array
return $formattedDatesList;
}

/**
* @param SelectDateModel $selectDateModel
*
* @return $this
*/
public function addSelectDate(SelectDateModel $selectDateModel): Event
{
$this->getSelectDates();
$this->selectDatesCache[] = $selectDateModel;
usort($this->selectDatesCache, function (SelectDateModel $a, SelectDateModel $b) {
return $a->date <=> $b->date;
});

return $this;
}

/**
* @return bool
*/
Expand Down
Loading

0 comments on commit 3187844

Please sign in to comment.