From 550bf161d37352be4e32c29bd3ae2f865db62a8e Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Sun, 7 Dec 2014 21:00:07 +0000
Subject: [PATCH] orm: Convert Topic to ORM

---
 include/class.priority.php       |   3 +-
 include/class.topic.php          |   9 ++-
 include/staff/helptopic.inc.php  |  32 ++++-----
 include/staff/helptopics.inc.php | 107 +++++++++++++++----------------
 4 files changed, 74 insertions(+), 77 deletions(-)

diff --git a/include/class.priority.php b/include/class.priority.php
index 67bb0d8e3..ac6976d29 100644
--- a/include/class.priority.php
+++ b/include/class.priority.php
@@ -59,8 +59,7 @@ class Priority extends VerySimpleModel {
             $objects->filter(array('ispublic'=>1));
 
         foreach ($objects as $row) {
-            list($id, $name) = $row;
-            $priorities[$id] = $name;
+            $priorities[$row[0]] = $row[1];
         }
 
         return $priorities;
diff --git a/include/class.topic.php b/include/class.topic.php
index 181b7a107..36df233a1 100644
--- a/include/class.topic.php
+++ b/include/class.topic.php
@@ -40,6 +40,12 @@ class Topic extends VerySimpleModel {
                     'page_id' => 'Page.id',
                 ),
             ),
+            'priority' => array(
+                'null' => true,
+                'constraint' => array(
+                    'priority_id' => 'Priority.priority_id',
+                ),
+            ),
         ),
     );
 
@@ -114,9 +120,6 @@ class Topic extends VerySimpleModel {
     }
 
     function getPage() {
-        if(!$this->page && $this->getPageId())
-            $this->page = Page::lookup($this->getPageId());
-
         return $this->page;
     }
 
diff --git a/include/staff/helptopic.inc.php b/include/staff/helptopic.inc.php
index 73db1e022..21dd951f3 100644
--- a/include/staff/helptopic.inc.php
+++ b/include/staff/helptopic.inc.php
@@ -118,11 +118,11 @@ if ($info['form_id'] == Topic::FORM_USE_PARENT) echo 'selected="selected"';
                 <select name="dept_id">
                     <option value="0">&mdash; <?php echo __('System Default'); ?> &mdash;</option>
                     <?php
