Open RoundAboutWEB opened 1 year ago
Not tested;
<?php
namespace humhub\modules\calendar\controllers;
use Yii;
use yii\web\Controller;
use humhub\modules\calendar\models\CalendarEntry;
use humhub\modules\calendar\permissions\ManageEntry;
use humhub\modules\calendar\permissions\CreateEntry;
use yii\web\NotFoundHttpException;
class CopyController extends Controller
{
/**
* Copies an existing calendar entry.
*
* @param int $id the ID of the entry to copy
* @param string|null $start_datetime optional start datetime for the new entry
* @param string|null $end_datetime optional end datetime for the new entry
* @return mixed response
* @throws \yii\web\ForbiddenHttpException if user does not have permission
* @throws \yii\web\NotFoundHttpException if entry is not found
*/
public function actionCopy($id, $start_datetime = null, $end_datetime = null)
{
// Check if the user has permission to manage or create calendar entries
if (!$this->contentContainer->can(ManageEntry::class) && !$this->contentContainer->can(CreateEntry::class)) {
throw new \yii\web\ForbiddenHttpException('You are not allowed to copy calendar entries.');
}
// Get the existing calendar entry
$existingEntry = CalendarEntry::findOne($id);
if (!$existingEntry) {
throw new NotFoundHttpException('Calendar entry not found.');
}
// Create a new calendar entry with the same attributes as the existing one
$newEntry = new CalendarEntry();
$newEntry->attributes = $existingEntry->attributes;
// Customize the title for the new entry
$newEntry->title = $existingEntry->title . ' (Copy)';
// Set custom start and end datetimes if provided, otherwise use the original entry's datetimes
$newEntry->start_datetime = $start_datetime ?? $existingEntry->start_datetime;
$newEntry->end_datetime = $end_datetime ?? $existingEntry->end_datetime;
// You can copy other attributes here if needed
// Save the new entry
if ($newEntry->save()) {
Yii::$app->getSession()->setFlash('success', 'Calendar entry copied successfully.');
} else {
Yii::$app->getSession()->setFlash('error', 'Error copying calendar entry.');
}
// Render the copy view with the appropriate parameters
$copyUrl = Url::toEditEntry($newEntry, null, $this->contentContainer, null);
return $this->renderAjax('copy', [
'calendarEntry' => $newEntry,
'contentContainer' => $this->contentContainer,
'copyUrl' => $copyUrl,
]);
}
}
<?= Button::primary(Yii::t('CalendarModule.base', 'Copy'))->action('ui.modal.load', $copyUrl, "[data-content-key=" . $calendarEntry->content->id . "]") ?>
Sometimes it would be very helpful if you could copy an appointment, as only small things change. So far I miss the function to copy an entry. Is this planned for the future?