Skip to content

Commit

Permalink
Merge pull request #76 from marc-farre/fix/module-disabling
Browse files Browse the repository at this point in the history
Fix: Disabling the module in a container (Space or User)
  • Loading branch information
luke- committed May 8, 2024
2 parents 4ed630d + df6d512 commit 52b9825
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 71 deletions.
16 changes: 6 additions & 10 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,9 @@ public function enable()
*/
public function disable()
{
/** @var ExternalCalendarEntry $entry */
foreach (ExternalCalendarEntry::find()->each() as $entry) {
$entry->hardDelete();
}
/** @var ExternalCalendar $entry */
foreach (ExternalCalendar::find()->all() as $entry) {
$entry->hardDelete();
/** @var ExternalCalendar $calendar */
foreach (ExternalCalendar::find()->all() as $calendar) {
$calendar->hardDelete();
}
parent::disable();
}
Expand Down Expand Up @@ -113,9 +109,9 @@ public function getContentContainerDescription(ContentContainerActiveRecord $con
public function disableContentContainer(ContentContainerActiveRecord $container)
{
parent::disableContentContainer($container);
/** @var ExternalCalendar $item */
foreach (ExternalCalendar::find()->contentContainer($container)->each() as $item) {
$item->hardDelete();
/** @var ExternalCalendar $calendar */
foreach (ExternalCalendar::find()->contentContainer($container)->each() as $calendar) {
$calendar->hardDelete();
}
}

Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Unreleased
- Enh #68: Make URL locations clickable
- Enh #69: Add `requirements.php`
- Fix #71: After module disabling, Content for ExternalCalendarEntry are not deleted
- Fix #76: Disabling the module in a container (Space or User)

1.4.2 (January 19, 2023)
-----------------------
Expand Down
109 changes: 48 additions & 61 deletions models/ExternalCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
use Colors\RandomColor;
use humhub\libs\Html;
use humhub\modules\content\components\ActiveQueryContent;
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\external_calendar\jobs\UpdateCalendarVisibility;
use humhub\modules\external_calendar\models\forms\ConfigForm;
use Yii;
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\external_calendar\permissions\ManageCalendar;
use humhub\modules\search\interfaces\Searchable;
use humhub\modules\content\models\Content;
use ICal\ICal;
use yii\base\InvalidCallException;
use Yii;
use yii\base\InvalidValueException;


Expand All @@ -39,72 +37,61 @@
class ExternalCalendar extends ContentActiveRecord implements Searchable
{
const ITEM_TYPE_KEY = 'external_calendar';

/**
* Sync Modes
*/
const SYNC_MODE_NONE = 0;
const SYNC_MODE_HOURLY = 1;
const SYNC_MODE_DAILY = 2;
/**
* Event Modes
*/
const EVENT_MODE_CURRENT_MONTH = 0;
const EVENT_MODE_ALL = 1;
/**
* @var array all given sync modes as array
*/
public static $syncModes = [
self::SYNC_MODE_NONE,
self::SYNC_MODE_HOURLY,
self::SYNC_MODE_DAILY
];
/**
* @var array all given sync modes as array
*/
public static $eventModes = [
self::EVENT_MODE_CURRENT_MONTH,
self::EVENT_MODE_ALL
];
/**
* @inheritdoc
*/
public $moduleId = 'external_calendar';

/**
* @inheritdoc
*/
public $wallEntryClass = 'humhub\modules\external_calendar\widgets\WallEntryCalendar';

/**
* @inheritdoc
*/
public $managePermission = ManageCalendar::class;

/**
* @var bool
*/
public $allowFiles = false;

/**
* @var int form field
*/
public $public;

/**
* @inheritdoc
*/
public $streamChannel = null;

/**
* @inheritdoc
*/
public $silentContentCreation = true;

/**
* Sync Modes
*/
const SYNC_MODE_NONE = 0;
const SYNC_MODE_HOURLY = 1;
const SYNC_MODE_DAILY = 2;

/**
* Event Modes
*/
const EVENT_MODE_CURRENT_MONTH = 0;
const EVENT_MODE_ALL = 1;

/**
* @var array all given sync modes as array
*/
public static $syncModes = [
self::SYNC_MODE_NONE,
self::SYNC_MODE_HOURLY,
self::SYNC_MODE_DAILY
];

/**
* @var array all given sync modes as array
*/
public static $eventModes = [
self::EVENT_MODE_CURRENT_MONTH,
self::EVENT_MODE_ALL
];

/**
* @inheritdoc
*/
Expand All @@ -121,19 +108,19 @@ public function init()

parent::init();

if(!$this->color) {
if (!$this->color) {
$this->color = RandomColor::one(['luminosity' => 'light']);
}

if($this->event_mode === null) {
if ($this->event_mode === null) {
$this->event_mode = static::EVENT_MODE_ALL;
}
}

public function afterFind()
{
parent::afterFind();
if($this->public === null) {
if ($this->public === null) {
$this->public = $this->content->visibility;
}
}
Expand All @@ -155,8 +142,8 @@ public function rules()
[['event_mode'], 'in', 'range' => self::$eventModes],
];

if(!$this->allowFiles) {
$result[] = [['url'], 'url', 'defaultScheme' => 'https', 'message' => Yii::t('ExternalCalendarModule.sync_result', "No valid ical url! Try an url with http / https.")];
if (!$this->allowFiles) {
$result[] = [['url'], 'url', 'defaultScheme' => 'https', 'message' => Yii::t('ExternalCalendarModule.sync_result', "No valid ical url! Try an url with http / https.")];
}

return $result;
Expand Down Expand Up @@ -194,7 +181,7 @@ public function getIcon()
*/
public function validateURL($attribute, $params)
{
if(!filter_var($this->url, FILTER_VALIDATE_URL)) {
if (!filter_var($this->url, FILTER_VALIDATE_URL)) {
$this->addError($attribute, Yii::t('ExternalCalendarModule.sync_result', "Bad URL"));
return false;
}
Expand Down Expand Up @@ -246,7 +233,7 @@ public function getSearchAttributes()
*/
public function beforeDelete()
{
foreach (ExternalCalendarEntry::findAll(['calendar_id' => $this->id]) as $item) {
foreach (ExternalCalendarEntry::find()->where(['calendar_id' => $this->id])->each() as $item) {
$item->delete();
}

Expand Down Expand Up @@ -286,7 +273,7 @@ public function afterSave($insert, $changedAttributes)

parent::afterSave($insert, $changedAttributes);

if($visibilityChanged) {
if ($visibilityChanged) {
Yii::$app->queue->push(new UpdateCalendarVisibility(['calendarId' => $this->id]));
}
}
Expand Down Expand Up @@ -353,26 +340,26 @@ public function getEventMode()
}
}

/**
* @return ExternalCalendarEntry[]
*/
public function getRecurringEventRoots()
{
return $this->getEntries(false)
->andWhere('external_calendar_entry.rrule IS NOT NULL')->all();
}

public function getEntries(bool $includeRecurrences = true): ActiveQueryContent
{
$query = $this->hasMany(ExternalCalendarEntry::class, ['calendar_id' => 'id']);

if(!$includeRecurrences) {
if (!$includeRecurrences) {
$query->andWhere('external_calendar_entry.parent_event_id IS NULL');
}

return $query->readable();
}

/**
* @return ExternalCalendarEntry[]
*/
public function getRecurringEventRoots()
{
return $this->getEntries(false)
->andWhere('external_calendar_entry.rrule IS NOT NULL')->all();
}

public function addAttributes(ICal $ical)
{
// add info to CalendarModel
Expand Down Expand Up @@ -404,9 +391,9 @@ public function getFullCalendarArray()
/**
* Syncronizes this external calendar
*
* @throws InvalidValueException
* @throws \yii\base\Exception
* @return static
* @throws \yii\base\Exception
* @throws InvalidValueException
*/
public function sync($rangeStart = null, $rangeEnd = null)
{
Expand Down

0 comments on commit 52b9825

Please sign in to comment.