/** * Insert object into DB based on previously recorded changes. * * @return boolean true on success */ function dbinsert() { global $DB; if ($this->ID != 0) { die('Existing object cannot be inserted!'); } $DB->begin(); if (Chapter::urlname_exists($this->urlname)) { // We gotta find another name: $sql = 'SELECT cat_urlname FROM T_categories WHERE cat_urlname REGEXP "^' . $this->urlname . '(-[0-9]+)?$"'; $highest_number = 0; foreach ($DB->get_results($sql) as $row) { if (preg_match('/-([0-9]+)$/', $row->cat_urlname, $matches)) { // This one has a number, we extract it: $existing_number = (int) $matches[1]; if ($existing_number > $highest_number) { // This is the new high $highest_number = $existing_number; } } } $this->set('urlname', $this->urlname . '-' . ($highest_number + 1)); } $r = parent::dbinsert(); $DB->commit(); return $r; }
/** * Update the DB based on previously recorded changes * * @return boolean true on success */ function dbupdate() { global $DB; // Start transaction because of urltitle validation $DB->begin('SERIALIZABLE'); // validate url title / slug if (empty($this->urlname) || isset($this->dbchanges['cat_urlname'])) { // Url title has changed or is empty $this->set('urlname', urltitle_validate($this->urlname, $this->name, $this->ID, false, $this->dbprefix . 'urlname', $this->dbIDname, $this->dbtablename)); } if (parent::dbupdate() === false) { // The update was unsuccessful $DB->rollback(); return false; } // The chapter was updated successful $DB->commit(); return true; }