Skip to content

Instantly share code, notes, and snippets.

@touol
Created March 21, 2020 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save touol/41b6366fd0bb42b94b70fda87efb05ea to your computer and use it in GitHub Desktop.
Save touol/41b6366fd0bb42b94b70fda87efb05ea to your computer and use it in GitHub Desktop.
<?php
/** @var modX $modx */
switch ($modx->event->name) {
case 'OnDocFormSave':
/** @var Ticket $resource */
if ($resource->class_key == "Ticket") {
//$modx->log(1, "TicketsResGroup ".print_r($resource->toArray(),1));
if(!empty($resource->res_groups)){
$resGroups = $resource->getMany('ResourceGroupResources');
$res_groups = $resource->res_groups;
foreach($resGroups as $old_rg){
$isset_group = false;
foreach($res_groups as $k=>$rg){
if($rg == 0) unset($res_groups[$k]);
if ($old_rg->document_group == $rg) {
unset($res_groups[$k]);
//$isset_groups = true;
$isset_group = true;
}
}
if(!$isset_group) $old_rg->remove();
}
if(!empty($res_groups)){ //add group
foreach($res_groups as $k=>$rg){
if($rg == 0) continue;
$resource->joinGroup((int)$rg);
}
}
}else{
$resGroups = $resource->getMany('ResourceGroupResources');
foreach($resGroups as $old_rg){
$old_rg->remove();
}
}
}
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment