public function save()
{
$this->events()->trigger(__CLASS__, 'before.save', $this);
$arraySave = array('name' => $this->getName(), 'identifier' => $this->getIdentifier(), 'description' => $this->getDescription(), 'updated_at' => new Expression('NOW()'));
try {
if ($this->getId() == null) {
$arraySave['created_at'] = new Expression('NOW()');
$this->insert($arraySave);
$this->setId($this->getLastInsertId());
} else {
$this->update($arraySave, array('id' => (int) $this->getId()));
}
$oldFilename = sprintf(GC_TEMPLATE_PATH . '/script/%s.phtml', $this->getOrigData('identifier'));
if (file_exists($oldFilename)) {
unlink($oldFilename);
}
file_put_contents($this->getFilePath(), $this->getData('content'));
$this->events()->trigger(__CLASS__, 'after.save', $this);
return $this->getId();
} catch (\Exception $e) {
$this->events()->trigger(__CLASS__, 'after.save.failed', $this);
throw new \Gc\Exception($e->getMessage(), $e->getCode(), $e);
}
}