Skip to content

Commit dd35a44

Browse files
author
Simon Karlen
committed
keep assignments in repeatable behavior
updated to do to sort repeatable tasks correctly
1 parent 3f4fb32 commit dd35a44

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

src/behaviors/RepeatableBehavior.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use yii\behaviors\BlameableBehavior;
1515
use yii\behaviors\TimestampBehavior;
1616
use yii\db\ActiveRecord;
17-
use yii\db\Expression;
1817
use yii\db\Query;
1918
use yii\di\Instance;
2019
use yii\helpers\ArrayHelper;
@@ -73,6 +72,13 @@ class RepeatableBehavior extends Behavior
7372
*/
7473
public $recurrenceDoneRelationProperty = 'task_id';
7574

75+
/**
76+
* @var array An array of relation names to keep in recurrent instance
77+
*/
78+
public $keepRelations = [
79+
'assignments'
80+
];
81+
7682
/**
7783
* @var bool If instance of this task is recurrent
7884
*/
@@ -100,13 +106,16 @@ public function deleteRecurrentInstance(\yii\base\ModelEvent $event)
100106
if (!$this->isRecurrentInstance()) {
101107
if ($this->owner->{$this->recurrenceParentIdProperty}) {
102108
/** @var \yii\db\ActiveRecord $record */
103-
$record = call_user_func([$this->owner, 'findOne'], $this->owner->getAttribute($this->recurrenceParentIdProperty))->getOriginalRecord();
109+
$record = call_user_func([$this->owner, 'findOne'],
110+
$this->owner->getAttribute($this->recurrenceParentIdProperty))->getOriginalRecord();
104111
/** @var \Recurr\Rule $rule */
105112
$rule = $record->{$this->recurrencePatternProperty};
106113

107114
$exDates = [];
108115
foreach ($rule->getExDates() as $exDate) {
109-
if (Yii::$app->formatter->asDate($exDate->date, 'yyyy-MM-dd') !== Yii::$app->formatter->asDate($this->owner->{$this->endDateProperty}, 'yyyy-MM-dd')) {
116+
if (Yii::$app->formatter->asDate($exDate->date,
117+
'yyyy-MM-dd') !== Yii::$app->formatter->asDate($this->owner->{$this->endDateProperty},
118+
'yyyy-MM-dd')) {
110119
$exDates[] = Yii::$app->formatter->asDate($exDate->date, 'yyyy-MM-dd');
111120
}
112121
}
@@ -247,7 +256,9 @@ public function createRecurrentInstance()
247256
$this->owner->setAttribute($this->statusProperty, $status);
248257

249258
foreach ($this->owner->getRelatedRecords() as $relatedRecordName => $relatedRecord) {
250-
unset($this->owner->{$relatedRecordName});
259+
if (!in_array($relatedRecordName, $this->keepRelations)) {
260+
unset($this->owner->{$relatedRecordName});
261+
}
251262
}
252263
}
253264

src/widgets/ToDo.php

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@
99

1010
use kartik\select2\Select2;
1111
use rmrevin\yii\fontawesome\FAR;
12+
use rmrevin\yii\fontawesome\FAS;
1213
use simialbi\yii2\kanban\KanbanAsset;
1314
use simialbi\yii2\kanban\KanbanSwiperAsset;
14-
use simialbi\yii2\kanban\models\ChecklistElement;
1515
use simialbi\yii2\kanban\models\Task;
1616
use simialbi\yii2\turbo\Frame;
1717
use simialbi\yii2\turbo\Modal;
1818
use simialbi\yii2\widgets\Widget;
1919
use Yii;
2020
use yii\bootstrap4\Html;
21-
use yii\db\Expression;
22-
use yii\db\Query;
2321
use yii\helpers\ArrayHelper;
2422
use yii\helpers\Url;
2523
use yii\web\JsExpression;
@@ -85,34 +83,31 @@ public function run()
8583
$this->view->registerAssetBundle(KanbanSwiperAsset::class);
8684
$this->view->registerAssetBundle(KanbanAsset::class);
8785

88-
$checklistQuery = new Query();
89-
$checklistQuery
90-
->select('end_date')
91-
->from(ChecklistElement::tableName())
92-
->where(new Expression('[[task_id]] = {{t}}.[[id]]'))
93-
->andWhere(['is_done' => false])
94-
->orderBy(['end_date' => SORT_ASC])
95-
->limit(1);
9686
$tasks = Task::find()
97-
->select([
98-
'{{t}}.*',
99-
'cEndDate' => $checklistQuery,
100-
'endDate' => new Expression('IF({{t}}.[[end_date]], {{t}}.[[end_date]], (SELECT [[cEndDate]]))')
101-
])
10287
->cache(60)
10388
->alias('t')
10489
->with(['checklistElements', 'comments'])
10590
->innerJoinWith('bucket bu')
10691
->innerJoinWith('board b')
10792
->innerJoinWith('assignments u')
10893
->where(['not', ['{{t}}.[[status]]' => Task::STATUS_DONE]])
109-
->andWhere(['{{u}}.[[user_id]]' => Yii::$app->user->id])
110-
->addOrderBy(new Expression('-[[endDate]] DESC'))
111-
->addOrderBy(new Expression('-{{t}}.[[start_date]] DESC'))
112-
->addOrderBy(['{{t}}.[[created_at]]' => SORT_ASC])
113-
->asArray(true);
94+
->andWhere(['{{u}}.[[user_id]]' => Yii::$app->user->id]);
11495

11596
$results = $tasks->all();
97+
usort($results, function ($a, $b) {
98+
/** @var $a Task */
99+
/** @var $b Task */
100+
if ($a->endDate === $b->endDate) {
101+
return 0;
102+
}
103+
if ($a->endDate === null && $b->endDate !== null) {
104+
return 1;
105+
}
106+
if ($a->endDate !== null && $b->endDate === null) {
107+
return -1;
108+
}
109+
return ($a->endDate < $b->endDate) ? -1 : 1;
110+
});
116111

117112
ob_start();
118113
if ($this->renderModal) {
@@ -182,15 +177,23 @@ function onHide() {
182177

183178
foreach ($results as $task) {
184179
$options = $this->itemOptions;
185-
$options['href'] = Url::to(["/{$this->kanbanModuleName}/task/update", 'id' => $task['id'], 'return' => 'todo']);
180+
$options['href'] = Url::to([
181+
"/{$this->kanbanModuleName}/task/update",
182+
'id' => $task['id'],
183+
'return' => 'todo'
184+
]);
186185
$options['data'] = [
187186
'toggle' => 'modal',
188187
'pjax' => '0',
189188
'turbo-frame' => 'task-modal-frame',
190189
'target' => '#task-modal'
191190
];
192191

193-
$content = Html::tag('h6', $task['subject'], ['class' => ['m-0']]);
192+
$subject = $task['subject'];
193+
if ($task->isRecurrentInstance()) {
194+
$subject = FAS::i('infinity')->transform('shrink-4.5')->mask('circle') . $subject;
195+
}
196+
$content = Html::tag('h6', $subject, ['class' => ['m-0']]);
194197
$small = $task['board']['name'];
195198

196199
if (($cnt = count($task['checklistElements'])) > 0) {

0 commit comments

Comments
 (0)