Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Menu for create calendar from wall stream #331

Merged
merged 13 commits into from
Nov 7, 2022
Merged
1 change: 1 addition & 0 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,5 @@ public static function onRestApiAddRules()

], 'calendar');
}

}
8 changes: 8 additions & 0 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,12 @@ public function getContainerPermissions($contentContainer = null)
}
return [];
}

/**
* @inheritdoc
*/
public function getContentClasses(?ContentContainerActiveRecord $contentContainer = null): array
{
return [CalendarEntry::class];
}
}
42 changes: 34 additions & 8 deletions controllers/EntryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

namespace humhub\modules\calendar\controllers;

use DateTime;
use humhub\modules\calendar\helpers\CalendarUtils;
use humhub\modules\calendar\models\CalendarEntryParticipant;
use humhub\modules\calendar\models\forms\CalendarEntryParticipationForm;
use humhub\modules\calendar\notifications\Invited;
use humhub\modules\calendar\widgets\ParticipantAddForm;
use humhub\modules\calendar\widgets\ParticipantItem;
use humhub\modules\calendar\helpers\Url;
use humhub\modules\calendar\models\forms\CalendarEntryForm;
use humhub\modules\stream\actions\Stream;
use humhub\modules\stream\actions\StreamEntryResponse;
use humhub\modules\user\models\User;
use humhub\modules\content\components\ContentContainerController;
use humhub\modules\calendar\permissions\CreateEntry;
use humhub\modules\calendar\models\CalendarEntry;
use humhub\widgets\ModalClose;
use Throwable;
Expand Down Expand Up @@ -175,27 +176,43 @@ public function actionRespond($id, $type)
return $this->asJson(['success' => true]);
}

/**
* Add a calendar Entry from wall stream
*
* @return string
* @throws Exception
* @throws HttpException
* @throws InvalidConfigException
* @throws Throwable
*/
public function actionAddFromWall()
{
$date = (new DateTime('now'))->setTime(date('H'), 0)->format(CalendarUtils::DATE_FORMAT_ATOM);
return $this->actionEdit(null, $date, $date, 1, 'month', true);
}

