+define('MAX_PRIORITY', 2000000000);
+define('MIN_PRIORITY', 0);
+define('MID_PRIORITY', floor((MAX_PRIORITY - MIN_PRIORITY) / 2));
+
+function new_lowest_priority($client_id) {
+ $lowest_ord = db_get_value('tasks', 'ord', 'where client_id=%i && state=%i order by ord asc limit 1', $client_id, TASK_QUEUED);
+ if($lowest_ord === false) {
+ return MID_PRIORITY;
+ }
+ if($lowest_ord == MIN_PRIORITY) {
+ reprioritize_tasks($client_id); # make room
+ $lowest_ord = db_get_value('tasks', 'ord', 'where client_id=%i && state=%i order by ord asc limit 1', $client_id, TASK_QUEUED);
+ }
+ return MIN_PRIORITY + floor(($lowest_ord - MIN_PRIORITY) / 2);
+}
+
+# keep everything in the same order, but space them out so there's room to squeeze things in anywhere
+function reprioritize_tasks($client_id) {
+ $ids = db_get_column('tasks', 'id', 'where client_id=%i && state=%i order by ord desc, id desc', $client_id, TASK_QUEUED);
+ $step = floor((MAX_PRIORITY - MIN_PRIORITY) / (count($ids) + 1));
+ $cur = MAX_PRIORITY;
+ foreach($ids as $id) {
+ $cur -= $step;
+ db_update('tasks', 'ord', $cur, 'where id=%i', $id);
+ }
+}
+
+
+# pass the task id and one of (up,down,top,bottom)
+function prioritize_task($id, $change) {
+ $row = db_get_row('tasks', 'client_id,ord', 'where id=%i', $id);
+ if(!$row) {
+ message('Database error #2242');
+ return;
+ }
+ list($client_id, $ord) = $row;
+ switch($change) {
+ case 'top':
+ list($highest_id, $highest_ord) = db_get_row('tasks', 'id,ord', 'where client_id=%i && state=%i order by ord desc limit 1', $client_id, TASK_QUEUED);
+ if($highest_id == $id) {
+ message('Already highest priority');
+ return;
+ }
+
+ if($highest_ord == MAX_PRIORITY) {
+ reprioritize_tasks($client_id); # make room
+ $highest_ord = db_get_value('tasks', 'ord', 'where client_id=%i && state=%i order by ord desc limit 1', $client_id, TASK_QUEUED);
+ }
+
+ $new_ord = MAX_PRIORITY - floor((MAX_PRIORITY - $highest_ord) / 2);
+ db_update('tasks', 'ord', $new_ord, 'where id=%i', $id);
+ return;
+ case 'bottom':
+ list($lowest_id, $lowest_ord) = db_get_row('tasks', 'id,ord', 'where client_id=%i && state=%i order by ord asc limit 1', $client_id, TASK_QUEUED);
+ if($lowest_id == $id) {
+ message('Already lowest priority');
+ return $lowest_ord;
+ }
+
+ if($lowest_ord == MIN_PRIORITY) {
+ reprioritize_tasks($client_id); # make room
+ $lowest_ord = db_get_value('tasks', 'ord', 'where client_id=%i && state=%i order by ord asc limit 1', $client_id, TASK_QUEUED);
+ }
+
+ $new_ord = MIN_PRIORITY + floor(($lowest_ord - MIN_PRIORITY) / 2);
+ db_update('tasks', 'ord', $new_ord, 'where id=%i', $id);
+ return;
+ case 'up':
+ case 'down':
+ if($change == 'up') {
+ $rows = db_get_rows('tasks', 'id,ord', 'where client_id=%i && state=%i order by ord desc, id desc', $client_id, TASK_QUEUED);
+ if($rows[0][0] == $id) {
+ message('Already highest priority');
+ return;
+ }
+ if($rows[1][0] == $id) {
+ prioritize_task($id, 'top');
+ return;
+ }
+ } else {
+ $rows = db_get_rows('tasks', 'id,ord', 'where client_id=%i && state=%i order by ord asc, id asc', $client_id, TASK_QUEUED);
+ if($rows[0][0] == $id) {
+ message('Already lowest priority');
+ return;
+ }
+ if($rows[1][0] == $id) {
+ prioritize_task($id, 'bottom');
+ return;
+ }
+ }
+ # find the one we're moving
+ $cur_index = 0;
+ $done = count($rows);
+ for($i = 2; $i < $done ; ++$i) {
+ if($rows[$i][0] == $id) {
+ $cur_index = $i;
+ break;
+ }
+ }
+ $before_ord = $rows[$cur_index - 1][1];
+ $before_before_ord = $rows[$cur_index - 2][1];
+ if(abs($before_before_ord - $before_ord) < 2) {
+ reprioritize_tasks($client_id);
+ $before_ord = db_get_value('tasks', 'ord', 'where id=%i', $rows[$cur_index - 1][0]);
+ $before_before_ord = db_get_value('tasks', 'ord', 'where id=%i', $rows[$cur_index - 2][0]);
+ if($before_before_ord == $before_ord) {
+ message('Programmer error #8592');
+ return;
+ }
+ }
+ $new_ord = $before_ord + floor(($before_before_ord - $before_ord) / 2);
+ db_update('tasks', 'ord', $new_ord, 'where id=%i', $id);
+ return;
+ default:
+ message('invalid change');
+ return;
+ }
+
+}
+