root/trunk/controllers/comments.php @ 112

Revision 112, 5.0 KB (checked in by divagater, 5 years ago)

Various updates

Line 
1<?php
2/**
3 * Comments controller class
4 *
5 * This is the access point to the npc_comments table.
6 *
7 * @filesource
8 * @author              Billy Gunn <billy@gunn.org>
9 * @copyright           Copyright (c) 2007
10 * @link                http://trac2.assembla.com/npc
11 * @package             npc
12 * @subpackage          npc.controllers
13 * @since               NPC 2.0
14 * @version             $Id: $
15 */
16
17/**
18 * Comments controller class
19 *
20 * Comments controller provides functionality, such as building the
21 * Doctrine querys and formatting output.
22 *
23 * @package     npc
24 * @subpackage  npc.controllers
25 */
26class NpcCommentsController extends Controller {
27
28    /**
29     * getAck
30     *
31     * An accessor method to retrieve an acknowlegement
32     *
33     * @return string   The acknowledgement
34     */
35    function getAck($id) {
36        $where = 'c.entry_type = 4';
37        $ack = $this->comments($id, $where);
38        return($ack[0]['author_name'] . "*|*" . $ack[0]['comment_data']);
39    }
40
41    /**
42     * getLastComment
43     *
44     * An accessor method to retrieve the latest comment
45     * that is not an aknowledgement.
46     *
47     * @return string   The comment
48     */
49    function getLastComment($id) {
50        $where = 'c.entry_type != 4';
51        $comment = $this->comments($id, $where);
52        if (isset($comment[0])) {
53            return($comment[0]['author_name'] . "*|*" . $comment[0]['comment_data']);
54        }
55
56        return(0);
57    }
58
59    /**
60     * getComments
61     *
62     * An accessor method to return comments
63     *
64     * @return string   json output
65     */
66    function getComments() {
67        return($this->jsonOutput($this->comments()));
68    }
69
70    /**
71     * getHostComments
72     *
73     * An accessor method to return all host comments
74     *
75     * @return string   json output
76     */
77    function getHostComments() {
78        $results = $this->flattenArray($this->comments(null, 'o.objecttype_id = 1'));
79        return($this->jsonOutput($results));
80    }
81
82    /**
83     * getServiceComments
84     *
85     * An accessor method to return all service comments
86     *
87     * @return string   json output
88     */
89    function getServiceComments() {
90        $results = $this->flattenArray($this->comments(null, 'o.objecttype_id = 2'));
91        return($this->jsonOutput($results));
92    }
93
94    /**
95     * deleteAllHostComments
96     *
97     * Delete all host comments.
98     *
99     * @return string   json output
100     */
101    function deleteAllHostComments() {
102
103        require_once("plugins/npc/controllers/nagios.php");
104
105        $seen = array();
106
107        $results = $this->flattenArray($this->comments(null, 'o.objecttype_id = 1'));
108       
109        for ($i = 0; $i < count($results); $i++) {
110            $host = $results[$i]['host_name'];
111            if (!isset($seen[$host])) {
112                $params = array(
113                    'command' => 'DEL_ALL_HOST_COMMENTS',
114                    'host_name' => $host
115                );
116                NpcNagiosController::command($params);
117                $seen[$host] = 1;
118            }   
119        }
120    }
121
122    /**
123     * deleteAllServiceComments
124     *
125     * Delete all service comments.
126     *
127     * @return string   json output
128     */
129    function deleteAllServiceComments() {
130
131        require_once("plugins/npc/controllers/nagios.php");
132
133        $seen = array();
134
135        $results = $this->flattenArray($this->comments(null, 'o.objecttype_id = 2'));
136       
137        for ($i = 0; $i < count($results); $i++) {
138            $host = $results[$i]['host_name'];
139            $service = $results[$i]['service_description'];
140            if (!isset($seen[$host][$service])) {
141                $params = array(
142                    'command' => 'DEL_ALL_SVC_COMMENTS',
143                    'host_name' => $host,
144                    'service_description' => $service,
145                );
146                NpcNagiosController::command($params);
147                $seen[$host][$service] = 1;
148            }   
149        }
150    }
151
152    /**
153     * comments
154     *
155     * Returns a an array of comments
156     *
157     * @return array  The comments
158     */
159    function comments($id=null, $where='') {
160
161        if ($this->id || $id) {
162            if ($where != '') {
163                $where .= ' AND ';
164            }
165            $where .= sprintf("c.object_id = %d", is_null($id) ? $this->id : $id);
166        }
167
168        $q = new Doctrine_Pager(
169            Doctrine_Query::create()
170                ->select('i.instance_name,'
171                        .'o.name1 AS host_name,'
172                        .'o.name2 AS service_description,'
173                        .'c.*')
174                ->from('NpcComments c')
175                ->leftJoin('c.Object o')
176                ->leftJoin('c.Instance i')
177                ->where("$where")
178                ->orderby( 'c.entry_time DESC, c.entry_time_usec DESC' ),
179            $this->currentPage,
180            $this->limit
181        );
182
183        $results = $q->execute(array(), Doctrine::FETCH_ARRAY);
184
185        // Set the total number of records
186        $this->numRecords = $q->getNumResults();
187
188        return($results);
189    }
190
191}
Note: See TracBrowser for help on using the browser.