Hello everyone!
I'm not sure the right thread is chosen, but anyway I'll continue.
My pages structure looks like Category->Subcategories->Pages
A task is to make PW to count Pages,when their amount is changed and write it to the Subcategory's and Category's appropriate field regardless of Pages amount change is made from admin of template. So I'm trying to hook into 'save', 'trash', 'restore' and 'delete' methods from within a site module.
'Save' hook seems to work fine, but hooking into 'trash' requires to track Page->parent and Page->parent->parent the the other way, because they are changed to 'Trash' and 'Root' when 'trash' execution is is finished. The other words, at the moment when I have to count Pages in Category and Subcategory, where the Page was 'thrashed' from, there is already no way to determine, what the Category and Subcategory contained this Page.
I'm trying to hook into 'trash' before execution, find Page Category and Subcategory and assign them to an module object property, so after the 'trash' execution I can hook into it again and count Pages in Category and Subcategory, saved in properties. But something causes my before 'trash' hook to execute two times: before and after 'trash', and as the result module object properties contain not Category and Subcategory, but 'Trash' and 'Root' pages.
Here is my Code:
public $country;
public $region;
/**
* Initialize the module
*
*/
public function init() {
$this->pages->addHookBefore('trash', $this, 'prepareTripSave');
$this->pages->addHookAfter('trashed', $this, 'afterTrash');
}
/**
* Counts 'Trips' in 'Region' and 'Country'
*
*/
public function countTrips($event) {
$page = $event->arguments('page');
if ($page->template == 'Trip') {
// do only when 'Country' and 'Region' are find
if(count($this->country) && count($this->region)) {
$this->message('ok');
$amount = $this->region->children("template=Trip")->count(); //count 'Trips' in 'Region'
$this->region->setOutputFormatting(false);
$this->region->amount = $amount;
$this->region->save("amount"); // save only field
$this->region->setOutputFormatting(true);
$amount = $this->country->find("template=Trip")->count(); //count 'Trips' in 'Country'
$this->country->setOutputFormatting(false);
$this->country->amount = $amount;
$this->country->save("amount"); // save only field
$this->country->setOutputFormatting(true);
} else {
$this->message('nop');
}
$this->message("Country: {$this->region->amount}; Region: {$this->country->amount}");
$this->region = '';
$this->country = '';
}
}
public function prepareTripSave($event) {
$page = $event->arguments('page');
if ($page->template == 'Trip') {
$this->region = $this->pages->get($page->parentID); // find 'Region' by 'Trip'->parentID
$this->country = $this->region->parent; // find 'Country' as a 'Region'->parent
$this->message("Before Region: {$this->region->title}; Country: {$this->country->title}");
}
}
public function afterTrash($event) {
$page = $event->arguments('page');
if ($page->template == 'Trip') {
$this->message("After Region: {$this->region->title}; Country: {$this->country->title}");
}
}
Thanks!