Created
November 19, 2016 23:09
-
-
Save thezenmonkey/42b97bd7357770762ae7badd0ab15ce0 to your computer and use it in GitHub Desktop.
onAfterWrite function to auto incriment blocks
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
public function onAfterWrite() | |
{ | |
parent::onAfterWrite(); | |
if ($this->hasExtension('FilesystemPublisher')) { | |
$this->republish($this); | |
} | |
$currentPage = Controller::curr()->getRequest()->requestVar('CMSMainCurrentPageID'); | |
// Check if Block is Used on Current Page and has a Sort of 0 | |
$query = new SQLSelect(); | |
$pageCount = $query->setFrom('SiteTree_Blocks') | |
->addWhere(array('BlockID' => $this->ID, 'SiteTreeID' => $currentPage, 'Sort' => 0)) | |
->setSelect('COUNT("SiteTree_Blocks"."ID")') | |
->execute()->value(); | |
if($pageCount > 0) { | |
//Update SiteTree_Blocks to increase count of current Block by One | |
$max = DB::query('SELECT MAX(Sort) FROM SiteTree_Blocks WHERE SiteTreeID = '.$currentPage)->value(); | |
$update = SQLUpdate::create('SiteTree_Blocks') | |
->addWhere(array("SiteTreeID" => $currentPage, "BlockID" => $this->ID)); | |
$update->assign("Sort", $max + 1); | |
$update->execute(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment