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

copy event #411

Open
RoundAboutWEB opened this issue Aug 23, 2023 · 1 comment
Open

copy event #411

RoundAboutWEB opened this issue Aug 23, 2023 · 1 comment

Comments

@RoundAboutWEB
Copy link

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?

@ArchBlood
Copy link

Not tested;

Example Controller;

<?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 Example

<?= Button::primary(Yii::t('CalendarModule.base', 'Copy'))->action('ui.modal.load', $copyUrl, "[data-content-key=" . $calendarEntry->content->id . "]") ?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants