objectManager: Fix lower-case letter in 'email Text'
[NewAppDB.git] / include / monitor.php
blobe955454a5f274ec346f7b3303ee2e0bef129cba2
1 <?php
2 /***************************************/
3 /* Monitor class and related functions */
4 /***************************************/
7 /**
8 * Monitor class for handling Monitors
9 */
10 class Monitor {
11 var $iMonitorId;
13 // variables necessary for creating a monitor
14 var $iAppId;
15 var $iVersionId;
16 var $iUserId;
18 /**
19 * Constructor.
20 * If $iMonitorId is provided, fetches Monitor.
22 function Monitor($iMonitorId="", $oRow = null)
24 if(!$iMonitorId && !$oRow)
25 return;
27 if(!$oRow)
29 $sQuery = "SELECT *
30 FROM appMonitors
31 WHERE monitorId = '".$iMonitorId."'";
32 $hResult = query_appdb($sQuery);
33 $oRow = query_fetch_object($hResult);
36 if($oRow)
38 $this->iMonitorId = $oRow->monitorId;
39 $this->iAppId = $oRow->appId;
40 $this->iVersionId = $oRow->versionId;
41 $this->iUserId = $oRow->userId;
45 function find($iUserId, $iVersionId=0)
47 if($iUserId && $iVersionId)
49 $sQuery = "SELECT *
50 FROM appMonitors
51 WHERE userId = '".$iUserId."'
52 AND versionId = '".$iVersionId."'";
53 $hResult = query_appdb($sQuery);
54 if( $oRow = query_fetch_object($hResult) )
56 $this->iMonitorId = $oRow->monitorId;
57 $this->iAppId = $oRow->appId;
58 $this->iVersionId = $oRow->versionId;
59 $this->iUserId = $oRow->userId;
64 function objectGetChildren($bIncludeDeleted = false)
66 /* We have none */
67 return array();
71 * Creates a new Monitor.
72 * Informs interested people about the creation.
73 * Returns true on success, false on failure
75 function create()
77 /* Check for duplicate entries */
78 $oMonitor = new monitor();
79 $this->iUserId = $_SESSION['current']->iUserId;
80 $oMonitor->find($this->iUserId, $this->iVersionId);
81 if($oMonitor->iVersionId)
82 return FALSE;
84 // create the new monitor entry
85 $hResult = query_parameters("INSERT INTO appMonitors (versionId, appId,".
86 "submitTime, userId) ".
87 "VALUES ('?', '?', ?, '?')",
88 $this->iVersionId, $this->iAppId,
89 "NOW()", $this->iUserId);
91 if($hResult)
93 $this->Monitor(query_appdb_insert_id());
94 $sWhatChanged = "New monitor\n\n";
95 $this->SendNotificationMail("add", $sWhatChanged);
96 return true;
97 } else
99 addmsg("Error while creating a new Monitor.", "red");
100 return false;
104 function update()
106 /* Stub */
107 return TRUE;
110 function unQueue()
112 return true; // We don't queue monitors
115 function objectGetSubmitterId()
117 return $this->iUserId;
120 function objectGetMailOptions($sAction, $bMailSubmitter, $bParentAction)
122 return new mailOptions();
125 function objectGetMail($sAction, $bMailSubmitter, $bParentAction)
127 $sSubject = null;
128 $sMsg = null;
130 if($this->iVersionId)
132 $sWhat = "version";
133 $sName = version::fullName($this->iVersionId);
134 $oVersion = new version($this->iVersionId);
135 $sUrl = $oVersion->objectMakeUrl();
136 } else
138 $sWhat = "application";
139 $oApp = new application($this->iAppId);
140 $sName = $oApp->sName;
141 $sUrl = $oApp->objectMakeUrl();
144 if($bMailSubmitter)
146 switch($sAction)
148 case "delete":
149 if($bParentActino)
151 $sSubject = "Monitored $sWhat deleted";
152 $sMsg = "The $sWhat $sName which you monitored has been ".
153 "deleted by ".$_SESSION['current']->iUserId.".";
155 break;
157 $aMailTo = null;
158 } else
160 $oUser = new user($this->iUserId);
161 $sUser = $oUser->sName;
162 switch($sAction)
164 case "delete":
165 if(!$bParentAction)
167 $sSubject = "Monitor for $sName removed: $sUser";
168 $sMsg = $sUrl;
170 break;
172 User::get_notify_email_address_list(null, $this->iVersionId);
174 return array($sSubject, $sMsg, $aMailTo);
177 function purge()
179 return $this->delete();
183 * Removes the current Monitor from the database.
184 * Informs interested people about the deletion.
186 function delete()
188 $hResult = query_parameters("DELETE FROM appMonitors WHERE monitorId = '?'", $this->iMonitorId);
190 if(!$hResult)
191 return FALSE;
193 return TRUE;
196 function SendNotificationMail($sAction="add",$sMsg=null)
198 /* Set variables depending on whether it is an application or version monitor */
199 if(isset($this->iVersionId))
201 $oVersion = new version($this->iVersionId);
202 $sAppName = version::fullName($this->iVersionId);
203 $sUrl = $oVersion->objectMakeUrl();
204 $sVersion = " version";
205 } else
207 $oApp = new application($this->iAppId);
208 $sAppName = Application::lookup_name($this->iAppId);
209 $sUrl = $oApp->objectMakeUrl();
212 switch($sAction)
214 case "add":
215 $sSubject = "Monitor for ".$sAppName;
216 $sSubject .= " added: ".$_SESSION['current']->sRealname;
217 $sMsg .= "$sUrl\n";
218 addmsg("You will now receive an email whenever changes are made ".
219 "to this application$sVersion.", "green");
220 break;
221 case "delete":
222 $sSubject = "Monitor for ".$sAppName;
223 $sSubject .= " removed: ".$_SESSION['current']->sRealname;
224 $sMsg .= "$sUrl\n";
225 addmsg("You will no longer receive an email whenever changes ".
226 "are made to this application$sVersion.", "green");
227 break;
229 $sEmail = User::get_notify_email_address_list(null, $this->iVersionId);
230 if($sEmail)
231 mail_appdb($sEmail, $sSubject ,$sMsg);
234 function objectGetState()
236 // We don't queue monitors
237 return 'accepted';
240 function canEdit()
242 if($_SESSION['current']->hasPriv("admin") ||
243 ($this->iUserId == $_SESSION['current']->iUserId))
244 return TRUE;
246 return FALSE;
249 function mustBeQueued()
251 return FALSE;
254 /* Stub */
255 function display()
257 return "";
260 /* Stub */
261 function getOutputEditorValues($aClean)
263 $this->iVersionId = $aClean['iVersionId'];
264 $this->iAppId = $aClean['iAppId'];
267 /* Stub */
268 function objectGetHeader()
270 return null;
273 function objectGetId()
275 return $this->iMonitorId;
278 /* Stub */
279 function objectGetTableRow()
281 return null;
284 /* Stub */
285 function objectMakeLink()
287 return "";
290 /* Stub */
291 function objectMakeUrl()
293 return "";
296 /* Stub */
297 function outputEditor()
299 return "";
302 function objectGetEntries($sState, $iRows = 0, $iStart = 0, $sOrderBy = '', $bAscending = true)
304 $hResult = query_parameters("SELECT * FROM appMonitors");
306 if(!$hResult)
307 return FALSE;
309 return $hResult;
312 function objectGetEntriesCount($sState)
314 $hResult = query_parameters("SELECT COUNT(DISTINCT monitorId) as count
315 FROM appMonitors");
317 if(!$hResult)
318 return FALSE;
320 $oRow = query_fetch_object($hResult);
322 if(!$oRow)
323 return FALSE;
325 return $oRow->count;
328 function allowAnonymousSubmissions()
330 /* That makes no sense */
331 return FALSE;
334 /* Retrieve the user's monitored versions */
335 function getVersionsMonitored($oUser)
337 $hResult = query_parameters("SELECT appId, versionId FROM appMonitors WHERE userId = '?'", $oUser->iUserId);
339 if(!$hResult || query_num_rows($hResult) == 0)
340 return NULL;
342 $aVersionsMonitored = array();
344 for($i = 0; $oRow = query_fetch_object($hResult); $i++)
345 $aVersionsMonitored[$i] = array($oRow->appId, $oRow->versionId);
347 return $aVersionsMonitored;