Skip to content

Instantly share code, notes, and snippets.

@outflux3
Created June 2, 2014 21:17
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 outflux3/0a013ea20b46e706a6f9 to your computer and use it in GitHub Desktop.
Save outflux3/0a013ea20b46e706a6f9 to your computer and use it in GitHub Desktop.
AssignPageTable
<?php
class AssignSession extends WireData implements Module {
/**
* Basic information about module
*/
public static function getModuleInfo() {
return array(
'title' => 'Assign Session',
'summary' => 'Assigns a session to the session_table field in class, upon save',
'href' => '',
'version' => 001,
'permanent' => false,
'autoload' => true,
'singular' => true,
);
}
/**
* Populate the default config data
*
*/
public function __construct() {
}
/**
* Initialize the module and setup hooks
*/
public function init() {
$this->pages->addHookAfter('saveReady', $this, 'AssignSession');
}
/**
*
* @param HookEvent $event
*/
public function AssignSession(HookEvent $event) {
// Accesses the $page object
$page = $event->arguments[0];
if($page->template == 'session') {
if(!$page->class) return; // if no class is selected, do nothing.
$class = wire('pages')->get("$page->class");
if($class->session_table->has($page)) return;
// add the session to the class.
$class->setOutputFormatting(false);
$class->session_table->add($page);
$class->save();
// show a message
$this->message("Session added to {$class->title} session table");
}
if($page->template == 'class') {
$sessionsUnset = $page->session_table->find("class=");
foreach($sessionsUnset as $su) {
$su->class = $page;
$su->setOutputFormatting(false);
$su->save();
$this->message("Class added to {$su->id}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment