public function process($doAudit = true) { // get the highest sort order if not set if ($this->row['sortOrder'] == -1) { $this->row['sortOrder'] = 100; $query = "SELECT MAX(`sortOrder`) sortOrder FROM navigation_items WHERE trackId = {$this->row['trackId']} AND parentId = {$this->row['parentId']}"; $rows = $db->fetchAll($query); $numRows = count($rows); if ($numRows) { $this->row['sortOrder'] = $rows[0]['sortOrder'] + 1; } } else { // check if any siblings have the same sort order $query = "SELECT COUNT(*) num FROM navigation_items WHERE trackId = {$this->row['trackId']} AND parentId = {$this->row['parentId']} AND sortOrder = {$this->row['sortOrder']} AND itemId <> {$this->row['itemId']}"; $rows = $db->fetchAll($query); $numRows = count($rows); if ($numRows) { if ($rows[0]['num']) { // raise the sort order by one on all siblings above this item $query = "UPDATE navigation_items SET sortOrder = (sortOrder + 1) WHERE trackId = {$this->row['trackId']} AND parentId = {$this->row['parentId']} AND sortOrder >= {$this->row['sortOrder']} AND itemId <> {$this->row['itemId']}"; $db->exec($query); } } } // if there is no track id set, get the track id of the parent if (!$this->row['trackId']) { $query = "SELECT trackId FROM navigation_items WHERE itemId = {$this->row['parentId']}"; $rows = $db->fetchAll($query); $numRows = count($rows); if ($numRows) { $this->row['trackId'] = $rows[0]['trackId']; } } // make sure there is a track id just in case it was added as a child // of a deleted item if ($this->row['trackId']) { parent::process($doAudit); } }