/** * 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; }