Skip to content

Commit

Permalink
Adds sync config
Browse files Browse the repository at this point in the history
Now you can sync between locale, group and "all"
  • Loading branch information
daftspunk committed Feb 18, 2024
1 parent 499001c commit 770eb6d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
49 changes: 42 additions & 7 deletions src/Database/Traits/Multisite.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ trait Multisite
/**
* @var array propagatable list of attributes to propagate to other sites.
*
* protected $propagatable = [];
* protected $propagatable = [];
*/

/**
* @var bool propagatableSync will enforce model structures between all sites
* @var bool|array propagatableSync will enforce model structures between all sites.
* When set to `false` will disable sync, set `true` will sync between the site group.
* The sync option allow sync to `all` sites, sites in the `group`, and sites the `locale`.
*
* protected $propagatableSync = false;
* Set to an array of options for more granular controls:
*
* - **sync** - logic to sync specific sites, available options: `all`, `group`, `locale`
* - **delete** - delete all linked records when any record is deleted, default: `true`
*
* protected $propagatableSync = false;
*/

/**
Expand Down Expand Up @@ -239,9 +246,27 @@ public function isMultisiteEnabled()
*/
public function isMultisiteSyncEnabled()
{
return property_exists($this, 'propagatableSync')
? (bool) $this->propagatableSync
: false;
if (!property_exists($this, 'propagatableSync')) {
return false;
}

if (!is_array($this->propagatableSync)) {
return ($this->propagatableSync['sync'] ?? false) !== false;
}

return (bool) $this->propagatableSync;
}

/**
* getMultisiteConfig
*/
public function getMultisiteConfig($key, $default = null)
{
if (!property_exists($this, 'propagatableSync') || !is_array($this->propagatableSync)) {
return $default;
}

return array_get($this->propagatableSync, $key, $default);
}

/**
Expand All @@ -250,7 +275,17 @@ public function isMultisiteSyncEnabled()
*/
public function getMultisiteSyncSites()
{
return Site::listSiteIdsInContext();
if ($this->getMultisiteConfig('sync') === 'all') {
return Site::listSiteIds();
}

$siteId = $this->{$this->getSiteIdColumn()} ?: null;

if ($this->getMultisiteConfig('sync') === 'locale') {
return Site::listSiteIdsInLocale($siteId);
}

return Site::listSiteIdsInGroup($siteId);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Support/Facades/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
* @method static bool hasMultiSite()
* @method static array listEnabled()
* @method static array listSiteIds()
* @method static array listSiteIdsInContext()
* @method static array listSiteIdsInGroup($siteId)
* @method static array listSiteIdsInLocale($siteId)
* @method static iterable listSites()
* @method static int|null getSiteIdFromContext()
* @method static mixed getSiteFromContext()
Expand Down

0 comments on commit 770eb6d

Please sign in to comment.