Last active
August 29, 2015 14:18
-
-
Save Da-Fecto/5e103d45586a29723128 to your computer and use it in GitHub Desktop.
Bidirectional save & remove of products & groups (Fieltype pages)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Groups & Products references guard. | |
* | |
* Bidirectional save & remove of products & groups | |
* | |
* templates: | |
* - 'group', pagefield: page_products | |
* - 'product', pagefield: page_groups | |
*/ | |
class GroupAndProducts extends WireData implements Module { | |
public static function getModuleInfo() { | |
return array( | |
'title' => 'Group And Products', | |
'summary' => 'Bidirectional save & remove of products & groups', | |
'version' => 1, | |
'autoload' => true, | |
); | |
} | |
public function init() { | |
$this->addHookAfter('Pages::saveReady', $this, 'hookPagesSave'); | |
} | |
/** | |
* Hook called when a page is saved | |
* | |
* Used to delete references to the page in any PageTable tables | |
* | |
*/ | |
public function hookPagesSave(HookEvent $event) { | |
$page = $event->arguments('page'); | |
$template = ($page->template != 'group' && $page->template != 'product') ? false : $page->template->name; | |
// False template, no glory | |
if ($template === false) return; | |
if ($template === 'product') { | |
$this->productSave($page); | |
} else if ($template === 'group') { | |
$this->groupSave($page); | |
} | |
} | |
/** | |
* Group page is saved so check the product references | |
* | |
*/ | |
protected function groupSave(Page $group) { | |
// $this->log->save('changes', json_encode($group->getChanges())); | |
if (in_array('page_products', $group->getChanges())) { | |
// Assign group to a product | |
foreach($group->page_products as $product) { | |
if (!$product->page_groups->has($group)) { | |
$product->page_groups->add($group); | |
$product->save('page_groups'); | |
//$product->setTrackChanges(false); | |
$message = sprintf($this->_('“%1$s” is added to the group: %2$s'), $product->title, $group->title); | |
$this->message($message); | |
} | |
} | |
// Remove group from a product | |
$products = $this->pages->find("template=product, page_groups=$group"); | |
foreach($products as $product) { | |
if (!$group->page_products->has($product)) { | |
$product->page_groups->remove($group); | |
$product->save('page_groups'); | |
//$product->setTrackChanges(false); | |
$message = sprintf($this->_('“%1$s” is removed from the group: %2$s'), $product->title, $group->title); | |
$this->message($message); | |
} | |
} | |
} | |
} | |
/** | |
* Product page is saved so check check references | |
* | |
*/ | |
protected function productSave(Page $product) { | |
// $this->log->save('changes', json_encode($product->getChanges())); | |
if (in_array('page_groups', $product->getChanges())) { | |
foreach($product->page_groups as $group) { | |
if (!$group->page_products->has($product)) { | |
$group->page_products->add($product); | |
$group->save('page_products'); | |
//$product->setTrackChanges(false); | |
$message = sprintf($this->_('“%1$s” is added to the group: %2$s'), $product->title, $group->title); | |
$this->message($message); | |
} | |
} | |
// Remove group from a product | |
$groups = $this->pages->find("template=group, page_products=$product"); | |
foreach($groups as $group) { | |
if (!$product->page_groups->has($group)) { | |
$group->page_products->remove($product); | |
$group->save('page_products'); | |
//$product->setTrackChanges(false); | |
$message = sprintf($this->_('“%1$s” is removed from the group: %2$s'), $product->title, $group->title); | |
$this->message($message); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment