Skip to content

Instantly share code, notes, and snippets.

@thezenmonkey
Created November 19, 2016 23:09
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 thezenmonkey/42b97bd7357770762ae7badd0ab15ce0 to your computer and use it in GitHub Desktop.
Save thezenmonkey/42b97bd7357770762ae7badd0ab15ce0 to your computer and use it in GitHub Desktop.
onAfterWrite function to auto incriment blocks
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