3 * Copyright (c) Xerox Corporation, Codendi Team, 2001-2009. All rights reserved
5 * This file is a part of Codendi.
7 * Codendi is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Codendi is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Codendi. If not, see <http://www.gnu.org/licenses/>.
20 require_once('HudsonWidget.class.php');
21 require_once('common/widget/Widget.class.php');
22 require_once('PluginHudsonJobDao.class.php');
24 abstract class HudsonJobWidget extends HudsonWidget {
37 function create(&$request) {
39 $vId = new Valid_Uint('job_id');
40 $vId->setErrorMessage("Can't add empty job id");
42 if ($request->valid($vId)) {
43 $job_id = $request->get('job_id');
44 $sql = 'INSERT INTO plugin_hudson_widget (widget_name, owner_id, owner_type, job_id) VALUES ($1,$2,$3,$4)';
45 $res = db_query_params($sql,array($this->id,$this->owner_id,$this->owner_type,$job_id));
46 $content_id = db_insertid($res,'plugin_hudson_widget','id');
51 function destroy($id) {
52 $sql = 'DELETE FROM plugin_hudson_widget WHERE id = $1 AND owner_id = $2 AND owner_type = $3';
53 db_query_params($sql,array($id,$this->owner_id,$this->owner_type));
56 function getInstallPreferences() {
58 $prefs .= '<strong>'._("Monitored job:").'</strong><br />';
60 $jobs = $this->getAvailableJobs();
62 foreach ($jobs as $job_id => $job) {
63 $prefs .= '<input type="radio" name="job_id" value="'.$job_id.'"> '.$job->getName().'<br />';
66 $selected_jobs_id = $this->getSelectedJobsId();
67 $jobs = $this->getAvailableJobs();
69 $only_one_job = (count($jobs) == 1);
70 foreach ($jobs as $job_id => $job) {
71 $selected = ($only_one_job)?'checked="checked"':'';
72 $prefs .= '<input type="radio" name="' . $this->widget_id . '_job_id" value="'.$job_id.'" ' . $selected . '> ' . $job->getName() ;
73 if (in_array($job_id, $selected_jobs_id)) {
74 $prefs .= ' <em>('._('Already used') .')</em>';
80 function hasPreferences() {
83 function getPreferences() {
85 $prefs .= '<strong>'._("Monitored job:").'</strong><br />';
87 $jobs = $this->getAvailableJobs();
89 foreach ($jobs as $job_id => $job) {
90 $selected = ($job_id == $this->job_id)?'checked="checked"':'';
91 $prefs .= '<input type="radio" name="' . $this->id . '" value="'.$job_id.'" ' . $selected . '> '.$job->getName().'<br />';
96 function updatePreferences(&$request) {
97 $request->valid(new Valid_String('cancel'));
98 if (!$request->exist('cancel')) {
99 $job_id = $request->get($this->id);
100 $sql = "UPDATE plugin_hudson_widget SET job_id=$1 WHERE owner_id = $2 AND owner_type = $3 AND id = $4";
101 $res = db_query_params($sql,array($job_id,$this->owner_id,$this->owner_type,(int)$request->get('content_id')));
106 * Returns the jobs selected for this widget
108 function getSelectedJobsId() {
109 $sql = "SELECT * FROM plugin_hudson_widget WHERE widget_name='" . $this->widget_id . "' AND owner_id = ". $this->owner_id ." AND owner_type = '". $this->owner_type ."'";
110 $res = db_query($sql);
112 $selected_jobs_id = array();
113 while ($data = db_fetch_array($res)) {
114 $selected_jobs_id[] = $data['job_id'];
116 return $selected_jobs_id;