-                    $sql='SELECT dept_id,dept_name FROM '.DEPT_TABLE.' dept ORDER by dept_name';
-                    if(($res=db_query($sql)) && db_num_rows($res)){
-                        while(list($id,$name)=db_fetch_row($res)){
+                    if (($depts=Dept::getDepartments())) {
+                        foreach ($depts as $id => $name) {
                             $selected=($info['dept_id'] && $id==$info['dept_id'])?'selected="selected"':'';
-                            echo sprintf('<option value="%d" %s>%s</option>',$id,$selected,$name);
+                            echo sprintf('<option value="%d" %s>%s</option>',
+                                    $id, $selected, $name);
                         }
                     }
                     ?>
@@ -169,11 +169,10 @@ if ($info['form_id'] == Topic::FORM_USE_PARENT) echo 'selected="selected"';
                 <select name="priority_id">
                     <option value="">&mdash; <?php echo __('System Default'); ?> &mdash;</option>
                     <?php
-                    $sql='SELECT priority_id,priority_desc FROM '.PRIORITY_TABLE.' pri ORDER by priority_urgency DESC';
-                    if(($res=db_query($sql)) && db_num_rows($res)){
-                        while(list($id,$name)=db_fetch_row($res)){
+                    if (($priorities=Priority::getPriorities())) {
+                        foreach ($priorities as $id => $name) {
                             $selected=($info['priority_id'] && $id==$info['priority_id'])?'selected="selected"':'';
-                            echo sprintf('<option value="%d" %s>%s</option>',$id,$selected,$name);
+                            echo sprintf('<option value="%d" %s>%s</option>', $id, $selected, $name);
                         }
                     }
                     ?>
@@ -231,9 +230,9 @@ if ($info['form_id'] == Topic::FORM_USE_PARENT) echo 'selected="selected"';
                     <option value="0">&mdash; <?php echo __('Unassigned'); ?> &mdash;</option>
                     <?php
                     if (($users=Staff::getStaffMembers())) {
-                        echo sprintf('<OPTGROUP label="%s">', sprintf(__('Agents (%d)'), count($user)));
+                        echo sprintf('<OPTGROUP label="%s">',
+                                sprintf(__('Agents (%d)'), count($users)));
                         foreach ($users as $id => $name) {
-                            $name = new PersonsName($name);
                             $k="s$id";
                             $selected = ($info['assign']==$k || $info['staff_id']==$id)?'selected="selected"':'';
                             ?>
@@ -243,15 +242,12 @@ if ($info['form_id'] == Topic::FORM_USE_PARENT) echo 'selected="selected"';
                         }
                         echo '</OPTGROUP>';
                     }
-                    $sql='SELECT team_id, name, isenabled FROM '.TEAM_TABLE.' ORDER BY name';
-                    if(($res=db_query($sql)) && ($cteams = db_num_rows($res))) {
-                        echo sprintf('<OPTGROUP label="%s">', sprintf(__('Teams (%d)'), $cteams));
-                        while (list($id, $name, $isenabled) = db_fetch_row($res)){
+                    if (($teams=Team::getTeams())) {
+                        echo sprintf('<OPTGROUP label="%s">',
+                                sprintf(__('Teams (%d)'), count($teams)));
+                        foreach ($teams as $id => $name) {
                             $k="t$id";
-                            $selected = ($info['assign']==$k || $info['team_id']==$id)?'selected="selected"':'';
-
-                            if (!$isenabled)
-                                $name .= ' '.__('(disabled)');
+                            $selected = ($info['assign']==$k || $info['team_id']==$id) ? 'selected="selected"' : '';
                             ?>
                             <option value="<?php echo $k; ?>"<?php echo $selected; ?>><?php echo $name; ?></option>
                         <?php
diff --git a/include/staff/helptopics.inc.php b/include/staff/helptopics.inc.php
index 1c9d3fd64..681efbbe0 100644
--- a/include/staff/helptopics.inc.php
+++ b/include/staff/helptopics.inc.php
@@ -1,31 +1,14 @@
 <?php
-if(!defined('OSTADMININC') || !$thisstaff->isAdmin()) die('Access Denied');
+if (!defined('OSTADMININC') || !$thisstaff->isAdmin()) die('Access Denied');
 
-$sql='SELECT topic.* '
-    .', dept.dept_name as department '
-    .', priority_desc as priority '
-    .' FROM '.TOPIC_TABLE.' topic '
-    .' LEFT JOIN '.DEPT_TABLE.' dept ON (dept.dept_id=topic.dept_id) '
-    .' LEFT JOIN '.TICKET_PRIORITY_TABLE.' pri ON (pri.priority_id=topic.priority_id) ';
-$sql.=' WHERE 1';
-$order_by = '`sort`';
 
-$page=($_GET['p'] && is_numeric($_GET['p']))?$_GET['p']:1;
-//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=sprintf(_N('Showing %d help topic', 'Showing %d help topics', $num), $num);
-else
-    $showing=__('No help topics found!');
+$page = ($_GET['p'] && is_numeric($_GET['p'])) ? $_GET['p'] : 1;
+$count = Topic::objects()->count();
+$pageNav = new Pagenate($count, $page, PAGE_LIMIT);
+$pageNav->setURL('helptopics.php', $_qstr);
+$showing = $pageNav->showing().' '._N('help topic', 'help topics', $count);
 
-// Get the full names and filter for this page
-$topics = array();
-while ($row = db_fetch_array($res))
-    $topics[] = $row;
-
-foreach ($topics as &$t)
-    $t['name'] = Topic::getTopicName($t['topic_id']);
+$order_by = ($cfg->getTopicSortMode() == 'm') ? 'sort' : 'topic';
 
 ?>
 <div class="pull-left" style="width:700px;padding-top:5px;">
@@ -67,52 +50,67 @@ foreach ($topics as &$t)
     <tbody class="<?php if ($cfg->getTopicSortMode() == 'm') echo 'sortable-rows'; ?>"
         data-sort="sort-">
     <?php
-        $total=0;
-        $ids=($errors && is_array($_POST['ids']))?$_POST['ids']:null;
-        if (count($topics)):
+        $ids= ($errors && is_array($_POST['ids'])) ? $_POST['ids'] : null;
+        if ($count) {
+            $topics = Topic::objects()
+                ->order_by(sprintf('%s%s',
+                            strcasecmp($order, 'DESC') ? '' : '-',
+                            $order_by))
+                ->limit($pageNav->getLimit())
+                ->offset($pageNav->getStart());
+
             $defaultDept = $cfg->getDefaultDept();
             $defaultPriority = $cfg->getDefaultPriority();
             $sort = 0;
-            foreach($topics as $row) {
+            foreach($topics as $topic) {
+                $id = $topic->getId();
                 $sort++; // Track initial order for transition
                 $sel=false;
-                if($ids && in_array($row['topic_id'],$ids))
+                if ($ids && in_array($id, $ids))
                     $sel=true;
 
-                if (!$row['dept_id'] && $defaultDept) {
-                    $row['dept_id'] = $defaultDept->getId();
-                    $row['department'] = (string) $defaultDept;
+                if ($topic->dept_id) {
+                    $deptId = $topic->dept_id;
+                    $dept = (string) $topic->dept;
+                } elseif ($defaultDept) {
+                    $deptId = $defaultDept->getId();
+                    $dept = (string) $defaultDept;
+                } else {
+                    $deptId = 0;
+                    $dept = '';
                 }
-
-                if (!$row['priority'] && $defaultPriority)
-                    $row['priority'] = (string) $defaultPriority;
-
+                $priority = $team->priority ?: $defaultPriority;
                 ?>
-            <tr id="<?php echo $row['topic_id']; ?>">
+            <tr id="<?php echo $id; ?>">
                 <td width=7px>
-                  <input type="hidden" name="sort-<?php echo $row['topic_id']; ?>" value="<?php
-                        echo $row['sort'] ?: $sort; ?>"/>
-                  <input type="checkbox" class="ckb" name="ids[]" value="<?php echo $row['topic_id']; ?>"
-                            <?php echo $sel?'checked="checked"':''; ?>>
+                  <input type="hidden" name="sort-<?php echo $id; ?>" value="<?php
+                        echo $topic->sort ?: $sort; ?>"/>
+                  <input type="checkbox" class="ckb" name="ids[]"
+                    value="<?php echo $id; ?>" <?php
+                    echo $sel ? 'checked="checked"' : ''; ?>>
                 </td>
                 <td>
-<?php if ($cfg->getTopicSortMode() == 'm') { ?>
-                    <i class="icon-sort"></i>
-<?php } ?>
-<a href="helptopics.php?id=<?php echo $row['topic_id']; ?>"><?php echo $row['name']; ?></a>&nbsp;</td>
-                <td><?php echo $row['isactive']?__('Active'):'<b>'.__('Disabled').'</b>'; ?></td>
-                <td><?php echo $row['ispublic']?__('Public'):'<b>'.__('Private').'</b>'; ?></td>
-                <td><?php echo $row['priority']; ?></td>
-                <td><a href="departments.php?id=<?php echo $row['dept_id']; ?>"><?php echo $row['department']; ?></a></td>
-                <td>&nbsp;<?php echo Format::datetime($row['updated']); ?></td>
+                    <?php
+                    if ($cfg->getTopicSortMode() == 'm') { ?>
+                        <i class="icon-sort"></i>
+                    <?php } ?>
+                    <a href="helptopics.php?id=<?php echo $id; ?>"><?php
+                    echo Topic::getTopicName($id); ?></a>&nbsp;
+                </td>
+                <td><?php echo $topic->isactive ? __('Active') : '<b>'.__('Disabled').'</b>'; ?></td>
+                <td><?php echo $topic->ispublic ? __('Public') : '<b>'.__('Private').'</b>'; ?></td>
+                <td><?php echo $priority; ?></td>
+                <td><a href="departments.php?id=<?php echo $deptId;
+                ?>"><?php echo $dept; ?></a></td>
+                <td>&nbsp;<?php echo Format::datetime($team->updated); ?></td>
             </tr>
             <?php
-            } //end of while.
-        endif; ?>
+            } //end of foreach.
+        }?>
     <tfoot>
      <tr>
         <td colspan="7">
-            <?php if($res && $num){ ?>
+            <?php if ($count) { ?>
             <?php echo __('Select');?>:&nbsp;
             <a id="selectAll" href="#ckb"><?php echo __('All');?></a>&nbsp;&nbsp;
             <a id="selectNone" href="#ckb"><?php echo __('None');?></a>&nbsp;&nbsp;
@@ -125,7 +123,8 @@ foreach ($topics as &$t)
     </tfoot>
 </table>
 <?php
-if($res && $num): //Show options..
+if ($count): //Show options..
+     echo '<div>&nbsp;'.__('Page').':'.$pageNav->getPageLinks().'&nbsp;</div>';
 ?>
 <p class="centered" id="actions">
 <?php if ($cfg->getTopicSortMode() != 'a') { ?>
-- 
GitLab