Skip to content

Commit

Permalink
Interfaces and style rework in an attempt to make Scribbles look good
Browse files Browse the repository at this point in the history
Concerns #253
  • Loading branch information
mikron-ia committed Oct 5, 2023
1 parent 4f01dde commit bfb86d0
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 61 deletions.
11 changes: 6 additions & 5 deletions common/models/Character.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use common\models\core\HasEpicControl;
use common\models\core\HasImportance;
use common\models\core\HasImportanceCategory;
use common\models\core\HasScribbles;
use common\models\core\HasSightings;
use common\models\core\HasVisibility;
use common\models\core\ImportanceCategory;
Expand Down Expand Up @@ -50,7 +51,7 @@
* @property GroupMembership[] $groupMemberships
* @property GroupMembership[] $groupMembershipsVisibleToUser
*/
class Character extends ActiveRecord implements Displayable, HasDescriptions, HasEpicControl, HasImportance, HasImportanceCategory, HasReputations, HasVisibility, HasSightings
class Character extends ActiveRecord implements Displayable, HasDescriptions, HasEpicControl, HasImportance, HasImportanceCategory, HasReputations, HasVisibility, HasScribbles, HasSightings
{
use ToolsForEntity;
use ToolsForHasDescriptions;
Expand Down Expand Up @@ -176,9 +177,9 @@ public function beforeSave($insert)
$this->importance_pack_id = $pack->importance_pack_id;
}

