3 # Copyright (C) 2008 Jason Woofenden
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 require_once('code/tasks.php');
20 function index_main() {
28 tem_show('main_body');
31 # pass multiple argumens for where-clause and printf-args just like db_get_rows()
32 function task_summary($tem_prefix, $where_clause/*, ... */) {
33 $args = func_get_args();
34 $args = array_slice($args, 1);
35 array_unshift($args, 'tasks', 'id,price,title,state,client_id,paid');
36 $rows = call_user_func_array('db_get_rows', $args);
39 $num_rows = count($rows);
41 tem_show($tem_prefix . '_plural');
43 tem_show($tem_prefix . '_singular');
46 foreach($rows as $row) {
47 list($id, $price, $title, $state, $client_id, $paid) = $row;
48 tem_set('task_id', $id);
49 tem_set('task_title', $title);
50 tem_set('task_price', $price);
51 tem_set('task_state', task_state_pretty($state));
52 if(logged_in_as_contractor()) {
53 tem_set('client', db_get_value('people', 'username', 'where id=%i', $client_id));
54 if($tem_prefix == 'queue') {
55 tem_show('queued_for');
58 if($num_rows > 1 && $tem_prefix == 'queue') {
59 tem_show('not_the_only_queued');
61 tem_show($tem_prefix . '_row');
62 if(!$paid && isset($GLOBALS['unpaid_totals'])) {
63 if(!isset($GLOBALS['unpaid_totals'][$client_id])) {
64 $GLOBALS['unpaid_totals'][$client_id] = array();
66 if(!isset($GLOBALS['unpaid_totals'][$client_id][$tem_prefix])) {
67 $GLOBALS['unpaid_totals'][$client_id][$tem_prefix] = 0;
69 $GLOBALS['unpaid_totals'][$client_id][$tem_prefix] += $price;
74 tem_set('task_total', $total);
76 tem_show($tem_prefix);
82 # pass multi-dimensional hash arr[client_id][status].
83 # status strings: 'finished_untested', 'finished_unpaid', 'queue'
84 function totals_summary($arr) {
86 $total_finished_untested = 0;
87 $total_finished_unpaid = 0;
90 foreach($arr as $client_id => $t) {
91 $finished_untested = 0;
95 if(isset($t['finished_untested'])) {
96 $finished_untested = $t['finished_untested'];
98 if(isset($t['finished_unpaid'])) {
99 $finished_unpaid = $t['finished_unpaid'];
101 if(isset($t['queue'])) {
102 $queue = $t['queue'];
104 if(isset($t['do_finish'])) {
105 $working = $t['do_finish'];
107 tem_set('tested_total', $finished_unpaid);
108 tem_set('done_total', $finished_unpaid + $finished_untested);
109 tem_set('not_tested_total', $finished_untested);
110 tem_set('queued_total', $queue + $working);
111 tem_set('client', db_get_value('people', 'username', 'where id=%i', $client_id));
112 tem_show('unpaid_totals_row');
113 $total_finished_untested += $finished_untested;
114 $total_finished_unpaid += $finished_unpaid;
115 $total_queue += $queue;
116 $total_working += $working;
119 tem_set('tested_total', $total_finished_unpaid);
120 tem_set('done_total', $total_finished_unpaid + $total_finished_untested);
121 tem_set('not_tested_total', $total_finished_untested);
122 tem_set('queued_total', $total_queue + $total_working);
123 tem_set('client', 'all');
124 tem_show('unpaid_totals_row');
126 tem_show('unpaid_totals');
130 function _index_main() {
131 $client_id = logged_in();
132 $ever_was_contractor = ever_was_contractor();
134 $contractor_full_name = db_get_value('people', 'name', 'where id=1');
135 if(!$contractor_full_name) {
136 die('To finish installing this site, make yourself an account in the people table with id=1');
138 list($contractor_name,) = explode(' ', $contractor_full_name, 2);
139 tem_set('contractor_name', $contractor_name);
140 tem_set('contractor_full_name', $contractor_full_name);
143 # make sure the client (not the contractor) has filled out the tiny agreement
144 if(!$ever_was_contractor) {
145 $tiny_agreement = db_get_value('people', 'tiny_agreement', 'where id=%i', $client_id);
146 if($tiny_agreement < 30 || $tiny_agreement > 999) {
147 return './tiny_agreement';
151 if($ever_was_contractor) {
155 if(logged_in_as_contractor()) {
156 tem_show('contractor_links');
157 tem_show('contractor_todo');
159 $GLOBALS['unpaid_totals'] = array();
160 task_summary('do_finish', 'where state=%i order by id desc', TASK_WORKING); # this affects queued_total
161 task_summary('do_fix', 'where state=%i order by client_id desc', TASK_BUG);
162 task_summary('do_price', 'where state=%i order by client_id desc', TASK_NEEDS_QUOTE);
164 task_summary('finished_untested', 'where state=%i order by client_id, finished_at desc', TASK_NEEDS_TESTING);
165 task_summary('finished_unpaid', 'where state=%i && paid=0 order by finished_at desc', TASK_FINISHED);
166 task_summary('queue', 'where state=%i order by client_id, ord desc', TASK_QUEUED);
167 totals_summary($GLOBALS['unpaid_totals']);
168 task_summary('finished_paid', 'where state=%i && paid = 1 order by finished_at desc limit 20', TASK_FINISHED);
170 tem_show('not_contractor_links');
172 $num_rows += task_summary('do_approve_price', 'where client_id=%i && state=%i order by id', $client_id, TASK_NEEDS_GO_AHEAD);
173 $num_rows += task_summary('do_clarify', 'where client_id=%i && state=%i order by id', $client_id, TASK_NEEDS_CLARIFICATION);
174 $num_rows += task_summary('do_test', 'where client_id=%i && state=%i order by id', $client_id, TASK_NEEDS_TESTING);
175 $num_rows += task_summary('do_draft', 'where client_id=%i && state=%i order by id', $client_id, TASK_DRAFT);
176 $num_rows += task_summary('do_on_hold', 'where client_id=%i && state=%i order by id', $client_id, TASK_ON_HOLD);
179 tem_show('nothing_needs_your_attention');
180 } elseif($num_rows == 1) {
181 tem_show('this_needs_your_attention');
183 tem_show('these_need_your_attention');
186 task_summary('queue', 'where client_id=%i && state=%i order by ord desc', $client_id, TASK_QUEUED);
188 task_summary('contractor_will', 'where client_id=%i && (state=%i || state=%i || state=%i) order by id desc', $client_id, TASK_NEEDS_QUOTE, TASK_BUG, TASK_WORKING);
190 task_summary('finished_unpaid', 'where client_id=%i && state=%i && paid=0 order by finished_at desc', $client_id, TASK_FINISHED);
191 task_summary('finished_paid', 'where client_id=%i && state=%i && paid=1 order by finished_at desc', $client_id, TASK_FINISHED);