Skip to content

Instantly share code, notes, and snippets.

@somatonic
Created December 19, 2012 08:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save somatonic/4335296 to your computer and use it in GitHub Desktop.
Save somatonic/4335296 to your computer and use it in GitHub Desktop.
maps two page fields POC
<?php
class MapPageField extends WireData implements Module{
public static function getModuleInfo(){
return array(
'title' => 'MapPageField',
'version' => 100,
'singular' => true,
'autoload' => true
);
}
// autoload module extends WireData
public function init(){
$this->set("tpl_team","basic-page");
$this->set("users_select","users_select");
$this->set("tpl_user","user");
$this->set("teams_select","teams_select");
$this->pages->addHookAfter("save",$this,"hookPageSave");
}
public function hookPageSave(HookEvent $event){
// get current page saved
$page = $event->argumentsByName("page");
// check if page is saved by this module to prevent recursion
if($page->skipme) return;
if($page->template == $this->tpl_user){
if(count($page->get($this->teams_select))){
// at least one team selected, iterate all team pages once
foreach($this->pages->find("template={$this->tpl_team}") as $team){
// iterate each selected team in the current user page to map
// and add or remove
foreach($page->get($this->teams_select) as $s_team) {
// if team page is a selected one and user not added, add user (page) to it
if($s_team === $team) {
if(!$team->get($this->users_select)->has($page)) {
$team->get($this->users_select)->add($page);
$team->skipme = true;
$team->save();
}
} else {
// if it's not in the selected team but found in field, remove page from it
if($team->get($this->users_select)->has($page)) {
$team->get($this->users_select)->remove($page);
$team->skipme = true;
$team->save();
}
}
}
}
} else {
// no team selected remove all references on team pages
$foundteams = $this->pages->find("template=$this->tpl_team,$this->user_select=$page");
if($foundteams){
foreach($foundteams as $team){
$team->get($this->users_select)->remove($page);
$team->skipme = true;
$team->save();
}
}
}
}
// same for team page save
if($page->template == $this->tpl_team){
if(count($page->get($this->users_select))){
// iterate all team pages once
foreach($this->pages->find("template={$this->tpl_user}") as $user){
foreach($page->get($this->users_select) as $s_user) {
if($s_user === $user) {
if(!$user->get($this->teams_select)->has($page)) {
$user->get($this->teams_select)->add($page);
$user->skipme = true;
$user->save();
}
} else {
if($user->get($this->teams_select)->has($page)) {
$user->get($this->teams_select)->remove($page);
$user->skipme = true;
$user->save();
}
}
}
}
} else {
// no users selected remove all references on users in case any found
$foundusers = $this->pages->find("template=$this->tpl_user,$this->teams_select=$page");
if($foundusers){
foreach($foundusers as $user){
$user->get($this->teams_select)->remove($page);
$user->skipme = true;
$user->save();
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment