1<?php
2
3// Sample code for project-specific preferences.
4// These prefs may include:
5//
6// - preferences for your application graphics
7// - application selection
8//  (i.e. if you have multiple apps, let user choose among them)
9//
10// Edit this file accordingly,
11// and put your version in html/project_specific/project_specific_prefs.inc
12
13// Project-specific prefs are represented in three ways:
14// - as an XML document (stored in the DB in this form)
15// - as a PHP structure
16// - as a set of HTTP GET form variables
17
18// This file exports the following functions
19// (called from html/inc/prefs_project.inc):
20//
21// project_specific_prefs_default()
22//      Returns XML for default preferences
23// project_specific_prefs_parse($xml)
24//      Parse prefs as XML, return prefs as PHP structure
25// project_specific_prefs_show($prefs, $columns=false)
26//      Show prefs as HTML (non-editable)
27// project_specific_prefs_edit($prefs, $error)
28//      Show prefs as HTML, editable.
29//      $error is a struct indicating which values were erroneous
30//      (value X is erroneous if $error->X is set)
31// project_specific_prefs_parse_form(&$error)
32//      Parse form variables into XML, and return it.
33//      Also error-check values, and return errors in $errors->*
34
35function option($name, $val) {
36    $x = ($name == $val) ?  "selected" : "";
37    return "<option name='$name' $x>$name\n";
38}
39
40define('COLOR_DESC', tra('Color scheme for graphics'));
41// xgettext:no-php-format
42define("MAX_GFX_CPU_PCT_DESC", tra("Maximum CPU % for graphics%10 ... 100%2", "<br><small>", "</small>"));
43define('APP_SELECT_DESC', tra('Run only the selected applications'));
44define('APP_SELECT_TOOLTIP', tra('Only get tasks for certain applications. Useful to focus on particular applications, or to exclude them.'));
45define('ACCEPT_ANY_DESC', tra('If no work for selected applications is available, accept work from other applications?'));
46define('NON_GRAPHICAL_DESC', tra("Use faster non-graphical applications if available?"));
47
48// stuff related to app filtering.
49// Note: in this implementation, if a user selects all apps,
50// no <app_id> elements are included in their prefs,
51// which means that if the project adds a new app such users will run it also.
52//
53if (APP_SELECT_PREFS) {
54    $app_array = array();
55    $apps = BoincApp::enum("deprecated=0");
56    foreach($apps as $app) {
57        $app_array[] = array($app->id, $app->user_friendly_name);
58    }
59} else {
60    $app_array = null;
61}
62
63function selected_app_text($prefs) {
64    global $app_array;
65    if (isset($prefs->app_ids)) {
66        $x = "";
67        foreach ($app_array as $app) {
68            $app_id = $app[0];
69            $app_name = $app[1];
70            if (in_array($app_id, $prefs->app_ids)) {
71                $x .= "$app_name: ".tra("yes")."<br>";
72            } else {
73                $x .= "$app_name: ".tra("no")."<br>";
74            }
75        }
76    } else {
77        $x = tra("(all applications)");
78    }
79    return $x;
80}
81
82function project_specific_prefs_default() {
83    $x = "";
84    if (COLOR_PREFS) {
85        $x .= "<color_scheme>Tahiti Sunset</color_scheme>\n";
86    }
87    if (GFX_CPU_PREFS) {
88        $x .= "<max_gfx_cpu_pct>20</max_gfx_cpu_pct>\n";
89    }
90    return $x;
91}
92
93function number_select($name, $max, $n) {
94    $sel = ($n == 0) ? "selected":"";
95    $x = "<select class=\"form-control input-sm\" name=$name> <option value=0 $sel>".tra("No limit")."</option>";
96    for ($i=1; $i<=$max; $i++) {
97        $sel = ($n == $i) ? "selected":"";
98        $x .= "<option value=$i $sel>$i</option>";
99    }
100    $x .= "</select>";
101    return $x;
102}
103
104function limit_string($val) {
105    if ($val) return $val;
106    return tra("No limit");
107}
108
109function project_specific_prefs_edit($prefs, $error=false) {
110    global $app_array;
111    if (COLOR_PREFS) {
112        $x = $prefs->color_scheme;
113        $y = "<select class=\"form-control input-sm\" name=color_scheme>
114                ".option("Tahiti Sunset", $x)
115                .option("Desert Sands", $x)."
116            </select>
117        ";
118        row2(COLOR_DESC, $y);
119    }
120
121    if (GFX_CPU_PREFS) {
122        $y = "<input size=5 name=max_gfx_cpu_pct value='$prefs->max_gfx_cpu_pct'>";
123        row2(MAX_GFX_CPU_PCT_DESC, $y, isset($error->max_gfx_cpu_pct));
124    }
125    if (APP_SELECT_PREFS) {
126        $x = "";
127        foreach ($app_array as $app) {
128            $app_id = $app[0];
129            $app_name = $app[1];
130            if (isset($prefs->app_ids)) {
131                $present = in_array($app_id, $prefs->app_ids);
132            } else {
133                $present = true;
134            }
135            $checked = $present?"checked":"";
136            $x .= "<input type=checkbox name=app_id_$app_id $checked> $app_name<br>";
137        }
138        tooltip_row2(APP_SELECT_TOOLTIP, APP_SELECT_DESC, $x);
139        $checked = $prefs->allow_non_preferred_apps?"checked":"";
140        row2(ACCEPT_ANY_DESC, "<input type=checkbox name=allow_non_preferred_apps $checked>");
141    }
142    if (NON_GRAPHICAL_PREF) {
143        $checked = $prefs->non_graphical?"checked":"";
144        row2(
145            NON_GRAPHICAL_DESC,
146            "<input type=checkbox name=non_graphical $checked>"
147        );
148    }
149    if (MAX_JOBS_PREF) {
150        $n = $prefs->max_jobs;
151        row2(
152            tra("Max # of jobs for this project"),
153            number_select("max_jobs", 8, $n)
154        );
155    }
156    if (MAX_CPUS_PREF) {
157        $n = $prefs->max_cpus;
158        row2(
159            tra("Max # of CPUs for this project"),
160            number_select("max_cpus", 8, $n)
161        );
162    }
163}
164
165function project_specific_prefs_parse_form(&$error) {
166    global $app_array;
167    $x = "";
168    if (COLOR_PREFS) {
169        $color_scheme = sanitize_tags($_GET["color_scheme"]);
170        $x .= "<color_scheme>$color_scheme</color_scheme>\n";
171    }
172    if (GFX_CPU_PREFS) {
173        $max_gfx_cpu_pct = get_str("max_gfx_cpu_pct");
174        if (!verify_numeric($max_gfx_cpu_pct, 0, 100)) {
175            $error->max_gfx_cpu_pct = true;
176        }
177        $x .= "<max_gfx_cpu_pct>$max_gfx_cpu_pct</max_gfx_cpu_pct>\n";
178    }
179
180    if (APP_SELECT_PREFS) {
181        $y = "<apps_selected>\n";
182        $some_unchecked = false;
183        foreach ($app_array as $app) {
184            $app_id = $app[0];
185            $present = isset($_GET["app_id_$app_id"]);
186            if ($present) {
187                $y .= "<app_id>$app_id</app_id>\n";
188            } else {
189                $some_unchecked = true;
190            }
191        }
192        $y .= "</apps_selected>\n";
193
194        if ($some_unchecked) {
195            $x .= $y;
196        }
197        if (isset($_GET["allow_non_preferred_apps"])) {
198            $x .= "<allow_non_preferred_apps>1</allow_non_preferred_apps>\n";
199        }
200    }
201
202    if (NON_GRAPHICAL_PREF) {
203        if (isset($_GET["non_graphical"])) {
204            $x .= "<non_graphical>1</non_graphical>\n";
205        }
206    }
207    if (MAX_JOBS_PREF) {
208        $val = $_GET["max_jobs"];
209        $x .= "<max_jobs>$val</max_jobs>\n";
210    }
211    if (MAX_CPUS_PREF) {
212        $val = $_GET["max_cpus"];
213        $x .= "<max_cpus>$val</max_cpus>\n";
214    }
215
216    return $x;
217}
218
219function project_specific_prefs_show($prefs, $columns=false) {
220    global $app_array;
221    if ($columns) {
222        if (COLOR_PREFS) {
223            row_defs(COLOR_DESC,"color_scheme", "", "", $prefs);
224        }
225        if (GFX_CPU_PREFS) {
226            row_defs(MAX_GFX_CPU_PCT_DESC, "max_gfx_cpu_pct", "", "", $prefs);
227        }
228        if (APP_SELECT_PREFS) {
229            $prefs->app_id_text = selected_app_text($prefs);
230            if ($prefs->home) $prefs->home->app_id_text = selected_app_text($prefs->home);
231            if ($prefs->school) $prefs->school->app_id_text = selected_app_text($prefs->school);
232            if ($prefs->work) $prefs->work->app_id_text = selected_app_text($prefs->work);
233            row_defs(APP_SELECT_DESC, "app_id_text", "", "", $prefs, APP_SELECT_TOOLTIP);
234            row_defs(ACCEPT_ANY_DESC, "allow_non_preferred_apps_text", "", "", $prefs);
235        }
236        if (NON_GRAPHICAL_PREF) {
237            row_defs(NON_GRAPHICAL_DESC, "non_graphical", "", "yesno", $prefs);
238        }
239        if (MAX_JOBS_PREF) {
240            row_defs(tra("Max # jobs"), "max_jobs", "", "minutes", $prefs);
241        }
242        if (MAX_CPUS_PREF) {
243            row_defs(tra("Max # CPUs"), "max_cpus", "", "minutes", $prefs);
244        }
245    } else {
246        if (COLOR_PREFS) {
247            row2(COLOR_DESC, $prefs->color_scheme);
248        }
249        if (GFX_CPU_PREFS) {
250            row2(MAX_GFX_CPU_PCT_DESC, $prefs->max_gfx_cpu_pct);
251        }
252        if (APP_SELECT_PREFS) {
253            tooltip_row2(APP_SELECT_TOOLTIP, APP_SELECT_DESC, selected_app_text($prefs));
254            row2(ACCEPT_ANY_DESC, $prefs->allow_non_preferred_apps_text);
255        }
256        if (NON_GRAPHICAL_PREF) {
257            row2(NON_GRAPHICAL_DESC, $prefs->non_graphical?tra("yes"):tra("no"));
258        }
259        if (MAX_JOBS_PREF) {
260            row2(tra("Max # jobs"), limit_string($prefs->max_jobs));
261        }
262        if (MAX_CPUS_PREF) {
263            row2(tra("Max # CPUs"), limit_string($prefs->max_cpus));
264        }
265    }
266}
267
268function project_specific_prefs_parse($prefs_xml) {
269    $prefs = new StdClass;
270    if (COLOR_PREFS) {
271        $prefs->color_scheme = parse_element($prefs_xml, "<color_scheme>");
272    }
273    if (GFX_CPU_PREFS) {
274        $prefs->max_gfx_cpu_pct = parse_element($prefs_xml, "<max_gfx_cpu_pct>");
275    }
276    if (APP_SELECT_PREFS) {
277        $cursor = 0;
278        while ($thisxml = parse_next_element($prefs_xml, "<app_id>", $cursor)) {
279            if (is_numeric($thisxml)) {
280                $n = (int) $thisxml;
281                $prefs->app_ids[] = $n;
282            }
283        }
284        $prefs->allow_non_preferred_apps = parse_element($prefs_xml, "<allow_non_preferred_apps>");
285        $prefs->allow_non_preferred_apps_text = $prefs->allow_non_preferred_apps?"yes":"no";
286    }
287    if (NON_GRAPHICAL_PREF) {
288        $prefs->non_graphical = parse_bool($prefs_xml, "non_graphical");
289    }
290    if (MAX_JOBS_PREF) {
291        $prefs->max_jobs = parse_element($prefs_xml, "<max_jobs>");
292    }
293    if (MAX_CPUS_PREF) {
294        $prefs->max_cpus = parse_element($prefs_xml, "<max_cpus>");
295    }
296    return $prefs;
297}
298
299$cvs_version_tracker[]="\$Id$";  //Generated automatically - do not edit
300
301?>
302