/**
*
* @param null $id calendar entry id
* @param null $start FullCalendar start datetime e.g.: 2020-01-01 00:00:00
* @param null $end FullCalendar end datetime e.g.: 2020-01-02 00:00:00
* @param null $cal whether or not the edit event came from the calendar view
* @param string|null $view FullCalendar view mode, 'month'
* @return string
* @param bool $wall True when a Calendary Entry is created/updated from wall stream
* @return string|Response
* @throws HttpException
* @throws Throwable
* @throws Exception
* @throws InvalidConfigException
*/
public function actionEdit($id = null, $start = null, $end = null, $cal = null, $view = null)
public function actionEdit($id = null, $start = null, $end = null, $cal = null, $view = null, $wall = null)
{
if (empty($id) && !$this->canCreateEntries()) {
throw new HttpException(403);
}

if (empty($id) && $this->canCreateEntries()) {
$calendarEntryForm = CalendarEntryForm::createEntry($this->contentContainer, $start, $end, $view);
$calendarEntryForm = CalendarEntryForm::createEntry($this->contentContainer, $start, $end, $view, $wall);
} else {
$calendarEntryForm = new CalendarEntryForm(['entry' => $this->getCalendarEntry($id)]);
if(!$calendarEntryForm->entry->content->canEdit()) {
Expand All @@ -208,7 +225,16 @@ public function actionEdit($id = null, $start = null, $end = null, $cal = null,
}

if ($calendarEntryForm->load(Yii::$app->request->post()) && $calendarEntryForm->save()) {
if(empty($cal)) {
if ($wall) {
$entry = StreamEntryResponse::getAsArray($calendarEntryForm->entry->content);
$entry['reloadWall'] = true;
$entry['content'] = $entry['output'];
$entry['output'] = $this->renderModalParticipation($calendarEntryForm->entry, null, true);

return $this->asJson($entry);
}

if (empty($cal)) {
return ModalClose::widget(['saved' => true]);
}

Expand All @@ -224,7 +250,7 @@ public function actionEdit($id = null, $start = null, $end = null, $cal = null,
return $this->renderAjax('edit', [
'calendarEntryForm' => $calendarEntryForm,
'contentContainer' => $this->contentContainer,
'editUrl' => Url::toEditEntry($calendarEntryForm->entry, $cal, $this->contentContainer)
'editUrl' => Url::toEditEntry($calendarEntryForm->entry, $cal, $this->contentContainer, $calendarEntryForm->wall)
]);
}

Expand Down Expand Up @@ -491,7 +517,7 @@ protected function getCalendarEntry($id): CalendarEntry
*/
private function canCreateEntries()
{
return $this->contentContainer->permissionManager->can(CreateEntry::class);
return (new CalendarEntry($this->contentContainer))->content->canEdit();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions controllers/GlobalController.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function actionSelect($start = null, $end = null)
}

foreach ($calendarMemberSpaceQuery->all() as $space) {
if ($space->permissionManager->can(CreateEntry::class)) {
if ((new CalendarEntry($space))->content->canEdit()) {
$contentContainerSelection[$space->contentcontainer_id] = $space->displayName;
}
}
Expand All @@ -131,7 +131,7 @@ public function actionSelectSubmit($start = null, $end = null)

$container = $contentContainer->getPolymorphicRelation();

if (!$container->permissionManager->can(CreateEntry::class)) {
if (!(new CalendarEntry($container))->content->canEdit()) {
throw new HttpException(403);
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/rest/CalendarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function actionCreate($containerId)
/** @var ContentContainerActiveRecord $container */
$container = $containerRecord->getPolymorphicRelation();

if (! $container->permissionManager->can(CreateEntry::class)) {
if (!(new CalendarEntry($container))->content->canEdit()) {
return $this->returnError(403, 'You are not allowed to create calendar entry!');
}

Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
--------------------
- Fix #327: Don't send notifications for canceled event
- Fix #334: Fix null in DateTime on PHP 8.1
- Enh #331: Menu for creating a Calendar Entry from wall stream


1.2.5 (July 15, 2022)
Expand Down
4 changes: 2 additions & 2 deletions helpers/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ public static function toGlobalCreate()
return static::to(['/calendar/global/select']);
}

public static function toEditEntry(CalendarEntry $entry, $cal = null, ContentContainerActiveRecord $container = null)
public static function toEditEntry(CalendarEntry $entry, $cal = null, ContentContainerActiveRecord $container = null, $wall = null)
{
if(!$container) {
$container = $entry->content->container;
}

return $container->createUrl('/calendar/entry/edit', ['id' => $entry->id, 'cal' => $cal]);
return $container->createUrl('/calendar/entry/edit', ['id' => $entry->id, 'cal' => $cal, 'wall' => $wall]);
}

public static function toEditEntryParticipation(CalendarEntry $entry, ContentContainerActiveRecord $container = null)
Expand Down
5 changes: 5 additions & 0 deletions models/CalendarEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ class CalendarEntry extends ContentActiveRecord implements Searchable, Recurrent
*/
public $is_public = Content::VISIBILITY_PUBLIC;

/**
* @inheritdoc
*/
protected $createPermission = CreateEntry::class;

/**
* @inheritdoc
*/
Expand Down
9 changes: 8 additions & 1 deletion models/forms/CalendarEntryForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,28 @@ class CalendarEntryForm extends Model
*/
public $recurrenceForm;

/**
* @var boolean defines if the Task is created from wall stream
*/
public $wall;

/**
* Will create a new CalendarEntryForm instance with new CalendarEntry model.
*
* @param $contentContainer
* @param string|null $start FullCalendar start datetime e.g.: 2020-01-01 00:00:00
* @param string|null $end FullCalendar end datetime e.g.: 2020-01-02 00:00:00
* @param string|null $view FullCalendar view mode, 'month'
* @param bool $wall True when a Calendary Entry is created/updated from wall stream
* @return CalendarEntryForm
* @throws Exception
*/
public static function createEntry($contentContainer, $start = null, $end = null, $view = null)
public static function createEntry($contentContainer, $start = null, $end = null, $view = null, $wall = null)
{
$instance = new static(['entry' => new CalendarEntry($contentContainer)]);
$instance->updateDateRangeFromCalendar($start, $end, null, false, $view);
$instance->setDefaults(); // Make sure default values are based on new start/end
$instance->wall = $wall;
return $instance;
}

Expand Down
4 changes: 2 additions & 2 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"name": "Calendar",
"description": "Calendar for spaces or user profiles.",
"keywords": ["calendar"],
"version": "1.2.6",
"version": "1.3.0",
"humhub": {
"minVersion": "1.10"
"minVersion": "1.13"
},
"homepage": "https://github.com/humhub/calendar",
"authors": [
Expand Down
616 changes: 468 additions & 148 deletions resources/js/fullcalendar.bundle.min.js

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions resources/js/humhub.calendar.Calendar.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions resources/js/humhub.calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ humhub.module('calendar', function (module, require, $) {
var modal = require('ui.modal');
var action = require('action');
var Content = require('content').Content;
var event = require('event');
var StreamEntry = require('stream').StreamEntry;

var Calendar = Widget.extend();

Expand Down Expand Up @@ -51,6 +53,7 @@ humhub.module('calendar', function (module, require, $) {
});

this.initTimeInput();
this.initSubmitAction();
};

Form.prototype.setEditMode = function (evt) {
Expand Down Expand Up @@ -85,6 +88,29 @@ humhub.module('calendar', function (module, require, $) {
});
};

Form.prototype.initSubmitAction = function () {
modal.global.$.one('submitted', onCalEntryFormSubmitted);
}

var onCalEntryFormSubmitted = function (evt, response) {
if (response.id) {
modal.global.$.one('hidden.bs.modal', function () {
var entry = StreamEntry.getNodeByKey(response.id);
if (entry.length) {
entry = new StreamEntry(entry);
entry.reload();
}
});
}

if (response.reloadWall) {
event.trigger('humhub:content:newEntry', response.content, this);
event.trigger('humhub:content:afterSubmit', response.content, this);
} else {
modal.global.$.one('submitted', onCalEntryFormSubmitted);
}
};

Form.prototype.toggleDateTime = function (evt) {
var $timeFields = modal.global.$.find('.timeField');
var $timeInputs = $timeFields.find('.form-control');
Expand Down
Loading