Created
June 2, 2014 21:17
-
-
Save outflux3/0a013ea20b46e706a6f9 to your computer and use it in GitHub Desktop.
AssignPageTable
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 | |
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