if (empty($this->importance_pack_id)) {
$pack = ImportancePack::create('Character');
$this->importance_pack_id = $pack->importance_pack_id;
if (empty($this->scribble_pack_id)) {
$pack = ScribblePack::create('Character');
$this->scribble_pack_id = $pack->scribble_pack_id;
}

return parent::beforeSave($insert);
Expand Down Expand Up @@ -218,7 +219,7 @@ static public function allowedVisibilities(): array
/**
* @return ActiveQuery
*/
public function getEpic(): ActiveQuery
public function getEpic(): ActiveQuery
{
return $this->hasOne(Epic::class, ['epic_id' => 'epic_id']);
}
Expand Down
4 changes: 2 additions & 2 deletions common/models/DescriptionPack.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace common\models;

use common\models\core\HasEpicControl;
use common\models\core\IsPack;
use common\models\core\IsEditablePack;
use common\models\core\Language;
use Yii;
use yii\behaviors\TimestampBehavior;
Expand All @@ -21,7 +21,7 @@
* @property Character[] $people
* @property Epic $epic
*/
final class DescriptionPack extends ActiveRecord implements Displayable, IsPack
final class DescriptionPack extends ActiveRecord implements Displayable, IsEditablePack
{
public static function tableName()
{
Expand Down
4 changes: 2 additions & 2 deletions common/models/ExternalDataPack.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace common\models;

use common\models\core\HasEpicControl;
use common\models\core\IsPack;
use common\models\core\IsEditablePack;
use common\models\core\Visibility;
use Yii;
use yii\behaviors\TimestampBehavior;
Expand All @@ -20,7 +20,7 @@
*
* @property Epic $epic
*/
class ExternalDataPack extends ActiveRecord implements IsPack
class ExternalDataPack extends ActiveRecord implements IsEditablePack
{
public static function tableName()
{
Expand Down
8 changes: 7 additions & 1 deletion common/models/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use common\models\core\HasEpicControl;
use common\models\core\HasImportance;
use common\models\core\HasImportanceCategory;
use common\models\core\HasScribbles;
use common\models\core\HasSightings;
use common\models\core\HasVisibility;
use common\models\core\ImportanceCategory;
Expand Down Expand Up @@ -55,7 +56,7 @@
* @property GroupMembership[] $groupCharacterMembershipsPassive
* @property GroupMembership[] $groupCharacterMembershipsPast
*/
class Group extends ActiveRecord implements Displayable, HasDescriptions, HasEpicControl, HasImportance, HasImportanceCategory, HasReputations, HasSightings, HasVisibility
class Group extends ActiveRecord implements Displayable, HasDescriptions, HasEpicControl, HasImportance, HasImportanceCategory, HasReputations, HasScribbles, HasSightings, HasVisibility
{
use ToolsForEntity;
use ToolsForHasDescriptions;
Expand Down Expand Up @@ -205,6 +206,11 @@ public function beforeSave($insert): bool
$this->importance_pack_id = $pack->importance_pack_id;
}

if (empty($this->scribble_pack_id)) {
$pack = ScribblePack::create('Group');
$this->scribble_pack_id = $pack->scribble_pack_id;
}

return parent::beforeSave($insert);
}

Expand Down
10 changes: 10 additions & 0 deletions common/models/Importance.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ public function getUser(): ActiveQuery
return $this->hasOne(User::class, ['id' => 'user_id']);
}

public static function createEmptyForPack(int $userId, ImportancePack $pack): self
{
$object = new Importance();
$object->user_id = $userId;
$object->importance_pack_id = $pack->importance_pack_id;
$object->importance = 0;

return $object;
}

/**
* Calculates the importance value
*
Expand Down
47 changes: 15 additions & 32 deletions common/models/ImportancePack.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace common\models;

use common\models\core\HasImportance;
use common\models\core\IsSelfFillingPack;
use common\models\tools\ToolsForSelfFillingPacks;
use Yii;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
Expand All @@ -17,8 +19,10 @@
* @property Group[] $groups
* @property Importance[] $importances
*/
class ImportancePack extends ActiveRecord
class ImportancePack extends ActiveRecord implements IsSelfFillingPack
{
use ToolsForSelfFillingPacks;

/**
* @var HasImportance
*/
Expand Down Expand Up @@ -105,48 +109,27 @@ public function getEpic(): Epic
return $this->getControllingObject()->getEpic()->one();
}

public function createEmptyContent(int $userId): Importance
{
return Importance::createEmptyForPack($userId, $this);
}

/**
* Recalculates pack importance objects
* @return bool
*/
public function recalculatePack(): bool
{
$result = $this->createAbsentImportanceObjects();
$result = $this->createAbsentRecords(
$this->getEpic(),
$this,
Importance::findAll(['importance_pack_id' => $this->importance_pack_id])
);

foreach ($this->importances as $importance) {
$result = $result && $importance->calculateAndSave();
}

return $result;
}

/**
* Creates new Importance objects for users that do not have them
* @return bool
*/
private function createAbsentImportanceObjects(): bool
{
$users = $this->getEpic()->participants;
$importanceObjectsRaw = Importance::findAll(['importance_pack_id' => $this->importance_pack_id]);
$importanceObjectsOrdered = [];

foreach ($importanceObjectsRaw as $importanceObject) {
$importanceObjectsOrdered[$importanceObject->user_id] = $importanceObject;
}

$result = true;

foreach ($users as $user) {
if (!isset($importanceObjectsOrdered[$user->user_id])) {
$importanceObject = new Importance();
$importanceObject->user_id = $user->user_id;
$importanceObject->importance_pack_id = $this->importance_pack_id;
$importanceObject->importance = 0;

$saveResult = $importanceObject->save();
$result = $result && $saveResult;
}
}
return $result;
}
}
4 changes: 2 additions & 2 deletions common/models/ParameterPack.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace common\models;

use common\models\core\HasEpicControl;
use common\models\core\IsPack;
use common\models\core\IsEditablePack;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
Expand All @@ -21,7 +21,7 @@
* @property Story[] $stories
* @property Epic $epic
*/
class ParameterPack extends ActiveRecord implements IsPack
class ParameterPack extends ActiveRecord implements IsEditablePack
{
/**
* @inheritdoc
Expand Down
10 changes: 10 additions & 0 deletions common/models/Scribble.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,14 @@ public static function find(): ScribbleQuery
{
return new ScribbleQuery(get_called_class());
}

public static function createEmptyForPack(int $userId, ScribblePack $pack): self
{
$object = new Scribble();
$object->user_id = $userId;
$object->scribble_pack_id = $pack->scribble_pack_id;
$object->favorite = false;

return $object;
}
}
23 changes: 19 additions & 4 deletions common/models/ScribblePack.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace common\models;

use common\models\core\HasEpicControl;
use common\models\core\IsPack;
use common\models\core\HasScribbles;
use common\models\core\IsEditablePack;
use common\models\core\IsSelfFillingPack;
use common\models\tools\ToolsForSelfFillingPacks;
use Yii;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
Expand All @@ -18,8 +21,10 @@
* @property Group[] $groups
* @property Scribble[] $scribbles
*/
class ScribblePack extends ActiveRecord implements IsPack
class ScribblePack extends ActiveRecord implements IsEditablePack
{
use ToolsForSelfFillingPacks;

public static function tableName(): string
{
return 'scribble_pack';
Expand Down Expand Up @@ -71,6 +76,16 @@ public function getScribbles()
return $this->hasMany(Scribble::class, ['scribble_pack_id' => 'scribble_pack_id']);
}

public static function create(string $class): ScribblePack
{
$pack = new ScribblePack(['class' => $class]);

$pack->save();
$pack->refresh();

return $pack;
}

public static function find(): ScribblePackQuery
{
return new ScribblePackQuery(get_called_class());
Expand All @@ -80,15 +95,15 @@ public function canUserReadYou(): bool
{
$className = 'common\models\\' . $this->class;
/** @var HasEpicControl $object */
$object = ($className)::findOne(['description_pack_id' => $this->description_pack_id]);
$object = ($className)::findOne(['scribble_pack_id' => $this->scribble_pack_id]);
return $object->canUserViewYou();
}

public function canUserControlYou(): bool
{
$className = 'common\models\\' . $this->class;
/** @var HasEpicControl $object */
$object = ($className)::findOne(['description_pack_id' => $this->description_pack_id]);
$object = ($className)::findOne(['scribble_pack_id' => $this->scribble_pack_id]);
return $object->canUserControlYou();
}
}
10 changes: 10 additions & 0 deletions common/models/core/HasEpic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace common\models\core;

use yii\db\ActiveQuery;

interface HasEpic
{
public function getEpic(): ActiveQuery;
}
7 changes: 7 additions & 0 deletions common/models/core/HasScribbles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace common\models\core;

interface HasScribbles
{
}
20 changes: 20 additions & 0 deletions common/models/core/IsEditablePack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace common\models\core;

/**
* Interface IsEditablePack
* @package common\models\core
*/
interface IsEditablePack extends IsPack
{
/**
* @return bool
*/
public function canUserReadYou(): bool;

/**
* @return bool
*/
public function canUserControlYou(): bool;
}
13 changes: 0 additions & 13 deletions common/models/core/IsPack.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,6 @@

namespace common\models\core;

/**
* Interface IsPack
* @package common\models\core
*/
interface IsPack
{
/**
* @return bool
*/
public function canUserReadYou(): bool;

/**
* @return bool
*/
public function canUserControlYou(): bool;
}
11 changes: 11 additions & 0 deletions common/models/core/IsSelfFillingPack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace common\models\core;

/**
* Interface for packs that self-fill if empty
*/
interface IsSelfFillingPack extends IsPack
{
public function createEmptyContent(int $userId);
}
30 changes: 30 additions & 0 deletions common/models/tools/ToolsForSelfFillingPacks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace common\models\tools;

use common\models\core\IsSelfFillingPack;
use common\models\Epic;

trait ToolsForSelfFillingPacks
{
private function createAbsentRecords(Epic $epic, IsSelfFillingPack $pack, array $objectsRaw): bool
{
$users = $epic->participants;
$objectsOrdered = [];

foreach ($objectsRaw as $object) {
$objectsOrdered[$object->user_id] = $object;
}

$result = true;

foreach ($users as $user) {
if (!isset($objectsOrdered[$user->user_id])) {
$scribbleObject = $pack->createEmptyContent($user->user_id);
$saveResult = $scribbleObject->save();
$result = $result && $saveResult;
}
}
return $result;
}
}

0 comments on commit bfb86d0

Please sign in to comment.