diff --git a/include/class.ticket.php b/include/class.ticket.php index 805f3d1a2026b259b16e29fd636440c7d8540066..2de04e4b3b19d46d0c56094074adfe1c1019781d 100644 --- a/include/class.ticket.php +++ b/include/class.ticket.php @@ -248,7 +248,7 @@ class Ticket { function getHelpTopic() { if(!$this->ht['helptopic'] && ($topic=$this->getTopic())) - $this->ht['helptopic'] = $topic->getName(); + $this->ht['helptopic'] = $topic->getFullName(); return $this->ht['helptopic']; } diff --git a/include/class.topic.php b/include/class.topic.php index 57230c9be8a3f9ea3f5004269923c1894496ad7e..7fb60b76c8781c7a8b24f997bb3c70e7a5ab6b42 100644 --- a/include/class.topic.php +++ b/include/class.topic.php @@ -29,14 +29,13 @@ class Topic { } function load($id=0) { + global $cfg; if(!$id && !($id=$this->getId())) return false; $sql='SELECT ht.* ' - .', IF(ht.topic_pid IS NULL, ht.topic, CONCAT_WS(" / ", ht2.topic, ht.topic)) as name ' .' FROM '.TOPIC_TABLE.' ht ' - .' LEFT JOIN '.TOPIC_TABLE.' ht2 ON(ht2.topic_id=ht.topic_pid) ' .' WHERE ht.topic_id='.db_input($id); if(!($res=db_query($sql)) || !db_num_rows($res)) @@ -47,6 +46,10 @@ class Topic { $this->page = $this->form = null; + // Handle upgrade case where sort has not yet been defined + if (!$this->ht['sort'] && $cfg->getTopicSortMode() == 'a') { + static::updateSortOrder(); + } return true; } @@ -75,7 +78,7 @@ class Topic { } function getName() { - return $this->ht['name']; + return $this->ht['topic']; } function getFullName() { @@ -194,8 +197,7 @@ class Topic { if (!$names) { $sql = 'SELECT topic_id, topic_pid, ispublic, isactive, topic FROM '.TOPIC_TABLE - . ' ORDER BY ' - . ($cfg->getTopicSortMode() == 'm' ? '`sort`' : '`topic_id`'); + . ' ORDER BY `sort`'; $res = db_query($sql); // Fetch information for all topics, in declared sort order @@ -216,10 +218,9 @@ class Topic { } $names[$id] = $name; } - - if ($cfg->getTopicSortMode() == 'a') - uasort($names, function($a, $b) { return strcmp($a, $b); }); } + if ($disabled && !$publicOnly) + return $names; // Apply requested filters $requested_names = array(); @@ -298,23 +299,46 @@ class Topic { else $sql.=',staff_id=0, team_id=0 '; //no auto-assignment! - if($id) { + $rv = false; + if ($id) { $sql='UPDATE '.TOPIC_TABLE.' SET '.$sql.' WHERE topic_id='.db_input($id); - if(db_query($sql)) - return true; - - $errors['err']='Unable to update topic. Internal error occurred'; + if (!($rv = db_query($sql))) + $errors['err']='Unable to update topic. Internal error occurred'; } else { if (isset($vars['topic_id'])) $sql .= ', topic_id='.db_input($vars['topic_id']); - $sql='INSERT INTO '.TOPIC_TABLE.' SET '.$sql.',created=NOW()'; - if(db_query($sql) && ($id=db_insert_id())) - return $id; + if ($vars['pid'] && $cfg->getTopicSortMode() != 'a') { + $sql .= ', `sort`='.db_input( + db_result(db_query('SELECT COALESCE(`sort`,0)+1 FROM '.TOPIC_TABLE + .' WHERE `topic_id`='.db_input($vars['pid'])))); + } - $errors['err']='Unable to create the topic. Internal error'; + $sql='INSERT INTO '.TOPIC_TABLE.' SET '.$sql.',created=NOW()'; + if (db_query($sql) && ($id = db_insert_id())) + $rv = $id; + else + $errors['err']='Unable to create the topic. Internal error'; + } + if ($cfg->getTopicSortMode() == 'a') { + static::updateSortOrder(); } + return $rv; + } - return false; + static function updateSortOrder() { + // Fetch (un)sorted names + $names = static::getHelpTopics(false, true); + uasort($names, function($a, $b) { return strcmp($a, $b); }); + + $update = array_keys($names); + foreach ($update as $idx=>&$id) { + $id = sprintf("(%s,%s)", db_input($id), db_input($idx+1)); + } + // Thanks, http://stackoverflow.com/a/3466 + $sql = sprintf('INSERT INTO `%s` (topic_id,`sort`) VALUES %s + ON DUPLICATE KEY UPDATE `sort`=VALUES(`sort`)', + TOPIC_TABLE, implode(',', $update)); + db_query($sql); } } diff --git a/include/staff/helptopics.inc.php b/include/staff/helptopics.inc.php index 21764b1d9999faa7101851f4a435cf71f946d406..04d3b5238189b00f1d49b7c1e2706cf7c703ac42 100644 --- a/include/staff/helptopics.inc.php +++ b/include/staff/helptopics.inc.php @@ -10,15 +10,12 @@ $sql='SELECT topic.* ' $sql.=' WHERE 1'; $order_by = ($cfg->getTopicSortMode() == 'm' ? '`sort`' : '`topic_id`'); -$total=db_count('SELECT count(*) FROM '.TOPIC_TABLE.' topic '); $page=($_GET['p'] && is_numeric($_GET['p']))?$_GET['p']:1; -$pageNav=new Pagenate($total, $page, PAGE_LIMIT); -$pageNav->setURL('helptopics.php'); //Ok..lets roll...create the actual query $query="$sql ORDER BY $order_by"; $res=db_query($query); if($res && ($num=db_num_rows($res))) - $showing=$pageNav->showing().' help topics'; + $showing="Showing $num help topics"; else $showing='No help topic found!'; @@ -33,7 +30,6 @@ foreach ($topics as &$t) if ($cfg->getTopicSortMode() == 'a') usort($topics, function($a, $b) { return strcmp($a['name'], $b['name']); }); -$topics = array_slice($topics, $pageNav->getStart(), $pageNav->getLimit()+2); ?> <div style="width:700px;padding-top:5px; float:left;"> <h2>Help Topics</h2> @@ -133,7 +129,6 @@ $topics = array_slice($topics, $pageNav->getStart(), $pageNav->getLimit()+2); </table> <?php if($res && $num): //Show options.. - echo '<div> Page:'.$pageNav->getPageLinks().' </div>'; ?> <p class="centered" id="actions"> <?php if ($cfg->getTopicSortMode() != 'a') { ?> diff --git a/scp/css/scp.css b/scp/css/scp.css index db677e17bd4b9cb8fbf15aaef5c0b4dd0d1d2ff7..89942a3927b1cfb3744619ba2f31d78675f2191a 100644 --- a/scp/css/scp.css +++ b/scp/css/scp.css @@ -530,7 +530,7 @@ a.print { color:#000; } -#actions button, .button { padding:2px 5px 3px; margin-right:10px; color:#777; font-weight:bold;} +#actions button, .button { padding:2px 5px 3px; margin-right:10px; color:#777;} .btn_sm { padding:2px 5px; diff --git a/scp/helptopics.php b/scp/helptopics.php index 1b4eff07e2e7075e4585b76a8b83161a7da319c3..9262444a5da1ff5b1cfe658f506100f203dbdec8 100644 --- a/scp/helptopics.php +++ b/scp/helptopics.php @@ -100,7 +100,7 @@ if($_POST){ foreach ($_POST as $k=>$v) { if (strpos($k, 'sort-') === 0 && is_numeric($v) - && $t = Topic::lookup(substr($k, 5))) + && ($t = Topic::lookup(substr($k, 5)))) $t->setSortOrder($v); } }