|
<?php |
|
|
|
|
|
class HelperAuthorTags extends WireData implements Module |
|
{ |
|
|
|
|
|
public static function getModuleInfo() |
|
{ |
|
return array( |
|
'title' => 'Helper Author Tags', |
|
'description' => 'Regenerate author tags on article save', |
|
'version' => 1, |
|
'singular' => true, |
|
'autoload' => true |
|
); |
|
} |
|
|
|
|
|
public function init() |
|
{ |
|
$this->pages->addHookAfter("save", $this, "hookPageSave"); |
|
} |
|
|
|
|
|
public function hookPageSave(HookEvent $event) |
|
{ |
|
$page = $event->argumentsByName("page"); |
|
/** |
|
* @var Pages $pages |
|
*/ |
|
$pages = wire('pages'); |
|
if ($page->template == 'works-single') { |
|
/** |
|
* @var tpl_works_single $page |
|
*/ |
|
if (!($page->isChanged('workAuthors') |
|
|| $page->isChanged('workTags') |
|
|| $page->isChanged('workMedium'))) { |
|
return; |
|
} |
|
foreach ($page->workAuthors as $author) { |
|
/** |
|
* @var tpl_authors_single $authorPage |
|
*/ |
|
$authorPage = $pages->get("id=$author"); |
|
|
|
// remove all tags first |
|
$authorPage->authorTags->removeAll(); |
|
|
|
// get all relevant tags again |
|
$authorTags = new PageArray(); |
|
$authorArticles = $pages->find("template=works-single, workAuthors=$author"); |
|
foreach ($authorArticles as $article) { |
|
$authorTags->import($article->workTags); |
|
} |
|
|
|
// sort tags by number of works |
|
$authorTags = $authorTags->sort('-numPages'); |
|
|
|
// add tags to the author's page and save it |
|
$authorPage->authorTags = $authorTags; |
|
$authorPage->save('authorTags'); |
|
|
|
// tell the user about it |
|
$this->message("Regenerated tag data for author $author->title"); |
|
} |
|
} |
|
} |
|
} |
|
|